Learning Outcomes
SLO | After this lecture you will be able to... | CLO | Bloom |
|---|---|---|---|
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 |
1. Alpha-Beta Pruning
§1.1 The idea
Slide L18-S06
Prune = cut off a branch that cannot change the decision. In the L17 tree (MIN nodes B [3, 12, 8], C [2, 4, 6], D [14, 5, 2]) MAX is sure of 3 after B. C's first leaf is 2, so C is worth at most 2 and will never be chosen; its leaves 4 and 6 are skipped.
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 |
§1.2 Alpha and beta
Slide L18-S07
Value | Meaning | Starts at | Changed by |
|---|---|---|---|
Alpha (α) | Best value MAX is already sure of | -∞ | MAX nodes (raise it) |
Beta (β) | Best value MIN is already sure of | +∞ | MIN nodes (lower it) |
Prune the remaining children of a node as soon as α ≥ β: the other player will never let the game reach this branch.
def alphabeta(node, maximizing=True, alpha=-math.inf, beta=math.inf, visited=None, pruned=None, path="A"): """Alpha-beta on a nested-list tree. visited: list of leaf paths evaluated; pruned: list of (path, alpha, beta) cut.""" if not isinstance(node, list): if visited is not None: visited.append((path, node)) return node if maximizing: v = -math.inf for i, c in enumerate(node): v = max(v, alphabeta(c, False, alpha, beta, visited, pruned, path + str(i + 1))) alpha = max(alpha, v) if alpha >= beta: if pruned is not None and i + 1 < len(node): pruned.append((path, [path + str(j + 1) for j in range(i + 1, len(node))], alpha, beta)) break return v v = math.inf for i, c in enumerate(node): v = min(v, alphabeta(c, True, alpha, beta, visited, pruned, path + str(i + 1))) beta = min(beta, v) if alpha >= beta: if pruned is not None and i + 1 < len(node): pruned.append((path, [path + str(j + 1) for j in range(i + 1, len(node))], alpha, beta)) break return v |
§1.3 Trace
Slide L18-S08
Step | Node | [α, β] arriving | What happens | Result |
|---|---|---|---|---|
1 | B (MIN) | [-∞, +∞] | Leaf 3: β = 3; 12, 8: no change | B = 3 |
2 | A (MAX) | - | α = 3 | α = 3 |
3 | C (MIN) | [3, +∞] | Leaf 2: β = 2; α ≥ β - prune 4 and 6 | C ≤ 2 |
4 | D (MIN) | [3, +∞] | 14: β = 14; 5: β = 5; 2: β = 2 | D = 2 |
5 | A (MAX) | - | max(3, 2, 2) | A = 3, play B |
§1.4 Move ordering
Slide L18-S09
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 |
Alpha-beta always returns the same value as minimax. How much it saves depends on the order of the moves: best moves first gives about b^(m/2) nodes (twice the depth in the same time); best moves last gives no pruning at all.
2. Constraint Satisfaction Problems
§2.1 Definition: map colouring
Slide L18-S11
A CSP is defined by variables (things to decide), domains (allowed values) and constraints (rules on combinations of values). A solution is a complete assignment that breaks no constraint. Example: colour the 7 regions of Australia with red, green, blue so that neighbours differ.
Part | Australia map colouring |
|---|---|
Variables | WA, NT, SA, Q, NSW, V, T |
Domains | {red, green, blue} |
Constraints | WA ≠ NT, WA ≠ SA, NT ≠ SA, NT ≠ Q, SA ≠ Q, SA ≠ NSW, SA ≠ V, Q ≠ NSW, NSW ≠ V |
Constraint graph | Nodes = variables, edges = constraints; SA has 5 edges, T none |
§2.2 A local CSP: the exam timetable
Slide L18-S12
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 |
CSP | Variables | Domains | Constraints |
|---|---|---|---|
Exam timetable | Course papers | Exam days | Shared students -> different days |
Sudoku | 81 cells | 1-9 | All different in each row, column, box |
8-queens | Column of each queen | Row 1-8 | No two queens attack |
3. Solving CSPs
§3.1 Backtracking search
Slide L18-S14
Assign one variable at a time; check each value against the assigned neighbours; on a clash try the next value; if no value fits, undo the previous variable (backtrack). Order WA, NT, SA, NSW, Q, V, T; colours red, green, blue:
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 |
§3.2 Heuristics and forward checking
Slide L18-S15
Technique | Rule | Why |
|---|---|---|
MRV | Choose the variable with the fewest legal values left | Fail early |
Degree | Tie-break: the variable in most constraints | SA first in Australia |
LCV | Try the value that rules out the fewest neighbour values | Keep options open |
Forward checking | After an assignment remove the value from neighbours' domains | An empty domain means backtrack now |
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 |
4. Common Mistakes
Wrong idea | Correct idea |
|---|---|
Alpha-beta can give a different answer. | Always the same value as minimax. |
Pruning depends only on the tree. | It depends on move order. |
MIN nodes change alpha. | MAX raises alpha; MIN lowers beta. |
A CSP solution is a path. | It is a complete, consistent assignment. |
MRV picks most values. | Fewest values left. |
Word Bank
Word | Simple meaning | Example |
|---|---|---|
Prune | Cut off a branch that cannot matter | Skip C's leaves 4 and 6 |
Alpha / beta | Values MAX / MIN are already sure of | [3, +∞] at C |
Constraint | A rule on allowed combinations | WA ≠ NT |
Domain | Allowed values of a variable | {red, green, blue} |
Backtrack | Undo the last choice and try another | Undo NSW = red |
Forward checking | Remove a used value from neighbours at once | SA = red removes red from WA |
Self-Check Questions
- Why can C's leaves 4 and 6 be skipped? (see §1.1)
- Define alpha and beta and the pruning condition. (see §1.2)
- Run alpha-beta on B [5, 9], C [7, 3, 8], D [6, 6]. (see §1.3)
- Why does move ordering matter? (see §1.4)
- Formulate the exam timetable as a CSP. (see §2.2)
- Explain MRV and forward checking with Australia. (see §3.2)
Reading: Russell & Norvig (4e) 5.3 (alpha-beta), 6.1-6.3 (CSPs, backtracking, forward checking).