Recap of Lecture 17 and Homework Check
LAST LECTURE · 5 MIN
Today: minimax looked at all 9 leaves of our tree. Did it need to? And what about puzzles with rules instead of opponents?
Today's Plan (50 minutes)
ROADMAP
0-5
Recap of Lecture 17 and homework check
S02
5-8
Plan and learning outcomes
S03-S04
8-20
Module 1: Alpha-beta pruning - idea, alpha and beta, trace, move ordering
S05-S09
20-27
Module 2: Constraint satisfaction problems - map colouring, exam timetable
S10-S12
27-35
Module 3: Backtracking search, MRV and forward checking
S13-S15
35-43
Class activity: prune the trees, solve the CSP
S16-S17
43-48
Common mistakes and recap quiz
S18-S19
48-50
Homework and next lecture
S20
Learning Outcomes (SLOs)
BY THE END OF THIS LECTURE YOU WILL BE ABLE TO
SLO 18.1
Apply alpha-beta pruning to a game tree: find the pruned branches and the root value
→ CLO3 · C3 Apply
SLO 18.2
Formulate a problem (map colouring, timetable) as a CSP with variables, domains and constraints
→ CLO3 · C3 Apply
SLO 18.3
Solve a CSP with backtracking and explain MRV, degree, LCV and forward checking
→ CLO3 · C3 Apply
- Course Learning Outcome(s) served
- CLO3: Apply various techniques and algorithms of AI used in general problem solving, optimization problems, constraint satisfaction problems, and game programming.
Slide 5
MODULE 1
Alpha-Beta Pruning
Same answer as minimax, fewer nodes
Do We Need Every Leaf?
MODULE 1 · THE IDEA
REAL OUTPUT: L18/AB_TREE2.PY
A
- Root value: alpha-beta 3
- Root value: minimax 3
- Leaves examined: 3, 12, 8, 2, 14, 5, 2
- = 7 of 9 leaves
- Pruned at C: alpha 3 >= beta 2
- 2 leaves skipped
B
C
≤2
D
✗
✗
Key step: C's first leaf is 2, so C is at most 2. MAX already has 3 from B, so C's other leaves (4, 6) cannot matter - skip them.
Alpha and Beta
MODULE 1 · ALGORITHM
ALPHA-BETA (GAMELIB.PY)
- def alphabeta(s, alpha, beta):
- if TERMINAL(s): return UTILITY(s)
- if PLAYER(s) == MAX:
- v = -inf
- for a in ACTIONS(s):
- v = max(v, alphabeta(RESULT(s,a), alpha, beta))
- alpha = max(alpha, v)
- if alpha >= beta: break # prune
- return v
- else: # MIN
- v = +inf
- for a in ACTIONS(s):
- v = min(v, alphabeta(RESULT(s,a), alpha, beta))
- beta = min(beta, v)
- if alpha >= beta: break # prune
- return v
Alpha (α)
Best value MAX is already sure of. Starts at -∞. Only MAX nodes raise it.
Beta (β)
Best value MIN is already sure of. Starts at +∞. Only MIN nodes lower it.
Prune when α ≥ β
The other player will never allow this branch - skip the rest.
Alpha-Beta Trace, Step by Step
MODULE 1 · WORKED TRACE
Same answer: minimax and alpha-beta both give 3 and choose B. Alpha-beta looked at 7 leaves, minimax at 9.
Move Ordering Decides the Savings
MODULE 1 · EFFICIENCY
REAL OUTPUT: L18/ORDERING.PY
- Original order value 3, leaves examined 7 of 9
- Worst order (best last) value 3, leaves examined 9 of 9
- Best order (D: 2 first) value 3, leaves examined 5 of 9
- Tic-tac-toe from the empty board: minimax 549,946 nodes, alpha-beta 18,297 nodes (3.3%)
- Chess depth 6: minimax 35^6 = 1.8e+09; perfect alpha-beta about 35^3 = 4.3e+04
- Chess depth 8: minimax 35^8 = 2.3e+12; perfect alpha-beta about 35^4 = 1.5e+06
- Chess depth 10: minimax 35^10 = 2.8e+15; perfect alpha-beta about 35^5 = 5.3e+07
Best first
Best last
In practice
Most pruning: about b^(m/2) nodes - twice the depth in the same time.
No pruning: alpha-beta = minimax (9 of 9 leaves).
Try captures and last search's best move first.
Slide 10
MODULE 2
Constraint Satisfaction Problems
Variables, values and rules
What Is a CSP? Map Colouring
MODULE 2 · DEFINITION
NT
Q
WA
SA
NSW
V
Not a path: only the final assignment matters - like 8-queens in L15.
T
Constraint graph of Australia
A Local CSP: The Exam Timetable
MODULE 2 · EXAMPLE
REAL OUTPUT: L18/TIMETABLE.PY
- AI clashes with DB, OS, SE
- DB clashes with AI, OS, Stats
- OS clashes with AI, DB, HCI
- SE clashes with AI, HCI, Stats
- HCI clashes with OS, SE
- Stats clashes with DB, SE
- Timetable: AI Mon, DB Tue, OS Wed, SE Tue, HCI Mon, Stats Mon (values tried 6)
- With only Mon and Tue: no solution (values tried 4) - AI, DB, OS all clash with each other
Slide 13
MODULE 3
Solving CSPs
Backtracking and smart choices
Backtracking Search
MODULE 3 · ALGORITHM
REAL OUTPUT: L18/MAP_BT.PY
BACKTRACKING (CSP.PY)
- WA = red
- NT = red x clash
- NT = green
- SA = red x clash
- SA = green x clash
- SA = blue
- NSW = red
- Q = red x clash
- Q = green x clash
- Q = blue x clash
- undo NSW
- NSW = green
- Q = red
- V = red
- T = red
- Solution: WA=red, NT=green, SA=blue, NSW=green, Q=red, V=red, T=red
- Values tried: 14
- def backtrack(a):
- if every variable assigned:
- return a
- v = next unassigned variable
- for val in DOMAIN(v):
- if val consistent with a:
- a[v] = val
- r = backtrack(a)
- if r: return r
- del a[v] # undo
- return None
Dead end: Q has no colour left - undo NSW and try its next colour.
Smarter Backtracking: MRV and Forward Checking
MODULE 3 · HEURISTICS
REAL OUTPUT: L18/HEURISTICS.PY
- Australia with MRV + degree + forward checking:
- SA = red NSW:G/B, NT:G/B, Q:G/B, V:G/B, WA:G/B
- NT = green Q:B, WA:B
- Q = blue NSW:G
- NSW = green V:B
- WA = blue
- V = blue
- T = red
- Values tried: 7
- n-queens, values tried (FC = forward checking):
- 8 queens: plain 876 FC 88 FC+MRV 75
- 16 queens: plain 160,712 FC 7,560 FC+MRV 44
- 24 queens: plain 9,878,316 FC 286,963 FC+MRV 43
24 queens: 9,878,316 tries plain - 43 tries with forward checking + MRV.
Activity Trees
CLASS ACTIVITY · USE THESE TREES
Tree 1 (2 plies)
Tree 2 (3 plies: MAX, MIN, MAX)
A
A
B
C
D
B
C
D
Activity: Prune the Trees, Solve the CSP
CLASS ACTIVITY · PAIRS · 8 MIN
Pairs, 6 min + 2 min check. Trees on the previous slide; Australia on S11; timetable on S12.
Tree 1: run alpha-beta. Which leaf is pruned? Root value?
After SA = red, which domains does forward checking change?
Tree 2: run alpha-beta. Which subtrees are pruned? Root value?
Timetable: HCI and Stats now also clash. Is 3 days still enough?
Australia: which variable does MRV + degree choose first? Why?
Chess to depth 8: nodes for minimax vs best-case alpha-beta?
Common Mistakes
WATCH OUT
WRONG IDEA
CORRECT IDEA
✗ Alpha-beta can give a different answer.
✓ It always gives the same value as minimax - it only skips work.
✗ Pruning depends only on the tree.
✓ It depends on move ORDER: best-first prunes most, best-last prunes nothing.
✗ MIN nodes change alpha.
✓ MAX nodes raise alpha; MIN nodes lower beta.
✗ A CSP solution is a path of moves.
✓ It is a complete assignment that breaks no constraint.
✗ MRV picks the variable with most values.
✓ Fewest values left - fail early; degree breaks ties.
Recap Quiz
CHECK YOUR UNDERSTANDING · 3 MIN
- Q1. Compared with minimax, alpha-beta gives:
- (a) A better value (b) The same value (c) A worse value (d) Only an estimate
- Q2. A branch is pruned when:
- (a) alpha ≥ beta (b) alpha = 0 (c) beta = +∞ (d) The leaf is negative
- Q3. With perfect move ordering, alpha-beta examines about:
- (a) b^m nodes (b) b^(m/2) nodes (c) b·m nodes (d) m^b nodes
- Q4. MRV chooses the variable with:
- (a) Most values (b) Fewest values left (c) Largest name (d) Most solutions
Homework and Next Lecture
BEFORE THE NEXT CLASS
- Task 1: Alpha-beta on: MAX root; MIN nodes B [5, 6, 7], C [4, 8, 9], D [3, 9, 1]. Write [α, β] at each node; which leaves are pruned? Root value?
- Task 2: Formulate 4-queens as a CSP (variables, domains, constraints) and solve it by backtracking (queen per column, rows 0-3 tried in order).
- Task 3: Add a Math paper to the timetable that clashes with AI, SE and HCI. Find a 3-day timetable.
- Read: Russell & Norvig 5.3 (alpha-beta) and 6.1-6.3 (CSPs, backtracking).
NEXT LECTURE
- Lecture 19:
- Rules and Expert Systems