artificial intelligence in game design board games and the minmax algorithm

31
Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Upload: penelope-robbins

Post on 17-Dec-2015

226 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Artificial Intelligence in Game Design

Board Games and the MinMax Algorithm

Page 2: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Board Games

• Examples:– Chess, checkers, tic tac toe, backgammon, etc.

• Characteristics:– Alternating moves:

player AI player AI …– Limited (but usually not small) set of possible moves

• Example: legal moves in chess

– Set of winning and losing board configurations• Examples: checkmate in chess, all pieces gone in checkers

• Goal of AI: – Choose next move so that will “likely” lead to victory

Key problem: What exactly does this mean?

Page 3: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Planning Against an Opponent

• Idea: Create sequence of steps that lead from initial board state to winning state

• Problem: Unlike normal planning, AI does not get to choose every step of path– Player gets to choose every other step in plan

– Will choose steps that defeat the AI

X X O X O

X

X O

X O

X O

X O

X

X X O X O

X

X O

X

O

Page 4: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Tree Representation

• Can represent possible moves as tree– Node = board state– Branch = possible moves from that state– Opponent controls every other branching of tree!

• Example: Tic Tac Toe– Simplified to remove duplicate branches for reflection, rotation

Root node = initial board state

X

X

XPossible X moves

X O X O X

O

X

O

X

O

X O X

O

X O X

O

X

O

X

O

X

O

Possible O moves

Page 5: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Choosing Next Move

• Simple idea: Choose next move so guaranteed win no matter which moves opponent makes

– No real game is this simple!

• Example:

I win I lose

I win I lose

I win

A B

C D E F

GH

Page 6: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Choosing Next Move

Reasoning:• If choose move A, then opponent will choose move D

and I lose.• If choose move B, I win, since:

– If opponent chooses move F, I win– If opponent chooses move E, I choose move G and win

• Therefore, choose move B– Guaranteed win regardless of opponent moves

Page 7: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Lookahead Problem

• Main Problem:No nontrivial game has tree which can be completely explored– m possible moves each turn– n turns until game ends– mn nodes in game tree

• Exponential growth

– Example: Chess • ~20 moves per turn

• ~50 turns per game

• ~2050 possible moves– Close to number of particles in universe!

Page 10: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Heuristic Evaluation of Boards

• TicTacToe Example (with AI playing X):

H(board) = 2 × # of possible rows/columns/diagonals where X could win in one move + + 1 × # of possible rows/columns/diagonals where X could win in two moves + - 2 × # of possible rows/columns/diagonals where O could win in one move + - 1 × # of possible rows/columns/diagonals where O could win in two moves

• Example:

X

O X

X O

H = 2 + 1 + 1 - 1 = 3

Page 11: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

MINMAX Algorithm

• Explore possible moves from current board position – Continue until reach maximum lookahead level

– Forms tree of possible moves and countermoves

• Apply heuristic measure to all boards at leafs of tree• Work from leafs to root of tree

– Measure of each node based on measure of its child nodes

• Choose current move that results in highest heuristic value

• AI makes move• Player makes move (may not be one expected, but can’t be worse)

Page 12: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

MINMAX Algorithm

• AI’s turn = MAX level– Choose move that gives highest heuristic

• AI should choose move that guarantees best result!

– Value of board position at that level = maximum of its children– Example: TicTacToe with AI playing X

X

O X

O

X

O X

X O

X X

O X

O

X X

O X

O

X

O X X

O

X

O X

X O

33 34 1

Best move

Bad move, but not relevant

Will choose move with highest heuristic, so value of reaching this board = 4

4

Page 13: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

MINMAX Algorithm

• Player’s turn = MIN level– Assume they choose move which is best for them

• Assume good for player = bad for AI

• Assume move that gives lowest heuristic

– Value of board position at that level = minimum of its children

X

X

O X O

-1-2-2 -1

Worstmove for AI

Bad moves for player, but can’t assume they will do this

Player will choose move with lowest heuristic, so value of reaching this board = -2

-2

X

O X

O X O

X

O X

O X O

O X

X

O X O

O X

X

O X O

Page 14: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Simple MinMax Example

• AI move• 3 levels of lookahead

Heuristic Measures

No time to explore moves past this point

8 -2 3 5 -5 -1 4 12

AI move

AI move

player move

Page 15: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Simple MinMax Example

• AI move• 3 levels of lookahead

Heuristic Measures

No time to explore moves past this point

8 -2 3 5 -5 -1 4 12

AI move

AI move

player move

Max(8, -2) = 8 Max(-5, -1) = -1Max(3, 5) = 5

Max(4, 12) = 12

Page 16: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Simple MinMax Example

• AI move• 3 levels of lookahead

Heuristic Measures

No time to explore moves past this point

8 -2 3 5 -5 -1 4 12

AI move

AI move

player move

Max(8, -2) = 8 Max(-5, -1) = -1Max(3, 5) = 5

Max(4, 12) = 12

Min(8, 5) = 5 Min(-1, 12) = -1

Page 17: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Simple MinMax Example

Heuristic Measures

No time to explore moves past this point

8 -2 3 5 -5 -1 4 12

AI move

player move

Max(8, -2) = 8 Max(-5, -1) = -1Max(3, 5) = 5

Max(4, 12) = 12

Min(8, 5) = 5 Min(-1, 12) = -1

AI move

Max(5, -1) = 5

Best possible outcome = 5

Follow this branch

Page 18: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

MinMax Example

Max(-1, 1, -2) = 1

X

X

X

Min(1, -1, 0, 1, 0) = -1

X O X O X

O

X

O

X

O

X O X

O

X O X

O

X

O

X

O

X

O

Heuristic Measures

No time to explore moves past this point

1     -1    0     1     0       1     2       -1    0     -2    -1    0 

Min(-1, 0, -2, -1, 0) = -2

Min(1, 2) = 1

Choose this move

Assume player will then do this

AI playing X

Maximum lookahead of 2 moves

Initial move choice for AI

Page 19: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Alpha-Beta Pruning

• Goal: Speed up MinMax algorithm

• Idea: Faster evaluation Can explore more levels in game tree Better decision making

– Can speed up process by factor of 10 – 100

Page 20: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Alpha-Beta Pruning

• Idea: Many branches cannot possibly improve score– Once this is known, stop exploring them

BestSoFar = max(-1, 1, ?) = 1

X

X

X

BestSoFar = -1X O

BestSoFar = min (-1, ?, ?, ?, ?)

BestSoFar = 1

-1

1) Minimum will never get higher than -1

2) So this can never be greater than -1, which means it cannot increase BestSoFar

3) Which means that there is no point in exploring any of these branches

Page 21: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Alpha-Beta Pruning

• Define α as value found at previously explored max branch – Can do no worse than α at this point

• Define β as a value found below the current branch– Can do no better than β down this branch

• If α ≥ β, there is no point in exploring this branch any further

α

BestSoFar = max (α, x) where x ≤ β = α if α ≥ β

β

BestSoFar = min (β, …) ≤ β

Page 22: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Alpha-Beta Pruning

Also applies to min branches• If β ≤ α, no point in exploring this branch any further• Opponent will not choose this branch, as guaranteed to

be better for AI (and worse for player) than other branch

β

BestSoFar = min (β, x) = β if β ≤ α

α

BestSoFar = max (α, …) ≥ α

Page 23: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Move Ordering

• Problem: Alpha-Beta pruning only works well if best branches explored first– Only know branch is not worth exploring if have already seen a better one

• Goal: Order branches so moves “most likely” to be good are explored first– This is very difficult!

X X

X

Probably no branches eliminated if explored in this order

Page 24: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Move Ordering

• Idea: Use results of last evaluation to order moves this time

A

player move chosen

CB

ED GF

Tree created in previous move

AI move chosen

Attempt to “reuse” this part of tree 7 2 4 8

2 4

4

Page 25: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Move Ordering

• Each board in subtree already has an evaluation– May no longer be completely accurate

– Should still be somewhat accurate

• Use those to determine order in which next evaluation done

A

CB

ED GF

7 2 4 8

2 4

4 A

C B

EDFG

8 4 7 2

24

4

Page 26: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Games with Random Component

• Example: Backgammon– Roll dice (random event)– Determine which piece or

pieces to move that many steps

• One piece 11 steps• Two pieces 5 and 6 steps• Which piece?

• Possible actions depend on random component

Page 27: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

Games with Random Component

• Can treat random event like extra level of game tree

Dice throw

12111098765432

Possible moves based on dice throw (8 in this case)

Page 28: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

MinMax in Random Games

• Generate tree of possible outcomes up to lookahead limit– Alternate possible moves and possible outcomes of random event– Start with possible AI moves after random event

• Point at which AI has to make decision about what to do

AI dice throw at current board state

Possible AI moves from current board for that dice throw

Possible player dice throws

Possible player moves based on board and their dice throw

Possible AI dice throws

Possible AI moves from this board for that dice throw

Page 29: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

MinMax in Random Games

• Use minmax to compute values at decision levels– AI and player make best possible move based on their dice throw

• Weight each node at random event level based on probability of the random event

1) Value of board for each possible move after dice throw of 8

12111098765432

-5 8 7 18 11 -1

2) Minmax uses this value

3) Probability of dice throw of 8 = 5/36

4) Weight this state = 5/36 * 18 = 2.5

Page 30: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

MinMax in Random Games

12111098765432

Probability: 1/36 1/18 1/12 1/9 5/36 1/6 5/36 1/9 1/12 1/18 1/36

Minmax value from child nodes: -18 -27 -24 0 18 18 18 27 24 36 36

Weightedvalue: -0.5 -1.5 -2 0 2.5 3 2.5 3 2 2 1

Total expected value = 12

• Compute value of board before random event based on expected value of boards after random event

Page 31: Artificial Intelligence in Game Design Board Games and the MinMax Algorithm

MinMax in Random Games

• Random component greatly increases size of tree• Example:

– 11 possibilities for dice throw– n moves lookahead

– Increases nodes to explore by factor of 11n

• Will probably not be able to look ahead more than 2 or 3 moves

• TD-Gammon – Plays at world championship level– Also uses learning