Evaluation function (eval) is the next one third part of the chess engine.
Eval is mathematisation of chess knowledge.
Eval - evaluates a chess position by assigning a numerical value to the position which indicates the probability of winning for a particular side.
The number assigned depends upon the available material and other positional factors.
The real use of eval is to instill chess knowledge in the engine.
It is because of eval, that an engine understands that a queen is more powerful piece than a rook or a knight.
The numerical advantage is expressed in "centipawns" {100 centipawns(c.p.) = 1 pawn}.
The usual mathematics of chess is:
pawn = 100 c.p.
bishop = 300 c.p.
knight = 300 c.p.
rook = 500 c.p.
queen = 900 c.p.
The king needs to be assigned a very large number (theoretically its infinite).
King is not assigned a value because both sides will always have a king.
Value returned by eval function:
Eval returns the advantage (or disadvantage) of one side with respect to other.
Eval does not return the absolute values of black pieces and white pieces but only the advantage i.e. the DIFFERENCE (subtraction) between them.
This difference is the advantage one player has over the other.
Thus for calculating the return value:
The total material is summed up for both the sides.
Black_Material = SUM (centipawn values of all black pieces and pawns)
White_Material = SUM (centipawn values of all white pieces and pawns)
E.g. If Black has 2 pawns and a knight and a king and White has 3 pawns and a rook and a king, then,
Black_Material = 100 + 100 + 300 = 500
White_Material = 100 + 100 + 100 + 500 = 800
Now depending upon the "perspective" eval will return "White_Material - Black_Material" OR "Black_Material - White_Material"
Perspective: The side with respect to which eval returns the positive score.
if perspective = white, then eval returns White Material - Black Material
if perspective = black, then eval returns Black Material - White Material
The perspective determines who is the maximising and who is the minimising player.
if perspective = white then, white is maximiser and black is minimiser
if perspective = black then, black is maximiser and white is minimiser
The concept of this mini-max distinction will be explained later while explaining search.
Right now it is enough to understand that:
Chess will be played between 2 players - black and white.
One player will be maximiser and other will be minimiser.
The job of maximiser is to maximise the value returned by eval as much as possible.
The job of minimiser is to minimise the value returned by eval as much as possible.
Changing perspective
Chess is a "zero sum" game.
The advantage of one player = the disadvantage of other player
Advantage for white = white_material - black_material
white's intention is to maximise it as it is advantage for him
black's intention is to minimise it as it is disadvantage for him
Similarly, advantage for black = black_material - white_material
now, black's intention is to maximise it as it is advantage for him
but white's intention is to minimise it as it is disadvantage for him
Hence, eval is symmetric and thus to change the perspective, we just multiply the value returned by eval with "-1".
E.g. Eval returned a value of +100 with white's perspective (white's advantage = black's disadvantage).
If we want the value with black's perspective,
we just multiply it with "-1".
Hence the value would become -100 (black's advantage = white's disadvantage).