Home

LaMoSca v0.05

Evaluating positions

Objective

In this version the program chooses its move by evaluating the positions that can be reached from the current one.

The metrics

The move is chosen according to how good the resulting position on the board is. This judgement, however, has to be expressed as a number so that the program can compute it. Several metrics are possible, each judging a different aspect of a position, and the evaluation used by programs is often a weighted combination of them.

Material

This is the simplest criterion, the one used by anyone who starts playing chess. The better a move manages to tip the balance of pieces on the board in one's favour, the better the move.
Using the traditional piece values (pawn = 1, knight = bishop = 3, rook = 5, queen = 10), this evaluation can be computed as the difference between the total value of the program's pieces and that of the opponent's pieces.

Position

The value of a piece depends not only on its type but also on where it stands on the board. A pawn, for example, is more important the more it controls the centre and the closer it gets to promotion.

This can be evaluated by adjusting the value of the pieces (usually multiplied by 100 so as to work with integers) by amounts that depend on the position of the piece and are stored in matrices. For pawns, for example, values like those in the first matrix alongside could be used to push them forward.

For bishops and knights, a matrix like the second one alongside could be used to draw them towards the centre.

The negative values in these matrices serve to encourage some classic opening moves.

0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 15, 20, 20, 15, 10, 5,
4, 8, 12, 16, 16, 12, 8, 4,
3, 6, 9, 12, 12, 9, 6, 3,
2, 4, 6, 8, 8, 6, 4, 2,
1, 2, 3, 4, 4, 3, 2, 1,
0, 0, 0, -4, -4, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 5, 5, 5, 5, 0, 0,
0, 0, 5, 10, 10, 5, 0, 0,
0, 0, 5, 10, 10, 5, 0, 0,
0, 0, 5, 5, 5, 5, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, -5, -5, 0, 0, -5, -5, 0

Mobility

The value of a piece also depends on how much it can take part in the game: positions in which the pieces are hampered in their movements count negatively.
An overall measure of mobility is the number of moves generated for the position: the more moves there are, the freer the pieces are to move. Other measures can be added for individual pieces (e.g. rooks on open files, and so on).

Pawns

The strength of pawns depends largely on their relative position and on how well they defend each other by forming complex structures.
Blocked, doubled, isolated or backward pawns lose value, while passed or connected pawns are an advantage.

King safety

Defending the king with minor pieces gives relative safety and leaves the heavier pieces free to attack the opponent's side.
King safety can therefore be evaluated from the king's distance to the nearest pieces, from their value and from the protection they enjoy in turn.

Balancing the factors

Combining different metrics is rather complex, because it can be hard to find the right relative weights while keeping the evaluation both fast and accurate. The difficulty comes from the fact that the evaluations often complement each other and their weight changes as the game goes on, so it is hard to appreciate the benefit of one metric or another.
As a first approximation it may be worth giving up a very refined evaluation and saving processing power for the search.

The code

In this version of the program the position is evaluated only on the basis of the material balance, adjusted according to the position of the pieces only for pawns, to push them forward, and for bishops and knights, to draw them towards the centre.

Because of the positional adjustments, the conventional material values have to be scaled by a factor of 100, so the values of pawn, knight, bishop, rook and queen become:

100, 300, 300, 500, 1000

Although this evaluation is simplistic, for now it is better not to complicate the code and the range of values it can produce. More refined evaluation functions can be studied in the programs listed in the index.

Running the program

Running the program with logging enabled, you can see the evaluation of the positions reached with each of the generated moves. For the opening, for example, the values are:

a2a3=1 a2a4=2 b2b3=2 b2b4=4 c2c3=3 c2c4=6 d2d3=8 d2d4=12 e2e3=8 e2e4=12 f2f3=3 f2f4=6 g2g3=2 g2g4=4 h2h3=1 h2h4=2 b1a3=5 b1c3=10 g1f3=10 g1h3=5

Since no pieces are exchanged, these values come only from positional evaluation. In particular, the highest-valued moves are the advances of the central pawns (d2d4, e2e4), which combine the value of the destination square with the negative weights of the starting squares.

Download

LaMoSca05.zip (7.56 k)

To be completed

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

To do

  • choosing the best move by evaluating positions over several levels of analysis.

References

Paolo Ciancarini:
"I Giocatori Artificiali";
Mursia (in Italian)

Links


Back Index Next