PDDL:写清任务,再验证给出的计划
Planning Domain Definition Language
读取一个边界明确的语言片段,拒绝不支持的结构,区分语法错误、动作失败与目标未达成。
pddl计划需要一份其他程序也能读懂的模型
一个包裹需要检查并封口。检查要求包裹仍然敞开,封口则会关闭它。“检查 → 封口”可行,“封口 → 检查”不可行。规划语言让建模者先写清这些规则,再交给某个搜索算法寻找动作序列。
PDDL,即规划领域定义语言,把可复用的领域与某个具体问题分开。领域声明谓词和动作,问题选择领域、提供初始事实并指定目标。规划器按这些声明搜索,验证器检查用户给出的计划。程序不会从 inspect 的英文拼写中自动理解什么才算检查。
PDDL 1.2 手册由 AIPS-98 规划竞赛委员会编写,Drew McDermott 任主席,日期为 1998 年 10 月。它的用途包括让不同规划器处理可比较的问题。手册当时就描述了超出基本 STRIPS 的结构,不能把某个解析器支持的片段当作整门语言。原始手册首页及第2节。
语法正确、功能支持与执行成功是三件事
PDDL 使用括号结构。open 这样的名字标识一个谓词,(open) 是零参数原子。(at ?parcel ?place) 这类参数化形式也属于 PDDL,但不在本篇的小型读取器范围内。:requirements 声明模型需要哪些语言功能;写上功能名,不会让一个不兼容的规划器自动支持它。名字不区分大小写,分号注释延续到下一个换行。手册印刷第2—6页。
完整程序使用以下刻意收窄的接口:
| 接受的形式 | 本读取器明确不支持 |
|---|---|
一份领域、一份问题;:strips 或省略需求声明 | 其他需求、对象、类型和常量 |
零参数谓词,以及带 :parameters () 的零参数动作 | 变量、参数列表和动作落地 |
前提和目标为正原子,或平坦的 and | 负前提、负目标、嵌套公式和量词 |
正效果,以及用 not 包裹原子的删除效果 | 条件、数值、持续动作与非确定效果 |
| 正初始事实;遗漏事实为假 | 未知初态与感知 |
这是教学读取器的边界,并不表示排除的结构都是非法 PDDL。未知字段和不支持的语法会抛出 ValueError,不会被静默忽略。同一原子同时出现在正负效果中,也被本接口拒绝。
对于接受的片段,初始事实给出完整布尔状态。执行前检查前提,然后删除效果移除事实,添加效果建立事实。没有涉及的事实保持原样:
最后一个条件在给定序列执行完后检查目标。这是顺序执行的确定性模型。删除表达式 (not (open)) 会改变状态,并不是要求从不完整知识库中证明经典否定。STRIPS 一章进一步推导迁移模型及反向计划搜索。
应当区分三类结果。文本格式错误、不支持的功能或未知名字,会触发加载或接口错误。已知动作缺少前提时,在指定步骤返回 inapplicable。整个序列都能执行,却仍可能以 goal-not-met 结束。只有 valid 表明这段特定序列在给定模型中满足目标;它不证明计划最短,也不证明现实执行一定成功。
先读声明,再逐步检查提议
解析器先用显式栈建立嵌套列表,语义加载器再检查文档头、领域引用、允许字段、谓词声明、动作名字和公式形状。重复字段、重复声明与未声明原子都被拒绝。规定的文档头之后,字段可以换序。本读取器要求三个动作字段都写出,尽管更广的 PDDL 语法可以允许省略。
返回模型包含规范化的不可变事实集合和动作字典。应直接使用加载器的输出;validate 不会重新验证任意手写或被修改过的模型字典。计划参数是动作名字字符串构成的 list 或 tuple,不是持续动作计划文件或带括号计划文本的解析器。所有计划名字都在执行前检查,因此末尾的未声明动作也会先触发错误,即使前面某个已知动作本来就会因前提不足而失败。
成功执行会记录每一步状态。不适用的动作不会改变最后记录的状态,并会报告缺少的前提。步骤从一开始编号;最终结果记录已执行动作数。目标事先成立时,空计划也可以合法。
# Python 3.10+; an explicit zero-argument, positive-STRIPS PDDL subset.
import re
def parse(text):
if type(text) is not str:
raise ValueError('PDDL input must be text')
text = re.sub(r';[^\n]*', ' ', text).lower()
roots, stack = [], []
for token in re.findall(r'\(|\)|[^\s()]+', text):
if token == '(':
node = []
(stack[-1] if stack else roots).append(node)
stack.append(node)
elif token == ')':
if not stack:
raise ValueError('unmatched closing parenthesis')
stack.pop()
elif stack:
stack[-1].append(token)
else:
raise ValueError('a document must contain one parenthesized form')
if stack or len(roots) != 1:
raise ValueError('unclosed or multiple top-level forms')
return roots[0]
def identifier(value):
if type(value) is not str or not re.fullmatch(r'[a-z][a-z0-9_-]*', value):
raise ValueError('invalid identifier')
return value
def header(text, category):
form = parse(text)
if len(form) < 2 or form[0] != 'define':
raise ValueError('expected define')
h = form[1]
if type(h) is not list or len(h) != 2 or h[0] != category:
raise ValueError('wrong document header')
return identifier(h[1]), form[2:]
def sections(rows, allowed):
result = {}
for row in rows:
if type(row) is not list or not row or type(row[0]) is not str:
raise ValueError('expected a named section')
if row[0] not in allowed or row[0] in result:
raise ValueError('unsupported or repeated section')
result[row[0]] = row[1:]
return result
def one(values):
if len(values) != 1:
raise ValueError('expected exactly one value')
return values[0]
def atom(form, predicates):
if type(form) is not list or len(form) != 1:
raise ValueError('only zero-argument atoms are supported')
p = identifier(form[0])
if p not in predicates:
raise ValueError('undeclared predicate')
return p
def literals(form, predicates, effects=False):
terms = form[1:] if type(form) is list and form and form[0] == 'and' else [form]
positive, negative = set(), set()
for term in terms:
if type(term) is list and len(term) == 2 and term[0] == 'not' and effects:
negative.add(atom(term[1], predicates))
else:
positive.add(atom(term, predicates))
if positive & negative:
raise ValueError('opposing effects are outside this subset')
return frozenset(positive), frozenset(negative)
def load(domain_text, problem_text):
domain, rows = header(domain_text, 'domain')
action_rows = [r for r in rows if type(r) is list and r and r[0] == ':action']
other = [r for r in rows if not (type(r) is list and r and r[0] == ':action')]
fields = sections(other, {':requirements', ':predicates'})
if fields.get(':requirements', [':strips']) != [':strips']:
raise ValueError('only :strips is supported')
if ':predicates' in fields and not fields[':predicates']:
raise ValueError('omit an empty predicates section')
predicates = []
for form in fields.get(':predicates', []):
if type(form) is not list or len(form) != 1:
raise ValueError('declare zero-argument predicates')
predicates.append(identifier(form[0]))
if len(set(predicates)) != len(predicates) or {'and', 'not'} & set(predicates):
raise ValueError('duplicate or reserved predicate')
predicates = frozenset(predicates)
actions = {}
for row in action_rows:
if len(row) < 2 or (len(row)-2) % 2:
raise ValueError('action needs key/value pairs')
key = identifier(row[1])
if key in actions:
raise ValueError('duplicate action')
f = sections([[row[i], row[i+1]] for i in range(2, len(row), 2)],
{':parameters', ':precondition', ':effect'})
if set(f) != {':parameters', ':precondition', ':effect'} or one(f[':parameters']) != []:
raise ValueError('supply all three action fields and empty parameters')
pre, _ = literals(one(f[':precondition']), predicates)
add, delete = literals(one(f[':effect']), predicates, effects=True)
actions[key] = (pre, add, delete)
problem, rows = header(problem_text, 'problem')
fields = sections(rows, {':domain', ':init', ':goal'})
if set(fields) != {':domain', ':init', ':goal'} or one(fields[':domain']) != domain:
raise ValueError('problem fields or domain reference do not match')
initial = frozenset(atom(f, predicates) for f in fields[':init'])
goal, _ = literals(one(fields[':goal']), predicates)
return dict(domain=domain, problem=problem, predicates=predicates,
actions=actions, initial=initial, goal=goal)
def validate(model, plan):
# plan is an API list of action names, not a parser for temporal plan files.
if type(plan) not in (list, tuple) or any(type(a) is not str for a in plan):
raise ValueError('plan must be a list or tuple of action names')
names = tuple(identifier(a.lower()) for a in plan)
if any(a not in model['actions'] for a in names):
raise ValueError('plan names an undeclared action')
state = model['initial']
history = [tuple(sorted(state))]
for step, key in enumerate(names, 1):
pre, add, delete = model['actions'][key]
if not pre <= state:
return dict(status='inapplicable', step=step,
missing=tuple(sorted(pre-state)), history=tuple(history))
state = (state-delete) | add
history.append(tuple(sorted(state)))
missing = tuple(sorted(model['goal']-state))
return dict(status='goal-not-met' if missing else 'valid', step=len(names),
missing=missing, history=tuple(history))
DOMAIN = '''(define (domain parcel)
(:requirements :strips)
(:predicates (open) (checked) (sealed))
(:action inspect :parameters () :precondition (open) :effect (checked))
(:action seal :parameters () :precondition (open)
:effect (and (sealed) (not (open)))))'''
PROBLEM = '''(define (problem one-parcel) (:domain parcel)
(:init (open)) (:goal (and (checked) (sealed))))'''
if __name__ == '__main__':
model = load(DOMAIN, PROBLEM)
for plan in [('inspect', 'seal'), ('seal', 'inspect'), ('inspect',), ()]:
print(plan, validate(model, plan))
print('case/comments:', load('; note\n'+DOMAIN.upper(), PROBLEM)==model)
try:
load(DOMAIN.replace(':strips', ':strips :typing'), PROBLEM)
except ValueError as error:
print('unsupported:', error)
完整示例输出如下:
('inspect', 'seal') {'status': 'valid', 'step': 2, 'missing': (), 'history': (('open',), ('checked', 'open'), ('checked', 'sealed'))}
('seal', 'inspect') {'status': 'inapplicable', 'step': 2, 'missing': ('open',), 'history': (('open',), ('sealed',))}
('inspect',) {'status': 'goal-not-met', 'step': 1, 'missing': ('sealed',), 'history': (('open',), ('checked', 'open'))}
() {'status': 'goal-not-met', 'step': 0, 'missing': ('checked', 'sealed'), 'history': (('open',),)}
case/comments: True
unsupported: only :strips is supported
第一个序列达成两个目标。第二个在第2步失败,因为封口移除了 open。只检查也能执行,但缺少 sealed。程序不做计划搜索:拒绝一个提议,并不能说明其他序列都不行。
令 为所有输入字符数, 为声明的谓词数, 为计划长度。按散列表操作的期望成本计,加载时间与空间均为 。验证器在每次成功动作之后保存排序后的状态快照,宽松界为:
计划字符项包含规范化与名字检查。k+1 还包含初始快照与最终缺失目标的排序,即使计划为空也要计入。上界不包含已经加载的模型,把谓词名字比较按单位操作计;长名字还要考虑字符成本。流式验证器可以不保留完整历史,从而减少空间。语法读取使用迭代,嵌套不消耗 Python 递归深度,但文档仍受可用内存限制。
文件兼容,也可能把任务写错
把 checked 写成目标,并不能保证名为 inspect 的动作代表有意义的检查。如果误把它的前提写成空,模型就可能接受不可能的工作流。如果封口漏写删除效果,反向顺序也可能错误地通过。这些是建模错误,括号和名字都正确也不会消除它们。
带参数谓词让同一动作模式引用许多对象。落地会实例化参数,可能产生大量动作;本读取器刻意省略这一步。时间、数值资源和重叠动作需要更丰富的语义。尤其不能把持续动作验证器的数值容差,直接搬进本例精确布尔模拟中。
官方 VAL 项目提供更广的计划验证工具。其固定版本应用指南展示了如何检查领域、问题与给定计划。处理本片段以外的模型时,应当把验证器版本、声明需求及验证条件与文件一起记录。本篇读过该指南,没有运行 VAL 可执行程序。VAL 提交3c7a1f3的应用指南。
改搜索算法前,先定位错误
练习 1:删除封口动作的删除效果
答案: 把效果改成 (sealed)。“封口 → 检查”现在会通过,因为 open 保持为真。这说明动作模型发生了变化,不能解释成规划能力变强。目标和动作名字都没有改变。
练习 2:声明类型,但不实现它
答案: 加入 :typing 后,load 会拒绝领域。只删掉这个标记却保留类型声明,也不会使它们受支持;相应字段或参数列表仍会被拒绝。如果静默退化处理,就会错误声称检查过原始模型。
练习 3:初始状态不写 open
答案: 两个动作都需要 open,因此无论先执行哪个,第一步都会失败。完整状态模型把遗漏原子当作假;它不据此推断现实包裹已经封闭,也没有实现未知初态。
练习 4:可执行的计划都合法吗?
答案: 不是。('inspect',) 可以执行,却缺少 sealed,返回 goal-not-met。空计划同样可执行,但在原问题中缺少两个目标。如果初始就同时有 checked 与 sealed,空计划在该模型下会成为合法计划。
把模型与验证证据放在一起
- PDDL—The Planning Domain Definition Language,1.2 版,AIPS-98 规划竞赛委员会,1998 年 10 月。原始手册。PDF 提取文本乱码,因此实际目视了首页及相关语法页。
- KCL-Planning,VAL applications。2021-10-18固定提交。这是文档证据,不是运行 VAL 的报告,也不声称对应其当前发布版本。
包裹模型、读取器、验证器和习题均为原创。独立测试用逐原子布尔更新对照执行,改变注释、大小写和字段顺序,并区分不支持的输入与可执行但未达目标的序列。搜索部分可以接着读经典规划;分支执行上的性质可以读时序逻辑。
先判断,再看解释
封口后再检查被判为inapplicable。这次验证能说明什么?
学习目标
- 分开领域、问题、规划器和验证器,读懂需求、前提与增删效果。
- 运行明确子集的读取与验证,区分不支持输入、不适用动作和未达目标。
- 解释合法计划只对给定模型与序列成立,既不证明最短也不证明现实成功。
先修与延伸
查看知识关联图 →参考文献
- PDDL — The Planning Domain Definition Language, version 1.2
AIPS-98 Planning Competition Committee; Drew McDermott (chair) · 1998 - VAL applications — fixed commit 3c7a1f330bdab0ba28a4762bb45c3f06c27fb6d4
KCL-Planning / VAL contributors · 2021
版本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项作者测试通过;64个三原子模型生成实际文本,每个检查长度0—3的15种双动作序列,共960查询,对照独立逐原子布尔执行的全部返回字段。
- 实际核对完整stdout、大小写/注释/字段换序、重复初始事实、空词表/空计划、1500层括号与错误输入;非法结构不跳过,已知不适用序列与未知动作名分开。
- 两语完整正文、同一程序/输出、2个显示式、6主锚点、4已答练习;仓库显示公式插件和strict KaTeX实际HTML通过。成本包含空计划的初始快照、最终缺失目标排序及计划字符串读取;英语出版空格已修。
- 原创零参数正STRIPS片段,无参数落地、负条件、嵌套公式、数值或时间,也不做搜索。AI自检后保持written,等待独立交叉;不声称实现完整PDDL或运行VAL。
- 完整复读两语正文、源码、六项作者测试与最终 metadata;六测试实际通过。区分加载/接口错误、inapplicable、goal-not-met 和 valid;先校验全部计划名字,后执行动作,未把拒绝一个计划等同证明问题无解。
- 独立参照以每个原子在前缀中的最后一次写入决定真值,不复用生产集合更新或作者逐位滚动状态。24 个种子 77051 的四原子三动作模型、961 个完整结果及历史逐项一致。另以已知语法树的独立序列化核对 96 种大小写、注释、空白及字段换序变体,并比较加载后的全部模型字段;验证前后模型未变。
- 39 个非法输入检查均得到 ValueError,覆盖括号/顶层形状、重复和未知字段、需求、参数、嵌套/负条件、条件及全称效果、冲突效果、问题引用、初始形式及计划类型/名字;晚出现的非法名字不会被较早的语义失败掩盖。额外核对删除效果遗漏会改变可执行性,而非改变搜索能力。
- 空计划的 64 谓词反例核对初态快照和最终缺失目标;最终成本式含 k+1 及 L_plan,分别覆盖初态/终态排序和规范化字符串。加载线性界按原始字符数、验证界按期望散列及单位名字比较解释;已加载模型不重新校验的接口前提明确。
- 最终两语严格 MDX/KaTeX 与实际 React HTML 通过:各 2 显示式、6 主锚点、4 已答练习。对 HTML 实体解码后,MathML TeX 注解与正文两公式逐字一致,包括修订后的成本式。完整代码/输出双语一致,直接链接文件存在;未运行全站构建或浏览器验收。
- 完整正文、公式、代码和习题迁入;测试只读取正式MDX,删除临时目录回退及草稿跳过,状态断言更新为checked。没有修改算法或独立参照,最终记录绑定两语SHA。
核对的来源位置:
- PDDL — The Planning Domain Definition Language, version 1.2 · Actually downloaded the official university-preserved PDF and viewed local renders of PDF pages 1,3,4,5,6,7. Page1: version1.2, eight committee members, McDermott chair, Yale CVC TR98-003/DCS TR1165, October1998. Page3 printed2: requirements, parameters/preconditions/effects. Page4 printed3: domain reference, initial facts and goal. Pages5–7 printed4–6: EBNF, semicolon comments, domain grammar, flexible field order, separate strict-subset convention, case-insensitive identifiers and default:strips requirements. Web-extracted text was unusable; Washington mirror required login and was not used. Did not inspect remaining pages or infer PDDL2.1 semantics.
- VAL applications — fixed commit 3c7a1f330bdab0ba28a4762bb45c3f06c27fb6d4 · Actually read the live applications guide, then fetched applications/README.md at exact commit3c7a1f330bdab0ba28a4762bb45c3f06c27fb6d4 via GitHub contents API; verified committer date2021-10-18T13:42:14Z. Read Validate section: separate domain/problem and supplied-plan checks, missing preconditions, overall constraints, verbose/tolerance invocation. Temporal epsilon described by VAL was not adopted in the exact sequential Boolean reader. No executable installed or run.
- PDDL — The Planning Domain Definition Language, version 1.2 · Independently viewed the preserved original-manual PNG renders for PDF pages 1, 4, 6 and 7. Page 1 confirms version 1.2, committee, McDermott chair, report numbers and October 1998. Printed page 3 shows the domain reference, initial facts and goal in a problem. Printed page 5 specifies semicolon comments, domain grammar, optional sections, nonempty predicate declarations and field-order freedom. Printed page 6 distinguishes the manual’s separate strict subset, states name/case rules and explains omitted requirements defaulting to :strips. No claim to inspect all 27 pages or validate PDDL 2.1 semantics.
- VAL applications — fixed commit 3c7a1f330bdab0ba28a4762bb45c3f06c27fb6d4 · Independently read the preserved fixed-commit applications README at /tmp/aiwiki-next-pddl/val-readme.md, especially Parser and Validate: separate domain/problem checks, supplied-plan validation, missing preconditions and temporal epsilon. New web fetches of the exact raw URL and commit API failed, so this peer does not claim independent live verification of the commit timestamp. The author records an earlier GitHub API verification of commit 3c7a1f330bdab0ba28a4762bb45c3f06c27fb6d4 and its 2021-10-18 date. No VAL binary was installed, built or run.
相关主题