The Minimax Algorithm
The Minimax Algorithm
Glenn Strong
[email protected]
February 10, 2011
Motivation
Here is a simple algorithm that will not work to calculate the winning move in
a game:
From the current position, calculate all the possible moves that could be
made from that position. For a game of noughts and crosses the result
might look like:
Expand each of these new positions so that they include the possible moves
for the other player:
Continue this expansion until a winning position for the required player
is found.
This algorithm will work, in the sense that it will locate a series of winning
moves for the game, but the cost of performing that calculation is enormous.
At the first level of the graph there will be one board. At the next 9, at the
next there will be 9 8 at the level past that there will be 9 8 7. In total:
n=8
X
0
9!
= 986410
(n + 1)!
This is not so big that we cannot calculate it, but it is alarming since noughts
and crosses is such a simple game.
Minimax
For a game which has a more complex tree than noughts and crosses (technically,
where the branching factor is higher) this simplistic approach cannot work. The
tree we are building will simply be too large. We need some mechanism to cut
down the number of subtrees that are built ideally we will be able to eliminate
moves which do not look promising and concentrate on those which seem likely
to lead to a positive outcome (a win).
We introduce the idea of a function (which we will call the cost function
which assigns a number to a game position representing how likely that position
is to lead to a win. This score can be calculated for each playable position.
If we knew that the scoring function was perfect then we could simply proceed to a solution by taking the move with the highest score and making that
move. We might then expect the opponent to play the move which has their
highest score (which will also be the move with the lowest score for us), and
so on. However, the scoring function is unlikely to be perfect. It is therefore
advisable to explore the possible moves further for example, during a piece
exchange in chess a superficially unattractive move may turn out to be advantageous if pursued further. The depth of the search is known as the ply of the
search (so a 4-ply search examines the tree to a depth of 4). In order to pursue
the tree further we will have to make guesses as to how the opponent will play.
The cost function can be used again to evaluate how the opponent is likely to
play. After evaluating some number of moves ahead we examine the total value
of the cost to each player. The goal is to find a move which will maximise the
value of our move and will minimise the value of the opponents moves. The
algorithm used is the minimax search procedure.
Smarter Minimax
function MINIMAX(N) is
begin
if N is deep enough then
return the estimated score of this leaf
else
Let N1, N2, .., Nm be the successors of N;
if N is a Min node then
return min{MINIMAX(N1), .., MINIMAX(Nm)}
else
return max{MINIMAX(N1), .., MINIMAX(Nm)}
end MINIMAX;
Figure 1: Minimax algorithm. Two player, perfect information zero-sum game
The key to this diagram is that the unevaluated locations will not affect
the score of the top node under minimax. Consider the node marked ? at
the bottom left of the tree (labelled A). It does not matter what this node
evaluates to because the min player will have already taken the decision to play
towards the node valued 10, and will have decided this on the basis that playing
the other node would reveal a more valuable move for the max player. This is
called a beta-cutoff.
Now consider the nodes labelled ? on the right-hand subtree. It does not
matter what they evaluate to since the max player will always want to play
the move scored 10 in order to keep the min player from having the option of
playing the 7 move. This is termed an alpha cutoff. Making this decision cuts
off a complete subtree from consideration on the right-hand side of the tree.
By applying and cutoffs we have managed to avoid some needless eval-
Improvements