Planning with preconditions and effects
Automated planning selects a sequence of actions that transforms an initial state into a state satisfying a goal. A state is a snapshot of relevant facts. A precondition describes what must be true before an action, and effects describe the changes that follow. Unlike a fixed scheduling assignment, the order of actions can change which choices are legal.
A simulated delivery task
A fictional package starts with the fact waiting. The action pick requires waiting, deletes waiting and adds held. The action deliver requires held, deletes held and adds delivered. The goal is the presence of delivered. Facts not explicitly changed persist. This small STRIPS-style representation uses positive preconditions and positive goals only, deterministic effects, disjoint add/delete lists and a finite set of named facts. An absent fact does not satisfy a positive precondition in this modeled world. It is not an assertion about real-world knowledge or sensor accuracy.
Validate an ordered sequence
Represent a state as a set of strings and each action by three lists: requires, delete, add. Test the preconditions before applying any effect; then remove deleted facts and add new facts. Do not mutate the input state. Return None for an inapplicable action. The plan checker must reject unknown action names, illegal actions anywhere in the sequence and a final state missing a goal fact. Even if a goal was achieved earlier, the final state must still satisfy it.
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"]))The first output is False: held is absent when deliver is attempted. The second is True: pick establishes held, deliver consumes it and establishes delivered. An empty sequence is valid when the initial state already satisfies the goal. An applicable action can still be unhelpful if it removes a fact needed later. This model only simulates effects; it does not control equipment, send messages or deliver anything.
Find a plan using breadth-first search
A planner can search over states, connecting each state to its legal successors. Reuse breadth-first search (Chapter 10): store the state and its action path in a queue, then test the goal when a state is removed. A frozenset is an immutable set that can be stored in the visited set. Repeated states need not be expanded because under this deterministic model the same state has the same available actions and effects regardless of how it was reached. Sort action names for a repeatable tie-break. A valid shortest plan here means the fewest actions, not minimum energy or time.
from collections import deque
def find_plan(initial, actions, goal): start = frozenset(initial) goal = set(goal) frontier = deque([(start, [])]) seen = {start} while frontier: state, path = frontier.popleft() if goal <= state: return path for name in sorted(actions): action = actions[name] if not set(action["requires"]) <= state: continue next_state = ((state - set(action["delete"])) | set(action["add"])) if next_state not in seen: seen.add(next_state) frontier.append((next_state, path + [name])) return None
actions = { "pick": {"requires": ["waiting"], "delete": ["waiting"], "add": ["held"]}, "deliver": {"requires": ["held"], "delete": ["held"], "add": ["delivered"]},}print(find_plan(["waiting"], actions, ["delivered"]))print(find_plan([], actions, ["delivered"]))print(find_plan(["delivered"], actions, ["delivered"]))The first output is ['pick', 'deliver']; the second is None because the initial empty state cannot make progress; the third is [] because the goal is already met. With k possible facts the state space can contain up to 2^k subsets. In this finite, fully specified model, exhausting the queue proves no reachable goal exists. For real systems, unmodeled facts, resource restrictions, imperfect sensors or timeouts invalidate that conclusion. A budgeted planner must distinguish search stopped from no plan found.
Planning, optimization and policies
A feasible plan reaches the goal. A cost-optimal plan minimizes a defined cost; with unequal nonnegative action costs, uniform-cost search can be appropriate instead of BFS. A policy maps states or observations to actions, possibly reacting to changing conditions rather than following a fixed sequence. Policy learning estimates or improves a policy from experience; classical planning can operate on explicit action models without learning. A solver or planner can verify constraints more reliably than free-form generated text alone. Never execute a generated plan without checking permissions, constraints, and current state; the examples here are offline simulations.
Practice and next step
Add a return action requiring held, deleting held and adding waiting. Predict whether the cycle changes the shortest valid plan. Add an action undo requiring delivered that deletes delivered and adds waiting. Explain why pick, deliver, undo fails the final goal. Run the project checker with a feasible schedule, an impossible schedule, and an invalid action order. Next chapter introduces reasoning when observations and outcomes are uncertain.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Planning with preconditions and effects — understanding check
Apply the chapter concepts to new situations.
Which fact is required before the simulated deliver action?
What does applying pick to {waiting, label_ok} produce?
The goal already holds in the initial state. What does find_plan return?
In the finite deterministic planning model, what does breadth-first search guarantee about the first plan it returns?
A legal action sequence reaches delivered, then an applicable undo action removes delivered. The goal requires delivered. What does valid_plan return?