concept
Planning with preconditions and effects
Simulate positive preconditions and disjoint add/delete effects on a finite fact set. Validate every action and the final goal. BFS yields a shortest action sequence for unit-cost deterministic actions. None indicates exhaustion; [] indicates a zero-action plan. Teaching interfaces, not Python built-ins.
Formal Syntax & Structure
artificial-intelligence syntaxartificial-intelligence
valid_plan(initial, actions, sequence, goal) → bool; find_plan(initial, actions, goal) → list or NoneWorking Example
Example Snippetartificial-intelligence
def apply_action(state, action): required = set(action["requires"]) if not required <= set(state): return None return (set(state) - set(action["delete"])) | set(action["add"])
def valid_plan(initial, actions, sequence, goal): state = set(initial) for name in sequence: if name not in actions: return False state = apply_action(state, actions[name]) if state is None: return False return set(goal) <= state
actions = { "pick": {"requires": ["waiting"], "delete": ["waiting"], "add": ["held"]}, "deliver": {"requires": ["held"], "delete": ["held"], "add": ["delivered"]},}print(valid_plan(["waiting"], actions, ["deliver"], ["delivered"]))print(valid_plan(["waiting"], actions, ["pick", "deliver"], ["delivered"]))