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

第37章 PerceptionEngine

第37章 PerceptionEngine

本章大纲

  1. Engine 定义
  2. 信息分析
  3. 元素提取
  4. 对象识别
  5. 属性识别
  6. 状态识别
  7. 关系识别
  8. 感知结果
  9. 完整代码示例

37.1 Engine 定义

在第14章中已经介绍过 Engine。

**Engine(引擎)**是 SAI Framework 中负责执行某一类明确处理任务的核心处理组件。

因此:

PerceptionEngine 是专门负责执行感知处理过程的 Engine。

它不是整个 Perception,也不是 Cognition。

可以理解为:

Perception
    ↓
定义“感知是什么”

PerceptionEngine
    ↓
负责“如何执行感知”

37.1.1 PerceptionEngine 的位置

完整位置:

External World
      ↓
InformationSource
      ↓
InformationReceiver
      ↓
Information
      ↓
SceneCollector
      ↓
Scene
      ↓
PerceptionEngine
      ↓
PerceptionResult
      ↓
CognitionEngine

因此 PerceptionEngine 是:

Scene
  ↓
感知处理
  ↓
结构化感知结果

37.1.2 PerceptionEngine 不负责什么

PerceptionEngine 不负责:

最终理解
复杂推理
最终决策
行为选择
学习
长期记忆

它主要负责:

信息分析
元素提取
对象识别
属性识别
状态识别
关系识别
感知结果形成

所以:

PerceptionEngine
        ↓
识别世界结构

CognitionEngine
        ↓
理解世界结构

37.2 信息分析

PerceptionEngine 的第一步不是立即创建 Object,而是分析输入信息。

输入可以是:

Information

也可以是:

Scene

例如:

客厅温度32℃,有人,风扇关闭。

系统首先需要确定:

有什么信息?
信息属于什么类型?
有哪些可识别数据?
信息来自哪里?
信息对应什么场景?

37.2.1 信息分析结构

可以把分析结果表示为:

Analysis
{
    source
    type
    content
    time
    context
}

例如:

Analysis
{
    source: "scene-001",
    type: "scene",
    content:
    {
        room: "living-room",
        temperature: 32,
        person: "present",
        fan: "off"
    }
}

37.2.2 信息分析不是认知

这里需要特别区分。

信息分析主要回答:

数据是什么结构?
有哪些字段?
有哪些候选信息?

而不是:

这意味着什么?
应该怎么办?

因此:

Information Analysis
        ↓
结构分析

Cognition
        ↓
意义理解

37.3 元素提取

信息分析完成以后,PerceptionEngine 从输入中提取 Element。

例如:

客厅温度32℃,有人,风扇关闭。

提取:

Element
├── living-room
├── temperature
├── 32
├── person
├── present
├── fan
└── off

37.3.1 元素类型

可以识别:

object
property
value
state
location
time
event
measurement

例如:

fan
→ object

temperature
→ property

32
→ value

off
→ state

living-room
→ location

37.3.2 元素提取的基本流程

Information / Scene
       ↓
读取内容
       ↓
识别数据项
       ↓
确定 Element Type
       ↓
确定 Element Value
       ↓
记录 Source / Time
       ↓
Element

37.3.3 PHP 示例

protected function extractElements($content)
{
    $elements = array();

    foreach ($content as $key => $value) {

        $type = 'value';

        if ($key === 'room') {
            $type = 'location';
        }

        if ($key === 'temperature') {
            $type = 'property';
        }

        if ($key === 'person') {
            $type = 'object';
        }

        if ($key === 'fan') {
            $type = 'object';
        }

        $elements[] = array(
            'type'  => $type,
            'value' => $value
        );
    }

    return $elements;
}

这个例子只是说明元素提取的基本结构。

实际系统需要根据不同 Information Type 建立更加明确的提取规则。


37.4 对象识别

Element 提取以后,PerceptionEngine 需要识别哪些 Element 可以形成 Object。

例如:

fan
person
living-room

其中:

fan → Object
person → Object
living-room → Object

而:

temperature
32
off

更适合成为:

Property
Value
State

37.4.1 对象识别流程

Elements
   ↓
筛选 Object Element
   ↓
确定 Object Type
   ↓
查找已有 Object
   ↓
Object Match
   ↓
已有 → 更新
不存在 → 创建

这一步与第35章的:

Element → Object

直接对应。


37.4.2 已有对象匹配

例如系统已经存在:

fan-001
type = fan

新场景再次发现:

fan

PerceptionEngine 不应该每次都创建:

fan-002
fan-003
fan-004

而应该首先进行对象匹配:

Element
   ↓
ObjectMatcher
   ↓
fan-001

然后更新它。


37.4.3 新对象

如果没有匹配到:

Element
fan

则:

Create Object
       ↓
fan-001

所以对象识别包含两个结果:

已有对象
   ↓
更新

新对象
   ↓
创建

37.5 属性识别

Object 建立以后,PerceptionEngine 继续识别 Property。

例如:

fan
speed = 1200
location = living-room

形成:

fan-001
├── speed = 1200
└── location = living-room

37.5.1 属性识别流程

Object
   ↓
相关 Elements
   ↓
Property Name
   ↓
Property Value
   ↓
Property Source
   ↓
Property

例如:

temperature = 32

识别为:

Property
{
    name: temperature,
    value: 32,
    source: temperature-001
}

37.5.2 属性更新

如果已有:

fan-001
speed = 800

新的感知结果:

speed = 1200

则:

800
 ↓
1200

形成:

PropertyChanged

而不是创建一个新的 Fan。


37.6 状态识别

状态是 PerceptionEngine 对 Object 当前状态的重要识别结果。

例如:

fan = off
person = present
room = occupied

形成:

fan-001
state = off

person-001
state = present

room-001
state = occupied

37.6.1 状态识别流程

Information / Scene
       ↓
状态 Element
       ↓
匹配 Object
       ↓
读取 State Value
       ↓
Object State

例如:

Element
type  = state
value = off
source = fan-001

形成:

fan-001
state = off

37.6.2 状态变化

如果上一状态:

off

当前状态:

on

PerceptionEngine 可以产生:

StateChange
{
    object: fan-001,
    old: off,
    new: on
}

这个变化结果可以继续进入:

Memory
Feedback
Reasoning
Learning

具体分发由 Central、Dispatcher 和 Coordinator 负责。


37.7 关系识别

当 Object 已经建立后,PerceptionEngine 还需要识别 Object 之间的 Relation。

例如:

客厅里有一个人和一个风扇。

可以形成:

person-001
    ↓
located_at
    ↓
room-001

以及:

fan-001
    ↓
located_at
    ↓
room-001

37.7.1 关系识别流程

Scene
 ↓
Object Identification
 ↓
Object Pair / Group
 ↓
Relation Evidence
 ↓
Relation Type
 ↓
Relation

这里的关键是:

Relation 必须具有信息依据。

不能因为两个 Object 同时存在,就自动产生关系。


37.7.2 常见感知关系

located_at
inside
contains
near
far
left_of
right_of
front_of
behind
part_of
belongs_to
connected_to

这些主要属于空间、结构或从属关系。


37.7.3 因果关系的边界

例如:

Fan → located_at → Room

这是感知关系。

但:

TemperatureHigh
→ causes
→ FanOn

已经进入因果规则和推理领域。

因此:

PerceptionEngine
→ 识别关系

ReasoningEngine
→ 使用关系推理

37.8 感知结果

PerceptionEngine 最终需要形成统一的:

PerceptionResult

结构可以定义为:

PerceptionResult
{
    scene
    elements
    objects
    properties
    states
    relations
    changes
    time
    source
}

37.8.1 感知结果示例

输入:

客厅温度32℃,有人,风扇关闭。

输出:

PerceptionResult
│
├── Scene
│   └── living-room
│
├── Elements
│   ├── room
│   ├── temperature
│   ├── 32
│   ├── person
│   ├── present
│   ├── fan
│   └── off
│
├── Objects
│   ├── room-001
│   ├── person-001
│   └── fan-001
│
├── Properties
│   ├── room.temperature = 32
│   └── person.location = living-room
│
├── States
│   ├── room = occupied
│   ├── person = present
│   └── fan = off
│
└── Relations
    ├── person → located_at → room
    └── fan → located_at → room

这就是 CognitionEngine 接收到的结构化输入。


37.9 PerceptionEngine 的内部结构

为了避免一个 Engine 变成巨大的单体类,可以继续划分内部处理器:

PerceptionEngine
│
├── InformationAnalyzer
│
├── ElementExtractor
│
├── ObjectRecognizer
│
├── PropertyRecognizer
│
├── StateRecognizer
│
└── RelationRecognizer

数据流:

Information / Scene
        ↓
InformationAnalyzer
        ↓
ElementExtractor
        ↓
ObjectRecognizer
        ↓
PropertyRecognizer
        ↓
StateRecognizer
        ↓
RelationRecognizer
        ↓
PerceptionResult

这样可以让 PerceptionEngine 成为流程协调者,而不是把所有具体规则全部塞进一个类。


37.10 完整代码示例

下面使用一个简化的 PHP OOP 示例,把本章的处理流程连接起来。

<?php

namespace SAI\Perception;

class PerceptionEngine
{
    public function process(array $scene)
    {
        $result = array(
            'scene'     => $scene,
            'elements'  => array(),
            'objects'   => array(),
            'properties'=> array(),
            'states'    => array(),
            'relations' => array(),
            'changes'   => array()
        );

        // 1. 信息分析
        $analysis = $this->analyze($scene);

        // 2. 元素提取
        $result['elements'] =
            $this->extractElements($analysis);

        // 3. 对象识别
        $result['objects'] =
            $this->recognizeObjects(
                $result['elements']
            );

        // 4. 属性识别
        $result['properties'] =
            $this->recognizeProperties(
                $result['objects'],
                $result['elements']
            );

        // 5. 状态识别
        $result['states'] =
            $this->recognizeStates(
                $result['objects'],
                $result['elements']
            );

        // 6. 关系识别
        $result['relations'] =
            $this->recognizeRelations(
                $result['objects'],
                $scene
            );

        return $result;
    }

    protected function analyze(array $scene)
    {
        return $scene;
    }

    protected function extractElements(array $data)
    {
        $elements = array();

        foreach ($data as $key => $value) {

            $type = 'value';

            if ($key === 'room') {
                $type = 'location';
            }

            if ($key === 'temperature') {
                $type = 'property';
            }

            if ($key === 'person') {
                $type = 'object';
            }

            if ($key === 'fan') {
                $type = 'object';
            }

            $elements[] = array(
                'type'  => $type,
                'name'  => $key,
                'value' => $value
            );
        }

        return $elements;
    }

    protected function recognizeObjects(
        array $elements
    ) {
        $objects = array();

        foreach ($elements as $element) {

            if ($element['type'] === 'object') {

                $objects[] = array(
                    'id' => $element['name'] . '-001',
                    'type' => $element['name']
                );
            }

            if ($element['type'] === 'location') {

                $objects[] = array(
                    'id' => 'room-001',
                    'type' => 'room',
                    'name' => $element['value']
                );
            }
        }

        return $objects;
    }

    protected function recognizeProperties(
        array $objects,
        array $elements
    ) {
        $properties = array();

        foreach ($elements as $element) {

            if ($element['name'] === 'temperature') {

                $properties[] = array(
                    'object' => 'room-001',
                    'name' => 'temperature',
                    'value' => $element['value']
                );
            }
        }

        return $properties;
    }

    protected function recognizeStates(
        array $objects,
        array $elements
    ) {
        $states = array();

        foreach ($elements as $element) {

            if ($element['name'] === 'person') {

                $states[] = array(
                    'object' => 'person-001',
                    'state' => $element['value']
                );
            }

            if ($element['name'] === 'fan') {

                $states[] = array(
                    'object' => 'fan-001',
                    'state' => $element['value']
                );
            }
        }

        return $states;
    }

    protected function recognizeRelations(
        array $objects,
        array $scene
    ) {
        $relations = array();

        $relations[] = array(
            'subject' => 'person-001',
            'type' => 'located_at',
            'object' => 'room-001'
        );

        $relations[] = array(
            'subject' => 'fan-001',
            'type' => 'located_at',
            'object' => 'room-001'
        );

        return $relations;
    }
}

37.11 调用 PerceptionEngine

输入场景:

$scene = array(
    'room' => 'living-room',
    'temperature' => 32,
    'person' => 'present',
    'fan' => 'off'
);

$engine = new PerceptionEngine();

$result = $engine->process($scene);

结果结构:

$result
│
├── scene
├── elements
├── objects
├── properties
├── states
├── relations
└── changes

例如:

objects
├── room-001
├── person-001
└── fan-001

属性:

properties
└── room-001
    temperature = 32

状态:

states
├── person-001 = present
└── fan-001 = off

关系:

relations
├── person-001 → located_at → room-001
└── fan-001 → located_at → room-001

37.12 PerceptionEngine 完整运行链

将本章全部内容连接起来:

Information / Scene
        ↓
┌──────────────────────┐
│   PerceptionEngine   │
└──────────────────────┘
        ↓
 Information Analysis
        ↓
 Element Extraction
        ↓
 Object Recognition
        ↓
 Property Recognition
        ↓
 State Recognition
        ↓
 Relation Recognition
        ↓
 PerceptionResult
        ↓
 CognitionEngine

从 SAI 整体来看:

External World
      ↓
InformationSource
      ↓
InformationReceiver
      ↓
Information
      ↓
SceneCollector
      ↓
Scene
      ↓
PerceptionEngine
      ↓
PerceptionResult
      ↓
CognitionEngine
      ↓
ReasoningEngine
      ↓
DecisionEngine
      ↓
BehaviorEngine

37.13 PerceptionEngine 与 EOM

第34章的 EOM:

Element
   ↓
Object
   ├── Property
   ├── State
   ├── Relation
   └── Method

第37章的 PerceptionEngine:

Scene
   ↓
Element
   ↓
Object
   ├── Property
   ├── State
   └── Relation

两者之间的关系是:

PerceptionEngine
        ↓
发现 / 识别
        ↓
EOM
        ↓
组织 / 保存

其中 Method 有一点特殊:

Element
 ↓
Object
 ↓
Property / State / Relation

Object Type / Capability Definition
 ↓
Method

也就是说,PerceptionEngine 主要发现当前世界中有什么,而 Object 的 Method 通常来自对象类型或能力定义。

因此:

PerceptionEngine
→ 识别世界

EOM
→ 组织世界

CognitionEngine
→ 理解世界

37.14 本章核心边界

PerceptionEngine 最容易出现的问题,是把所有“智能处理”全部放进去。

这是错误的。

正确边界:

PerceptionEngine
├── 信息分析
├── 元素提取
├── 对象识别
├── 属性识别
├── 状态识别
├── 关系识别
└── 感知结果

而:

CognitionEngine
→ 理解

ReasoningEngine
→ 推理

DecisionEngine
→ 决策

BehaviorEngine
→ 行为

形成明确的 Engine 分工:

PerceptionEngine
       ↓
“发现什么”

CognitionEngine
       ↓
“理解什么”

ReasoningEngine
       ↓
“能够推出什么”

DecisionEngine
       ↓
“选择什么”

BehaviorEngine
       ↓
“执行什么”

本章小结

第37章建立了 PerceptionEngine(感知引擎)

核心定义:

PerceptionEngine 是 SAI Framework 中负责执行感知处理的核心 Engine,它将 Information 或 Scene 经过分析、元素提取、对象识别、属性识别、状态识别和关系识别,形成结构化 PerceptionResult,并将结果交给后续 Cognition。

完整过程:

Information / Scene
        ↓
信息分析
        ↓
元素提取
        ↓
对象识别
        ↓
属性识别
        ↓
状态识别
        ↓
关系识别
        ↓
PerceptionResult
        ↓
Cognition

PerceptionEngine 的核心输出:

PerceptionResult
├── Elements
├── Objects
├── Properties
├── States
├── Relations
└── Changes

最终形成:

Scene
 ↓
PerceptionEngine
 ↓
Element
 ↓
Object
 ├── Property
 ├── State
 └── Relation
 ↓
PerceptionResult
 ↓
CognitionEngine

因此,第36章解决的是:

Perception 是什么?

第37章解决的是:

Perception 如何通过 Engine 实际执行?

下一章进入 第38章 Cognition,将从“识别出了什么”正式进入“理解了什么”:

Perception
    ↓
“我看到了 Fan、Room、Person、32℃、OFF”
    ↓
Cognition
    ↓
“我理解这些对象、属性、状态和关系代表什么”

以上 PHP 代码是本章的设计示例,用于说明 SAI Framework 的 OOP 结构,未作为实际项目代码执行验证。

Leave a Reply

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