分步实践任务说明
共 3 个核验步骤步骤 1:实现类 PDDL 动作前置条件校验与增删表转移
从标准输入读取 JSON 规划配置,包含 `mode`(`apply` 或 `plan`)、`init`、`goal`、`actions`(每个动作含 `name`、`pre`、`add`、`del`)。 当 `mode="apply"` 时,依次执行 `sequence` 中的动作: 1. 在 `apply_action(state, action)` 中检查 `pre` 是否全部包含于当前状态集合 `state`,若不满足则返回 `None`; 2. 满足时按 `(state - del) | add` 计算并返回新状态; 3. 若动作名不存在或前置条件不满足,立即打印 `INVALID <action_name>` 并结束;全部合法执行后打印 `STATE <按字典序逗号拼接的谓词>`(空集打印 `STATE EMPTY`)及 `GOAL_REACHED yes|no`。
步骤 2:实现 BFS 前向搜索求最短符号规划
当 `mode="plan"` 时,实现 `bfs_plan(init_state, goal_preds, actions, max_depth)`: 1. 若初始状态已包含 `goal_preds`,直接返回空序列 `[]`,输出 `PLAN_LEN 0` 和 `PLAN EMPTY`; 2. 使用队列执行宽度优先搜索(BFS),每层按 `actions` 列表给定顺序扩展合法后继状态,确保找到最短动作序列; 3. 找到满足 `goal_preds <= state` 的状态时,输出 `PLAN_LEN <步数>` 及 `PLAN <a1> -> <a2> -> ...`。
步骤 3:深度上限截断、环路去重与不可达检测
完善搜索边界处理: 1. 当搜索深度达到 `max_depth` 时停止继续扩展该节点; 2. 借助 `visited` 集合自动跳过已探索状态,防止 `move_living_kitchen` 与 `move_kitchen_living` 或 `patrol_hall` 与 `reset_hall` 等可逆动作引发死循环; 3. 若在 `max_depth` 内无法到达目标,或目标逻辑上不可达,输出 `NO_PLAN`。
起步代码框架预览
Pythonimport json
import sys
from collections import deque
def apply_action(state, action):
"""若前置条件满足,返回应用增删表后的新 frozenset 状态;否则返回 None。"""
# TODO 1: 检查 action["pre"] 是否全部属于 state,返回 (state - del) | add
raise NotImplementedError("请补全 apply_action")
def bfs_plan(init_state, goal_preds, actions, max_depth):
"""使用宽度优先搜索在 max_depth 步内求最短动作名序列;无解返回 None。"""
# TODO 2: 实现 BFS 前向状态空间搜索,按 actions 定义顺序扩展并使用 visited 去重
raise NotImplementedError("请补全 bfs_plan")
def main():
raw = sys.stdin.read().strip()
if not raw:
return
cfg = json.loads(raw)
init_state = frozenset(cfg.get("init", []))
goal_preds = frozenset(cfg.get("goal", []))
actions = cfg.get("actions", [])
mode = cfg.get("mode", "plan")
if mode == "apply":
# TODO 3: 按 sequence 顺序执行动作并输出状态与达标判断
raise NotImplementedError("请补全 apply 模式")
else:
max_depth = int(cfg.get("max_depth", 12))
plan = bfs_plan(init_state, goal_preds, actions, max_depth)
if plan is None:
print("NO_PLAN")
else:
print(f"PLAN_LEN {len(plan)}")
print("PLAN " + (" -> ".join(plan) if plan else "EMPTY"))
if __name__ == "__main__":
main()