0% found this document useful (0 votes)
5 views67 pages

Ch5 - Adversarial Search

Adversarial search is a strategy used in competitive environments where an opponent actively works against the planner, exemplified in games like chess. The minimax algorithm is a key method in adversarial search, where one player maximizes their score while the opponent minimizes it, and the alpha-beta pruning technique optimizes the search process. The document discusses the properties, complexities, and applications of these algorithms in game playing scenarios.

Uploaded by

Alaa Ismail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views67 pages

Ch5 - Adversarial Search

Adversarial search is a strategy used in competitive environments where an opponent actively works against the planner, exemplified in games like chess. The minimax algorithm is a key method in adversarial search, where one player maximizes their score while the opponent minimizes it, and the alpha-beta pruning technique optimizes the search process. The document discusses the properties, complexities, and applications of these algorithms in game playing scenarios.

Uploaded by

Alaa Ismail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Adversarial Search

Chapter 5

1
What is Adversial Search

Adverisal Search is:


• A method applied to a situation where you are planning while another
actor prepares against you.

• Search when there is an "enemy" or "opponent" changing the state of


the problem every step in a direction you do not want.
• Examples: Chess, business, trading, war.

3
Game Playing

Why do AI researchers study game playing?

1. It’s a good reasoning problem, formal and nontrivial.

2. Direct comparison with humans and other


computer programs is easy.

3
What Kinds of Games?
Mainly games of strategy with the following
characteristics:
1. Sequence of moves to play
2. Rules that specify possible moves
3. Rules that specify a payment for each move
4. Objective is to maximize your payment

4
Games vs. Search Problems

• Unpredictable opponent → specifying a move


for every possible opponent reply

• Time limits → unlikely to find goal, must


approximate

5
Games vs. Search Problems

5
Two-Player Game
Opponent’s Move

Generate New Position

Game yes
Over?
no
Generate Successors

Evaluate Successors

Move to Highest-Valued Successor

no Game yes
Over?

6
Games as Adversarial Search
• States:
– board configurations
• Initial state:
– the board position and which player will move
• Successor function:
– returns list of (move, state) pairs, each indicating a legal
move and the resulting state
• Terminal test:
– determines when the game is over
• Utility function:
–gives a numeric value in terminal states
(e.g., -1, 0, +1 for loss, tie, win)
7
Game Tree (2-player, Deterministic, Turns)

computer’s turn

opponent’s turn

computer’s turn The computer is Max.


The opponent is Min.
opponent’s turn

leaf nodes are At the leaf nodes, the


evaluated utility function
is employed. Big value
means good, small is ba8d.
Mini-Max Terminology
• move: a move by both players
• ply: a half-move
• utility function: the function applied to leaf nodes
• backed-up value
– of a max-position: the value of its largest successor
– of a min-position: the value of its smallest successor
• minimax procedure: search down several levels; at
the bottom level apply the utility function, back-up
values all the way up to the root node, and that node
selects the move.
9
Minimax
• Perfect play for deterministic games
• Idea: In Minimax the two players are called maximizer
and minimizer. The maximizer tries to get the highest
score possible while the minimizer tries to do the
opposite and get the lowest score possible.
• E.g., 2-ply game:

10
80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
11
80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
12
80

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
13
30

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
14
30

30

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
15
30

30 25

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
16
30

30 25

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
17
30

30

30 25

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
18
30

30

30 25

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
19
30

30 20

30 25 20

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
20
30

30 20

30 25 20 05

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
21
30

30 20

30 25 20 05

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
22
30

30 20

30 25 20 05

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
23
20

20

30 20

30 25 20 05

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
24
20

20 15

30 20 15 60

30 25 20 05 10 15 45 60

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
25
20

20 15

30 20 15 60

30 25 20 05 10 15 45 60

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
26
20

20 15

30 20 15 60

30 25 20 05 10 15 45 60

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
27
Tic-Tac-Toe
Minimax Strategy
• Why do we take the min value every other
level of the tree?

• These nodes represent the opponent’s choice


of move.

• The computer assumes that the human will


choose that move that is of least value to the
computer.
28
Minimax algorithm
Adversarial analogue of DFS

29
Properties of Minimax
• Complete?
– Yes (if tree is finite)
• Optimal?
– Yes (against an optimal opponent)
b: legal moves,
m: depth of the tree
• Time complexity?
– O(bm)
• Space complexity?
– O(bm) (depth-first exploration)

30
Good Enough?
• Chess:
– branching factor b≈35

– game length m≈100

– search space bm ≈ 35100 ≈10154

• The Universe:
– number of atoms ≈ 1078

– age ≈ 1018 seconds

– 108 moves/sec x 1078 x 1018 =10104

• Exact solution completely infeasible


31
80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
32
30

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
33
30

30 25

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
34
30 Do we need to check
this node?

30 25

80 30 25 ?? 55 20 05 65 40 10 70 15 50 45 60 75
35
30 No - this branch is guaranteed to be
worse than what max already has

30 25

80 30 25 X
?? 55 20 05 65 40 10 70 15 50 45 60 75
36
30

30 20
Do we need to check
this node?

30 25 20 05

80 30 25 X
35 55 20 05 ?? 40 10 70 15 50 45 60 75
37
30

30 20

30 25 20 05

80 30 25 X
35 55 20 05 X
?? 40 10 70 15 50 45 60 75
38
Alpha-Beta
• The alpha-beta procedure can speed up a
depth-first minimax search.
• Alpha: a lower bound on the value that a max node
may ultimately be assigned
v>

• Beta: an upper bound on the value that a


minimizing node may ultimately be assigned
v<

39
Alpha-Beta
MinVal(state, alpha, beta){
if (terminal(state))
return utility(state);
for (s in children(state)){
child = MaxVal(s,alpha,beta);
beta = min(beta,child);
if (alpha>=beta) return child;
}
return best child (min); }

alpha = the highest value for MAX along the path


beta = the lowest value for MIN along the path
40
Alpha-Beta
MaxVal(state, alpha, beta){
if (terminal(state))
return utility(state);
for (s in children(state)){
child = MinVal(s,alpha,beta);
alpha = max(alpha,child);
if (alpha>=beta) return child;
}
return best child (max); }

alpha = the highest value for MAX along the path


beta = the lowest value for MIN along the path
41
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the path

α=-∞
β=∞

α=-

β=∞

α=-

β=∞

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
42
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the path

α=-∞
β=∞

α=-

β=∞

α=-∞ 80
β=80

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
43
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the path

α=-∞
β=∞

α=-

β=∞

α=-∞
30
β=30

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
44
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the path

α=-∞
β=∞

α=30
β=∞ 30

α=-∞
30
β=30

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
45
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the path

α=-∞
β=∞

α=30
β=∞ 30

α=30
β=∞
α=-∞
30
β=30

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
46
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the path

α=-∞
β=∞

α=30
β=∞ 30

β≤ α
α=30
β=25 prune!
α=-∞
30 25
β=30

80 30 25 X
35 55 20 05 65 40 10 70 15 50 45 60 7547
75
47
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the path

α=-∞ 30
β=30

α=30
β=∞ 30

α=30
β=25
α=-∞
30 25
β=30

80 30 25 X
35 55 20 05 65 40 10 70 15 50 45 60 7548
75
48
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the path

α=-∞ 30
β=30

α=30 α=-∞
β=∞ 30 β=30

α=30
β=25
α=-∞ α=-∞
30 25
β=30 β=30

80 30 25 X
35 55 20 05 65 40 10 70 15 50 45 60 7549
75
49
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the path

α=-∞ 30
β=30

α=30 α=20
β=∞ 30 β=30 20

α=30 α=20
β=25 β=30
α=-∞ α=-∞ 20
30 25
β=30 β=20

80 30 25 X
35 55 20 05 65 40 10 70 15 50 45 60 7550
75
50
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the path

α=-∞ 30
β=30

α=30 α=20
β=∞ 30 β=30 20

α=30 α=20
β=25 β=05
α=-∞ α=-∞ 20
30 25 05
β=30 β=20

80 30 25 X
35 55 20 05 65 40 10 70 15 50 45 60 7551
75
51
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the path

α=-∞ 30
β=30

α=30 α=20
β=∞ 30 β=30 20

β≤ α
α=30 α=20
β=25 β=05 prune!
α=-∞ α=-∞ 20
30 25 05
β=30 β=20

80 30 25 X
35 55 20 05 X
65 40 10 70 15 50 45 60 7552
75
52
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the path

α=-∞ 20
β=20

α=30 α=20
β=∞ 30 β=30 20

α=30 α=20
β=25 β=05
α=-∞ α=-∞ 20
30 25 05
β=30 β=20

80 30 25 X
35 55 20 05 X
65 40 10 70 15 50 45 60 7553
75
53
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the path

α=-∞ 20
β=20

α=30 α=20
β=∞ 30 β=30 20

α=30 α=20
β=25 β=05
α=-∞ α=-∞ 20
30 25 05
β=30 β=20

80 30 25 X
35 55 20 05 X
65 40 10 70 15 50 45 60 7554
75
54
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the path

α=20
20
β=∞

α=20
30 20 β=∞

α=20
30 25 20 05 β=∞

80 30 25 X
35 55 20 05 X
65 40 10 70 15 50 45 60 7555
75
55
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the path

α=20
20
β=∞

α=20
30 20 β=∞

α=20
30 25 20 05 β=10 10

80 30 25 X
35 55 20 05 X
65 40 10 70 15 50 45 60 7556
75
56
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the path

α=20
20
β=∞

α=20
30 20 10 β=∞

α=20
30 25 20 05 β=10 10

80 30 25 X
35 55 20 05 X
65 40 10 70 15 50 45 60 7557
75
57
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the path

α=20
20
β=∞

α=20
30 20 10 β=∞

α=20
α=20 β=15
30 25 20 05 β=10 10 15

80 30 25 X
35 55 20 05 X
65 40 10 70 15 50 45 60 7558
75
58
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the path

α=20
20
β=∞

α=20
30 20 15 β=∞

α=20
α=20 β=15
30 25 20 05 β=10 10 15

80 30 25 X
35 55 20 05 X
65 40 10 70 15 50 45 60 7559
75
59
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the path

α=20
20 15
β=15

α=20
30 20 15 β=∞

α=20
α=20 β=15
30 25 20 05 β=10 10 15

80 30 25 X
35 55 20 05 X
65 40 10 70 15 50 45 60 7560
75
60
α=20
α - the best value 20 β=∞
for max along the path β≤ α
β - the best value prune!
for min along the path

α=20
20 15
β=15

α=20
30 20 15 β=∞ X
α=20
α=20 β=15
30 25 20 05 β=10 10 15
X X

80 30 25 X
35 55 20 05 X
65 40 10 70 15 50 X
X 45 X
60 X
75
61
Bad and Good Cases for Alpha-Beta Pruning
Properties of α-β
• Pruning does not affect final result. This means that it gets the exact same
result as does full minimax.

• Good move ordering improves effectiveness of pruning

• With "perfect ordering," time complexity = O(bm/2)


→ doubles depth of search

• A simple example of reasoning about ‘which computations are relevant’ (a


form of metareasoning)

63
Evaluation Functions
Tic Tac Toe
• Let p be a position in the game
• Define the utility function f(p) by
– f(p) =
• largest positive number if p is a win for computer
• smallest negative number if p is a win for opponent
• RCDC – RCDO
– where RCDC is number of rows, columns and diagonals in
which computer could still win
– and RCDO is number of rows, columns and diagonals in
which opponent could still win.

70
Sample
Evaluations
• X = Computer; O = Opponent

O O O X
X X X

X O X O
rows rows
cols cols
diags diags
71

You might also like