EPD destruction tests

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: EPD destruction tests

Post by bob »

chrisw wrote: Mon Feb 24, 2020 11:02 am
bob wrote: Mon Feb 24, 2020 1:16 am or if the popcnt of the OR of all pieces is < 16 (ignoring king here). So simply test for queens, rooks, bishops, knights <= 9, pawns <= 8, plus total <= 15.
Your logic is bugged here. Two errors. Brings to mind the question: is it a code bug if the programmer doesn't know what he is doing and codes according to his own mal-thinking.
By all means, explain the flaws.

1. one side can't have more than 15 non-kings, period. So popcnt(all pieces but kings) will be <= 15 or else the position is not legal.

2. Total rooks, or bishops, or knights <= 10 (yes, I put 9, but knew it was 10). Total queens <= 9 (had that right).

"second bug" since I assume you were talking about knight/bishop/rook which can be 10 but can't go over...

Code has been in Crafty for many years so you could verify those if you want... just a moment's lapse in thinking...
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: EPD destruction tests

Post by bob »

I coded this in a simple way. Things like wp are just the popcnt(white pawns).

if (wp > 8) {
Print(4095, "illegal position, too many white pawns\n");
error = 1;
}
if (wp + wn > 10) {
Print(4095, "illegal position, too many white knights\n");
error = 1;
}
if (wp + wb > 10) {
Print(4095, "illegal position, too many white bishops\n");
error = 1;
}
if (wp + wr > 10) {
Print(4095, "illegal position, too many white rooks\n");
error = 1;
}
if (wp + wq > 9) {
Print(4095, "illegal position, too many white queens\n");
error = 1;
}
if (wk == 0) {
Print(4095, "illegal position, no white king\n");
error = 1;
}
if (wk > 1) {
Print(4095, "illegal position, multiple white kings\n");
error = 1;
}
if (wp + wn + wb + wr + wq > 15) {
Print(4095, "illegal position, too many white pieces\n");
error = 1;
}

Since that is hardly ever executed I made it simple so that it would be a very small piece of code that can be proven to be bug-free. Repeat for black. I use the "error=1" idea so that I can display ALL the flaws in the position, rather than just exiting after the first. At the bottom, it returns error which is initialized to zero and stays there unless one or more errors are found.

BTW the above will catch all illegal positions. Can't have more than 9 or 10 of each piece type. 8 for pawns. Adding up all pieces and pawns has to be <= 15.

Ah. Cut and pasted too quickly. Have a quick errand to run, will come back and correct the above. Did not notice my checking continued farther down to deal with more than 8 promotions. The trick I recall was to compute extra piece counts, (queens > 1, etc) and that + pawns <= 8.
Terje
Posts: 347
Joined: Tue Nov 19, 2019 4:34 am
Location: https://github.com/TerjeKir/weiss
Full name: Terje Kirstihagen

Re: EPD destruction tests

Post by Terje »

bob wrote: Mon Feb 24, 2020 6:00 pm I coded this in a simple way. Things like wp are just the popcnt(white pawns).

if (wp > 8) {
Print(4095, "illegal position, too many white pawns\n");
error = 1;
}
if (wp + wn > 10) {
Print(4095, "illegal position, too many white knights\n");
error = 1;
}
if (wp + wb > 10) {
Print(4095, "illegal position, too many white bishops\n");
error = 1;
}
if (wp + wr > 10) {
Print(4095, "illegal position, too many white rooks\n");
error = 1;
}
if (wp + wq > 9) {
Print(4095, "illegal position, too many white queens\n");
error = 1;
}
if (wk == 0) {
Print(4095, "illegal position, no white king\n");
error = 1;
}
if (wk > 1) {
Print(4095, "illegal position, multiple white kings\n");
error = 1;
}
if (wp + wn + wb + wr + wq > 15) {
Print(4095, "illegal position, too many white pieces\n");
error = 1;
}

Since that is hardly ever executed I made it simple so that it would be a very small piece of code that can be proven to be bug-free. Repeat for black. I use the "error=1" idea so that I can display ALL the flaws in the position, rather than just exiting after the first. At the bottom, it returns error which is initialized to zero and stays there unless one or more errors are found.

BTW the above will catch all illegal positions. Can't have more than 9 or 10 of each piece type. 8 for pawns. Adding up all pieces and pawns has to be <= 15.
A position with 6wp 4wn 4wb 0wr 0wq 1wk will pass the test, despite having 4 promoted pieces for only 2 missing pawns, no?
chrisw
Posts: 4345
Joined: Tue Apr 03, 2012 4:28 pm

Re: EPD destruction tests

Post by chrisw »

bob wrote: Mon Feb 24, 2020 5:52 pm
chrisw wrote: Mon Feb 24, 2020 11:02 am
bob wrote: Mon Feb 24, 2020 1:16 am or if the popcnt of the OR of all pieces is < 16 (ignoring king here). So simply test for queens, rooks, bishops, knights <= 9, pawns <= 8, plus total <= 15.
Your logic is bugged here. Two errors. Brings to mind the question: is it a code bug if the programmer doesn't know what he is doing and codes according to his own mal-thinking.
By all means, explain the flaws.

1. one side can't have more than 15 non-kings, period. So popcnt(all pieces but kings) will be <= 15 or else the position is not legal.

2. Total rooks, or bishops, or knights <= 10 (yes, I put 9, but knew it was 10). Total queens <= 9 (had that right).

"second bug" since I assume you were talking about knight/bishop/rook which can be 10 but can't go over...

Code has been in Crafty for many years so you could verify those if you want... just a moment's lapse in thinking...
I appear to have preempted you with my prior post.

Actually, you have three programmer bugs. The one you just noticed q=9, the others 10.
Secondly, that the 9 or 10 needs modifying by a promoted pawn adjustment.
Thirdly that testing independently for NBRQ < whatever, will potentially break because (eg) 15 pieces must have at least 2N, 2B, 2R and 1Q
chrisw
Posts: 4345
Joined: Tue Apr 03, 2012 4:28 pm

Re: EPD destruction tests

Post by chrisw »

Terje wrote: Mon Feb 24, 2020 6:07 pm
bob wrote: Mon Feb 24, 2020 6:00 pm I coded this in a simple way. Things like wp are just the popcnt(white pawns).

if (wp > 8) {
Print(4095, "illegal position, too many white pawns\n");
error = 1;
}
if (wp + wn > 10) {
Print(4095, "illegal position, too many white knights\n");
error = 1;
}
if (wp + wb > 10) {
Print(4095, "illegal position, too many white bishops\n");
error = 1;
}
if (wp + wr > 10) {
Print(4095, "illegal position, too many white rooks\n");
error = 1;
}
if (wp + wq > 9) {
Print(4095, "illegal position, too many white queens\n");
error = 1;
}
if (wk == 0) {
Print(4095, "illegal position, no white king\n");
error = 1;
}
if (wk > 1) {
Print(4095, "illegal position, multiple white kings\n");
error = 1;
}
if (wp + wn + wb + wr + wq > 15) {
Print(4095, "illegal position, too many white pieces\n");
error = 1;
}

Since that is hardly ever executed I made it simple so that it would be a very small piece of code that can be proven to be bug-free. Repeat for black. I use the "error=1" idea so that I can display ALL the flaws in the position, rather than just exiting after the first. At the bottom, it returns error which is initialized to zero and stays there unless one or more errors are found.

BTW the above will catch all illegal positions. Can't have more than 9 or 10 of each piece type. 8 for pawns. Adding up all pieces and pawns has to be <= 15.
A position with 6wp 4wn 4wb 0wr 0wq 1wk will pass the test, despite having 4 promoted pieces for only 2 missing pawns, no?
Yup, it’s the independent testing of the piece counts that is the logic fail.

Which brings me to the question, does a programmer logic fail but correctly coded as the logic fail, count as a bug?
Ras
Posts: 2495
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: EPD destruction tests

Post by Ras »

I'm using this:

Code: Select all

if ((wkings != 1) || (bkings != 1)) return(POS_NO_KING);
if ((wpieces > 16) || (bpieces > 16)) return(POS_TOO_MANY_PIECES);
if ((wpawns > 8) || (bpawns > 8)) return(POS_TOO_MANY_PAWNS);

promoted_pieces = 0;
if (wqueens > 1)  promoted_pieces += wqueens - 1;
if (wrooks > 2)   promoted_pieces += wrooks - 2;
if (wbishops > 2) promoted_pieces += wbishops - 2;
if (wknights > 2) promoted_pieces += wknights - 2;
if (promoted_pieces > 8 - wpawns) return(POS_OVERPROM);

promoted_pieces = 0;
if (bqueens > 1)  promoted_pieces += bqueens - 1;
if (brooks > 2)   promoted_pieces += brooks - 2;
if (bbishops > 2) promoted_pieces += bbishops - 2;
if (bknights > 2) promoted_pieces += bknights - 2;
if (promoted_pieces > 8 - bpawns) return(POS_OVERPROM);
Rasmus Althoff
https://www.ct800.net
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: EPD destruction tests

Post by bob »

OK, am back. Here is a better cut and paste from a longer terminal window.

Code: Select all

  wp = PopCnt(Pawns(white));
  wn = PopCnt(Knights(white));
  wb = PopCnt(Bishops(white));
  wr = PopCnt(Rooks(white));
  wq = PopCnt(Queens(white));
  wk = PopCnt(Kings(white));

   if (wp > 8) {
    Print(4095, "ERROR-- illegal position, too many white pawns\n");
    error = 1;
  }
  if (wn > 10) {
    Print(4095, "ERROR-- illegal position, too many white knights\n");
    error = 1;
  }
  if (wb > 10) {
    Print(4095, "ERROR-- illegal position, too many white bishops\n");
    error = 1;
  }
  if (wr > 10) {
    Print(4095, "ERROR-- illegal position, too many white rooks\n");
    error = 1;
  }
  if (wq > 9) {
    Print(4095, "ERROR-- illegal position, too many white queens\n");
    error = 1;
  }
  if (wk == 0) {
    Print(4095, "ERROR-- illegal position, no white king\n");
    error = 1;
  }
  if (wk > 1) {
    Print(4095, "ERROR-- illegal position, multiple white kings\n");
    error = 1;
  }
  if (wp + wn + wb + wr + wq > 15) {
    Print(4095, "ERROR-- illegal position, too many white pieces\n");
    error = 1;
  }
  //  remove original pieces from the counts so that wq and such will only count promoted queens, etc.
  wq = Max(wq-1, 0);
  wr = Max(wr-2, 0);
  wb = Max(wb-2, 0);
  wn = Max(wn-2, 0);
  if (wq + wr + wb + wn > 8 - wp) {
    Print(4095, "ERROR-- white has promoted too many pawns\n");
    error = 1;
  }
  if (Pawns(white) & (rank_mask[RANK1] | rank_mask[RANK8])) {
    Print(4095, "ERROR-- illegal position, white pawns on first/eighth rank(s)\n");
    error = 1;
  }
This was modified a few days ago when the FEN parsing thread started. I ran dozens of intentionally broken FEN strings through it and to the best of my knowledge it works. Could have always overlooked a special case, but I tried all the obvious ones in a large batch test.

There is a hole in the above but I don't see any way to correct it without game history. IE if you have two white queens on the board, they COULD be 1 original + 1 promoted; OR, they could be two promoted queens. Can't determine that without information that is not included in FEN. I ignore the possibility myself since there appears to be no way to fix it.
chrisw
Posts: 4345
Joined: Tue Apr 03, 2012 4:28 pm

Re: EPD destruction tests

Post by chrisw »

Ras wrote: Mon Feb 24, 2020 6:24 pm I'm using this:

Code: Select all

if ((wkings != 1) || (bkings != 1)) return(POS_NO_KING);
if ((wpieces > 16) || (bpieces > 16)) return(POS_TOO_MANY_PIECES);
if ((wpawns > 8) || (bpawns > 8)) return(POS_TOO_MANY_PAWNS);

promoted_pieces = 0;
if (wqueens > 1)  promoted_pieces += wqueens - 1;
if (wrooks > 2)   promoted_pieces += wrooks - 2;
if (wbishops > 2) promoted_pieces += wbishops - 2;
if (wknights > 2) promoted_pieces += wknights - 2;
if (promoted_pieces > 8 - wpawns) return(POS_OVERPROM);

promoted_pieces = 0;
if (bqueens > 1)  promoted_pieces += bqueens - 1;
if (brooks > 2)   promoted_pieces += brooks - 2;
if (bbishops > 2) promoted_pieces += bbishops - 2;
if (bknights > 2) promoted_pieces += bknights - 2;
if (promoted_pieces > 8 - bpawns) return(POS_OVERPROM);
Cool. Looks like solved.

I had one more thought. If a pawn is not opposed (ie there is no enemy pawn in front of it on same file) there has to be at least one piece already removed. One piece for every unopposed pawn.
Unfortunately could be either sides piece for any particular pawn, Also true for any fully open files (I think they require two captures).
For example my EPD in the first post with 16 passed pawns is actually impossible.
Overkill, though, I guess.
Although the thought did arise, if one could eliminate many impossible positions, say in.a random suite of many random artificial created positions, then we could have a stab at the approximate number of actual total possible positions.
Then quite a few if those could be knocked out on basis too much material imbalance. AnywAy, it drops the upper bound.

Then, my imagination is running away, we train a neural net on some sample of actual real game positions, against what we know are outside the rules of chess, and use that net on another set of randomly created positions and ask it, real or artificial?
Final stab at estimating count of actual real total positions.
Dann Corbit
Posts: 12549
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: EPD destruction tests

Post by Dann Corbit »

Per side:
For kings, max piece count per side is 1
For all other pieces, max count per piece is initial count + 8
For total piece count, 7 (initial non-king pieces) + 8 (promotions) = 15 non-king pieces
For each piece above 7, pawns must be no more than 8 - (piece count above 7)
So, if you have 15 pieces you cannot have any pawns.
It isn't possible for both sides to have 15 pieces. I think the formula for max pieces for both sides would be very complicated.
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
chrisw
Posts: 4345
Joined: Tue Apr 03, 2012 4:28 pm

Re: EPD destruction tests

Post by chrisw »

Dann Corbit wrote: Mon Feb 24, 2020 10:08 pm Per side:
For kings, max piece count per side is 1
For all other pieces, max count per piece is initial count + 8
For total piece count, 7 (initial non-king pieces) + 8 (promotions) = 15 non-king pieces
For each piece above 7, pawns must be no more than 8 - (piece count above 7)
So, if you have 15 pieces you cannot have any pawns.
It isn't possible for both sides to have 15 pieces. I think the formula for max pieces for both sides would be very complicated.
For a pawn to promote it has to have a clear pathway to rank 8. Thus is only possible if at least one piece (of either colour unfortunately) has been captured. This is also true of any pawn that has no enemy pawns between it and rank 8.

I would imagine those numbers (promoted pawns and runnable pawns) could get used to modify the test constants for count piece tests.
Likewise open files reduce total possible piecepawn count by 2. Edit: er, whoops they could be open because pawns were promoted, so wrong, or maybe half wrong, there might be a number there