faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/DataStructures/Min… · Web viewwhich...
Transcript of faculty.cse.tamu.edufaculty.cse.tamu.edu/slupoli/notes/DataStructures/Min… · Web viewwhich...
Min-Max TreesReading a General Min-Max Trees
General Min-Max TreeThe Structure
originally from: https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning#/media/File:AB_pruning.svg
The explanation
States
1
o levels of a game treeo alternate states
by default, starts with max, but doesn’t have too think of min and max as 2 players
o last level is NOT a state, called utility (covered later) State options or actions
o does the decision structure and options for that stateo notice not always consistent number of options
number of options might shrink as the different states take turns Utility values
o think of these as values max covets o sometimes these values can be -/+ where
- is good for “min” states + is good for “max” states
Calculating General Min-Max Trees Values not WHY/HOW it works yet!! bottom up, recursively
o since the utility values are at the bottom use the current state to select which value is coveted
o at “min” state, look for the lowest value of the children optionso opposite for “max” state
repeat for each level, but alternate focuso one level is min, the next is max
2
Calculating a Min-Max TreeSetup
Starting from just about the Utility values
3
Continuing up
And up
4
Until we reach the root
5
Solve you own
#1
#2
(more on next page)
6
#3
#4 Questions1. notice that in a few of the examples above, there were negative values, what
could that stand for?2. why are state options not consistent? (why do some nodes have more links
and others)3. What were the differences in the exercises?
7
Simple Minimax function recursive what does it return exactly?
o which decisions set (or branch) of the tree gives MAX the best odds of winning
or look at it as what options should MAX choose that MIN has the least defense
uses min and max functionso which then calls the alternativeo min maxo max min
Psuedo Code for simple Min-Max function
8
Minimax Recursively
= max( min(A), min(B), min(C) )= max( min(26,28,23), min(9,12,10), min(35,22,19) ) // call stack at max= max(23, 9,19) )= 23
9
Min-Max Game Tree Theory solving games and algorithms in a smart way
o optimal decisions Player 1 (max) wants to maximize it’s chances of winning but Player 2 (min) wants to maximize it’s chances of winning too!
o other player has a say!o creating your own “AI” to play games
how to program a computer to play intelligently one winner, one loser (zero sum)
o two players chess, tic-tac-toe, battleship perfect information (from video)
o one or more best moves imperfect information
o playing simultaneouslyo Rock-Paper-Scissors-Spock
large decision treeso probably not a binary treeo each level is each option possibleo tic-tac-toe = 9!
empty board = 9 options then 8 (since we care about both X and O) then so on
o if too big, we limit the depth for time and memory space
10
Perfect Information Min-Max Game Tree
o chess = 1040 this board
1. Google Tic-Tac-Toe, play the version Google gives2. Play a few games on easy, then impossible!!3. In “impossible mode”, start in the middle. Then reset the game. Did the
computer respond the same way each time? Why?4. In Tic-Tac-Toe “easy”, what logic did they use to pick a location?5. In Tic-Tac-Toe “medium”, what logic did they use to pick a location?
11
Revisiting Utility each individual result returns only -1 (Y wins), 0 (draw), +1 (X wins)
o this works well for tic-tac-toe but combined within a grouping is the number you will see bottom values represent MAX’s opportunities to win (obviously the higher the
number the better) some might shift the values to give greater meaning
o -100 – Y will win, take this patho -10 – Y could win if it takes this patho this works for chess
Utility values explained in a Game Tree
Notice that the blue values are the farthest leavesWhat does -1, 0 , +1 indicate?What path would X (MAX) have taken? Then, what would O(MIN) respond?from https://www.ocf.berkeley.edu/~yosenl/extras/alphabeta/alphabeta.html
12
Pruning a Min-Max Tree Since we have limited time available, we want to avoid unnecessary
computation in the minimax tree ways of determining that certain branches will not be useful expands on min-max function but adds
o alphao betao pruning (or skip testing the rest of the branches)
pruning does not affect final results Can store information along an entire path, not just at most recent levels!
o 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)
13
Minimax functions with pruningMax-Value(s, alpha, Beta) if terminal(s) return U(s) v = -infinity // set this node’s base value for c in next-states(s) // check all leaf nodes unless pruning v’ = Min-Value(c, alpha, Beta) // go deeper if possible if v’ > v { v = v’ } // set v to highest value visited by leaves if v’ >= Beta { return v } // prune since another action lower if v’ > alpha { alpha = v’} return v
1. alpha is possibly updated by the MAX of successors evaluated so far2. If the value that would be returned is ever > b, then stop work on
this branch3. If all children are evaluated without pruning, return the MAX of
their values1. How many values are returned?2. How many values are passed in Min-value?
Min-Value(s, alpha, Beta) if terminal(s) return U(s) v = +infinity for c in next-states(s) v’ = Max-Value(c, alpha, Beta) if v’ < v { v = v’ } if v’ <= alpha { return v } if v’ < Beta { Beta = v’} return v
1. Beta is possibly updated by the MIN of successors evaluated so far2. If the value that would be returned is ever < a, then stop work on
this branch3. If all children are evaluated without pruning, return the MIN of
their values
Min-Max Function Trackersmin max
if v’ < v { v = v’ }
if v’<= alpha {return v }
if v’ < Beta { Beta = v’}
if v’ > v { v = v’ }
if v’>= Beta {return v }
if v’ > alpha {alpha=v’}
Example Min-Max Tree Pruned
14
Notice the middle branch had found a minimum value early in it’s set that was ALREADY lower than the 3 in the leftmost branch.Then the branch with 2 would never be chosen since MAX(3, 2) = 3.
Pruning ExercisesScenario #1 Scenario #2
“I want something higher than 3, but I need it from the min of (14,23,2)”1. What does “current” value mean?2. Was there any pruning? Why?3. Was there a “max” value change
anywhere?
1. Was there any pruning? Why?2. Was there a “max” value change
anywhere?
15
Alpha-Beta pruning example
1. Any pruning?2. What was the max?
view the results using alpha and Beta here
16
Try these exercises (I will start #1)#1
I will demonstrate:
http://inst.eecs.berkeley.edu/~cs61b/fa14/ta-materials/apps/ab_tree_practice/
(more next page)
17
#3
check your answer here or here (manually insert values):
18
Alpha-Beta Pruning Expanded “Good ordering”* of moves can make this pruning much more efficient
o Evaluating “best” branch first yields better likelihood of pruning later branches
o Perfect ordering reduces time to bm/2
o i.e. doubles the depth you can search to! A good evaluation function might help you order your available moves
o First order the SUCCESSOR nodes on the UTILITY value This happens before completely expanding that node
o Then, expand DFS on the node with best value
Good Ordering can save time!!Good ordering Bad (not really) ordering
Easy to find min and max!
Stats of a Min-Max Tree depth first exploration (search) terms
o m = depth of the tree how many turns
o b = legal moves how many options
memory usageo b x m
this could be a lot run time
o O(bm) but why? (to build?, then searching would be much easier) for chess the AVERAGE “m” is round 40, where b is usually
between 50 and 100 on average impractical for chess, but basis
19
SolutionsCalculate the Min-Max Tree
https://www.cc.gatech.edu/~riedl/classes/2014/cs3600/homeworks/minimax-soln.pdf
http://arantxa.ii.uam.es/~asuarez/docencia/ai/english/slides/T6_games_exercises.pdf
20
Pruning ExercisesNo Change!But more work since more node values to look at
Big Change!But more work since more node values to look at
21
Alpha-Beta Pruning Exercises
https://youtu.be/zp3VMe0Jpf8?t=294
Alpha-Beta Pruning Practicehttp://inst.eecs.berkeley.edu/~cs61b/fa14/ta-materials/apps/ab_tree_practice/
Demo of Minimax http://homepage.ufp.pt/jtorres/ensino/ia/alfabeta.html
Sources22
https://www.geeksforgeeks.org/minimax-algorithm-in-game-theory-set-1-introduction/ https://www.neverstopbuilding.com/blog/2013/12/13/tic-tac-toe-understanding-the-minimax-algorithm13https://www.cs.cornell.edu/courses/cs312/2002sp/lectures/rec21.htm
pruninghttps://www.youtube.com/watch?v=zp3VMe0Jpf8
Thanks to Jinhoa Chen ’18 for the revised graphic
23