Newbie observation in Stockfish's code (Phase affects PAWN?)

Discussion of chess software programming and technical issues.

Moderator: Ras

Isaac
Posts: 265
Joined: Sat Feb 22, 2014 8:37 pm

Newbie observation in Stockfish's code (Phase affects PAWN?)

Post by Isaac »

I have noticed something "strange" to my eyes in Stockfish.
When I modify the line 264 of material.cpp, I would think that this would only affect the game phase. However it seems that it also affects "PAWN". Here's why:


The modification I did in the code was to change this:

Code: Select all

Phase game_phase(const Position& pos) {

  Value npm = pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK);

  return  npm >= MidgameLimit ? PHASE_MIDGAME
        : npm <= EndgameLimit ? PHASE_ENDGAME
        : Phase(((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit));
}
to

Code: Select all

Phase game_phase(const Position& pos) {

  Value npm = pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK);

  return  npm >= MidgameLimit ? PHASE_MIDGAME
        : npm <= EndgameLimit ? PHASE_ENDGAME
        : Phase(  155.8*erf (  (1.0/(MidgameLimit-EndgameLimit))*((double)npm-MidgameLimit) )  +128  );
}
So that when npm is between EndgameLimit (which is worth 3998) and MidgameLimit (which is worth 15581), then Phase (x) differs for the 2 versions. x still goes from 0 to 128 in both cases but it differs a bit. Here's a picture if you consider this information to be important (I doubt it, but still): Image.

Now, when npm is below EndgameLimit, the game phase is worth 100% Endgame. This is what happens for example in the follow FEN: 4KR2/2PP1P1P/8/8/3p4/3p4/2kp4/2q5.
The command "eval" returns

Code: Select all

           Eval term |    White    |    Black    |     Total     
                     |   MG    EG  |   MG    EG  |   MG     EG   
---------------------+-------------+-------------+---------------
Material, PST, Tempo |   ---   --- |   ---   --- |  -5.92  -5.13 
  Material imbalance |   ---   --- |   ---   --- |  -0.17  -0.17 
               Pawns |   ---   --- |   ---   --- |  +2.01  +2.52 
             Knights |  0.00  0.00 |  0.00  0.00 |  +0.00  +0.00 
             Bishops |  0.00  0.00 |  0.00  0.00 |  +0.00  +0.00 
               Rooks |  0.00  0.00 |  0.00  0.00 |  +0.00  +0.00 
              Queens |  0.00  0.00 |  0.02  0.10 |  -0.02  -0.10 
            Mobility | -0.03  0.00 |  0.09  0.23 |  -0.12  -0.23 
         King safety |  0.00 -0.08 |  0.00 -0.08 |  +0.00  +0.00 
             Threats |  0.00  0.00 |  0.00  0.00 |  +0.00  +0.00 
        Passed pawns | 11.42 21.75 |  3.31  8.49 |  +8.11 +13.26 
               Space |  0.00  0.00 |  0.00  0.00 |  +0.00  +0.00 
---------------------+-------------+-------------+---------------
               Total |   ---   --- |   ---   --- |  +3.59  +9.55 

Scaling:   0.00% MG, 100.00% * 100.00% EG.
Total evaluation: -9.55
, as you can see, 100% end game. That's for the original Stockfish. Now, with my modification, "eval" returns

Code: Select all

           Eval term |    White    |    Black    |     Total     
                     |   MG    EG  |   MG    EG  |   MG     EG   
---------------------+-------------+-------------+---------------
Material, PST, Tempo |   ---   --- |   ---   --- |  -5.92  -5.13 
  Material imbalance |   ---   --- |   ---   --- |  -0.17  -0.17 
               Pawns |   ---   --- |   ---   --- |  +1.80  +2.41 
             Knights |  0.00  0.00 |  0.00  0.00 |  +0.00  +0.00 
             Bishops |  0.00  0.00 |  0.00  0.00 |  +0.00  +0.00 
               Rooks |  0.00  0.00 |  0.00  0.00 |  +0.00  +0.00 
              Queens |  0.00  0.00 |  0.02  0.10 |  -0.02  -0.10 
            Mobility | -0.03  0.00 |  0.09  0.23 |  -0.12  -0.23 
         King safety |  0.00 -0.08 |  0.00 -0.08 |  +0.00  +0.00 
             Threats |  0.00  0.00 |  0.00  0.00 |  +0.00  +0.00 
        Passed pawns | 11.42 21.75 |  3.31  8.49 |  +8.11 +13.26 
               Space |  0.00  0.00 |  0.00  0.00 |  +0.00  +0.00 
---------------------+-------------+-------------+---------------
               Total |   ---   --- |   ---   --- |  +3.40  +9.47 

Scaling:   0.00% MG, 100.00% * 100.00% EG.
Total evaluation: -9.47
, still 100% end game. So it is safe to assume that npm is below 3998 and so my modification does not modify the game phase.

However look at the table returned by the "eval" command. We can see that everything is equal, except for Pawns | --- --- | --- --- | values. I don't understand why they would differ.
What's going on here?
So I decided to check exactly what is returned in the table for that row, I found this in evaluate.cpp, line 993:

Code: Select all

format_row(ss, "Pawns", PAWN);
but I don't understand/know where to look at to see what "PAWN" is.
If I understand well, the value of PAWN is different for the normal Stockfish and the modified one.
But I see no reason why they would differ for the change I have made in the code.

I would appreciate if someone could shed some light.
Thank you.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Newbie observation in Stockfish's code (Phase affects PA

Post by Sven »

It looks like a bug in evaluate.cpp, function format_row().

Code: Select all

    case PST: case IMBALANCE: case PAWN: case TOTAL:
        ss << std::setw(20) << name << " |   ---   --- |   ---   --- | "
           << std::setw(6)  << to_cp(mg_value(wScore)) << " "
           << std::setw(6)  << to_cp(eg_value(wScore)) << " \n";
is obviously meant as

Code: Select all

    case PST: case IMBALANCE: case PAWN: case TOTAL:
        ss << std::setw(20) << name << " |   ---   --- |   ---   --- | "
           << std::setw(6)  << to_cp(mg_value(wScore - bScore)) << " "
           << std::setw(6)  << to_cp(eg_value(wScore - bScore)) << " \n";
The bug causes the whole "Pawns" output row to be wrong even before your change.

EDIT: "PAWN" is an enumeration constant defined in types.h, its value should always be 1.

Sven
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Newbie observation in Stockfish's code (Phase affects PA

Post by mcostalba »

Thanks. Fix applied and pushed.