第92章 SAI 完整运行实例
第91章建立了 SAI 的完整生命周期:
信息接收 → 感知 → 理解 → 记忆 → 推理 → 决策 → 行为 → 行动 → 外部执行 → 反馈 → 经验 → 学习 → 更新 → 再次运行
第92章不再分别讨论各个模块,而是让一个 SAI Individual 从创建开始,真正完整运行两轮。
为了保持与第87~91章连续,本章继续使用:
Individual_A
Vehicle_A
Obstacle_A
整个案例仍然采用对象、属性、状态、关系、事实、规则、经验、离散计算完成,不引入大模型机制。
1. 创建 Individual
首先创建一个可以独立运行的 SAI Individual。
Individual_A
基本结构:
Individual_A
├── Identity
├── State
├── Ability
├── Memory
├── Central
└── InformationReceiver
例如:
$individual = array(
'id' => 'Individual_A',
'name' => 'SAI Individual A',
'type' => 'ICAI',
'state' => 'CREATED',
'ability' => array(
'Perception',
'Cognition',
'Memory',
'Reasoning',
'Decision',
'Behavior',
'Action',
'Expression',
'Learning'
)
);
启动:
CREATED
↓
INITIALIZING
↓
READY
最终:
Individual_A
state = READY
此时 Individual 还没有处理任何外部信息。
2. 创建场景
创建当前世界场景:
Scene_A
场景:
Scene_A
├── Road_A
├── Lane_1
├── Vehicle_A
└── Obstacle_A
当前车辆:
Vehicle_A
state = MOVING
speed = 40 km/h
前方对象:
Obstacle_A
state = MOVING
speed = 10 km/h
distance = 20 m
场景初始结构:
$scene = array(
'id' => 'Scene_A',
'road' => 'Road_A',
'lane' => 'Lane_1',
'objects' => array(
'Vehicle_A',
'Obstacle_A'
),
'state' => 'ACTIVE'
);
这里的 Scene 是 Individual 对当前环境的内部结构化表示。
3. 输入信息
车辆传感器提供新的 Information:
Vehicle_A.speed = 40
Obstacle_A.speed = 10
Obstacle_A.distance = 20
同时获得:
Vehicle_A front_of Obstacle_A
输入结构:
$information = array(
'id' => 'info_001',
'source' => 'VehicleSensor_A',
'type' => 'SENSOR_DATA',
'data' => array(
'vehicle_speed' => 40,
'obstacle_speed' => 10,
'obstacle_distance' => 20
),
'timestamp' => time()
);
进入:
InformationReceiver
4. 感知信息
InformationReceiver 将信息交给 Perception。
Perception 对输入进行结构化:
Information
↓
Perception
↓
Elements
得到:
Element_001
type = OBJECT
value = Vehicle_A
Element_002
type = PROPERTY
object = Vehicle_A
property = speed
value = 40
Element_003
type = OBJECT
value = Obstacle_A
Element_004
type = PROPERTY
object = Obstacle_A
property = speed
value = 10
Element_005
type = PROPERTY
object = Obstacle_A
property = distance
value = 20
感知阶段只负责:
获取和结构化信息。
还没有进行决策。
5. 建立对象
Individual 根据 Elements 建立 Object。
Vehicle_A
Vehicle_A
├── id = Vehicle_A
├── type = VEHICLE
├── state = MOVING
├── speed = 40
└── lane = Lane_1
Obstacle_A
Obstacle_A
├── id = Obstacle_A
├── type = VEHICLE
├── state = MOVING
├── speed = 10
└── distance = 20
注意:
Vehicle_A
是 Individual 内部的 Object。
它不是物理车辆本身。
6. 建立关系
Individual 建立 Object 之间的关系:
Vehicle_A
│
│ front_of
▼
Obstacle_A
以及:
Vehicle_A
│
│ located_on
▼
Lane_1
│
│ part_of
▼
Road_A
形成:
$relations = array(
array(
'subject' => 'Vehicle_A',
'relation' => 'front_of',
'object' => 'Obstacle_A'
),
array(
'subject' => 'Vehicle_A',
'relation' => 'located_on',
'object' => 'Lane_1'
),
array(
'subject' => 'Lane_1',
'relation' => 'part_of',
'object' => 'Road_A'
)
);
现在 Individual 不仅知道:
Vehicle_A
Obstacle_A
还知道:
Vehicle_A 在 Obstacle_A 后方
以及它们所在的道路结构。
7. 读取记忆
当前场景建立后,Individual 从 Memory 中读取已有资源。
例如:
Fact
Vehicle_A.speed = 40
Obstacle_A.speed = 10
Obstacle_A.distance = 20
Rule
IF
distance < 30
AND
relative_speed > 20
THEN
risk = HIGH
Experience
过去曾经出现:
distance = 25
relative_speed = 28
当时:
DECELERATE
结果:
SAFE
因此:
Memory
├── Fact
├── Relation
├── Rule
└── Experience
被提供给 Reasoning 和 Decision。
8. 执行推理
Individual 开始 Reasoning。
首先计算相对速度:
relative_speed
=
Vehicle_A.speed
-
Obstacle_A.speed
=
40 - 10
=
30 km/h
形成 Derived Fact:
Vehicle_A.relative_speed = 30
然后执行 Rule:
distance < 30
当前:
20 < 30
结果:
TRUE
第二个条件:
relative_speed > 20
当前:
30 > 20
结果:
TRUE
组合:
TRUE AND TRUE
=
TRUE
因此:
Conclusion:
Vehicle_A.risk = HIGH
形成:
ReasoningResult
├── relative_speed = 30
├── risk = HIGH
├── rule = Rule_001
└── state = CONFIRMED
9. 产生决策
Reasoning 已经告诉 Individual:
risk = HIGH
现在进入 Decision。
产生 Candidate:
Candidate_1 = CONTINUE
Candidate_2 = DECELERATE
Candidate_3 = STOP
Candidate_4 = TURN
进行风险和优先级比较:
| Candidate | Risk | Priority | 状态 |
|---|---|---|---|
| CONTINUE | HIGH | LOW | 不优先 |
| DECELERATE | MEDIUM | HIGH | 可选 |
| STOP | LOW | HIGH | 可选 |
| TURN | MEDIUM/HIGH | MEDIUM | 条件不足 |
假设当前环境允许正常减速,Individual 选择:
Decision:
DECELERATE
形成:
DecisionResult
├── selected = DECELERATE
├── risk = MEDIUM
├── priority = HIGH
└── state = DECIDED
10. 产生行为
Decision 不能直接操作车辆。
Individual 创建 Behavior:
Behavior_001
type = DECELERATE
target = Vehicle_A
state = READY
行为过程:
检查车辆状态
↓
设置目标速度
↓
等待反馈
↓
读取实际速度
↓
确认结果
因此:
Decision
↓
Behavior
11. 产生 Action
Behavior 创建具体 Action:
Action_001
method = CHECK_STATE
target = Vehicle_A
然后:
Action_002
method = SET_SPEED
target = Vehicle_A
speed = 20
unit = km/h
再准备:
Action_003
method = READ_SPEED
target = Vehicle_A
最后:
Action_004
method = VERIFY
target = Vehicle_A
其中核心外部操作:
SET_SPEED = 20 km/h
因此:
Behavior
↓
Action
12. Template 表现
Action 需要转换成外部环境能够使用的结构。
Expression:
Expression_001
type = ACTION
target = Vehicle_A
command = SET_SPEED
speed = 20
unit = km/h
使用 Template:
vehicle_command
模板:
{
"vehicle": "{{vehicle}}",
"command": "{{command}}",
"speed": "{{speed}}",
"unit": "{{unit}}"
}
数据:
vehicle = Vehicle_A
command = SET_SPEED
speed = 20
unit = km/h
Template Engine 进行结构化处理。
结果:
{
"vehicle": "Vehicle_A",
"command": "SET_SPEED",
"speed": 20,
"unit": "km/h"
}
这里 JSON 只是通信数据结构。
13. Adapter 执行
Renderer 将结构转换成车辆控制系统需要的表现形式:
DeviceRenderer
↓
Vehicle Command
然后:
Vehicle Command
↓
VehicleAdapter
VehicleAdapter 负责连接:
Individual
↓
VehicleAdapter
↓
Vehicle Control System
↓
Vehicle_A
Adapter 不重新进行:
Reasoning
Decision
Risk
它只负责执行连接。
最终车辆实际发生变化:
Vehicle_A
40 km/h
↓
30 km/h
↓
20 km/h
此时:
Individual 的内部 Decision 已经作用于 External World。
14. 获取反馈
车辆控制系统返回:
VehicleResponse
例如:
{
"vehicle": "Vehicle_A",
"command": "SET_SPEED",
"state": "SUCCESS",
"speed": 20
}
Adapter 接收后形成 Feedback:
Feedback_001
├── source = Vehicle_A
├── action = SET_SPEED
├── state = SUCCESS
├── actual_speed = 20
└── timestamp
然后:
External World
↓
Vehicle Response
↓
Adapter
↓
Feedback
↓
Information
重新进入 Individual。
15. 保存经验
Individual 将本次执行过程组织成 Experience。
Experience_001
结构:
Condition:
distance = 20
relative_speed = 30
risk = HIGH
Decision:
DECELERATE
Behavior:
DECELERATE
Action:
SET_SPEED = 20
Result:
SUCCESS
Feedback:
speed = 20
Outcome:
SAFE
形成:
ExperienceMemory
这意味着 Individual 不只是保存:
“曾经执行过”
而是保存:
在什么条件下,作出了什么决策,执行了什么行为,产生了什么结果。
16. 学习
现在进入 Learning。
Learning 获取:
Experience
+
Feedback
+
Memory
+
Current Knowledge
进行分析。
例如本次经验:
distance = 20
relative_speed = 30
risk = HIGH
DECELERATE
SAFE
如果该经验经过验证,可以形成新的知识资源:
Knowledge_001
例如:
在距离较小且相对速度较高的情况下,
减速行为能够降低车辆风险。
在系统内部不是保存成一句自然语言,而可以形成结构化知识:
Knowledge_001
condition:
distance < 30
relative_speed > 20
risk:
HIGH
behavior:
DECELERATE
result:
SAFE
这样可以被后续 Reasoning、Decision 使用。
17. 更新记忆
Learning 完成以后,Individual 更新 Memory。
例如:
Memory
├── Fact
├── Relation
├── Rule
├── Experience
├── Knowledge
└── History
增加:
Experience_001
以及:
Knowledge_001
同时更新车辆历史:
Vehicle_A.speed
t1 = 40
t2 = 30
t3 = 20
于是 Individual 不仅知道当前:
speed = 20
还知道:
speed decreasing
形成历史变化。
18. 再次运行
现在第一轮生命周期结束。
但 Individual 不停止。
外部世界继续变化。
例如:
第一轮:
Vehicle_A.speed = 20
Obstacle_A.distance = 20
经过一段时间后,新信息进入:
Vehicle_A.speed = 20
Obstacle_A.speed = 10
Obstacle_A.distance = 12
新的 Information:
Information_002
再次进入:
InformationReceiver
然后重新运行:
Information
↓
SceneCollector
↓
Perception
↓
Element
↓
Object
↓
Relation
↓
Understanding
↓
Memory
↓
Reasoning
↓
Decision
↓
Behavior
↓
Action
↓
Expression
↓
Template
↓
Renderer
↓
Adapter
↓
External World
↓
Feedback
↓
Experience
↓
Learning
↓
Update
↓
Information
18.1 第二轮可能出现不同 Decision
第二轮:
Vehicle_A.speed = 20
Obstacle_A.speed = 10
distance = 12
相对速度:
20 - 10 = 10 km/h
虽然:
distance = 12 < 30
但:
relative_speed = 10
不满足:
relative_speed > 20
所以原 Rule:
distance < 30
AND
relative_speed > 20
变成:
TRUE AND FALSE
=
FALSE
因此:
risk = HIGH
这一结论不能继续直接成立。
Individual 需要重新评估当前风险。
这就是生命周期循环的重要意义:
Individual 不会因为上一轮得到了某个结论,就永远使用旧结论。
而是:
新信息
↓
新场景
↓
新感知
↓
新认知
↓
重新推理
↓
重新决策
19. 两轮运行对比
第一轮:
distance = 20
relative_speed = 30
↓
risk = HIGH
↓
Decision = DECELERATE
↓
speed = 20
↓
Feedback = SUCCESS
↓
Experience
第二轮:
distance = 12
relative_speed = 10
↓
原 HIGH-RISK Rule 不成立
↓
重新评估
↓
根据当前状态重新 Decision
这说明:
SAI 不是一次性计算器
而是:
连续状态变化
+
连续信息输入
+
连续认知
+
连续推理
+
连续决策
+
连续行为
20. 完整运行代码结构示意
如果将本章的完整过程抽象成一个 Individual Runtime,可以表示:
class IndividualRuntime
{
public function run($information)
{
// 1. 接收信息
$input = $this->receiveInformation($information);
// 2. 场景采集
$scene = $this->collectScene($input);
// 3. 感知
$elements = $this->perceive($scene);
// 4. 对象
$objects = $this->buildObjects($elements);
// 5. 关系
$relations = $this->buildRelations($objects);
// 6. 理解
$understanding = $this->understand(
$objects,
$relations
);
// 7. 记忆
$memory = $this->readMemory($understanding);
// 8. 推理
$reasoning = $this->reason(
$understanding,
$memory
);
// 9. 决策
$decision = $this->decide(
$reasoning,
$memory
);
// 10. 行为
$behavior = $this->createBehavior($decision);
// 11. Action
$action = $this->createAction($behavior);
// 12. Expression
$expression = $this->express($action);
// 13. Template / Renderer
$rendered = $this->render($expression);
// 14. Adapter / External World
$feedback = $this->executeExternal(
$rendered
);
// 15. Experience
$experience = $this->saveExperience(
$decision,
$action,
$feedback
);
// 16. Learning
$knowledge = $this->learn(
$experience
);
// 17. Update
$this->updateMemory(
$knowledge,
$experience
);
// 18. 返回本轮结果
return array(
'reasoning' => $reasoning,
'decision' => $decision,
'behavior' => $behavior,
'action' => $action,
'feedback' => $feedback,
'experience' => $experience,
'knowledge' => $knowledge
);
}
}
这个代码只是把本章的结构关系映射成 PHP OOP。
每一个方法在真正的 SAI 系统中,都应该对应前面章节定义的独立组件,而不是把所有逻辑塞进一个类。
21. SAI Individual 两轮完整生命周期
第一轮:
Individual_A
│
▼
Information
│
▼
SceneCollector
│
▼
Perception
│
▼
Element
│
▼
Object
│
▼
Relation
│
▼
Understanding
│
▼
Memory
│
▼
Reasoning
│
▼
Conclusion
│
▼
Decision
│
▼
Behavior
│
▼
Action
│
▼
Expression
│
▼
Template
│
▼
Renderer
│
▼
Adapter
│
▼
External World
│
▼
Feedback
│
▼
Experience
│
▼
Learning
│
▼
Update
│
└──────────┐
▼
Information
│
▼
第二轮运行
本章核心模型
第92章可以把 SAI Individual 的运行压缩成:
创建 Individual
↓
创建 Scene
↓
Information
↓
Perception
↓
Element
↓
Object
↓
Relation
↓
Understanding
↓
Memory
↓
Reasoning
↓
Conclusion
↓
Decision
↓
Behavior
↓
Action
↓
Expression
↓
Template
↓
Renderer
↓
Adapter
↓
External World
↓
Feedback
↓
Experience
↓
Learning
↓
Memory Update
↓
再次 Information
↺
本章核心定义
| 步骤 | Individual 实际完成的工作 |
|---|---|
| 创建 Individual | 建立独立运行主体 |
| 创建场景 | 建立当前外部世界的内部表示 |
| 输入信息 | 接收外部或内部 Information |
| 感知信息 | 从 Information 获取结构化 Elements |
| 建立对象 | 将 Elements 组织成 Object |
| 建立关系 | 建立 Object、Property、State 之间的关系 |
| 读取记忆 | 获取 Fact、Rule、Experience 等历史资源 |
| 执行推理 | 应用 Fact、Rule、Experience 得出 Conclusion |
| 产生决策 | 比较 Candidate、Risk、Priority 后选择方案 |
| 产生行为 | 将 Decision 组织成执行过程 |
| 产生 Action | 形成具体可执行操作 |
| Template 表现 | 按模板组织 Action/Result 数据 |
| Adapter 执行 | 将结果连接到目标环境 |
| 获取反馈 | 获得外部世界真实执行结果 |
| 保存经验 | 保存 Condition、Decision、Action、Result、Feedback |
| 学习 | 从经验和反馈中获取知识 |
| 更新记忆 | 更新 Fact、Experience、Knowledge、History 等资源 |
| 再次运行 | 接收新信息,进入下一轮生命周期 |
最终,第92章真正展示的是:
SAI Individual 不是按照预先写死的一条直线只运行一次,而是在外部世界持续变化的情况下,不断接收新信息、重新感知、重新理解、重新推理、重新决策、重新行动,并把实际结果转化为经验和知识,再更新自身。
因此,SAI Individual 的完整运行模型可以最终写成:
世界
↓
信息
↓
感知
↓
元素
↓
对象
↓
关系
↓
理解
↓
记忆
↓
推理
↓
决策
↓
行为
↓
行动
↓
表现
↓
外部执行
↓
反馈
↓
经验
↓
学习
↓
更新
↓
世界的新状态
↓
新信息
↓
再次运行
这就是从 “SAI 生命周期” 到 “SAI Individual 实际运行” 的完整闭环。