Introducing The Min-Max Algorithm: Paulo Pinto 28 July 2002
Introducing The Min-Max Algorithm: Paulo Pinto 28 July 2002
Paulo Pinto
28 July 2002
3 Optimization 1
6 An example implementation 5
7 Conclusion 5
8 References 6
1 Introduction
There are plenty of applications for AI, but games are the most interesting to the public.
Nowadays every major OS comes with some games.
So it is no surprise that there are some algorithms that were devised with games in mind.
1
MinMax (GamePosition game) {
return MaxMove (game);
}
return best_move;
}
3 Optimization
However only very simple games can have their entire search tree generated in a short time. For most games this isn’t
possible, the universe would probably vanish first. So there are a few optimizations to add to the algorithm.
First a word of caution, optimization comes with a price. When optimizing we are trading the full information
about the game’s events with probabilities and shortcuts. Instead of knowing the full path that leads to victory, the
decisions are made with the path that might lead to victory. If the optimization isn’t well choosen, or it is badly applied,
then we could end with a dumb AI. And it would have been better to use random moves.
One basic optimization is to limit the depth of the search tree. Why does this help? Generating the full tree could
take ages. If a game has a branching factor of 3, which means that each node has tree children, the tree will have the
folling number of nodes per depth:
The sequence shows that at depth n the tree will have 3n nodes. To know the total number of P generated nodes, we
n
need to sum the node count at each level. So the total number of nodes for a tree with depth n is n=0 3n . For many
games, like chess that have a very big branching factor, this means that the tree might not fit into memory. Even if it
2
Depth Node Count
0 1
1 3
2 9
3 27
... ...
n 3n
3
node, and we can safely ignore all the remaining children.
This all means that sometimes the search can be aborted because we find out that the search subtree won’t lead us
to any viable answer.
This optimization is know as alpha-beta cuttoffs and the algorithm is as follows:
– the alpha value which holds the best MAX value found;
– the beta value which holds the best MIN value found.
• At MAX level, before evaluating each child path, compare the returned value with of the previous path with the
beta value. If the value is greater than it abort the search for the current node;
• At MIN level, before evaluating each child path, compare the returned value with of the previous path with the
alpha value. If the value is lesser than it abort the search for the current node.
The Listing 4 shows the full pseudocode for MinMax with alpha-beta cuttoffs. How better does a MinMax with
alpha-beta cuttoffs behave when compared with a normal MinMax? It depends on the order the search is searched. If
the way the game positions are generated doesn’t create situations where the algorithm can take advantage of alpha-
beta cutoffs then the improvements won’t be noticible. However, if the evaluation function and the generation of game
positions leads to alpha-beta cuttoffs then the improvements might be great.
6 An example implementation
An example is always a good way to show how an algorithm might be implemented. Back in 1999, I and a friend of
mine have implemented a checkers game as a Java application for the AI class in the university. I have recently ported
the game to C#.
The MinMax algortihm isn’t a great implementation. In fact I should mention that the best thing about it is that it
works. However I think that it presents a way that the algorithm might be implemented and as an example it is good
enough.
The game uses MinMax with alpha-beta cutoffs for the computer moves. The evaluation function is an weighted
average of the positions occupied by the checker pieces. The figure 3 shows the values for each board position. The
value of each board position is multiplied by the type of the piece that rests on that position, described in Table 6. The
Listing 5 shows the Java implementation of the evaluation function. It has been slightly modified for the article.
4
Figure 1: A search tree
5
Please note that the code uses a vector, 0-31 based, for the board game positions.
The game code is available at:
• C# version - http://www.progtools.org/graphics/projects/sharp_checkers
7 Conclusion
The MinMax might not be the best answer for all kind of computer games that need to have AI that resembles human
behaviour. But given a good implementation it can create a tought adversary. I hope that this article has gave you some
insight on the MinMax algorithm and how you might use it on your games.
8 References
1 - Russell Stuart J., Norvig Peter. "Artificial Intelligence. A modern approach.". Prentice Hall, 1995. ISBN
0-13-103805-2.
2 - Bratko Ivan. "PROLOG. Programming for artificial intelligence" Addison-Wesley, 1990. ISBN 0-201-41606-9
3 - Rich Elaine, Knight Kevin. "Artificial Intelligence". McGraw-Hill Inc., 1991. ISBN 0-07-100894-2
4 - Analytical Reasoning FAQ. Available at http://www.west.net/ stewart/lwfaq.htm
6
MinMax (GamePosition game) {
return MaxMove (game);
}
7
MinMax (GamePosition game) {
return MaxMove (game);
}
8
// Snippet from Computer.java.
// Contains the evaluation function
/**
* Evaluation function.
*/
private int eval (CheckersBoard board) {
int colorKing;
int colorForce = 0;
int enemyForce = 0;
int piece;
try {
// Searchs all board positions for pieces
// and evaluates each position.
for (int i = 0; i < 32; i++) {
piece = board.getPiece (i);
if (piece != CheckersBoard.EMPTY)
if (piece == color || piece == colorKing)
colorForce += calculateValue (piece, i);
else
enemyForce += calculateValue (piece, i);
}
}
catch (BadCoord bad) {
bad.printStackTrace ();
System.exit (-1);
}
/**
* Measures the value of a checkers piece, given
* it’s position in the board
*/
private int calculateValue (int piece, int pos) {
int value;
10