New CZ3005 Module 3 - Constraint Satisfaction and Adversarial Search
New CZ3005 Module 3 - Constraint Satisfaction and Adversarial Search
Artificial Intelligence
https://personal.ntu.edu.sg/hanwangzhang/
Email: [email protected]
Office: N4-02c-87
Lesson Outline
• Constraint Satisfaction
• Adversarial search (Game Playing)
Constraint Satisfaction Problem (CSP)
Goal: discover some state that satisfies a given set of
constraints
Example: Sudoku Example: Minesweeper
1 3
7 6
5 9
R
9
R
1 6
2
7 4 5
8 4
1
Examples: Real-world CSPs
• Assignment problems
• e.g. who teaches what class
• Timetabling problems
• e.g. which class is offered when and where?
• Hardware configuration
• Transportation scheduling
• Factory scheduling
• Floor-planning
CSP
State
• defined by variables with values from domain
Example: 8-queens
• Variables: locations of each of the eight queens
• Values: squares on the board
Goal test
• a set of constraints specifying allowable combinations of values for subsets of
variables
Example: 8-queens
R
• Goal test: No two queens in the same row, column or diagonal
Example: Cryptarithmetic Puzzle
S E N D
+ M O R E
M O N E Y
Variables: D, E, M, N, O, R, S, Y
Value ∈ Domain: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Constraints
Y = D + E or Y = D + E - 10, etc
D ≠ E, D ≠ M, D ≠ N, etc.
M ≠ 0, S ≠ 0 (unary constraints: concern the value of a
single variable)
Example: Map Colouring
Colour a map so that no adjacent parts have the same colour
• Variables: Countries Ci
• Domains: {Red, Blue, Green}
• Contraints: C1 ≠ C2, C1 ≠ C5, etc.
• binary constraints
Some Definitions
Example
• Row the 1st queen occupies: ∈ {1, 2, 3, 4, 5, 6, 7, 8 }
(similarly, for )
R
Heuristics for CSPs
Plain backtracking is an uninformed algorithm!!
More intelligent search that takes into consideration
• Which variable to assign next
• What order of the values to try for each variable
• Implications of current variable assignments for the other unassigned
variables
• forward checking and constraint propagation
Search Tree of 4-Queens with Constraint
Propagation
Example (Map Colouring)
NT
Northern
Territory Q
Queensland WA
Western
Australia South
SA NSW
Australia
New South
Wales
V
Victoria
T
Tasmania
Example (Map Colouring)...
NT
Q
WA
SA
NSW
V
T
WA NT Q NSW V SA T
Example (Map Colouring)...
WA NT Q NSW V SA T
Example (Map Colouring)...
WA NT Q NSW V SA T
Example (Map Colouring)...
WA NT Q NSW V SA T
Search Tree of 4-Queens with Constraint
Propagation
Most Constraining Variable
To reduce the branching factor on future choices by selecting the variable that is
involved in the largest number of constraints on unassigned variables.
Least Constraining Value
Allow 1 value
for SA
Allow 0 values
for SA
Choose the value that leaves maximum flexibility for subsequent variable
assignments
Min-Conflicts Heuristic (8-queens)
• A local heuristic search method for solving CSPs
• Given an initial assignment, selects a variable in the scope of a violated
constraint and assigns it to the value that minimises the number of violated
constraints
2 3
2 3
1
2 2
3 3
1 2
2 3
0
Games as Search Problems
Abstraction
• Ideal representation of real world problems
• e.g. board games, chess, go, etc. as an abstraction of war games
• Perfect information, i.e. fully observable
• Accurate formulation: state space representation
Uncertainty
• Account for the existence of hostile agents (players)
• Other agents acting so as to diminish the agent's well-being
• Uncertainty (about other agents' actions):
• not due to the effect of non-deterministic actions
• not due to randomness
Contingency problem
Games as Search Problems...
Complexity
• Games are abstract but not simple
• e.g. chess: average branching factor = 35, game length > 50
complexity = (only for legal moves)
Deterministic Chance
Example: Chess
R
Win (+1), loss(-1) or draw (0)
Game Tree for Tic-Tac-Toe
MAX(X)
X X X
MIN(O) X X X
X X X
X O X O X
O
MAX(X)
…
• The utility value of the terminal state
X O X X O X O is from the point of view of MAX
MIN(O) X X …
… … … …
• MAX uses the search tree to
determine the best move
X O X X O X X O X
TERMINAL …
O X O O X X
O X X O X O O
Utility -1 0 +1
A Toy Min-Max Example
A Toy Min-Max Example
Step 1:
Expand the whole game tree.
Initialize each node to –Inf for max
+Inf for min
A Toy Min-Max Example
Step 2:
Update the value from btm to top
Step 2:
Update the value from btm to top
Search strategy
• Find a sequence of moves that leads to a terminal state (goal)
2. Calculate utility
a) Assess the utility of each terminal state
b) Determine the best utility of the parents of the terminal state
c) Repeat the process for their parents until the root is reached
3. Select the best move (i.e. the move with the highest utility value)
Perfect Decisions by Minimax Algorithm
Perfect decisions: no time limit is Example
imposed
• generate the complete MAX 3
search tree 𝐴1
𝐴2
𝐴3
• After placing the new piece, the pieces from the
opponent player will be captured and become the
pieces from the same Player
O X
O considers the game now X O
X X
X
X X X
Game Tree Othello 4
X O
MAX(X) O X
X O
MIN(O) X X
X
MAX(X) O O O X O X O
X X O X X O
X O X X O
X X X X X
O X O O X O O O X O O X X X X X X X O X O
X X X X X X X X X O X X X X X X X
X X X X X O X O X O X X X
X
X O X X X X X
X X X O X O X
MIN(O) O X O X O X
Imperfect Decisions
For chess, branching factor ≈ 35, each player typically makes 50 moves for the
complete game tree, need to examine positions
Time/space requirements complete game tree search is intractable
impractical to make perfect decisions
Modifications to minimax algorithm
1. replace utility function by an estimated desirability of the position
• Evaluation function
2. partial tree search
• E.g., depth limit
• Replace terminal test by a cut-off test
Evaluation Functions
Returns an estimate of the expected utility of the game from a given position
Example (Chess)
Define features
e.g. , (number of white queens) – (number of black queens)
X O
MIN(O) X X 3( - ) + 2( - ) + ( - )
X
MAX(X) O O O X O X O
X X O X X O
X O X X O
X X X X X
O X O O X O O O X O O X X X X X X X O X O
X X X X X X X X X O X X X X X X X
X X X X X O X O X O X X X
5 4 4 5 X 5 6 4 9
X O X X X X X
X X X O X O X
MIN(O) O X O X O X
3 3 3
Minimax Search for Othello 4
X O
MAX(X) 3 O X
X O
MIN(O) 3 X X
X
MAX(X) O O O X O X O
5 X X 3 O X 9 X O
X O X X O
X X X X X
O X O O X O O O X O O X X X X X X X O X O
X X X X X X X X X O X X X X X X X
X X X X X O X O X O X X X
5 4 4 5 X 5 6 4 9
X O X X X X X
X X X O X O X
O X O X O X
MIN(O) 3 3 3
Quiescent Search
The evaluation function should only be applied to quiescent positions
– positions that are not likely to have large variations in evaluation in the near future
Example (Chess)
Positions in which favorable captures can be made
Black: to move
White: about to lose
#turns
#Moves = 250150
#stone positions
Another Example: Monte Carlo Search Tree (MCST)
MCST Key Steps
#Wins/#Games
MCST for Go is very expensive & slow
Solution:
1. For selection/expansion, no longer stats
(xx/xx) but f = g+h
2. For simulation, no longer real self-play but f =
g+h
MCST: Update f = g() + h()
f f f f f f f f
f f f f f f f f
No real play,
just an estimation