CSE 4355 W2 · Class 1 States, actions, goals: formulating problems
Sakeef Sani ↗ ↓ PDF

CSE 4355Week 2Class 1Saturday · 9:30 AM – 10:50 AM80 min

States, actions, goals: formulating problems

Before a computer can solve a problem, someone has to write it down properly. Today we learn to write it down, then take our first steps searching it.

S present · L lecturer notes · → next step · F full screen · P presenter window
01 Cold open 0–4 min

A bomb, a fountain and two jugs

Die Hard with a Vengeance (1995). Simon's riddle: a 5-gallon jug, a 3-gallon jug, a fountain. Put exactly 4 gallons on the scale or the bomb goes off.

McClane and Zeus shout at each other for a full minute. A computer would not shout. It would try things systematically until something works. That is search, and it only works if the problem is written down precisely first.

Today's version: 4 L and 3 L jugs, and you need exactly 2 L. How many pours at minimum?

Pop culture

Same idea, different costume: Google Maps getting you from Mirpur 10 to Motijheel, a Rubik's cube solver, the fastest route through your Among Us tasks. Different problems, one recipe: states, actions, a goal, a cost.

02 Concept 4–8 min

The problem-solving agent

A goal-based agent that plans a whole sequence of actions before moving works in four phases:

1Goal formulation"Be in Cox's Bazar by tonight"
2Problem formulationWhich states and actions to consider
3SearchSimulate action sequences until one reaches the goal
4ExecutionCarry out the plan, step by step
Gotcha

Executing the plan without looking is open-loop control. It is safe only if the environment is fully observable, deterministic and known (last week's properties). A flooded road on the way breaks it.

function SIMPLE-PROBLEM-SOLVING-AGENT(percept)
  state ← UPDATE-STATE(state, percept)
  if plan is empty:
    goal    ← FORMULATE-GOAL(state)
    problem ← FORMULATE-PROBLEM(state, goal)
    plan    ← SEARCH(problem)
    if plan = failure: return NoOp
  action ← FIRST(plan); plan ← REST(plan)
  return action

Adapted from Russell & Norvig's simple problem-solving agent. Everything today lives in FORMULATE-PROBLEM; Weeks 2–4 live in SEARCH.

03 Concept 8–13 min

A problem, in five parts

PartNotationDhaka → Cox's Bazar
1 · Initial states₀In(Dhaka)
2 · ActionsACTIONS(s){Go(Cumilla), Go(Chandpur), Go(Brahmanbaria)}
3 · Transition modelRESULT(s, a)RESULT(In(Dhaka), Go(Cumilla)) = In(Cumilla)
4 · Goal testIS-GOAL(s)s = In(Cox's Bazar)
5 · Path costΣ c(s, a, s′)sum of road km (or minutes, or taka)

Parts 1–3 together define the state space: every state reachable from s₀ by some sequence of actions.

Solution

A sequence of actions from the initial state to a goal state.

Optimal solution

A solution with the lowest path cost among all solutions.

Pro tip

A solution is a plan (actions), not a single final state. "Cox's Bazar" is not an answer; "Cumilla, Feni, Chattogram, Cox's Bazar" is.

04 Live lab 13–18 min

Live lab: explore the water-jug state space

Water-jug state space State = (litres in 4 L jug, litres in 3 L jug). Goal: exactly 2 L in the 4 L jug.
12344 L jug0 L1233 L jug0 L
litres in 4 L jug →litres in 3 L jug →0,0
State(0, 0)
Actions taken0
States found1 / 14

Click an action. Every new state you reach appears in the graph.

Click actions and watch the state graph grow: each new (x, y) you reach becomes a node.

Then press Solve with BFS: it explores level by level and returns the shortest plan.

Pro tip

Find all 14 states. Why are 6 grid points never reachable?

05 Worked example 18–23 min

Worked example: formulating the water jug

Given

A 4 L jug and a 3 L jug, no markings, a tap with unlimited water, a drain. You can fill a jug, empty it, or pour one into the other until the source is empty or the target is full.

Find

All five parts of the formulation, and an optimal solution.

Method: Rich & Knight's production rules; Russell & Norvig's five-part template

Solution

Predict the answer first. Then reveal one step at a time.

06 Concept 23–27 min

Abstraction: what to leave out

The real trip to Cox's Bazar involves the bus company, the weather, the tea stop and 400 km of potholes. Our state is one word: In(city). Abstraction = removing detail. It is valid if every abstract solution expands into a real one, useful if each abstract action is easier than the original problem.

The state space is then a graph: nodes are states, edges are actions, weights are step costs. Search = finding a path in that graph without drawing all of it.

Think deeper

Why is the state for route finding just 'which city I am in', and not 'the whole map'?

Argue it in pairs for two minutes. There may be more than one defensible answer.

07 Live lab 27–32 min

Live lab: the 8-puzzle

8-puzzle State = where every tile is. Actions = move the blank Up / Down / Left / Right.
Moves0
Branching hereb = 2
Misplaced tiles0
Inversions0 → even: solvable
Arrangements9! = 362,880
Reachable from goal9!/2 = 181,440
Branching factor2, 3 or 4 (avg ≈ 2.67)
Hardest instance31 moves (optimal)

Click a tile next to the blank to slide it.

States: location of each of the 8 tiles and the blank.

Initial: any state (but see parity!).

Actions: move the blank Left, Right, Up, Down. ACTIONS(s) depends on where the blank is.

Goal test: matches 1–8 in order, blank last.

Path cost: 1 per move.

Pop culture

In the 1880s Sam Loyd offered $1,000 for solving the 15-puzzle with 14 and 15 swapped. Safe money: it is on the wrong side of the parity split.

08 Concept 32–35 min

Sundarbans crossing (Life of Pi edition)

Three bawalis (wood-cutters) and three tigers must cross a khal in a boat that holds at most two. Anyone can row (it is a puzzle). If on either bank the tigers ever outnumber the bawalis present there, lunch is served.

The classic "missionaries and cannibals", with better scenery.

State: (B, T, boat) = bawalis and tigers on the start bank, boat side (L/R). The other bank is implied.

Initial: (3, 3, L). Goal: (0, 0, R).

Actions: the boat carries {1B, 2B, 1T, 2T, 1B+1T}, only if the result is safe on both banks.

Cost: 1 per crossing.

All (B, T, boat)4 × 4 × 2 = 32
Safe20
Reachable16
09 Your turn 35–40 min

Your turn · 1: eight queens, two ways

04:00
1 Exam CQ

Place 8 queens on a chessboard so that no two attack each other (same row, column or diagonal).

(a) Give an incremental formulation and estimate how many action sequences a naive version must consider.

(b) Improve it so the state space is tiny.

(c) Give a complete-state formulation and its number of states. Which formulation suits which kind of search?

Work it out first.
Press Solve when the class has an answer.

10 Concept 40–43 min

Why state-space size matters: bd

If every state has b successors (branching factor) and the shallowest goal is at depth d, a level-by-level search generates up to

1+b+b2+⋯+bd=bd+1−1b−1=O(bd)1+b+b^2+\cdots+b^d=\frac{b^{d+1}-1}{b-1}=O(b^d)
dNodes (b = 10)TimeMemory
411,11111 ms11 MB
6≈ 10⁶1.1 s1.1 GB
8≈ 10⁸2 min111 GB
10≈ 10¹⁰3 hours11 TB
12≈ 10¹²13 days1 PB

Assumes 10⁶ nodes per second and 1 KB per node.

ProblemStates
Water jug (4, 3)14
Sundarbans crossing16
8-queens (smart)2,057
8-puzzle181,440
15-puzzle≈ 1.05 × 10¹³
Rubik's cube≈ 4.3 × 10¹⁹
Gotcha

Memory runs out long before patience does. That single fact drives next week (IDS).

11 Worked example 43–48 min

Worked example: route finding, BFS by hand

Given

The map below (km approximate). Start Dhaka, goal Cox's Bazar.

Find

BFS expansion order, and the route returned with its cost.

10510012019065656010090359515075100DhakaSBrahmanbariaSylhetCumillaChandpurFeniNoakhaliChattogramBandarbanCox's BazarGschematic · km approximate

Method: graph-search BFS (Russell & Norvig)

Solution

Predict the answer first. Then reveal one step at a time.

12 Live lab 48–54 min

Live lab: BFS vs DFS on the road to Cox's Bazar

BFS vs DFS: Dhaka → Cox's Bazar Step through the search. Watch how the frontier data structure alone changes which route you get.
10510012019065656010090359515075100DhakaSBrahmanbariaSylhetCumillaChandpurFeniNoakhaliChattogramBandarbanCox's BazarGschematic · road km approximate
frontierexploredbeing expandedparent linksolution

Frontier

FIFO queue: pop the front (left)
DHK

Explored set

never expand these again
empty

initStart: frontier = [Dhaka]

Expanded0
Max frontier1
Cost—
Hops—
13 Concept 54–57 min

Judging a search strategy: four questions

CriterionBFS (FIFO)DFS (LIFO)
Complete? finds a solution if one existsYes, if b is finiteGraph search: yes in finite spaces. No in infinite ones (or with loops, as tree search)
Optimal? finds the cheapestYes, if all step costs are equalNo
TimeO(bd)O(bm)
SpaceO(bd): keeps a whole levelO(bm): one path plus siblings
node = frontier.pop(0)   # BFS: oldest first (queue)
node = frontier.pop()    # DFS: newest first (stack)

In Python, that one argument is the whole difference between the two algorithms.

b = branching factor (max successors)

d = depth of the shallowest goal

m = maximum depth of any path (may be ∞)

Pro tip

Next week's question: can we get DFS's O(bm) memory and BFS's completeness and optimality? Yes: iterative deepening.

14 Your turn 57–61 min

Your turn · 2: trace DFS by hand

2 Exam CQ
SstartABCDEFGgoal

All edges cost 1. Run graph-search DFS from S to G. Break ties alphabetically; goal test when a node is popped.

Give the expansion order, the stack after each expansion, and the path returned. Then compare with BFS.

Work it out first.
Press Solve when the class has an answer.

15 Think deeper 61–63 min

Critical thinking · is BFS ever wrong?

3 Trap

A classmate says: "BFS always finds the shortest path, so Google Maps could just run BFS."

Using the Dhaka → Cox's Bazar map, is BFS's route optimal? Under what condition is BFS optimal, and what would you use instead?

Work it out first.
Press Solve when the class has an answer.

16 Concept 63–67 min

Planning with STRIPS: states as sets of facts

STRIPS (Fikes & Nilsson, SRI, 1971) writes each action as three lists: PRE facts that must hold, ADD facts made true, DEL facts made false. Anything not mentioned stays the same.

s′=(s∖Del)∪Adds' = (s \setminus \text{Del}) \cup \text{Add}
STRIPS kitchen: one cup of cha State = a set of true facts. An action applies only if all its preconditions are in the state; then delete-list out, add-list in.

Current state

KettleEmptyStoveOffCupEmptyHaveLeavesHaveMilkHaveSugar

Goal (all must hold)

○ CupHasTea○ MilkIn○ SugarIn✓ StoveOff
Plan empty: click actions →
17 Your turn 67–70 min

Your turn · 3: a hospital delivery robot

4 Design

A robot carries medicine between rooms of a hospital (Store, Pharmacy, Ward3; Store–Pharmacy and Pharmacy–Ward3 are adjacent). It carries one package at a time.

Initial: At(R, Store), At(M, Pharmacy), HandEmpty, plus the Adj facts. Goal: At(M, Ward3).

Write STRIPS operators Move, PickUp, Drop and a plan.

Work it out first.
Press Solve when the class has an answer.

18 Real world 70–73 min

Real world: planning a spray drone's day

From the startup

An agro-drone spraying a paddy field must cover every cell with enough battery and pesticide. Naive state: (cell, battery, tank, set of sprayed cells). A 20 × 20 field has 400 cells, so the "sprayed set" alone has 2400 ≈ 2.6 × 10120 values.

Real planners abstract: fix a lawn-mower (boustrophedon) sweep, so the state becomes (row, battery, tank) and the only decision is when to return to refill. Ambulances in Dhaka: state (junction, time of day), step costs from live traffic, not km.

Think deeper

Our formulation assumed a fully observable, deterministic, known world. Is the real world ever like that?

Argue it in pairs for two minutes. There may be more than one defensible answer.

19 Pop quiz 73–78 min

Pop quiz

Score 0

Question 1 / 5

Which of these is not one of the five parts of a problem formulation?

20 Recap 78–80 min

Three things to remember

01

A problem = initial state, ACTIONS, RESULT, goal test, path cost. A solution is an action sequence; optimal = lowest cost.

02

Formulation and abstraction decide the state-space size (8-queens: 10¹⁴ vs 2,057). Search cost grows like bd.

03

BFS = FIFO queue, complete, optimal for equal costs, O(bd) memory. DFS = LIFO stack, O(bm) memory, not optimal.

HW Before next class

Formulate the Die Hard 3 riddle (5 and 3 gallon jugs, measure exactly 4 in the big jug) in five parts. Find an optimal solution and count the reachable states. Assignment 1 is published this week; Class Test 1 next week covers UCS, DLS, IDS and local search, and assumes today's BFS/DFS.

Work it out first.
Press Solve when the class has an answer.