首页 理论 架构 工程 文档 白皮书 著作 研究 案例 下载 博客 关于 开始使用 →

第270章 Method API|方法 API

第270章 Method API|方法 API

270.1 方法 API 的提出背景

第269章建立了 Cognition API|认知 API,使当前场景能够进入认知系统,并形成:

Scene→CognitionScene\rightarrow Cognition

认知形成以后,并不能直接等同于执行。

机器认识当前世界之后,还必须解决一个重要问题:

在当前目标、当前对象、当前状态和当前场景条件下,应当采用什么方法实现目标?

因此,在 Cognition 与 Behavior 之间,需要建立独立的 Method|方法 层。

其基本结构为:

Scene
↓
Cognition
↓
Method
↓
Behavior
↓
Action
↓
Device

由此建立:

Method API|方法 API

Method API 是 ICAI 系统中负责方法识别、方法匹配、方法选择、方法生成、方法验证、方法调用和方法状态管理的标准程序接口。

它将:

Cognition→MethodCognition\rightarrow Method

正式转化为可独立调用的软件工程接口。


270.2 Method API 的定义

Method API|方法 API,是用于管理机器认知过程中方法结构的程序接口。

其核心功能包括:

  1. 创建方法;
  2. 获取方法;
  3. 更新方法;
  4. 删除方法;
  5. 查询方法;
  6. 匹配方法;
  7. 选择方法;
  8. 验证方法;
  9. 获取方法参数;
  10. 调用方法;
  11. 保存方法历史;
  12. 返回方法结果。

可以定义:

MethodAPI(Ct,Gt,Ot,St,Sct)→MtMethodAPI(C_t,G_t,O_t,S_t,Sc_t)\rightarrow M_t

其中:

  • CtC_t:当前认知;
  • GtG_t:当前目标;
  • OtO_t:当前对象;
  • StS_t:当前状态;
  • SctSc_t:当前场景;
  • MtM_t:当前方法。

因此:

Mt=F(Ct,Gt,Ot,St,Sct)M_t=F(C_t,G_t,O_t,S_t,Sc_t)

Method API 的职责不是直接执行设备动作,而是确定或建立实现目标所需要的方法结构


270.3 方法的定义

Method|方法,是机器针对特定目标,在当前认知和条件下组织行为过程的一种结构化实现方式。

方法位于:

Cognition
↓
Method
↓
Behavior

因此方法不是认知本身,也不是行为本身。

可以区分:

Cognition=认识当前情况Cognition=认识当前情况 Method=确定实现目标的方式Method=确定实现目标的方式 Behavior=按照方法形成动态行为过程Behavior=按照方法形成动态行为过程 Action=具体执行单元Action=具体执行单元

所以:

Cognition≠Method≠Behavior≠ActionCognition\neq Method\neq Behavior\neq Action

例如:

Goal:拿起鸡蛋

Cognition:
鸡蛋位于桌面
手靠近鸡蛋
鸡蛋处于稳定状态

Method:
Grasp

Behavior:
接近 → 接触 → 施加作用 → 保持 → 提升

Action:
移动手
↓
接触
↓
施加压力
↓
提升

由此形成完整的认知到执行链。


270.4 方法的基本结构

一个方法可以定义为:

M=(I,N,G,O,C,S,P,R,B)M=(I,N,G,O,C,S,P,R,B)

其中:

  • II:Method ID,方法标识;
  • NN:Method Name,方法名称;
  • GG:Goal,适用目标;
  • OO:Object,适用对象;
  • CC:Condition,方法条件;
  • SS:State,适用状态;
  • PP:Parameters,动态参数;
  • RR:Rules,方法规则;
  • BB:Behavior,方法产生的行为结构。

软件结构可以表示为:

Method
├── ID
├── Name
├── Goal
├── Object
├── Condition
├── State
├── Parameters
├── Rules
└── Behavior

方法不是一个简单的函数名称,而是一个包含适用条件、参数和行为关系的认知工程对象。


270.5 方法与目标

方法首先受到目标约束。

定义:

Mt=F(Gt,Ct,Ot,St)M_t=F(G_t,C_t,O_t,S_t)

其中目标 GtG_t 决定方法的方向。

例如:

Goal = Grasp

可能需要:

Method = Grasp

如果目标是:

Goal = Move

则可能形成:

Method = Move

如果目标是:

Goal = Release

则形成:

Method = Release

因此:

Goal→MethodGoal\rightarrow Method

但是目标本身不能唯一决定方法。

还必须结合:

Object
+
State
+
Relation
+
Scene
+
Capability

最终形成方法选择。


270.6 方法与对象

同一个目标作用于不同对象时,方法可能需要根据对象属性动态调整。

例如:

Goal = Grasp

对象分别为:

Egg
Glass
Bottle

虽然方法类别都可能是:

Grasp

但实际参数可能不同。

例如:

Pgrasp=F(Size,Weight,Shape,Surface,Stability)P_{grasp}=F(Size,Weight,Shape,Surface,Stability)

其中:

  • Size:尺寸;
  • Weight:重量;
  • Shape:形状;
  • Surface:表面特征;
  • Stability:稳定性。

因此:

Grasp
↓
Object Attributes
↓
Dynamic Parameters
↓
Behavior

方法不需要为每一种对象建立一个完全独立的程序。

可以建立一个通用方法:

Grasp(Object,Attribute,State,Relation)Grasp(Object,Attribute,State,Relation)

然后根据当前对象数据计算具体参数。

这使方法具有对象泛化能力。


270.7 方法与状态

方法的选择还受到当前状态影响。

例如:

Object = Egg
State = Stable

可能形成:

Method = Grasp

但是如果:

Object = Egg
State = Broken

则原来的抓取方法可能失效。

因此:

Valid(M)=F(G,O,S,Sc)Valid(M)=F(G,O,S,Sc)

可以定义方法适用条件:

Condition(M)=C1∧C2∧C3Condition(M)=C_1\land C_2\land C_3

例如:

Object Exists
∧
Object Reachable
∧
Object Stable
∧
Target Valid

满足条件:

Condition(M)=TrueCondition(M)=True

才允许方法进入行为阶段。


270.8 方法匹配

Method API 的重要功能是 Method Matching|方法匹配

定义:

Match(M,G,O,S,Sc)={1,Conditions satisfied0,otherwiseMatch(M,G,O,S,Sc)= \begin{cases} 1,& Conditions\ satisfied\\ 0,& otherwise \end{cases}

其中:

  • MM:候选方法;
  • GG:目标;
  • OO:对象;
  • SS:状态;
  • ScSc:场景。

基本流程:

Goal
↓
Current Cognition
↓
Candidate Methods
↓
Condition Matching
↓
Object Matching
↓
State Matching
↓
Scene Matching
↓
Valid Methods

最终:

M∗=Select({Mi∣Match(Mi)=1})M^*=Select(\{M_i\mid Match(M_i)=1\})

其中 M∗M^* 为当前选择的方法。


270.9 方法选择

如果多个方法都满足条件,则需要进一步选择。

可以定义:

M∗=Select(M1,M2,⋯ ,Mn,Ct,Gt)M^*=Select(M_1,M_2,\cdots,M_n,C_t,G_t)

选择条件可以包括:

目标匹配度
+
对象匹配度
+
状态适应性
+
场景适应性
+
方法优先级
+
执行成本
+
设备能力

形成:

Candidate Methods
↓
Method Matching
↓
Method Evaluation
↓
Method Selection
↓
Selected Method

方法选择结果进入 Behavior Engine。


270.10 方法参数

方法必须支持动态参数。

这是 ICAI 方法工程的重要组成部分。

定义:

Pt=F(At,St,Rt,Sct)P_t=F(A_t,S_t,R_t,Sc_t)

其中:

  • PtP_t:当前方法参数;
  • AtA_t:当前属性;
  • StS_t:当前状态;
  • RtR_t:当前关系;
  • SctSc_t:当前场景。

例如抓取方法:

Pt=(Position,Velocity,Force,Pressure)P_t= (Position,Velocity,Force,Pressure)

可以形成:

Method = Grasp

Parameters
├── Position
├── Velocity
├── Force
└── Pressure

这些参数不是固定值,而是当前运行状态计算得到的动态值。


270.11 属性作为方法参数

第209章已经建立了 Dynamic Attribute|动态属性。

第210章建立了 Attribute Vector|属性向量。

现在可以进一步形成:

Attribute→MethodParameterAttribute\rightarrow MethodParameter

例如:

Xt=[Position,Velocity,Force,Pressure,Distance,Stability]TX_t= [ Position, Velocity, Force, Pressure, Distance, Stability ]^T

方法可以读取:

Pt=M(Xt)P_t=M(X_t)

因此:

Dynamic Attribute
↓
Attribute Vector
↓
Method Parameter
↓
Behavior

这使方法能够直接处理实时数据。

例如:

Hand Position
Hand Velocity
Egg Position
Egg Stability
Hand-Egg Distance

共同决定:

Grasp Method Parameters

270.12 方法规则

方法内部可以具有明确规则。

例如:

Distance>Threshold→ApproachDistance>Threshold \rightarrow Approach Distance≤Threshold→ContactDistance\leq Threshold \rightarrow Contact Contact=True∧Force>Fmin→HoldContact=True\land Force>F_{min} \rightarrow Hold Hold=True→LiftHold=True \rightarrow Lift

由此:

Method
↓
Rule
↓
Condition
↓
Behavior State

方法规则可以由离散数学条件表达。

例如:

C=C1∧C2∧C3C=C_1\land C_2\land C_3

其中:

  • C1C_1:对象存在;
  • C2C_2:目标有效;
  • C3C_3:当前状态满足方法要求。

方法规则并不是固定绑定某一个对象,而是对对象属性和状态进行条件判断。


270.13 方法与行为

方法确定之后,进入 Behavior。

定义:

Bt=F(Mt,Gt,Ot,St,Pt)B_t=F(M_t,G_t,O_t,S_t,P_t)

即:

Method
+
Goal
+
Object
+
State
+
Parameters
↓
Behavior

例如:

Method = Grasp

产生:

Behavior
├── Approach
├── Contact
├── Apply Force
├── Hold
└── Lift

因此:

Method→BehaviorMethod\rightarrow Behavior

是 ICAI 行为工程的重要转换关系。


270.14 方法与动作

行为进一步产生动作:

Act=F(Bt,Pt,St,Dt)Ac_t=F(B_t,P_t,S_t,D_t)

其中:

  • BtB_t:行为;
  • PtP_t:方法动态参数;
  • StS_t:当前状态;
  • DtD_t:可用设备。

形成:

Method
↓
Behavior
↓
Action
↓
Device

例如:

Method = Grasp
↓
Behavior = Approach → Contact → Hold
↓
Action = Move / Contact / ApplyForce
↓
Device = Robotic Hand

因此 Method API 不直接控制设备。

它只负责形成进入行为和动作层所需要的方法结构。


270.15 Method API 的基本接口

Method API 可以提供:

/api/method/create
/api/method/get
/api/method/update
/api/method/delete
/api/method/list
/api/method/query
/api/method/match
/api/method/select
/api/method/validate
/api/method/parameters
/api/method/history

其中:

create:创建方法;

get:获取方法;

update:更新方法;

delete:删除方法;

list:获取方法集合;

query:查询方法;

match:匹配候选方法;

select:选择方法;

validate:验证方法;

parameters:计算或获取方法参数;

history:获取方法使用历史。

也可以采用资源型接口:

/api/methods
/api/methods/{id}
/api/methods/{id}/match
/api/methods/{id}/parameters
/api/methods/{id}/validate
/api/methods/{id}/history

270.16 Method API 请求结构

可以定义:

{
    "request_id": "M10001",
    "goal": {
        "type": "grasp",
        "target": "egg_01"
    },
    "cognition_id": "C10001",
    "scene_id": "SC10001",
    "object_id": "egg_01",
    "state": {
        "type": "stable"
    },
    "parameters": {
        "position": {},
        "force": 0,
        "pressure": 0
    },
    "timestamp": 1788500000
}

请求流程:

JSON
↓
Method API
↓
Method Controller
↓
Method Service
↓
Method Engine
↓
Method Matching
↓
Method Selection
↓
Method Result

270.17 Method API 响应结构

方法响应可以定义:

{
    "request_id": "M10001",
    "status": "success",
    "method": {
        "id": "method_grasp",
        "type": "grasp",
        "target": "egg_01"
    },
    "parameters": {
        "position": {},
        "force": 0.8,
        "pressure": 0.5
    },
    "behavior": {
        "type": "approach_contact_hold_lift"
    },
    "timestamp": 1788500000
}

其结构可以抽象为:

MR=(I,M,P,B,S,T)MR=(I,M,P,B,S,T)

其中:

  • II:请求标识;
  • MM:方法;
  • PP:方法参数;
  • BB:行为;
  • SS:结果状态;
  • TT:时间。

270.18 MethodController

PHP OOP 中可以建立:

class MethodApiController
{
    protected $service;

    public function match()
    {
        $input = $this->getRequestData();

        $result = $this->service->match($input);

        return $this->json($result);
    }

    public function select()
    {
        $input = $this->getRequestData();

        $result = $this->service->select($input);

        return $this->json($result);
    }

    public function parameters()
    {
        $input = $this->getRequestData();

        $result = $this->service->parameters($input);

        return $this->json($result);
    }
}

Controller 只负责:

Request
↓
Route
↓
Service
↓
Response

不直接执行方法计算。


270.19 MethodService

MethodService 负责组织方法服务流程:

class MethodService
{
    protected $engine;

    public function match($input)
    {
        $this->validate($input);

        return $this->engine->match($input);
    }

    public function select($input)
    {
        $this->validate($input);

        return $this->engine->select($input);
    }

    public function parameters($input)
    {
        $this->validate($input);

        return $this->engine->calculateParameters($input);
    }

    protected function validate($input)
    {
        return true;
    }
}

形成:

Method API
↓
Method Controller
↓
Method Service
↓
Method Engine

270.20 MethodEngine

MethodEngine 是方法计算的核心工程对象。

class MethodEngine
{
    public function match($input)
    {
        $methods = $this->loadCandidateMethods($input);

        $matched = array();

        foreach ($methods as $method) {
            if ($this->isMatch($method, $input)) {
                $matched[] = $method;
            }
        }

        return $matched;
    }

    public function select($input)
    {
        $methods = $this->match($input);

        return $this->selectBest($methods, $input);
    }

    public function calculateParameters($input)
    {
        return array(
            'position' => $input['position'],
            'force'    => $input['force'],
            'pressure' => $input['pressure']
        );
    }
}

其中:

match()

负责方法匹配;

select()

负责方法选择;

calculateParameters()

负责动态参数计算。


270.21 MethodModel

MethodModel 表示软件系统中的方法对象。

class MethodModel
{
    protected $id;
    protected $name;
    protected $goal;
    protected $objectType;
    protected $conditions;
    protected $states;
    protected $parameters;
    protected $rules;
    protected $behavior;

    public function getId()
    {
        return $this->id;
    }

    public function getGoal()
    {
        return $this->goal;
    }

    public function getParameters()
    {
        return $this->parameters;
    }

    public function getRules()
    {
        return $this->rules;
    }
}

其结构关系为:

Method Theory
↓
Method Object Model
↓
MethodModel
↓
Method Instance
↓
Runtime Method

270.22 MethodRepository

方法数据可以通过 MethodRepository 进行持久化。

class MethodRepository
{
    public function save($method)
    {
        // Save method
    }

    public function find($id)
    {
        // Find method
    }

    public function query($condition)
    {
        // Query methods
    }

    public function update($id, $method)
    {
        // Update method
    }

    public function delete($id)
    {
        // Delete method
    }
}

数据库可以建立:

cognitive_methods

例如:

id
method_name
method_type
goal_type
object_type
conditions
rules
status
created_at
updated_at

动态参数可以独立保存:

cognitive_method_parameters

方法历史可以保存:

cognitive_method_history

270.23 方法验证

方法进入运行阶段之前必须进行验证。

定义:

Valid(M)=Valid(G)∧Valid(O)∧Valid(C)∧Valid(S)∧Valid(P)Valid(M)=Valid(G)\land Valid(O)\land Valid(C)\land Valid(S)\land Valid(P)

其中:

  • GG:目标;
  • OO:对象;
  • CC:条件;
  • SS:状态;
  • PP:参数。

进一步:

Goal Valid
+
Object Valid
+
Condition Valid
+
State Valid
+
Parameter Valid
↓
Method Valid

如果:

Object = Egg
State = Broken
Method = Grasp

而方法规则要求:

State = Stable

则:

Valid(M)=FalseValid(M)=False

此时不能进入行为执行阶段。


270.24 方法动态参数计算

方法参数是 ICAI 动态行为工程的重要连接点。

例如:

Pt=F(Xt,St,Rt)P_t=F(X_t,S_t,R_t)

其中:

Xt=[Position,Velocity,Force,Pressure,Distance,Stability]TX_t= [ Position, Velocity, Force, Pressure, Distance, Stability ]^T

假设当前对象距离为 dtd_t,速度为 vtv_t,作用力为 ftf_t

方法可以根据当前数据计算:

Pt=(pt,vt,ft)P_t=(p_t,v_t,f_t)

当对象变化:

Xt→Xt+1X_t\rightarrow X_{t+1}

则:

Pt→Pt+1P_t\rightarrow P_{t+1}

因此方法参数可以实时变化。

这意味着:

Method
不是固定动作程序

而是:

Method
+
Current Data
↓
Dynamic Parameters
↓
Current Behavior

270.25 方法状态

方法本身也可以具有状态。

定义:

MStMS_t

表示当前 Method State。

例如:

Created
↓
Matched
↓
Selected
↓
Prepared
↓
Executing
↓
Completed

如果执行过程中条件失效:

Executing
↓
Condition Changed
↓
Invalid
↓
Interrupted

因此:

MSt→MSt+1MS_t\rightarrow MS_{t+1}

方法状态可以保存:

cognitive_method_state

并记录:

method_id
state
timestamp
reason

270.26 方法历史

方法使用过程可以形成历史:

HM={(t1,M1),(t2,M2),⋯ ,(tn,Mn)}H_M=\{(t_1,M_1),(t_2,M_2),\cdots,(t_n,M_n)\}

例如:

t1 → Grasp selected
t2 → Grasp parameters calculated
t3 → Grasp executing
t4 → Grasp completed

方法历史可以与行为历史、动作历史和反馈历史连接。

形成:

Cognition History
↓
Method History
↓
Behavior History
↓
Action History
↓
Feedback History

由此为后续经验结构提供连续运行数据。


270.27 Method API 与设备能力

方法本身不等于设备。

同一个方法可以由不同设备执行。

例如:

Method = Move

可以由:

Robot Arm
Mechanical Platform
Mobile Device

等不同设备实现。

因此:

Method→CapabilityRequirementMethod\rightarrow CapabilityRequirement

再:

CapabilityRequirement→DeviceCapabilityRequirement\rightarrow Device

设备匹配可以表示:

Match(M,D)=1Match(M,D)=1

当:

Capability(D)⊇Requirement(M)Capability(D)\supseteq Requirement(M)

时,设备满足方法要求。

因此:

Method
↓
Capability Requirement
↓
Device Matching
↓
Device

这一结构为下一阶段 Device API 建立基础。


270.28 Method API 与 Behavior API

Method API 完成:

Cognition→MethodCognition\rightarrow Method

下一层需要完成:

Method→BehaviorMethod\rightarrow Behavior

因此:

Cognition API
↓
Method API
↓
Behavior API

三层职责分别为:

Cognition API

形成当前认知。

Method API

确定实现目标的方法。

Behavior API

形成动态行为过程。

随后:

Behavior API
↓
Action API
↓
Device API

形成完整执行链。


270.29 Method API 的实时运行

实时运行过程中:

World
↓
Element
↓
Object
↓
Attribute
↓
Relation
↓
State
↓
Scene
↓
Cognition
↓
Method

当方法参数发生变化:

Attribute Changed
↓
State Changed
↓
Scene Changed
↓
Cognition Changed
↓
Method Re-Evaluation

因此方法不是一次选择后永久固定。

可以表示:

Mt+1=F(Ct+1,Gt+1,Ot+1,St+1,Sct+1)M_{t+1}=F(C_{t+1},G_{t+1},O_{t+1},S_{t+1},Sc_{t+1})

如果当前世界发生变化,方法也可以重新匹配。

这使 ICAI 的方法层成为动态运行结构。


270.30 方法重新匹配

当反馈进入系统:

Action
↓
World Change
↓
Feedback
↓
State Update
↓
Scene Update
↓
Cognition Update
↓
Method Re-Matching

因此:

Feedback→ReCognition→ReMethodFeedback\rightarrow ReCognition\rightarrow ReMethod

例如:

原方法:
Grasp Egg

执行过程中:

Egg Position Changed

则:

Current Method
↓
Parameter Invalid
↓
Method Re-Evaluation
↓
New Parameters
↓
Continue Behavior

如果对象状态已经无法满足原方法:

Method Invalid
↓
Select New Method

由此形成动态方法系统。


270.31 Method API 的完整结构

到第270章,ICAI API 层已经进一步形成:

Element API
↓
Object API
↓
Attribute API
↓
Relation API
↓
State API
↓
Scene API
↓
Cognition API
↓
Method API

理论关系为:

E→O→A→R→S→Sc→C→ME\rightarrow O\rightarrow A\rightarrow R\rightarrow S\rightarrow Sc\rightarrow C\rightarrow M

继续向执行层扩展:

M→B→Ac→D→FM\rightarrow B\rightarrow Ac\rightarrow D\rightarrow F

最终:

Element
↓
Object
↓
Attribute
↓
Relation
↓
State
↓
Scene
↓
Cognition
↓
Method
↓
Behavior
↓
Action
↓
Device
↓
Feedback
↓
Re-Cognition

这形成从机器世界结构到方法选择的连续认知工程链。


270.32 Method API 的系统定位

Method API 在 ICAI 系统中的正式定位为:

Method API 是连接机器认知与机器行为的标准程序接口,负责根据当前目标、认知、对象、属性、关系、状态和场景,对方法进行匹配、选择、验证和参数计算,并向行为系统提供结构化方法结果。

其核心关系为:

Ct→Mt→BtC_t\rightarrow M_t\rightarrow B_t

其中:

  • CtC_t:当前认知;
  • MtM_t:当前方法;
  • BtB_t:当前行为。

因此 Method API 是认知层向行为层过渡的重要接口。


270.33 本章总结

第270章建立了 Method API|方法 API

第269章完成:

Scene→CognitionScene\rightarrow Cognition

第270章继续建立:

Cognition→MethodCognition\rightarrow Method

方法不是简单的函数名称,而是由:

Goal
+
Object
+
Condition
+
State
+
Parameters
+
Rules
+
Behavior

共同形成的结构化机器认知对象。

方法选择可以表示为:

M∗=Select(M1,M2,⋯ ,Mn,Ct,Gt)M^*=Select(M_1,M_2,\cdots,M_n,C_t,G_t)

方法参数可以表示为:

Pt=F(At,St,Rt,Sct)P_t=F(A_t,S_t,R_t,Sc_t)

方法进一步形成行为:

Bt=F(Mt,Gt,Ot,St,Pt)B_t=F(M_t,G_t,O_t,S_t,P_t)

最终:

Method→Behavior→ActionMethod\rightarrow Behavior\rightarrow Action

因此,ICAI 从第263章到第270章已经形成:

Element API
↓
Object API
↓
Attribute API
↓
Relation API
↓
State API
↓
Scene API
↓
Cognition API
↓
Method API

其完整机器认知结构进一步成为:

World
↓
Real-Time Data
↓
Element
↓
Object
↓
Attribute
↓
Relation
↓
State
↓
Scene
↓
Cognition
↓
Method
↓
Behavior
↓
Action
↓
Device
↓
World Change
↓
Feedback
↓
Re-Cognition

至此,ICAI 已经不只是能够描述“机器看到了什么”和“机器认识到了什么”,而是进一步建立了:

机器在当前认知条件下如何形成实现目标的方法。

第271章可以继续建立 Behavior API|行为 API,完成:

Method→BehaviorMethod\rightarrow Behavior

使方法正式进入动态行为运行层。

Leave a Reply

Your email address will not be published. Required fields are marked *