Stockfish - material balance/imbalance evaluation

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Ralph Stoesser
Posts: 408
Joined: Sat Mar 06, 2010 9:28 am

Stockfish - material balance/imbalance evaluation

Post by Ralph Stoesser »

Could somebody explain the below code pls.?

I've read Kaufman's paper about the evaluation of material imbalance,
but I wonder what exactly Tord Romstad's polynomial function does.

Where do the coefficients come from?
Is this a formula which translates Kaufman's findings or is it something independent?


material.cpp, starting line 324, SF 1.7.1

Code: Select all

    // Redundancy of major pieces, formula based on Kaufman's paper
    // "The Evaluation of Material Imbalances in Chess"
    // http://mywebpages.comcast.net/danheisman/Articles /evaluation_of_material_imbalance.htm
    if (pieceCount[c][ROOK] >= 1)
        matValue -= sign * ((pieceCount[c][ROOK] - 1) * RedundantRookPenalty + pieceCount[c][QUEEN] * RedundantQueenPenalty);

    them = opposite_color(c);
    v = 0;

    // Second-degree polynomial material imbalance by Tord Romstad
    //
    // We use NO_PIECE_TYPE as a place holder for the bishop pair "extended piece",
    // this allow us to be more flexible in defining bishop pair bonuses.
    for &#40;pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; pt1++)
    &#123;
        pc = pieceCount&#91;c&#93;&#91;pt1&#93;;
        if (!pc&#41;
            continue;

        vv = LinearCoefficients&#91;pt1&#93;;

        for &#40;pt2 = NO_PIECE_TYPE; pt2 <= pt1; pt2++)
            vv +=  pieceCount&#91;c&#93;&#91;pt2&#93; * QuadraticCoefficientsSameColor&#91;pt1&#93;&#91;pt2&#93;
                 + pieceCount&#91;them&#93;&#91;pt2&#93; * QuadraticCoefficientsOppositeColor&#91;pt1&#93;&#91;pt2&#93;;

        v += pc * vv;
    &#125;
    matValue += sign * v;
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Stockfish - material balance/imbalance evaluation

Post by mcostalba »

Only "Redundancy of major pieces" is from Kaufman's paper, "Second-degree polynomial material imbalance" is fully from Tord....well idea is from Tord, implementation and coefficents tuning from me and Joona (and it took more then one month!)
Ralph Stoesser
Posts: 408
Joined: Sat Mar 06, 2010 9:28 am

Re: Stockfish - material balance/imbalance evaluation

Post by Ralph Stoesser »

Hi Marco,

Wow, one month of work! Kudos!
I never thought that "piece counting" could be that hard. :)

How did you find the coefficients?
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Stockfish - material balance/imbalance evaluation

Post by mcostalba »

Ralph Stoesser wrote:Hi Marco,

Wow, one month of work! Kudos!
I never thought that "piece counting" could be that hard. :)

How did you find the coefficients?
Well implemantation of course took just few minutes, the problem is the tuning of the corfficents that took a lot.

We used automatic tuning, but we needed different attempts before to find a way to actually get out something good from background noise. This material balance gives some gain, but is not a big gain (at least we didn't find a big gain) so changing the coefficents resulted in _almost_ no change, that's the reason that automatic tuning took a very long time and many attempts before to converge to something measurable.
Jan Brouwer
Posts: 201
Joined: Thu Mar 22, 2007 7:12 pm
Location: Netherlands

Re: Stockfish - material balance/imbalance evaluation

Post by Jan Brouwer »

It seems to add for each of my pieces a value which is a linear function of the number of my and your pieces of each piece type not more valuable than the piece under consideration.
It is probably a "2nd order" correction to a base piece value.
I don't quite understand the need for LinearCoefficients[].
User avatar
Eelco de Groot
Posts: 4565
Joined: Sun Mar 12, 2006 2:40 am
Full name:   

Re: Stockfish - material balance/imbalance evaluation

Post by Eelco de Groot »

Jan Brouwer wrote:It seems to add for each of my pieces a value which is a linear function of the number of my and your pieces of each piece type not more valuable than the piece under consideration.
It is probably a "2nd order" correction to a base piece value.
I don't quite understand the need for LinearCoefficients[].
Just like two bishops of opposite color can be more than the sum of the two pieces alone, that idea is generalized to all the possible piece pairs, including the relations with pieces of the other side. The relation is put into the form of a + bx + cxy, a second order approximation of the real relation but the a falls out of the relation because the sum of the equations for White and Black goes through the origin, otherwise this introduces color asymmetry. vv is if I rememer correctly then the b coefficient for the piece type x, vv is multiplied with the piececount of the corresponding piecetype, x in our description, and after adding the second order relations this is added to the term v.

Eelco
Debugging is twice as hard as writing the code in the first
place. Therefore, if you write the code as cleverly as possible, you
are, by definition, not smart enough to debug it.
-- Brian W. Kernighan
zamar
Posts: 613
Joined: Sun Jan 18, 2009 7:03 am

Re: Stockfish - material balance/imbalance evaluation

Post by zamar »

In short, the idea is that pieces affect the value of each other. Here we give bonus and penalties for different duos (like bishop and knight, pawn and pawn, queen and rook, etc.). "Bishop pair" is a special piece in this implementation.

But as Marco already mentioned, this is not a big deal, maybe around +15 elo or sth like that. We expected more, but fine tuning was extremely difficult.
Joona Kiiski
Ralph Stoesser
Posts: 408
Joined: Sat Mar 06, 2010 9:28 am

Re: Stockfish - material balance/imbalance evaluation

Post by Ralph Stoesser »

Thanks Eelco. Now it's clearer what this code does.
But I still wonder a little why there is no distinction between white colored bishop and black colored bishop, because our white colored bishop would relate in another way to their white colored bishop than to their black colored bishop.

Besides I wonder whether it is a good idea to relate our piece type to their piece type. I must think a while about it, hopefully not a full month.;)

Btw: Thanks also @Joona.
Ralph Stoesser
Posts: 408
Joined: Sat Mar 06, 2010 9:28 am

Re: Stockfish - material balance/imbalance evaluation

Post by Ralph Stoesser »

zamar wrote: "Bishop pair" is a special piece in this implementation.
Yeah, this is well documented, but not the idea behind the imbalance calculation itself.
Also I cannot see an advantage in using a special bishop pair type, because, at least to me, it seems very hard to play around with this code anyway.
User avatar
Eelco de Groot
Posts: 4565
Joined: Sun Mar 12, 2006 2:40 am
Full name:   

Re: Stockfish - material balance/imbalance evaluation

Post by Eelco de Groot »

Ralph Stoesser wrote:Thanks Eelco. Now it's clearer what this code does.
But I still wonder a little why there is no distinction between white colored bishop and black colored bishop, because our white colored bishop would relate in another way to their white colored bishop than to their black colored bishop.

Besides I wonder whether it is a good idea to relate our piece type to their piece type. I must think a while about it, hopefully not a full month.;)

Btw: Thanks also @Joona.
Hello Ralph,

Yes, I think you are right that that is something of a weakness. I had not noticed this myself yet, good that you say it! Also from the top of my head elsewhere in the code when we have two bishops it is assumed these are opposite coloured bishops. It is commented that it would be better to do an explicit check because one or more of these bishops might be underpromoted, so they could be of the same colour.

The our piece to their type piece relationships are I think fairly necessary because this is all about imbalanced positions, well otherwise the whole calculation would always result in zero of course. So for every combination of our pieces, there are still countless ways of how the opponents pieces are assembled. Each of these combinations can be thought of as a subgame in the game of chess, and every transition from one combination to another -especially if an imbalance is created- breaks some "symmetries" to use an analogy from cosmology, it changes the rules of the game slightly, in an n-dimensional landscape I imagine it is a bit like a crossing, like climbing or descending a mountain range to a different valley, a little higher or a little lower in average altitude, where there live different people, in a matter of speaking, using different mores, a different language or playing different games.

Every transition can improve or worsen your position, and to each new landscape you have to adapt the program. It could maybe be said that mastery of these transitions distinguishes the true masters of the game, it forces the flow of the game from opening to endgame.

Regards, Eelco
Debugging is twice as hard as writing the code in the first
place. Therefore, if you write the code as cleverly as possible, you
are, by definition, not smart enough to debug it.
-- Brian W. Kernighan