ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information...

51
ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems

description

ADVERSARIAL SEARCH  Optimal decisions  MinMax algorithm  α-β pruning  Imperfect, real-time decisions 3

Transcript of ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information...

Page 1: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

ARTIFICIAL INTELLIGENCE (CS 461D)

Princess Nora UniversityFaculty of Computer & Information

Systems

Page 2: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

(CHAPTER-5)ADVERSARIAL SEARCH

Page 3: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

3

ADVERSARIAL SEARCH

Optimal decisions MinMax algorithm α-β pruning Imperfect, real-time decisions

Page 4: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

4

ADVERSARIAL SEARCH

Page 5: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

Search problems seen so far: Single agent. No interference from other agents and no competition.

Game playing: Multi-agent environment. Cooperative games. Competitive games adversarial search

Specifics of adversarial search: Sequences of player’s decisions we control. Decisions of other players we do not control.

Page 6: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

Games vs. search problems

• "Unpredictable" opponent specifying a move for every possible opponent reply

• Time limits unlikely to find goal, must approximate

Page 7: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

7

Game as Search ProblemMinMax Algorithm

Page 8: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

8

Game setup

Consider a game with Two players: (Max) and (Min) Max moves first and they take turns until the game is over. Winner gets award,

loser gets penalty.

Games as search:

Initial state: e.g. board configuration of chess

Successor function: list of (move, state) pairs specifying legal moves.

Goal test: Is the game finished?

Utility function: Gives numerical value of terminal states. E.g. win (+1), lose (-1)

and draw (0) in tic-tac-toe

Max uses search tree to determine next move.

Page 9: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

9

MAX has 9 possible first moves, etc.

Utility value is always from the point of view of MAX.

High values good for MAX and bad for MIN.

Example of an ADVERSARIAL two player Game

Tic-Tac-Toe (TTT)

Page 10: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

10

Example of an adversarial 2 person game:Tic-tac-toe

Page 11: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

11

How to Play a Game by Searching

• General Scheme• Consider all legal moves, each of which will lead to some new

state of the environment (‘board position’)• Evaluate each possible resulting board position• Pick the move which leads to the best board position.• Wait for your opponent’s move, then repeat.

• Key problems• Representing the ‘board’• Representing legal next boards• Evaluating positions• Looking ahead

Page 12: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

12

Game TreesRepresent the problem space for a game by a tree

Nodes represent ‘board positions’ (state)edges represent legal moves.

Root node is the position in which a decision must be made.

Evaluation function f assigns real-number scores to `board positions.’

Terminal nodes (leaf) represent ways the game could end, labeled with the desirability of that ending (e.g. win/lose/draw or a numerical score)

Page 13: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

13

MAX & MIN Nodes

• When I move, I attempt to MAXimize my performance.• When my opponent moves, he attempts to MINimize my

performance.TO REPRESENT THIS:

• If we move first, label the root MAX; if our opponent does, label it MIN.

• Alternate labels for each successive tree level.• if the root (level 0) is our turn (MAX), all even levels will

represent turns for us (MAX), and all odd ones turns for our opponent (MIN).

Page 14: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

14

Evaluation functions

• Evaluations how good a ‘board position’ is• Based on static features of that board alone

• Zero-sum assumption lets us use one function to describe goodness for both players.• f(n)>0 if we are winning in position n• f(n)=0 if position n is tied• f(n)<0 if our opponent is winning in position n

• Build using expert knowledge (Heuristic),

• Tic-tac-toe: f(n)=(# of 3 lengths possible for me)- (# 3 lengths possible for you)

Page 15: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

15

Heuristic measuring for adversarial tic-tac-toe

Maximize E(n)

E(n) = 0 when my opponent and I have equal number of possibilities.

Page 16: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

16

MinMax Algorithm

• Main idea: choose move to position with highest minimax value. = best achievable payoff against best play.

• E.g., 2-ply game:

Page 17: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

17

MinMax Algorithm

Page 18: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

18

MinMax steps

Int MinMax (state s, int depth, int type){if( terminate(s)) return Eval(s);if( type == max ){

for ( child =1, child <= NmbSuccessor(s); child++){

value = MinMax(Successor(s, child), depth+1, Min)if( value > BestScore) BestScore = Value;

}}if( type == min ){

for ( child =1, child <= NmbSuccessor(s); child++){

value = MinMax(Successor(s, child), depth+1, Max)if( value < BestScore) BestScore = Value;

}}return BestScore;

}

Page 19: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

19

Minimax tree

Max

Min

Max

Min

-24-8-14-73-100-5-70-12-370412-3212823

Page 20: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

20

Minimax tree

Max

Min

Max

Min

100

-24-8-14-73-100-5-70-12-370412-3212823

28 -3 12 70 -3 -73 -14 -8

Page 21: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

21

Minimax treeMax

Min

Max

Min

100

-24-8-14-73-100-5-70-12-470412-3212823

21 -3 12 70 -4 -73 -14 -8

-3 -4 -73

Page 22: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

22

Minimax tree

Max

Min

Max

Min

100

-24-8-14-73-100-5-70-12-470412-3212823

21 -3 12 70 -4 -73 -14 -8

-3 -4 -73

-3

Page 23: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

23

Minimax

max

min

max

min

Page 24: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

24

Minimaxmax

min

max

min 10 9 14 13 2 1 3 24

10 14 2 24

10 2

10

Page 25: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

25

MinMax Analysis• Time Complexity: O(bd)

• Space Complexity: O(b*d) • Optimality: Yes

Problem: Game Resources Limited!• Time to make an action is limited

• Can we do better ? Yes !• How ? Cutting useless branches !

Page 26: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

26

a Cuts• If the current max value is greater than the successor’s min value, don’t explore that min subtree any more

Page 27: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

27

a Cut example

10021 -3 12 70 -4 -73 -14

-3 -4

-3

-73

Max

Max

Min

Page 28: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

28

a Cut example

• Depth first search along path 1

10021 -3 12 -70 -4 -73 -14

Max

Max

Min

Page 29: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

29

a Cut example

• 21 is minimum so far (second level)• Can’t evaluate yet at top level

10021 -3 12 -70 -4 -73 -14

Max

Max

Min

21

Page 30: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

30

a Cut example

• -3 is minimum so far (second level)• -3 is maximum so far (top level)

10021 -3 12 -70 -4 -73 -14

Max

Max

Min-3

-3

Page 31: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

31

a Cut example

• -70 is now minimum so far (second level)• -3 is still maximum (can’t use second node yet)

10021 -3 12 -70 -4 -73 -14

Max

Max

Min

-3

-3

-70

Page 32: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

32

a Cut example

• Since second level node will never be > -70, it will never be chosen by the previous level

• We can stop exploring that node

10021 -3 12 -70 -4 -73 -14

Max

Max

Min-3

-3

-70

Page 33: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

33

a Cut example

• Evaluation at second level is -73

10021 -3 12 -70 -4 -73 -14

Max

Max

Min-3

-3

-70 -73

Page 34: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

34

a Cut example

• Again, can apply a cut since the second level node will never be > -73, and thus will never be chosen by the previous level

10021 -3 12 -70 -4 -73 -14

Max

Max

Min-3

-3

-70 -73

Page 35: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

35

a Cut example

• As a result, we evaluated the Max node without evaluating several of the possible paths

10021 -3 12 -70 -4 -73 -14

Max

Max

Min

-3

-3

-70 -73

Page 36: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

36

b cuts

• Similar idea to a cuts, but the other way around• If the current minimum is less than the successor’s max

value, don’t look down that max tree any more

Page 37: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

37

b Cut example

• Some subtrees at second level already have values > min from previous, so we can stop evaluating them.

10021 -3 12 70 -4 73 -14

Min

Min

Max

21

21

70 73

Page 38: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

38

a-b Pruning

• Pruning by these cuts does not affect final result• May allow you to go much deeper in tree

• “Good” ordering of moves can make this pruning much more efficient

• Evaluating “best” branch first yields better likelihood of pruning later branches

• Perfect ordering reduces time to bm/2

• i.e. doubles the depth you can search to!

Page 39: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

39

a-b Pruning

• Can store information along an entire path, not just at most recent levels!

• Keep along the path:• a: best MAX value found on this path

(initialize to most negative utility value)• b: best MIN value found on this path

(initialize to most positive utility value)

Page 40: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

40

The Alpha and the Beta

• For a leaf, a = b = utility• At a max node:

• a = largest child utility found so far• b = b of parent

• At a min node:• a = a of parent• b = smallest child utility found so far

• For any node:• a <= utility <= b• “If I had to decide now, it would be...”

Page 41: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

41

Alpha-Beta example

Page 42: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

The Alpha-Beta Procedure

Example: max

min

max

min

Page 43: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

The Alpha-Beta Procedure

4

Example: max

min

max

minb = 4

Page 44: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

The Alpha-Beta Procedure

4

Example: max

min

max

minb = 4

5

Page 45: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

The Alpha-Beta Procedure

4

Example: max

min

max

minb = 3

5 3

a = 3

Page 46: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

The Alpha-Beta Procedure

4

Example: max

min

max

minb = 3

5 3

a = 3

b = 1

1

Page 47: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

The Alpha-Beta Procedure

4

Example: max

min

max

minb = 3

5 3

a = 3

b = 1

1

b = 3

Page 48: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

The Alpha-Beta Procedure

4

Example: max

min

max

minb = 3

5 3

a = 3

b = 1

1

b = 3

b = 8

8

Page 49: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

The Alpha-Beta Procedure

4

Example: max

min

max

minb = 3

5 3

a = 3

b = 1

1

b = 3

b =6

8 6

Page 50: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

The Alpha-Beta Procedure

4

Example: max

min

max

minb = 3

5 3

a = 3

b = 1

1

b = 3

b =6

8 6 7

a = 6

Page 51: ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.

51

Thank you

End of Chapter 5