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

第61章 从认知模型到状态机

第61章 从认知模型到状态机

第60章建立了:

Individual
    ↓
Cognitive Model
    ↓
Perception
Memory
Knowledge
Reasoning
Decision
Action
Learning

但是,一个认知模型如果只是静态结构,还不能真正运行。

要让认知模型按照一定顺序发生变化、响应输入、执行方法、产生结果并进入下一阶段,就必须引入:

State Machine(状态机)

因此第61章解决的是:

认知模型
    ↓
认知状态
    ↓
状态转换
    ↓
状态机
    ↓
可执行认知流程

61.1 为什么需要状态机

认知不是静态的。

同一个个体在不同时间可能处于:

Idle
Perceiving
Recognizing
Matching
Reasoning
Deciding
Acting
Learning

例如:

输入尚未到达
    ↓
Idle

输入到达:

Idle
 ↓
Perceiving

完成感知:

Perceiving
 ↓
Recognizing

识别完成:

Recognizing
 ↓
Matching

匹配完成:

Matching
 ↓
Reasoning

因此:

认知模型描述“有什么”,状态机描述“现在正在发生什么”。


61.2 Cognitive Model 与 State Machine

二者不是同一个概念。

Cognitive Model
=
认知系统的结构

而:

State Machine
=
认知系统的运行过程

可以表示:

Cognitive Model
        │
        ├── Objects
        ├── Memory
        ├── Knowledge
        ├── Methods
        └── Rules
                │
                ↓
          State Machine
                ↓
          Runtime Process

因此:

Model = Structure
State Machine = Process

61.3 什么是状态

状态是:

系统在某一时刻所处的可识别运行条件。

例如:

Idle

表示:

没有正在执行的认知任务

而:

Perceiving

表示:

系统正在处理输入信息

因此状态不是简单的字符串。

它代表:

Current Cognitive Condition

61.4 认知状态

WSaiOS 可以把认知状态定义为:

Cognitive State
{
    state_id
    state_type
    context
    input
    memory_state
    task_state
    result
}

例如:

state_type = matching

表示当前认知过程处于匹配阶段。


61.5 状态的基本结构

一个完整状态可以表示为:

State
├── Identity
├── Condition
├── Context
├── Input
├── Process
├── Output
└── Transition

其中:

Identity
    当前是什么状态

Condition
    为什么进入该状态

Context
    当前上下文

Input
    当前输入

Process
    当前执行过程

Output
    当前产生结果

Transition
    下一步可以进入什么状态

61.6 什么是状态转换

状态转换表示:

系统从一个状态进入另一个状态的过程。

基本结构:

State A
   ↓
Condition
   ↓
State B

例如:

Idle
 ↓
input_received
 ↓
Perceiving

又例如:

Matching
 ↓
match_success
 ↓
Reasoning

所以:

Transition
=
State
+
Condition
+
Action
+
Next State

61.7 State Machine 的基本结构

一个状态机至少包含:

State
Transition
Event
Condition
Action

可以表示为:

             Event
               ↓
Current State
      ↓
 Condition
      ↓
   Action
      ↓
Next State

因此:

State Machine
=
States
+
Transitions
+
Events
+
Conditions
+
Actions

61.8 认知状态机

将前面的认知模型放入状态机:

Idle
 ↓
Perception
 ↓
Element Extraction
 ↓
Object Recognition
 ↓
Matching
 ↓
Reasoning
 ↓
Decision
 ↓
Action
 ↓
Learning
 ↓
Idle

这就是一个最基本的:

Cognitive State Machine

61.9 状态与 Method

第59章已经定义:

Method
=
认知任务的工程实现

因此状态机中的每个状态可以调用 Method。

例如:

Perceiving
    ↓
perceive()

Recognizing
    ↓
recognize()

Matching
    ↓
match()

Reasoning
    ↓
reason()

Decision
    ↓
decide()

Learning
    ↓
learn()

形成:

State
  ↓
Method
  ↓
Result
  ↓
Transition

这使状态机真正具备执行能力。


61.10 State 不等于 Method

这一点非常重要。

State
=
系统当前处于什么阶段

而:

Method
=
系统在该阶段执行什么操作

例如:

State:
Matching

对应:

Method:
matchObject()

所以:

State ≠ Method

而是:

State
 ↓
Invoke Method
 ↓
Result
 ↓
Transition

61.11 Event

状态机不能无缘无故转换。

通常需要 Event。

例如:

input_received
object_detected
match_completed
reasoning_completed
decision_created
action_completed
learning_completed

形成:

Current State
       +
Event
       ↓
Transition

例如:

Idle
+
input_received
       ↓
Perceiving

61.12 Condition

Event 出现以后,还可能需要 Condition。

例如:

Matching
    ↓
match_score >= 0.8
    ↓
Reasoning

而:

Matching
    ↓
match_score < 0.8
    ↓
Memory Retrieval

所以:

Event
    ↓
Condition
    ↓
Transition

可以形成条件分支。


61.13 Action

状态转换过程中可以执行 Action。

例如:

Matching
 ↓
match_completed
 ↓
Action:
store_match_result()
 ↓
Reasoning

因此完整转换可以定义为:

Transition
{
    from
    event
    condition
    action
    to
}

例如:

{
    "from": "matching",
    "event": "match_completed",
    "condition": "score >= 0.8",
    "action": "store_result",
    "to": "reasoning"
}

这已经非常接近实际程序中的状态机定义。


61.14 认知状态机的分支

真实认知过程不是一条直线。

例如:

                    ┌──→ Memory Retrieval
                    │
Matching ───────────┤
                    │
                    └──→ Reasoning

如果匹配成功:

Matching
 ↓
Reasoning

如果没有找到匹配:

Matching
 ↓
Memory Retrieval

如果仍然没有结果:

Memory Retrieval
 ↓
Unknown

因此状态机可以表达:

确定
不确定
失败
成功
等待
重试

等认知状态。


61.15 Unknown State

认知系统不能假设所有输入都可以识别。

因此必须允许:

Unknown

例如:

Input
 ↓
Perception
 ↓
Recognition
 ↓
Unknown

这表示:

当前个体认知模型中没有足够的信息确定该对象。

随后可以:

Unknown
 ↓
Memory Search

或者:

Unknown
 ↓
Learning

因此 Unknown 本身也是一种有效认知状态。


61.16 Error State

除了 Unknown,还需要:

Error

例如:

Method 执行失败
Data 数据错误
Rule 无法执行
State 转换失败

可以进入:

Error

状态。

于是:

Cognitive State Machine

不仅描述正常认知流程,也描述异常流程。


61.17 Waiting State

认知系统还可能需要等待:

Waiting

例如:

Waiting
 ↓
External Input
 ↓
Perceiving

因此一个完整系统可能存在:

Idle
Waiting
Perceiving
Recognizing
Matching
Reasoning
Deciding
Acting
Learning
Unknown
Error

61.18 状态机与认知循环

第60章建立了认知循环:

Perception
 ↓
Matching
 ↓
Reasoning
 ↓
Decision
 ↓
Action
 ↓
Learning

第61章将其形式化:

State Machine

变成:

Idle
 ↓
Perceiving
 ↓
Recognizing
 ↓
Matching
 ↓
Reasoning
 ↓
Deciding
 ↓
Acting
 ↓
Learning
 └────────→ Idle

因此:

状态机是认知循环的工程执行模型。


61.19 状态机与数据结构

第58章解决:

理论对象
 ↓
数据对象

第61章进一步解决:

理论状态
 ↓
状态数据

例如:

理论:

State = Matching

数据:

{
    "state": "matching",
    "task_id": 1001,
    "object_a": 2001,
    "object_b": 3001
}

于是状态也可以被系统保存、读取和恢复。


61.20 状态机与 Method 的完整关系

现在可以形成:

State
  ↓
Event
  ↓
Condition
  ↓
Method
  ↓
Result
  ↓
Transition
  ↓
Next State

例如:

Matching
    ↓
match_requested
    ↓
load_objects()
    ↓
matchObject()
    ↓
score = 0.92
    ↓
score >= 0.8
    ↓
Reasoning

这就是一个完整的认知执行单元。


61.21 状态机与 Matching

第59章的 Matching Algorithm 可以直接嵌入状态机。

Recognizing
     ↓
Matching
     ↓
Matching Algorithm
     ↓
Matching Result
     ↓
Condition

例如:

score >= 0.8

进入:

Reasoning

而:

score < 0.8

进入:

Memory Retrieval

因此:

Matching

不仅产生结果,还可以:

驱动认知状态发生变化。


61.22 状态机的数据模型

工程上可以定义:

State
{
    id,
    name,
    type,
    context,
    status
}

Transition:

Transition
{
    id,
    from_state,
    event,
    condition,
    action,
    to_state
}

Event:

Event
{
    id,
    type,
    payload,
    timestamp
}

于是:

State
+
Transition
+
Event

就构成一个可以实际运行的数据模型。


61.23 Cognitive State Machine

最终可以形成:

CognitiveStateMachine
{
    current_state,
    states,
    transitions,
    events,
    context,
    memory,
    task
}

运行过程:

receive(event)
       ↓
find transition
       ↓
check condition
       ↓
execute action
       ↓
invoke method
       ↓
update state
       ↓
store result

这就从理论状态机进入了实际的:

Runtime Cognitive Engine

61.24 状态机不是知识库

必须再次明确边界。

Knowledge
=
系统知道什么
Memory
=
系统保存什么
State
=
系统现在处于什么状态
Method
=
系统正在执行什么操作
State Machine
=
系统如何从一个运行状态进入另一个运行状态

因此:

Knowledge ≠ State
Memory ≠ State
Method ≠ State

它们是不同层次的认知工程结构。


61.25 状态机与个体模型

第60章:

Individual Model

描述:

这个个体具有什么认知结构

第61章:

Cognitive State Machine

描述:

这个个体当前如何运行

因此:

Individual Model
        ↓
Cognitive State Machine
        ↓
Runtime

最终:

Individual
   ↓
Model
   ↓
State Machine
   ↓
Execution
   ↓
Experience
   ↓
Learning
   ↓
Model Update

形成完整闭环。


61.26 从认知模型到运行系统

到此可以建立一个完整的工程层级:

Theory
   ↓
Cognitive Model
   ↓
Data Model
   ↓
State Model
   ↓
State Machine
   ↓
Method Execution
   ↓
Runtime

对应:

理论
 ↓
认知结构
 ↓
数据结构
 ↓
状态结构
 ↓
状态转换
 ↓
算法执行
 ↓
系统运行

这意味着 WSaiOS 的认知理论开始真正进入运行时架构


61.27 第61章核心定义

定义一:State

State 是认知系统在特定时间和上下文中的可识别运行条件。

定义二:Transition

Transition 是认知系统从一个状态根据事件、条件和动作进入另一个状态的过程。

定义三:State Machine

State Machine 是由状态、事件、条件、动作和状态转换构成的可执行过程模型。

定义四:Cognitive State Machine

Cognitive State Machine 是将个体认知模型转化为可执行状态转换过程的工程机制。


61.28 第61章最终模型

最终形成:

             Individual
                  ↓
          Individual Model
                  ↓
         Cognitive Structure
                  ↓
          Cognitive States
                  ↓
        ┌─────────────────┐
        │  State Machine  │
        └────────┬────────┘
                 ↓
        ┌─────────────────┐
        │     Event       │
        └────────┬────────┘
                 ↓
        ┌─────────────────┐
        │    Condition    │
        └────────┬────────┘
                 ↓
        ┌─────────────────┐
        │     Method      │
        └────────┬────────┘
                 ↓
        ┌─────────────────┐
        │     Result      │
        └────────┬────────┘
                 ↓
          State Transition
                 ↓
             Next State

最终可以压缩成一句话:

认知模型定义个体具有什么认知结构,状态机定义这些认知结构如何按照事件、条件和方法持续运行。

因此,第61章完成了一个关键转换:

认知模型
    ↓
状态模型
    ↓
状态机
    ↓
认知执行

到这里,WSaiOS 已经从:

Element
→ Object
→ Class
→ Relation
→ Method
→ Matching
→ Data
→ Individual
→ Cognitive Model

进一步进入:

Cognitive State
→ State Transition
→ State Machine
→ Runtime Cognition

这为后续建立 第62章:从状态机到认知执行引擎 奠定基础。

Leave a Reply

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