第209章 Dynamic Attribute|动态属性
属性是动态变化的数据,同时成为 Method 的参数
第208章已经完成:
Class
↓
Object Instance
↓
Current State
现在进入行为逻辑工程真正重要的一步:
Object Instance
↓
Dynamic Attribute
↓
Method Parameter
第209章不再把 Attribute 理解成一个静态变量,而是把它定义为:
对象在实时运行过程中可以持续变化,并能够直接参与认知计算和行为方法计算的数据。
209.1 Static Attribute|静态属性
传统 OOP 中,属性通常表现为:
$object->name = 'Egg';
$object->weight = 50;
这些属性主要描述对象本身。
例如:
Egg
├── name = Egg
├── material = biological
└── type = food
这种属性可以长期保持稳定。
但是机器行为逻辑真正需要的大量信息并不是这样的静态信息。
209.2 Dynamic Attribute|动态属性
机器面对现实世界时,很多属性会不断变化:
Position
位置
Distance
距离
Velocity
速度
Force
力度
Pressure
压力
Temperature
温度
Direction
方向
Acceleration
加速度
Stability
稳定度
例如机械手接近鸡蛋:
Distance
20 cm
↓
10 cm
↓
5 cm
↓
1 cm
↓
0 cm
因此:
Distance
不是一个固定值,而是:
Distance(t)Distance(t)
即:
随时间变化的动态属性。
209.3 Dynamic Attribute 的核心结构
可以把动态属性表示为:
A(t)=ValueA(t)=Value
更完整地:
A(t)=(Name,Value,Unit,Timestamp,Source)A(t)= (Name,Value,Unit,Timestamp,Source)
例如:
Distance
Value = 0.05
Unit = meter
Timestamp = t
Source = vision_sensor
因此一个 Dynamic Attribute 不仅有:
Value
还具有:
Name
Value
Unit
Timestamp
Source
这使它能够参与实时计算。
209.4 Attribute Instance|属性实例
因此第204章的 Attribute Class 在运行时形成:
Attribute Class
↓
Attribute Instance
例如:
$distance = new Attribute(
'distance',
0.05,
'meter'
);
形成:
Distance Attribute Instance
name = distance
value = 0.05
unit = meter
它属于某一个具体 Object Instance。
Egg #1001
↓
Distance Attribute
↓
0.05 m
209.5 动态更新
动态属性必须可以更新。
例如:
$distance->setValue(0.03);
再变成:
$distance->setValue(0.01);
最终:
Distance(t0) = 0.05
Distance(t1) = 0.03
Distance(t2) = 0.01
所以:
At+1=Update(At,Dt)A_{t+1}=Update(A_t,D_t)
其中:
A
Attribute
D
Real-Time Data
209.6 Dynamic Attribute 不是数据库字段
这里需要明确 ICAI 与普通数据模型的区别。
普通程序可能认为:
position
pressure
force
只是:
数据库字段
而 ICAI 中:
Dynamic Attribute
是:
实时认知对象的一部分。
它具有自己的:
Value
变化
时间
来源
单位
状态
因此:
Object
↓
Dynamic Attribute
↓
Current Value
成为机器当前认知数据的一部分。
209.7 Attribute 与 State
第206章建立了 State。
现在需要区分:
Attribute
属性
与:
State
状态
例如:
Egg
Pressure = 0.20
这是一个 Dynamic Attribute。
而:
Egg
State = being_grasped
这是 State。
关系可以理解为:
Dynamic Attribute
↓
实时变化的数据
↓
State Evaluation
↓
Current State
所以:
Attribute 提供动态数据,State 表示由这些数据形成的当前状态。
209.8 Dynamic Attribute 与 Relation
Relation 同样可以使用动态属性。
例如:
Hand → near → Egg
关系中的:
Distance = 0.05m
就是动态参数。
继续移动:
Distance = 0.01m
系统可能判断:
near
↓
contacts
因此:
Dynamic Attribute
↓
Relation Evaluation
↓
Relation State
这使 Relation 不再是简单的字符串连接。
209.9 Dynamic Attribute 与 Method
这一章最重要的地方就是:
Dynamic Attribute 可以成为 Method 的参数。
例如:
Force
Pressure
Distance
Velocity
Position
Stability
全部可以进入 Method。
可以抽象为:
Action=Method(A1,A2,A3,…,An)Action = Method(A_1,A_2,A_3,\ldots,A_n)
例如:
Movement=F(Position,Distance,Velocity)Movement = F(Position,Distance,Velocity)
又例如抓取力度:
GripForce=F(Pressure,Force,Stability)GripForce = F(Pressure,Force,Stability)
这里的 F 就是后续 Method。
209.10 不为每一个场景写程序
这正是你的行为逻辑工程要解决的问题。
传统程序容易变成:
GrabEgg()
GrabGlass()
GrabBottle()
GrabFruit()
每一种对象写一套。
ICAI 则应该逐渐形成:
Object
↓
Dynamic Attributes
↓
Method
↓
Calculated Action
例如:
Object = Egg
Distance = 0.02
Pressure = 0.15
Force = 0.10
Stability = 0.92
这些动态属性进入同一个:
Grasp Method
抓取方法
方法根据参数计算:
Required Force
所需力度
而不是因为对象叫“鸡蛋”就调用一套写死的鸡蛋程序。
209.11 属性就是 Method 参数
这一点可以进一步明确:
Dynamic Attribute
↓
Current Value
↓
Method Parameter
例如:
Distance = 0.03
Force = 0.12
Pressure = 0.18
Velocity = 0.05
进入:
$method->execute(
$distance,
$force,
$pressure,
$velocity
);
因此:
属性并不是 Method 外部的数据仓库,而是 Method 运行时的输入。
209.12 参数可以来自多个 Object
Method 不一定只读取一个对象的属性。
例如:
Hand
├── Position
├── Velocity
└── Force
Egg
├── Position
├── Fragility
└── Stability
Method 可以计算:
Hand Position
+
Egg Position
+
Hand Velocity
+
Egg Stability
形成:
Movement / Grasp Calculation
于是:
Object A Attributes
+
Object B Attributes
↓
Method
↓
Action
这一步开始真正进入对象之间的行为计算。
209.13 Position 与 Force 的关系
例如你前面提出的:
施加力度和移动位置应该有数学关系。
可以在软件工程中表示为:
Position
↓
Distance
↓
Velocity
↓
Force
↓
Pressure
例如:
d(t)=∣Phand(t)−Pobject(t)∣d(t)=|P_{hand}(t)-P_{object}(t)|
其中:
P_hand
手的位置
P_object
物体的位置
d
距离
进一步:
v(t)=d(t)dtv(t)=\frac{d(t)}{dt}
得到移动速度。
再进一步,根据任务和物体属性计算:
F(t)=f(d,v,m,s,…)F(t)=f(d,v,m,s,\ldots)
其中:
d = Distance
v = Velocity
m = Object Property
s = Stability
这里并不要求使用复杂 AI。
完全可以由软件工程中的数学方法完成。
209.14 Dynamic Attribute 的计算链
因此可以建立:
Real-Time Data
实时数据
↓
Dynamic Attribute
动态属性
↓
Derived Attribute
派生属性
↓
Method Parameter
方法参数
↓
Method Calculation
方法计算
↓
Action Parameter
动作参数
例如:
Position
↓
Distance
↓
Velocity
↓
Required Force
↓
Motor Command
这就是行为逻辑工程的重要基础。
209.15 Derived Attribute|派生属性
并不是所有属性都直接来自传感器。
有些属性可以计算得到。
例如:
Position A
Position B
计算:
Distance=(x1−x2)2+(y1−y2)2+(z1−z2)2Distance = \sqrt{ (x_1-x_2)^2+ (y_1-y_2)^2+ (z_1-z_2)^2 }
得到:
Distance
再由:
Distance(t0)
Distance(t1)
计算:
Velocity≈Distance(t1)−Distance(t0)t1−t0Velocity \approx \frac{Distance(t1)-Distance(t0)} {t1-t0}
得到:
Velocity
因此:
Raw Data
↓
Attribute
↓
Calculated Attribute
形成动态属性链。
209.16 Dynamic Attribute Class
第209章可以把 Attribute Class 进一步工程化:
class Attribute
{
protected $name;
protected $value;
protected $unit;
protected $timestamp;
protected $source;
public function __construct(
$name,
$value = null,
$unit = null,
$source = null
) {
$this->name = $name;
$this->value = $value;
$this->unit = $unit;
$this->source = $source;
$this->timestamp = time();
}
public function getName()
{
return $this->name;
}
public function getValue()
{
return $this->value;
}
public function setValue($value)
{
$this->value = $value;
$this->timestamp = time();
}
public function getUnit()
{
return $this->unit;
}
public function getSource()
{
return $this->source;
}
public function getTimestamp()
{
return $this->timestamp;
}
}
这是一个非常基础的 Dynamic Attribute 实现。
暂时不加入复杂依赖。
209.17 Object 中管理 Dynamic Attributes
Object:
class Object
{
protected $attributes = array();
public function addAttribute(Attribute $attribute)
{
$this->attributes[$attribute->getName()] = $attribute;
}
public function getAttribute($name)
{
return isset($this->attributes[$name])
? $this->attributes[$name]
: null;
}
}
于是:
$egg->addAttribute($pressure);
$egg->addAttribute($force);
$egg->addAttribute($position);
运行时:
$pressure =
$egg->getAttribute('pressure')->getValue();
就可以得到:
Pressure = 0.20
209.18 Method 读取动态属性
后续 Method 可以直接获取:
$distance =
$hand->getAttribute('distance')->getValue();
$velocity =
$hand->getAttribute('velocity')->getValue();
$stability =
$egg->getAttribute('stability')->getValue();
然后:
$force = $method->calculate(
$distance,
$velocity,
$stability
);
于是:
Hand
├── Distance
└── Velocity
Egg
└── Stability
↓
Method
↓
Force
这就是你现在提出的:
属性就是方法参数。
在工程上的直接体现。
209.19 Dynamic Attribute 不等于行为
还要保持一个边界:
Attribute
不负责:
“我要抓取”
它只提供:
Distance = 0.02
Force = 0.10
Pressure = 0.15
然后:
Method
负责计算。
因此:
Attribute
提供数据
Method
处理数据
Action
执行结果
三者不能混在一起。
209.20 从属性进入行为逻辑
到这里,行为逻辑已经开始出现:
Real-Time Data
↓
Dynamic Attribute
↓
Current State
↓
Method Parameters
↓
Method Calculation
↓
Action
这与传统:
Input
↓
Fixed Rule
↓
Output
不同。
ICAI 更接近:
World
↓
Dynamic Object
↓
Dynamic Attributes
↓
Current State
↓
Method
↓
Action
↓
World Change
然后重新进入:
Feedback
↓
Dynamic Attribute Update
形成闭环。
209.21 本章核心工程原则
第209章正式确立一个后续行为工程的重要原则:
不为每一个现实场景编写固定行为程序,而让通用 Method 接收不同 Object 在当前时刻产生的 Dynamic Attributes。
因此:
同一个 Method
↓
不同 Object
↓
不同 Dynamic Attributes
↓
不同计算结果
↓
不同 Action
这才有可能处理:
鸡蛋
玻璃
水果
瓶子
工具
机械零件
等大量不同现实对象,而不是为每一种对象单独编写行为代码。
209.22 当前 ICAI 工程链
到第209章,整个结构进一步形成:
Real-Time Data
实时数据
↓
Element
元素
↓
Object Instance
对象实例
↓
Dynamic Attribute
动态属性
↓
Relation
关系
↓
Current State
当前状态
↓
Scene Instance
场景实例
而行为方向开始出现:
Dynamic Attribute
↓
Method Parameter
↓
Method
↓
Action
因此后面的核心就非常清楚了:
数据
↓
属性
↓
状态
↓
方法参数
↓
动态计算
↓
行为
第209章真正完成的不是“把 Attribute 做得更复杂”,而是完成了从“认知对象的数据结构”向“行为计算输入”的转换。
下一步自然进入 第210章 Dynamic Method|动态方法:
Dynamic Attributes
↓
Method Parameters
↓
Mathematical Calculation
↓
Method Result
正式研究你现在真正要解决的:
同一个通用 Method,如何根据不同对象、不同属性、不同实时状态,动态计算出不同的行为结果,而不需要为每一个场景重新写程序。