AI Wiki中文/EN

层次任务网络规划:先选分解,再逐步检查动作

Hierarchical task network planning

实现有界、完全有序的任务分解器,检查方法条件与原子动作,返回可核验计划,并区分有界穷尽和预算中断。

本页目录直觉理解数学与方法代码实现局限与误解实验与练习研究脉络

清单齐全,顺序仍可能错误

一台小型仪器需要校准、贴新标签和最终检验。校准会移除旧标签。因此,“贴标签、校准、检验”会在最后一步失败,尽管三项动作都列进了清单;“校准、贴标签、检验”才能成功。

层次任务网络规划同时表示具体动作,以及较大任务允许怎样拆解。这里的 prepare 是复合任务。方法把它替换成有序子任务列表;calibrate 等原子任务则有改变状态的操作符。选中方法是一项搜索决定,并不保证整个方法体都能成功。

1999年的 SHOP 论文发展了按执行顺序规划的有序任务分解:规划器因而知道下一任务之前的状态。其语义区分原子任务与方法,并允许在分解方案间回溯。下面的程序使用更小的有限词汇,并明确设置分解上限。Nau 等,§2.1—2.3。

方法改变待办任务,不直接改变世界

模型确定、状态完全已知、动作顺序执行。原子与任务名都是有限字符串,没有变量或参数代换。状态恰好列出已声明原子中为真的部分,其余为假。每个原子任务对应一个操作符,包含正负前提,以及互不相交的增删效果。复合任务名与原子任务名不重合;多个方法可以对应同一个复合任务。

一个搜索配置包含当前状态 s、有序待办列表和剩余方法额度 f;符号 ∥ 表示列表拼接。位于首位的原子任务改变状态,并从列表消失:

⟨s,(a)∥R,f⟩→a⟨(s∖Ea−)∪Ea+,R,f⟩if Pa+⊆s and Pa−∩s=∅.\langle s,(a)\mathbin{\|}R,f\rangle\xrightarrow{a}\langle(s\setminus E_a^-)\cup E_a^+,R,f\rangle\quad\text{if }P_a^+\subseteq s\text{ and }P_a^-\cap s=\varnothing.

首位复合任务 c 的某个方法 m 包含必须为真的条件 G⁺、必须为假的条件 G⁻,以及有序方法体 B。应用方法消耗一次额度,并替换这一次出现的 c:

⟨s,(c)∥R,f⟩→m⟨s,Bm∥R,f−1⟩if f>0, Gm+⊆s, Gm−∩s=∅.\langle s,(c)\mathbin{\|}R,f\rangle\xrightarrow{m}\langle s,B_m\mathbin{\|}R,f-1\rangle\quad\text{if }f>0,\ G_m^+\subseteq s,\ G_m^-\cap s=\varnothing.

这些条件在复合任务轮到列表首位时检查,不要求在整个方法体执行期间持续成立。后续复合任务会看到前面原子动作造成的变化;每个原子动作仍须在自己的执行位置检查前提。方法可用本身不能证明计划成立。

成功是指经过允许的分解和可用的原子动作后,待办列表为空。本接口没有额外终态目标。如果 prepare 应当蕴含 certified,就需要正确的方法库,或另加经过检查的后置条件。空方法体合法,也消耗一次方法额度;它不会自动把任何原子变真。

这里的初始网络和每个方法体都完全有序:方法体必须完成,才轮到原列表剩余任务。程序不支持兄弟子任务交错、绑定约束或保护区间。Erol、Hendler 与 Nau 的一般任务网络形式系统区分这些能力,复杂度结论也依赖相应限制。尤其不能把涉及部分有序初始网络的不可判定性结论,直接套到本接口上。1994年论文,印刷第1125—1126页。

在完整候选分支之间回溯

以下完整 Python 3.10+ 程序使用显式深度优先栈,优先探索最先声明的可用方法。它返回首次成功的分解,不保证原子动作最少。某个动作即使在当前状态可执行,若允许的方法无法产生它,也不能擅自加入任务的解。

max_methods 限制每个候选推导的方法应用总数,不是整个搜索的尝试次数,也不是嵌套深度。失败分支不消耗兄弟分支的额度。max_nodes 另外限制整个运行检查的非空配置数;检查一个配置可能测试多个方法条件。这两个上限都不包含输入校验时间。

found 返回原子计划、终态以及方法/动作事件证据。bounded-failure 表示方法额度内的全部候选均已穷尽,不证明更大额度下也无解。unknown 表示仍有未检查的非空配置,但结点预算让搜索停止。空任务列表在结点预算检查之前成功,包括预算为零的情况。cutoffs 统计已检查、却遇到零额度的复合任务配置;即使该任务没有可用方法也会计数,因此它不等于丢失的解数量。

搜索前完整校验所有声明,包括未使用的方法。输入使用列表或元组,名字必须是非空字符串。原子声明、复合任务声明、原子任务名及方法 ID 在各自命名空间内不可重复。方法头必须是已声明的复合任务,方法体每次出现的任务都必须已声明。重复事实会合并,重复任务会保留。矛盾条件或效果、未知名字、负额度、布尔额度和非整数额度均被拒绝。

def seq(value):
    if type(value) not in (list, tuple):
        raise ValueError('expected list or tuple')
    return tuple(value)


def named(value):
    if type(value) is not str or not value:
        raise ValueError('expected nonempty name')
    return value


def bound(value):
    if type(value) is not int or value < 0:
        raise ValueError('expected nonnegative integer')
    return value


def solve(atoms, actions, compounds, methods, initial, tasks,
          max_methods=4, max_nodes=None):
    atoms = tuple(named(a) for a in seq(atoms))
    compounds = tuple(named(t) for t in seq(compounds))
    if len(set(atoms)) != len(atoms) or len(set(compounds)) != len(compounds):
        raise ValueError('duplicate declaration')
    universe = frozenset(atoms)
    compound_set = frozenset(compounds)

    def facts(values):
        values = frozenset(named(v) for v in seq(values))
        if not values <= universe:
            raise ValueError('undeclared atom')
        return values

    operators = {}
    for raw in seq(actions):
        row = seq(raw)
        if len(row) != 5:
            raise ValueError('action needs id, positive, negative, add, delete')
        aid = named(row[0])
        if aid in operators or aid in compound_set:
            raise ValueError('duplicate or ambiguous task name')
        positive, negative, add, delete = map(facts, row[1:])
        if positive & negative or add & delete:
            raise ValueError('contradictory action fields')
        operators[aid] = (positive, negative, add, delete)
    declared = compound_set | operators.keys()

    def task_list(values):
        values = tuple(named(t) for t in seq(values))
        if any(t not in declared for t in values):
            raise ValueError('undeclared task')
        return values

    choices = {t: [] for t in compounds}
    method_ids = set()
    for raw in seq(methods):
        row = seq(raw)
        if len(row) != 5:
            raise ValueError('method needs id, head, positive, negative, body')
        mid, head = named(row[0]), named(row[1])
        if mid in method_ids or head not in choices:
            raise ValueError('duplicate method or noncompound head')
        method_ids.add(mid)
        positive, negative = facts(row[2]), facts(row[3])
        if positive & negative:
            raise ValueError('contradictory method guard')
        choices[head].append((mid, positive, negative, task_list(row[4])))
    initial, tasks = facts(initial), task_list(tasks)
    max_methods = bound(max_methods)
    if max_nodes is not None:
        max_nodes = bound(max_nodes)
    # Each alternative owns immutable state, pending tasks and certificate.
    stack = [(initial, tasks, max_methods, (), ())]
    nodes = action_checks = method_checks = cutoffs = 0

    def answer(status, state=None, moves=None, proof=None):
        return dict(status=status, plan=moves, proof=proof,
                    final=None if state is None else tuple(a for a in atoms if a in state),
                    nodes=nodes, action_checks=action_checks,
                    method_checks=method_checks, cutoffs=cutoffs)

    while stack:
        state, pending, fuel, moves, proof = stack[-1]
        if not pending:
            return answer('found', state, moves, proof)
        if max_nodes is not None and nodes >= max_nodes:
            return answer('unknown')
        stack.pop()
        nodes += 1
        task, tail = pending[0], pending[1:]
        if task in operators:
            action_checks += 1
            positive, negative, add, delete = operators[task]
            if positive <= state and negative.isdisjoint(state):
                successor = (state - delete) | add
                stack.append((successor, tail, fuel, moves + (task,),
                              proof + (('action', task),)))
        elif fuel == 0:
            cutoffs += 1
        else:
            # Reverse the pushes so the first declared method is tried first.
            for mid, positive, negative, body in reversed(choices[task]):
                method_checks += 1
                if positive <= state and negative.isdisjoint(state):
                    stack.append((state, body + tail, fuel - 1, moves,
                                  proof + (('method', mid),)))
    return answer('bounded-failure')


ATOMS = ('powered', 'calibrated', 'labelled', 'certified')
ACTIONS = (
    ('calibrate', ('powered',), (), ('calibrated',), ('labelled',)),
    ('label', ('powered',), (), ('labelled',), ()),
    ('certify', ('calibrated', 'labelled'), (), ('certified',), ()),
)
METHODS = (
    ('label-first', 'prepare', ('powered',), (), ('label', 'calibrate', 'certify')),
    ('calibrate-first', 'prepare', ('powered',), (), ('calibrate', 'label', 'certify')),
)


if __name__ == '__main__':
    args = (ATOMS, ACTIONS, ('prepare',), METHODS, ('powered',), ('prepare',))
    answer = solve(*args, max_methods=1)
    print('plan:', answer['plan'])
    print('proof:', answer['proof'])
    print('final:', answer['final'])
    print('work:', tuple(answer[k] for k in ('nodes', 'action_checks', 'method_checks', 'cutoffs')))
    print('node cap 6:', solve(*args, max_methods=1, max_nodes=6)['status'])
    print('method cap 0:', solve(*args, max_methods=0)['status'])
    recursive = (('again', 'loop', (), (), ('loop',)), ('stop', 'loop', (), (), ()))
    r = solve((), (), ('loop',), recursive, (), ('loop',), max_methods=2)
    print('recursive:', r['status'], r['proof'], r['nodes'], r['cutoffs'])

实际输出为:

plan: ('calibrate', 'label', 'certify')
proof: (('method', 'calibrate-first'), ('action', 'calibrate'), ('action', 'label'), ('action', 'certify'))
final: ('powered', 'calibrated', 'labelled', 'certified')
work: (7, 6, 2, 0)
node cap 6: unknown
method cap 0: bounded-failure
recursive: found (('method', 'again'), ('method', 'stop')) 3 1

第一个方法在 calibrate 移除 labelled 后失败。该分支的状态和计划不会泄漏到下个候选:栈中每项保存不可变的分支数据。成功证据只记录自身推导;六次动作检查则包含两条分支。结点预算6时,成功分支的最终检验还未被检查;预算7允许这一步,并随即返回成功。

为什么上限能保证终止

令 r 为初始任务数,q 为方法额度,b 为最长方法体的长度;没有方法时取 b = 0。每次分解最多引入 b 个任务,因此一条分支最多执行 r + qb 个原子任务,另加至多 q 次分解。待办列表与证据长度都不超过:

H=r+q(b+1).H=r+q(b+1).

令 M 为同一方法头对应的方法数最大值,无方法时为零;B = max(1,M)。已检查非空配置数 N 有保守上界:

N≤∑i=0HBi.N\le\sum_{i=0}^{H}B^i.

每条分支有限,每个结点的候选也有限,所以不设结点预算的搜索终止。沿返回证据归纳,可证明每次分解均获允许、每个原子动作均可用。反过来,使用至多 q 次方法的每份成功推导都出现在这棵有限搜索树中;穷尽它就完整回答了该有界问题。这里没有只按世界状态去重的已访问集合:世界相同、待办任务不同,义务仍然不同。

令 D 为包含常数1的原始输入规模,n 为原子数,其余量沿用上面的定义。这个实现的保守成本为:

T=O ⁣(D+(N+1)(M+1)(n+H+1)),S=O ⁣(D+(1+MH)(n+H+1)).T=O\!\left(D+(N+1)(M+1)(n+H+1)\right),\qquad S=O\!\left(D+(1+MH)(n+H+1)\right).

时间包含方法条件扫描,以及列表和证据复制。栈在每层分支上只保留线性数量的兄弟候选,每项保存状态、任务与证据元组。上界采用期望哈希及名字、计数器的单位成本模型;长字符串和大整数位长度还会增加成本。即使物理状态空间很小,增加 q 也可能大幅扩大搜索。

层次结构也限定哪些计划算数

方法库包含领域知识,也可能漏掉有用的方案。找到一串层次结构之外的可执行动作,不能补上这个缺口。反过来,空方法或草率方法可能让某个名字对应的任务“完成”,却没有达到名字暗示的意图。原子动作模型、分解规则以及预期结果需要分别检查。

不设方法上限时,loop → loop 会让朴素深度优先规划器永远停在同一分支,即使后面还有能解决任务的空方法。SHOP 论文明确区分有限搜索的完备性与这种无限分支失败。§2.3,PDF第2—3页 本实现允许递归,但递归必须消耗额度。不断增大上限,最终会容纳任何一份固定的有限推导;这不保证任意固定结点预算都能搜索到它。

程序没有实例化、合一、部分排序、动作成本、并发动作、不确定观察或执行监控。它不是 SHOP 或一般 HTN 语义的完整实现。语言模型列出的子任务也不自动成为 HTN 证据;任务声明、方法选择、条件和原子效果都需要实际检查。

检查分解允许做什么

  1. 第一个 prepare 方法为什么失败?给出成功的原子计划,并解释工作量计数。

    答案:最先贴的标签被校准移除,检验因此缺少 labelled。第二个方法得到 calibrate, label, certify。根配置检查两个方法条件;两条分支各有三个原子配置,因此共六次动作检查、七个已检查非空配置。返回证据只包含成功分支。

  2. 比较方法额度零、结点预算零和初始任务列表为空。哪种失败能证明不受额度限制的无解?

    答案:对 prepare 而言,方法额度零会在检查并截断该复合任务后返回 bounded-failure;方法额度为正、结点预算零时返回 unknown。初始任务为空时,即使两项上限都是零,也返回 found 和计划 ()。有界穷尽只涉及给定额度;预算中断不提供任何无解证明。

  3. 某方法要求 p 为真,方法体是 off, child。off 删除 p;唯一的 child 方法要求 p 为假,随后恢复它。这合法吗?如果父条件意在全程保持,又怎样?

    答案:按进入时检查条件的语义合法:父任务看到 p 真,子任务看到 off 后的 p 假。维持要求会禁止中间这段变化,需要额外约束;当前方法条件并不是维持要求。

  4. 方法依次为 loop → loop 和 loop → ()。为什么额度2时返回包含 again、stop 的证据?这说明结果最短,或者无上限 DFS 能终止吗?

    答案:DFS 先递归分解两次,遇到零额度的复合任务后截断;随后,第二层保存的空方法候选成功。选中的证据是 again, stop,原子计划为空。根层直接选 stop 只需更少分解,但本程序由输入顺序决定结果。取消上限会让第一条递归分支无限延伸。测试另外把 wait, finish 方法放在 finish 方法之前,说明首次计划连原子动作数也未必最少。

比较规划器之前,先读任务语义

1999年的 SHOP 来源是作者托管的六页技术报告版本,标明 CS-TR-3981 / UMIACS-TR-99-04,并注明将发表于 IJCAI-99。其中的简化操作符与更丰富的实现特性,都不等同于本章明确带前提的无变量教学语言。这里没有复用原始 Lisp 代码、交通例子或性能数字。

1994年的分析把分解与完成赋予形式化含义,并区分命题式、完全有序等限制。本章的额度证明只针对有限教学搜索树,不是对无限制 HTN 重新给出复杂度分类。两篇论文都适合用来辨认规划器实际解决的是哪一种问题。

后续可读经典规划,比较状态目标搜索;偏序规划则讨论顺序自由度。这里的测试按精确方法次数独立生成完整分解森林,再检查进入方法时的条件和原子效果,分别核对合法任务结构与可执行性,能发现仅看终态会遗漏的错误。

先判断,再看解释

prepare方法先贴标签再校准,校准会移除标签,而最终检验需要标签。仅因该方法进入条件满足,能宣称准备完成吗?

选择一个答案

学习目标

  • 区分改变世界的原子动作与改变待办结构的方法,检查进入条件、顺序约束与分解证据。
  • 运行有界总序HTN搜索,区分每条推导的方法额度与整个搜索的结点预算。
  • 证明有界可靠性、完备性与终止,解释递归、无方法、空体和方法库限制。

先修与延伸

查看知识关联图 →

参考文献

  1. SHOP: Simple Hierarchical Ordered Planner — CS-TR-3981 / UMIACS-TR-99-04
    Dana Nau; Yue Cao; Amnon Lotem; Héctor Muñoz-Avila · 1999
  2. HTN Planning: Complexity and Expressivity
    Kutluhan Erol; James Hendler; Dana S. Nau · 1994

版本1 · 编写状态: 已完成记录中的技术复核

查看技术检查记录(AI)

Codex · 2026-09-05 · Codex technical check: primary-source locations, original runnable models, independent finite-state oracles, bilingual review and second-agent cross-checks; not independent human peer review.

  • 原创仪器校准/标签/检验例,两语6锚点、5完全相同显示式、同一完整Python、7行实际输出与4已答练习;进入方法时检查条件、原子动作在执行点校验、顺序方法体与成功仅表示任务清空均明确。
  • 9项测试实际通过,独立参照先按精确方法结点数生成完整分解森林,不复用待办列表栈改写;随后用布尔字典逐事件检查条件与效果。2858模型、605完整森林、214可执行推导、198份返回证据吻合。包含全部两复合任务的49体组合×9条件组合×2初态×3额度,另100种子随机模型的两额度与专门边界。
  • 主例节点7/动作检查6/方法检查2/截断0与完整证据实算;节点预算0至6未知、7成功。递归again/stop额度2给3节点/1截断及again,stop证据;空任务双零成功,无方法/只有自递归有界失败。失败分支不泄漏状态或方法额度,父条件不当维持要求,改变方法顺序可改变原子计划长度。
  • 全部声明先校验,包括已空任务仍隐藏的非法方法;重复事实合并、重复任务保留、原子与复合任务名互斥、矛盾增删与条件、未知名字及布尔/浮点额度拒绝。一般有界完备性另按有限分支树证明,H=r+q(b+1),时间/空间包含候选栈、状态/待办/证据复制与空输入常数;不声称全HTN或最短计划。
  • strict MDX/KaTeX及React实际HTML两语各5式/6标题/4strong答案通过,5项实际数学注解逐字一致且成本完整,无强调残留;按固定Humanizer中性技术文风复读,未把扫描当正文审校。来源实际读取与curl失败分开登记。仅/tmp、review written、AI作者自检,待独立AI交叉;无仓库修改/浏览器/全站构建/人工审校。
  • 复读完整双语正文、Python、9 项测试及最终 metadata。方法条件在进入时检查,原子前提在执行时检查;方法不改状态,可为空体,只替换首位任务本次出现。成功是待办列表为空,不是未声明的终态目标。
  • 独立采用 FIFO 配置可达性与位编码状态,配置为状态/任务串/已用方法次数,未复用作者分解森林参照。340 个有界实例、555 个配置的存在性全部一致:80 个新种子小模型和 5 个边界模型,各检查额度 0—3。
  • 用布尔向量与原声明重放 153 份返回证据,核对顺序任务、条件、效果、方法次数、计划与终态;45 份非空证据删除最后事件后均被拒绝。另执行 1,000 次节点预算边界运行,核对零预算、完成/穷尽界、三态与输入不变。
  • 核对主例结果与计数、兄弟分支状态/额度隔离、总方法次数与嵌套深度之别、递归截断、进入条件与维持要求之别,以及不宣称最短计划。复核 H=r+q(b+1)、有限分支终止、保守节点界及包含复制/空输入的哈希成本模型;未发现缺陷。
  • 作者 9 项测试运行一次通过。独立严格 KaTeX/React 渲染最终两语,每语 5 显示式、6 锚点、4 答案,5 项完整数学注解一致。最终 5 文件 SHA 与清单一致。这是 AI 技术检查,不是人工同行审校或证明助手验证。
  • 完整正文、公式、代码和习题迁入;测试只读取正式MDX,删除临时目录回退及草稿跳过,状态断言更新为checked。没有修改算法或独立参照,最终记录绑定两语SHA。
  • 出版导航复核:已完成主题转向正式正文,移除相应待写标签;时间规划仍为待写目录。记录可逆导航替换,其余正文、公式、程序、练习及既有证据保留;最终双语SHA重新绑定。

核对的来源位置:

  • SHOP: Simple Hierarchical Ordered Planner — CS-TR-3981 / UMIACS-TR-99-04 · Actually opened the author-hosted six-page PDF via web. Read section 2.1, PDF page 2, lines 97–144, distinguishing ground states, primitive/compound tasks, operator syntax and method guards/bodies; section 2.2 lines 145–182 on current-state reduction, ordered execution and empty tasks; section 2.3, PDF pages 2–3, lines 183–213, including explicit incompleteness of plain DFS on an infinite leftmost branch and the iterative-deepening option. Read section 3 lines 232–257 and 318–334 to distinguish richer Lisp/axiom/conditional-method features from this interface. The header labels CS-TR-3981 / UMIACS-TR-99-04 and to appear in IJCAI-99; the author publications page also identifies 1999. Text extraction has spaced letters but the definitions were actually read. A local curl attempt failed DNS resolution; no local PDF download/hash or visual inspection is claimed.
  • HTN Planning: Complexity and Expressivity · Actually opened the original author-hosted six-page PDF through web. Read printed page 1125, lines 154–225: primitive/compound/goal tasks, task constraints, method heads/bodies, ground/propositional and totally ordered restrictions. Read printed page 1126, lines 235–318: completions/reductions and finite-step solution family; Theorem 1 retains an exception for the initial task network, Theorem 2 uses a decreasing finite level mapping, and Theorem 3 treats wholly totally ordered networks separately. Do not claim the paper’s richer formalism or complexity classification for the bounded program. Local curl failed DNS; no local PDF copy/hash, full-paper proof audit or visual scan claimed.
  • SHOP: Simple Hierarchical Ordered Planner — CS-TR-3981 / UMIACS-TR-99-04 · Actually opened original author-hosted six-page PDF through web. Read title/header, sections 2.1–2.2 on primitive/compound syntax, current-state method applicability, ordered reductions and empty task success; section 2.3 across PDF pages 2–3 distinguishes finite-search completeness from an infinite leftmost branch. Read the simplified operator syntax to confirm that the article explicitly introduces its own primitive preconditions. No local PDF, visual inspection or old benchmark reproduction claimed.
  • HTN Planning: Complexity and Expressivity · Actually read author-hosted PDF text, printed pages 1124–1126: primitive/compound tasks and constraints; positive and negative temporal truth constraints; methods, completions and reductions; Theorem 1 retains the partially ordered input-network exception, Theorem 2 uses a finite decreasing level map, and Theorem 3 treats wholly total-order cases separately. Checked the article does not transfer the richer formalism or unrestricted complexity classes to its bounded language.

相关主题

没有匹配结果,试试英文名或缩写。

已显示 0 条结果

将整个查询放入双引号,可按短语匹配。