Home

LaMoSca v0.06

MiniMax search

Objective

This version of the program implements the MiniMax search algorithm; more efficient algorithms are introduced in later versions.

The search

How good a move is cannot be judged only from the position it leads to immediately: we need to foresee the final position of a series of moves made up of the opponent's best replies and the program's possible counter-moves.
The set of variations forms a tree, and choosing the best move means finding the optimal path through it.

The MiniMax algorithm

MiniMax

MiniMax is an algorithm for choosing the best move among all those that can be generated. Ignoring the opponent's replies, the move to choose in a given position would be the one leading to the position with the highest evaluation (based on the value of the pieces, their position on the board, and so on).

Taking the opponent's reply into account as well, the opponent will in turn try to maximise their own position, and therefore to minimise the value of ours. In other words, the opponent's best reply is the one that leaves us in the least favourable of the possible positions.

This reasoning can be extended to any number of levels of analysis, evaluating in turn the program's moves and all the opponent's possible replies. All the variations together form a tree of moves and replies. The leaves of the tree are the final positions of the variations, and they are the only ones that are evaluated: all the intermediate positions are ignored.

Starting from the leaf positions and passing their evaluations back towards the root (the current position):

  • the levels corresponding to the opponent's replies pass the maximum values back towards the root, since the program will choose the best position reachable from that level (Max level);
  • the levels corresponding to the program's moves pass back the minimum values, since the opponent will try to force us into the worst position (Min level).

The algorithm takes its name (MiniMax) from this alternation between maximum and minimum values. Here is an example of pseudocode for the minimising and maximising functions:

Min(Level) {
 if (Level == LEAF) return Evaluate();
 GenerateMoves();
 Minimum = INFINITY;
 for (all moves) {
 MakeMove();
 Value = Max(Level+1);
 UnmakeMove();
 if (Value < Minimum) Minimum = Value;
 }
 return Minimum;
}
Max(Level) {
 if (Level == LEAF) return Evaluate();
 GenerateMoves();
 Maximum = - INFINITY;
 for (all moves) {
 MakeMove();
 Value = Min(Level+1);
 UnmakeMove();
 if (Value > Maximum) Maximum = Value;
 }
 return Maximum;
}

Running the program

In this version of the program the maximum number of levels to analyse is set by the constant "MAXLIVELLI", while the depth actually used can be changed with the WinBoard command "sd".

In the opening, with two levels of analysis, the program's moves are generated first and then the opponent's replies are evaluated. For the program's first move, "a2a3", the minimising function is used and the values are (the program's log is in Italian: "livello" means level, "trovata" found, "valore" value, "mossa" move):

min livello 2: m2:b8a6=-4 m2:b8c6=-9 m2:g8f6=-9 m2:g8h6=-4 m2:a7a6=0 m2:a7a5=-1 m2:b7b6=-1 m2:b7b5=-3 m2:c7c6=-2 m2:c7c5=-5 m2:d7d6=-7 m2:d7d5=-11 m2:e7e6=-7 m2:e7e5=-11 m2:f7f6=-2 m2:f7f5=-5 m2:g7g6=-1 m2:g7g5=-3 m2:h7h6=0 m2:h7h5=-1
min livello 2: Trovata=1, Valore=-11

Going on with all the other moves, the level 1 maximising function has the following values:

M1:a2a3=-11 M1:a2a4=-10 M1:b2b3=-10 M1:b2b4=-8 M1:c2c3=-9 M1:c2c4=-6 M1:d2d3=-4 M1:d2d4=0 M1:e2e3=-4 M1:e2e4=0 M1:f2f3=-9 M1:f2f4=-6 M1:g2g3=-10 M1:g2g4=-8 M1:h2h3=-11 M1:h2h4=-10 M1:b1a3=-7 M1:b1c3=-2 M1:g1f3=-2 M1:g1h3=-7

So the choice is

Max livello 1: Trovata=1, Valore=0, Mossa=d2d4

With 4 levels, however, we see that d2d4 is not chosen. In fact we get

M1:d2d4=-6; m2:b8c6=-6; M3:b1c3=-101; m4:c6d4=-101

In practice, with 4 levels the search cannot go beyond the knight's capture of the pawn with c6d4. As a result the more cautious move is chosen

M1:b1c3=-2

A poor choice of moves caused by insufficient search depth (typically because of sequences of captures) is called the "horizon effect".

Download

LaMoSca06.zip (8.33 k)

To be completed

  • generation of castling, promotions and en passant captures;
  • detection of drawn positions;

To do

  • removing the horizon effect;
  • simplifying the code;
  • efficiency.

Links


Back Index Next