Stockfish's passed pawns assert

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Necromancer
Posts: 33
Joined: Wed Nov 23, 2016 1:30 am
Location: Brazil

Stockfish's passed pawns assert

Post by Necromancer »

Quick question...while studying sf's passed pawns code, we see this at evaluate.cpp line 625:

Code: Select all

assert(!(pos.pieces(PAWN) & forward_bb(Us, s)));
I don't understand this asssertion. Suppose white has passed pawns on
e5 and e6, then this assert would fail. What I'm missing? Thank you.
The truth comes from inside.
https://github.com/fernandotenorio/Tunguska
User avatar
Eelco de Groot
Posts: 4565
Joined: Sun Mar 12, 2006 2:40 am
Full name:   

Re: Stockfish's passed pawns assert

Post by Eelco de Groot »

Necromancer wrote:Quick question...while studying sf's passed pawns code, we see this at evaluate.cpp line 625:

Code: Select all

assert(!(pos.pieces(PAWN) & forward_bb(Us, s)));
I don't understand this asssertion. Suppose white has passed pawns on
e5 and e6, then this assert would fail. What I'm missing? Thank you.
Only the pawn on e6 is counted as a passed pawn, because the other one can't make progress on its own. (You could still count it as passed but it would get very little points if it does not have a cleared square in front of it and if blocked, the other passed pawn apparently also can't make progress so together they would get probably too much if both are counted as normal passed pawns).
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
Necromancer
Posts: 33
Joined: Wed Nov 23, 2016 1:30 am
Location: Brazil

Re: Stockfish's passed pawns assert

Post by Necromancer »

Eelco de Groot wrote:
Necromancer wrote:Quick question...while studying sf's passed pawns code, we see this at evaluate.cpp line 625:

Code: Select all

assert(!(pos.pieces(PAWN) & forward_bb(Us, s)));
I don't understand this asssertion. Suppose white has passed pawns on
e5 and e6, then this assert would fail. What I'm missing? Thank you.
Only the pawn on e6 is counted as a passed pawn, because the other one can't make progress on its own. (You could still count it as passed but it would get very little points if it does not have a cleared square in front of it and if blocked, the other passed pawn apparently also can't make progress so together they would get probably too much if both are counted as normal passed pawns).
I thought about that, but what about the assert call? From what I saw both pawns would be set on the passed pawn bitboard.
The truth comes from inside.
https://github.com/fernandotenorio/Tunguska
User avatar
Eelco de Groot
Posts: 4565
Joined: Sun Mar 12, 2006 2:40 am
Full name:   

Re: Stockfish's passed pawns assert

Post by Eelco de Groot »

No, I don't think so, see line 153-156 in pawns.cpp

Code: Select all

        // Passed pawns will be properly scored in evaluation because we need
        // full attack info to evaluate them.
        if (!stoppers && !(ourPawns & forward_bb(Us, s)))
            e->passedPawns[Us] |= s;
Why we exactly put in this assert I don't know...
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
Necromancer
Posts: 33
Joined: Wed Nov 23, 2016 1:30 am
Location: Brazil

Re: Stockfish's passed pawns assert

Post by Necromancer »

Ah I missed that, now the assert makes sense. Thanks!
The truth comes from inside.
https://github.com/fernandotenorio/Tunguska
Lyudmil Tsvetkov
Posts: 6052
Joined: Tue Jun 12, 2012 12:41 pm

Re: Stockfish's passed pawns assert

Post by Lyudmil Tsvetkov »

advanced programmers' code seems to be an enigma to mere mortals.

with asserts, it is more or less clear: you waste couple of microseconds on an assert check to only be sure the definition is correct.

what I can not grasp entirely, however, is why they first construct some classes, and then immediately after that destroy them:

class class1; ~class1();

maybe it has to do with memory allocation and releasing memory at the appropriate time, but, no matter how hard I try, the concept is utterly beyond me.

maybe one day I will understand... :)
Lyudmil Tsvetkov
Posts: 6052
Joined: Tue Jun 12, 2012 12:41 pm

Re: Stockfish's passed pawns assert

Post by Lyudmil Tsvetkov »

btw., just think how redundant even the very simple concept of a passer is:

- redundant with doubled pawns
- redundant with isolated pawns
- redundant with any pawn psqt
- redundant with connected pawns
- redundant with storming pawns (pawn danger) that are passers, etc., etc.

maybe, with different SF definitions for doubled pawns, isolated pawns, etc., considering also the pawn behind the passer also as a passer, or half-passer, would fare better, who knows?

so, what is obviously true, is that eval terms and their values would depend on the tuning environment. on the other hand, there is little doubt that, purely chesswise, just about any eval term has an immutable perfect nature with an immutable perfect score assigned to it; for example, rook on an open file is precisely 31cps in the mg/22cps in the eg, passer on d6/e6 is precisely 70cps mg/80cps eg, etc.

so how to reconcile those 2 contradictory notions: that an eval term and its value are a function of the tuning environment, and that each and every eval term has a strictly predefined immutable value attached to it independetnly of the context it is used within?

as far as I see it, only way is to reduce redundancy of terms to 0 by building definitions with at least 20-30 conditions, like if pawn & !passer & !stroming pawn & !connected pawn & ! isolated pawn & !doubled pawn, etc., etc. then score pawn_psqt.

how many programmers would be out there to apply a similar definition that would slow the code at superbullet to an extent that every similar patch would fail?
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Stockfish's passed pawns assert

Post by Sven »

Lyudmil Tsvetkov wrote:advanced programmers' code seems to be an enigma to mere mortals.

with asserts, it is more or less clear: you waste couple of microseconds on an assert check to only be sure the definition is correct.
"couple of" = zero, since assertions are not used in the release version (removed at compile time), and the performance of the debug version is irrelevant.
Lyudmil Tsvetkov wrote:what I can not grasp entirely, however, is why they first construct some classes, and then immediately after that destroy them:

class class1; ~class1();
You can't grasp that since you will never see that in correct C++ code :roll:
Lyudmil Tsvetkov wrote:maybe it has to do with memory allocation and releasing memory at the appropriate time, but, no matter how hard I try, the concept is utterly beyond me.

maybe one day I will understand... :)

Code: Select all

assert(No_you_won_t);
Lyudmil Tsvetkov
Posts: 6052
Joined: Tue Jun 12, 2012 12:41 pm

Re: Stockfish's passed pawns assert

Post by Lyudmil Tsvetkov »

Sven Schüle wrote:"couple of" = zero, since assertions are not used in the release version (removed at compile time), and the performance of the debug version is irrelevant.
thanks for the explanation, Sven!
I enjoy having access to quick guidance in the matter, instead of browsing through innumerable sites...
Sven Schüle wrote: You can't grasp that since you will never see that in correct C++ code :roll:
that is what I rememebr from SF code. maybe I missed to mention that it was executed as a virtual function within the class, or something else of the kind, which I do not quite understand one way or another. not as easy as malloc, realloc and free.

maybe you will explain further, quick guidance is always appreciated. :)
Sven Schüle wrote:

Code: Select all

assert(No_you_won_t);
man, I tried it, and assertion failed. :)

PS. laugh out to your heart's content; we will see how quickly you will learn Bulgarian, when never learned anything similar and having just a couple of months for the entire exercise...