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

第215章 Method Selection|方法选择

第215章 Method Selection|方法选择

第214章已经解决:

多个 Method
    ↓
Method Evaluation
    ↓
得到每个 Method 的当前评价值

第215章解决下一步:

ICAI 如何从多个已经评价过的 Method 中,选择当前最适合执行的 Method。

因此本章不是重新评价 Method,而是把:

Evaluation

转化为:

Selection

215.1 Method Selection 的位置

当前完整链路:

Current State
      ↓
Candidate Methods
      ↓
Method Evaluation
      ↓
Method Selection
      ↓
Selected Method
      ↓
Action

三个阶段必须严格分开:

Generation
    ↓
产生候选方法

Evaluation
    ↓
评价候选方法

Selection
    ↓
选择最终方法

所以:

Method Evaluation ≠ Method Selection

215.2 Selection 的基本定义

定义:

Selection = Select(Evaluations, State)

也就是:

当前 State
+
候选 Method 的 Evaluation
        ↓
Selected Method

最基础形式:

SelectedMethod
=
argmax(E(M,S))

即:

在当前状态下,选择评价值最高的可用 Method。

例如:

M001 = 0.71
M002 = 0.86
M003 = 0.63

则:

SelectedMethod = M002

215.3 不能简单理解为“最高分”

虽然第一版可以:

最高 Score → Selected Method

但真正的 Selection 还必须考虑:

Availability
Constraint
Execution Readiness
Conflict
Threshold

因此完整关系:

Evaluation
    ↓
Eligibility
    ↓
Selection

一个 Method 即使评价很高,如果:

不可执行
资源不足
违反约束
状态已经变化

也不能选择。


215.4 Method Eligibility

因此增加:

Method Eligibility

即:

当前 Method 是否具备被选择的资格。

可以定义:

class MethodSelector
{
    public function select(
        array $evaluations,
        State $state
    ): ?MethodEvaluation {

        $eligible = [];

        foreach ($evaluations as $evaluation) {

            if (!$this->isEligible(
                $evaluation,
                $state
            )) {
                continue;
            }

            $eligible[] = $evaluation;
        }

        if (empty($eligible)) {
            return null;
        }

        usort(
            $eligible,
            function (
                MethodEvaluation $a,
                MethodEvaluation $b
            ) {
                return
                    $b->getScore()
                    <=>
                    $a->getScore();
            }
        );

        return $eligible[0];
    }

    protected function isEligible(
        MethodEvaluation $evaluation,
        State $state
    ): bool {

        return true;
    }
}

这里形成:

Evaluation
     ↓
Eligibility Check
     ↓
Eligible Methods
     ↓
Ranking
     ↓
Selected Method

215.5 Selection Threshold

不能保证任何情况下都有可接受的方法。

例如:

M001 = 0.42
M002 = 0.38
M003 = 0.31

虽然:

M001

是最高的。

但如果系统规定:

MinimumScore = 0.50

那么:

M001 < 0.50

所以:

No Selection

而不是强行选择 M001。

可以定义:

protected float $minimumScore = 0.50;

判断:

if ($evaluation->getScore() < $this->minimumScore) {
    continue;
}

于是:

最高分
≠
一定选择

真正的逻辑是:

满足最低条件
+
当前最优
=
Selected Method

215.6 Selection 与 State

这是第215章的核心。

即使:

M001

刚刚被选中:

State S1

只要 State 发生变化:

S1 → S2

原来的选择就可能失效。

因此:

Selection(M,S1)

与:

Selection(M,S2)

是两个不同的计算。

例如:

S1
数据完整
    ↓
CompareMethod

状态改变:

S2
发现数据缺失
    ↓
RepairMethod

所以:

State Change
    ↓
Re-Evaluation
    ↓
Re-Selection

而不是:

第一次选择
    ↓
一直执行到底

215.7 Selection 不产生新 Method

需要保持一个重要边界:

Method Generation

负责:

产生 Method

而:

Method Selection

只负责:

从候选 Method 中选择

因此:

Selection

不应该直接创建一个全新的方法。

结构:

Method Repository
      ↓
Candidate Methods
      ↓
Evaluator
      ↓
Evaluations
      ↓
Selector
      ↓
Selected Method

215.8 Method Selection Class

可以形成第一版:

class MethodSelector
{
    protected float $minimumScore;

    public function __construct(
        float $minimumScore = 0.50
    ) {
        $this->minimumScore = $minimumScore;
    }

    public function select(
        array $evaluations,
        State $state
    ): ?MethodEvaluation {

        $eligible = [];

        foreach ($evaluations as $evaluation) {

            if (
                !$this->isEligible(
                    $evaluation,
                    $state
                )
            ) {
                continue;
            }

            if (
                $evaluation->getScore()
                < $this->minimumScore
            ) {
                continue;
            }

            $eligible[] = $evaluation;
        }

        if (!$eligible) {
            return null;
        }

        usort(
            $eligible,
            function (
                MethodEvaluation $a,
                MethodEvaluation $b
            ) {
                return
                    $b->getScore()
                    <=>
                    $a->getScore();
            }
        );

        return $eligible[0];
    }

    protected function isEligible(
        MethodEvaluation $evaluation,
        State $state
    ): bool {

        return true;
    }
}

这就是最基础的:

MethodSelector

215.9 Selection Result

最好不要只返回一个 Method

应该建立:

SelectionResult

因为系统还需要知道:

选择了谁
为什么
候选有哪些
最终分数是多少
是否通过阈值

例如:

class SelectionResult
{
    protected ?MethodEvaluation $selected = null;

    protected array $candidates = [];

    protected string $status = 'none';

    public function setSelected(
        MethodEvaluation $evaluation
    ): void {
        $this->selected = $evaluation;
        $this->status = 'selected';
    }

    public function getSelected():
        ?MethodEvaluation
    {
        return $this->selected;
    }

    public function setCandidates(
        array $candidates
    ): void {
        $this->candidates = $candidates;
    }

    public function getStatus(): string
    {
        return $this->status;
    }
}

这样 Selection 产生的不是:

M002

而是:

SelectionResult
{
    status: selected,
    selected: M002,
    score: 0.86
}

215.10 为什么需要 Selection Result

因为 ICAI 后续必须知道:

为什么选择 M002?

系统不需要自然语言解释。

它可以保存:

SelectionResult
│
├── selected_method
├── selected_score
├── threshold
├── candidate_count
├── rejected_methods
└── selection_state

例如:

Selected:
M002

Score:
0.86

Threshold:
0.50

Candidates:
3

Rejected:
M001
M003

这样 Selection 本身就是一个可追踪的机器状态。


215.11 Tie Handling

可能出现:

M001 = 0.82
M002 = 0.82

这时不能简单认为:

第一个 = 最优

应该进入 Tie Resolution。

第一层可以使用:

Score
 ↓
Historical Performance
 ↓
Cost
 ↓
Risk
 ↓
Priority

例如:

M001
score = 0.82
cost = 0.30

M002
score = 0.82
cost = 0.15

则:

M002

优先。

因此 Selection 可以有:

Primary Score
    ↓
Tie Break

215.12 Selection 与 Constraint

约束必须优先于普通评分。

例如:

M001
Score = 0.95

但是:

Constraint = violated

那么:

M001 → 不可选择

而:

M002
Score = 0.80
Constraint = valid

最终:

Selected = M002

所以:

Constraint

不是简单的减分项。

有些 Constraint 是:

Hard Constraint

一旦违反:

直接排除

而另一些可以是:

Soft Constraint

才可以进入评分。

这一区分会成为后续 Decision System 的基础。


215.13 Selection 的完整计算

第215章可以把 Selection 定义为:

Candidates
    ↓
Eligibility
    ↓
Hard Constraint Check
    ↓
Minimum Score Check
    ↓
Ranking
    ↓
Tie Resolution
    ↓
Selected Method

完整形式:

                     Candidate Methods
                            │
                            ▼
                       Evaluation
                            │
                            ▼
                       Eligibility
                            │
                 ┌──────────┴──────────┐
                 │                     │
              Invalid                Valid
                 │                     │
              Reject                   ▼
                              Constraint Check
                                      │
                                      ▼
                                Score Threshold
                                      │
                                      ▼
                                   Ranking
                                      │
                                      ▼
                                Tie Resolution
                                      │
                                      ▼
                              Selected Method

215.14 Selection 与 Action 的边界

选择完成后:

Selected Method

才进入:

Action

因此:

Cognition
   ↓
Dynamic Method
   ↓
Evaluation
   ↓
Selection
   ↓
Selected Method
   ↓
Action

这里每一层都有明确职责:

核心职责
Cognition 当前需要解决什么
Dynamic Method 当前有哪些可能方法
Evaluation 每个方法当前有多合适
Selection 当前最终选哪个
Action 执行所选方法

这几个层次不能合并。


215.15 Selection 后状态变化

选择并不意味着整个过程结束。

例如:

State S1
 ↓
Select M002
 ↓
Action
 ↓
Partial Result

如果结果导致:

State S2

那么系统应该重新:

S2
 ↓
Generate Candidates
 ↓
Evaluate
 ↓
Select

因此 ICAI 开始形成:

              ┌──────────────────────┐
              │                      │
              ▼                      │
         Current State              │
              │                      │
              ▼                      │
       Dynamic Method               │
              │                      │
              ▼                      │
       Method Evaluation            │
              │                      │
              ▼                      │
       Method Selection             │
              │                      │
              ▼                      │
            Action                  │
              │                      │
              ▼                      │
           Feedback ────────────────┘

这意味着:

Method Selection 是一次状态相关的选择,而不是永久决策。


215.16 与 Decision 的区别

这里提前建立一个边界非常重要。

Method Selection

回答:

用什么方法?

而未来的:

Decision

回答:

采取什么决定?

例如:

Goal:
寻找供应商

Method Selection:
选择 MultiConditionSearchMethod

Action:
执行搜索

Result:
发现 5 个候选

Decision:
选择 Supplier B

所以:

Method Selection ≠ Object Decision

前者选择:

方法

后者选择:

对象 / 行为 / 结果

这会让 ICAI 后面的 Decision Class 保持清晰。


215.17 第215章最终结构

至此,ICAI 方法体系已经形成:

                    Cognition
                        │
                        ▼
                Current State
                        │
                        ▼
                Dynamic Method
                        │
                        ▼
              Candidate Methods
                        │
                        ▼
              Method Evaluation
                        │
                        ▼
              Method Selection
                        │
                        ▼
                Selected Method
                        │
                        ▼
                     Action

其中:

Dynamic Method

解决:

有哪些可能的方法?
Method Evaluation

解决:

这些方法当前分别有多合适?
Method Selection

解决:

当前最终选择哪个?

215.18 本章核心公式

最终可以定义:

S*
=
Select(
    {M₁, M₂, ..., Mₙ},
    State
)

其中:

S*

是当前选择结果。

更完整:

S*
=
argmax(
    E(M,S)
)

但必须满足:

Eligibility(M,S) = true

Constraint(M,S) = valid

E(M,S) ≥ Threshold

因此:

Selected Method
=
Highest Evaluated
+
Eligible
+
Constraint Valid
+
Threshold Passed

第215章完成后的 ICAI 方法链

第211章 Method Class
        ↓
方法对象

第213章 Dynamic Method
        ↓
根据当前状态产生可能方法

第214章 Method Evaluation
        ↓
评价每个可能方法

第215章 Method Selection
        ↓
选择当前最适合的方法

        ↓

Selected Method
        ↓
Action

这样 Method 已经完成从静态对象 → 动态产生 → 状态评价 → 当前选择的完整演化。

下一步自然进入 第216章 Method Execution|方法执行:不是重新讨论 Action,而是定义“被 Selection 选中的 Method 如何转化为一个可执行的 Action Sequence”。

Leave a Reply

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