How to implement KPK ?
Moderators: hgm, Rebel, chrisw
-
- Posts: 27808
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: How to implement KPK ?
Why not just use the bitbase? It is only 64KB.
-
- Posts: 588
- Joined: Thu Mar 09, 2006 4:47 pm
- Location: Singapore
Re: How to implement KPK ?
Because there is no free lunchhgm wrote:Why not just use the bitbase? It is only 64KB.
Though magic bitboard move generation should be the best, there is no codes that is in the public domain. I am still using my rotated birboard.
Same for bitbase.
Rasjid.
-
- Posts: 27808
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: How to implement KPK ?
You could write your own code to generate it, and put it in the public domain.
That seems simpler, and much less bug-prone then the code you are trying to write now.
Let me make a first attempt. (Completely untested...)
That seems simpler, and much less bug-prone then the code you are trying to write now.
Let me make a first attempt. (Completely untested...)
Code: Select all
/* Public-domain KPK bitbase code by H.G. Muller */
#define WTM 0
#define BTM 1
char dtc[2][64][64][64]; // 512 KB
char bitbase[2*62*64*6];
char steps[] = {1, -1, 16, -16, 15, -15, 17, -17};
void
Init ()
{
int wk, bk, p;
for(wk=0; wk<64; wk++) for(bk=0; bk<64; bk++) for(p=0; p<64; p++) {
if(bk == wk) dtc[BTM][wk][p][bk] = 1, dtc[WTM][wk][p][bk] = -1; else
if(wk == p) dtc[BTM][wk][p][bk] = dtc[WTM][wk][p][bk] = -1; else
if(bk == p) dtc[BTM][wk][p][bk] = 1; else // with WTM black just captured P
if(p >= 56) dtc[WTM][wk][p][bk] = 1; // promoted and on move = win
}
}
void
BlackPass ()
{
int wk, bk, p, d;
for(wk=0; wk<64; wk++) for(bk=0; bk<64; bk++) for(p=0; p<64; p++) {
int O88 = (bk & 070) + bk; // 0x88 square number
if(dtc[BTM][wk][p][bk]) continue; // already decided
dtc[BTM][wk][p][bk] = 1; // assume lost, if no escape will be found
for(d=0; d<8; d++) {
int to = O88 + steps[d];
if(to & 0x88) continue; // off board
if(dtc[WTM][wk][p][to] <= 0) { dtc[BTM][wk][p][bk] = 0; break; } // escape
}
}
}
void
WhitePass ()
{
int wk, bk, p, d;
for(wk=0; wk<64; wk++) for(bk=0; bk<64; bk++) for(p=0; p<64; p++) {
int O88 = (wk & 070) + wk; // 0x88 square number
if(dtc[WTM][wk][p][bk]) continue; // already decided
if(bk != p) { // pawn not captured, so can move
if(p+8 != bk && dtc[BTM][wk][p+8][bk] > 0) { dtc[WTM][wk][p][bk] = 1; continue; }
if(p+7 == bk && (p & 7) != 0) { dtc[WTM][wk][p][bk] = 1; continue; }
if(p+9 == bk && (p & 7) != 7) { dtc[WTM][wk][p][bk] = 1; continue; }
}
for(d=0; d<8; d++) {
int to = O88 + steps[d];
if(to & 0x88) continue; // off board
if(dtc[BTM][to][p][bk] > 0) { dtc[WTM][wk][p][bk] = 1; break; }
}
}
}
void
Pack ()
{
int wk, bk, p, i;
for(wk=0; wk<64; wk++) for(bk=0; bk<64; bk++) for(p=8; p<56; p++) {
int index = (bk >> 3) + 8*wk + 8*64*(p-8);
bitbase[2*index] |= (dtc[WTM][wk][p][bk] > 0) << (bk & 7);
bitbase[2*index+1] |= (dtc[BTM][wk][p][bk] > 0) << (bk & 7);
}
}
void
Build ()
{
int i;
Init();
for(i=0; i<100; i++) // won't take more than 100 moves
WhitePass(), BlackPass();
Pack();
}
-
- Posts: 27808
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: How to implement KPK ?
The white pass and black pass look very similar, so perhaps it is more elegant to merge them into one routine:
I guess the bk == p positions should be set to -1 in Init(), and not to 1; the Pass() code does not ever test the state of the daughter position in dtc when it captures the black King with a Pawn, but just assigns a win. So the only testing of this position that ever is important is when the black King stepped onto the Pawn, whether on the previous ply or longer before that. The btm cases have to be marked as draw, to prevent black being forced to 'step off' the Pawn again by zugzwang when white does not have a King move to capture the black King, and tries another one.
Code: Select all
void
Pass (int stm)
{
int wk, bk, p, d;
for(wk=0; wk<64; wk++) for(bk=0; bk<64; bk++) for(p=0; p<64; p++) {
int k = stm == WTM ? wk : bk
int O88 = (k & 070) + k; // 0x88 square number of King
if(dtc[stm][wk][p][bk]) continue; // already decided
dtc[stm][wk][p][bk] = stm; // assume lost if btm, undecided if wtm
if(bk != p && stm == WTM) { // pawn not captured, so can move
if(p+8 != bk && dtc[BTM][wk][p+8][bk] > 0) { dtc[WTM][wk][p][bk] = 1; continue; }
if(p+7 == bk && (p & 7) != 0) { dtc[WTM][wk][p][bk] = 1; continue; }
if(p+9 == bk && (p & 7) != 7) { dtc[WTM][wk][p][bk] = 1; continue; }
}
for(d=0; d<8; d++) {
int to = O88 + steps[d];
if(to & 0x88) continue; // off board
if(stm == WTM ? dtc[BTM][to][p][bk] > 0 : dtc[WTM][wk][p][to] > 0)
{ dtc[stm][wk][p][bk] = !stm; break; }
}
}
}
void
Build ()
{
int i;
Init();
for(i=0; i<100; i++) // won't take more than 100 moves
Pass(WTM), Pass(BTM);
Pack();
}
-
- Posts: 3232
- Joined: Mon May 31, 2010 1:29 pm
- Full name: lucasart
Re: How to implement KPK ?
mine is 24 KB: 64*64*24*2 = 24576 * 8 bits = 24 KB. But it's not public domain and never will be: GPL stays GPL, that's the whole point of it.hgm wrote:Why not just use the bitbase? It is only 64KB.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
-
- Posts: 27808
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: How to implement KPK ?
Ah, you make use of left-right symmetry. Nice idea. I now did that too, by adding a >>1 to the Pawn square during packing, and incrementing it in steps of 2. This should throw away all positions where the Pawn is on an odd file. (A bit wasteful, as the generation still uses the full set of positions, but the assumption was this would be infinitely fast anyway, or that speed would not be important because you save it on a file.)
The following code is tested in the sense that it compiles (there was a missing semicolon), and runs without segfaulting (I had forgotten to convert the 0x88 to-square back to a normal square index.)
When I run it, it prints
where = and - together count the non-won positions (which are not properly distinguished by the algorithm), and + the number of listed wins.
[Edit] O, I guess it still needs a stalemate correction.
The following code is tested in the sense that it compiles (there was a missing semicolon), and runs without segfaulting (I had forgotten to convert the 0x88 to-square back to a normal square index.)
Code: Select all
/* Public-domain KPK bitbase code by H.G. Muller */
#define WTM 0
#define BTM 1
char dtc[2][64][64][64]; // 512 KB
char bitbase[2*64*64*3];
char steps[] = {1, -1, 16, -16, 15, -15, 17, -17};
void
Init ()
{
int wk, bk, p;
for(wk=0; wk<64; wk++) for(bk=0; bk<64; bk++) for(p=0; p<64; p++) {
if(bk == wk) dtc[BTM][wk][p][bk] = 1, dtc[WTM][wk][p][bk] = -1; else
if(wk == p) dtc[BTM][wk][p][bk] = dtc[WTM][wk][p][bk] = -1; else
if(bk == p) dtc[BTM][wk][p][bk] = -1; else // with WTM black just captured P
if(p >= 56) dtc[WTM][wk][p][bk] = 1; // promoted and on move = win
}
}
void
Pass (int stm)
{
int wk, bk, p, d;
for(wk=0; wk<64; wk++) for(bk=0; bk<64; bk++) for(p=0; p<64; p++) {
int k = stm == WTM ? wk : bk;
int O88 = (k & 070) + k; // 0x88 square number of King
if(dtc[stm][wk][p][bk]) continue; // already decided
dtc[stm][wk][p][bk] = stm; // assume lost if btm, undecided if wtm
if(bk != p && stm == WTM) { // pawn not captured, so can move
if(p+8 != bk && dtc[BTM][wk][p+8][bk] > 0) { dtc[WTM][wk][p][bk] = 1; continue; }
if(p+7 == bk && (p & 7) != 0) { dtc[WTM][wk][p][bk] = 1; continue; }
if(p+9 == bk && (p & 7) != 7) { dtc[WTM][wk][p][bk] = 1; continue; }
}
for(d=0; d<8; d++) {
int to = O88 + steps[d];
if(to & 0x88) continue; // off board
to -= to>>1 & 070;
if(stm == WTM ? dtc[BTM][to][p][bk] > 0 : dtc[WTM][wk][p][to] > 0)
{ dtc[stm][wk][p][bk] = !stm; break; }
}
}
}
void
Pack ()
{
int wk, bk, p, i;
for(wk=0; wk<64; wk++) for(bk=0; bk<64; bk++) for(p=8; p<56; p+=2) {
int index = (bk >> 3) + 8*wk + 8*64*(p-8>>1);
bitbase[2*index] |= (dtc[WTM][wk][p][bk] > 0) << (bk & 7);
bitbase[2*index+1] |= (dtc[BTM][wk][p][bk] > 0) << (bk & 7);
}
}
void
Build ()
{
int i;
Init();
for(i=0; i<100; i++) // won't take more than 100 moves
Pass(WTM), Pass(BTM);
Pack();
}
Code: Select all
white to move: 211980+, 23085=, 27079-
black to move: 140386+, 113694=, 8064-
[Edit] O, I guess it still needs a stalemate correction.
-
- Posts: 3232
- Joined: Mon May 31, 2010 1:29 pm
- Full name: lucasart
Re: How to implement KPK ?
This is no idea of mine. It's a great classic, and rather trivial. Many have done it before me. Some might have done it even before I was bornhgm wrote:Ah, you make use of left-right symmetry. Nice idea.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
-
- Posts: 588
- Joined: Thu Mar 09, 2006 4:47 pm
- Location: Singapore
Re: How to implement KPK ?
As Deng Xiaopeng said: "It does not matter if a cat is red or black as long as it catches the mouse" - but don't take too long.hgm wrote:You could write your own code to generate it, and put it in the public domain.
That seems simpler, and much less bug-prone then the code you are trying to write now.
Let me make a first attempt. (Completely untested...)
Code: Select all
/* Public-domain KPK bitbase code by H.G. Muller */ #define WTM 0 #define BTM 1 char dtc[2][64][64][64]; // 512 KB char bitbase[2*62*64*6]; char steps[] = {1, -1, 16, -16, 15, -15, 17, -17}; void Init () { int wk, bk, p; for(wk=0; wk<64; wk++) for(bk=0; bk<64; bk++) for(p=0; p<64; p++) { if(bk == wk) dtc[BTM][wk][p][bk] = 1, dtc[WTM][wk][p][bk] = -1; else if(wk == p) dtc[BTM][wk][p][bk] = dtc[WTM][wk][p][bk] = -1; else if(bk == p) dtc[BTM][wk][p][bk] = 1; else // with WTM black just captured P if(p >= 56) dtc[WTM][wk][p][bk] = 1; // promoted and on move = win } } void BlackPass () { int wk, bk, p, d; for(wk=0; wk<64; wk++) for(bk=0; bk<64; bk++) for(p=0; p<64; p++) { int O88 = (bk & 070) + bk; // 0x88 square number if(dtc[BTM][wk][p][bk]) continue; // already decided dtc[BTM][wk][p][bk] = 1; // assume lost, if no escape will be found for(d=0; d<8; d++) { int to = O88 + steps[d]; if(to & 0x88) continue; // off board if(dtc[WTM][wk][p][to] <= 0) { dtc[BTM][wk][p][bk] = 0; break; } // escape } } } void WhitePass () { int wk, bk, p, d; for(wk=0; wk<64; wk++) for(bk=0; bk<64; bk++) for(p=0; p<64; p++) { int O88 = (wk & 070) + wk; // 0x88 square number if(dtc[WTM][wk][p][bk]) continue; // already decided if(bk != p) { // pawn not captured, so can move if(p+8 != bk && dtc[BTM][wk][p+8][bk] > 0) { dtc[WTM][wk][p][bk] = 1; continue; } if(p+7 == bk && (p & 7) != 0) { dtc[WTM][wk][p][bk] = 1; continue; } if(p+9 == bk && (p & 7) != 7) { dtc[WTM][wk][p][bk] = 1; continue; } } for(d=0; d<8; d++) { int to = O88 + steps[d]; if(to & 0x88) continue; // off board if(dtc[BTM][to][p][bk] > 0) { dtc[WTM][wk][p][bk] = 1; break; } } } } void Pack () { int wk, bk, p, i; for(wk=0; wk<64; wk++) for(bk=0; bk<64; bk++) for(p=8; p<56; p++) { int index = (bk >> 3) + 8*wk + 8*64*(p-8); bitbase[2*index] |= (dtc[WTM][wk][p][bk] > 0) << (bk & 7); bitbase[2*index+1] |= (dtc[BTM][wk][p][bk] > 0) << (bk & 7); } } void Build () { int i; Init(); for(i=0; i<100; i++) // won't take more than 100 moves WhitePass(), BlackPass(); Pack(); }
As Dr. Robert Hyatt said: "In my forty years in computer chess, I have yet to see a game that played to a KPK ending." - but there is always a tomorrow.
As Herr Muller said: "I have already put the KPK bitbase generator in the public domain. If you don't understand simple retrograde analysis starting from the promotion squares and about the concept of un-make, you should not be in chess programming. Go! Join a choir and make some sound!" - don't be too certain that that someone would't be signed up by Sony later.
As another said:"If your program can't outplay Houdini 5, don't give the excuse that it is because you have no KPK bitbase or that you discovered a bug in your KPK routine. Just say - ' Robert is really a genius'"
It does not matter very much if I use the KPK bitbase. If there is a bug in my search, then it will still not be able to play a KPK ending.
Best Regards,
Rasjid.
-
- Posts: 27808
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: How to implement KPK ?
I see KPK quite often.
Anyway, to further the public-domain cause, the following code actually runs, and prints some numbers that seem to make sense (like the number of stalemates, which matches what I calculated by hand). I even put in the initial double-push, which is important for rule-of-squares situations.
Btw, this is the dumb algorithm, which does not use any unmoves. It just attempts regular moving from all not-yet-decided positions, to look for a move to a won position for white, and to a non-lost position for black.
Printing the stats of the positions with the Pawn on rank 2-7 afterwards gives:
where + means won to white, = means draw, - illegal by white's fault or black stalemated. (White stalemates are not separately identified, they count with the other draws.)
Anyway, to further the public-domain cause, the following code actually runs, and prints some numbers that seem to make sense (like the number of stalemates, which matches what I calculated by hand). I even put in the initial double-push, which is important for rule-of-squares situations.
Btw, this is the dumb algorithm, which does not use any unmoves. It just attempts regular moving from all not-yet-decided positions, to look for a move to a won position for white, and to a non-lost position for black.
Code: Select all
/* Public-domain KPK bitbase code by H.G. Muller */
#define WTM 0
#define BTM 1
char dtc[2][64][64][64]; // 512 KB
char bitbase[2*64*64*3];
char steps[] = {1, -1, 16, -16, 15, -15, 17, -17}; // 0x88 King steps
void
Init ()
{ /* 3 = bK captured, 2 = can capture bK, 1 = won, 0 = undecided, -1 = broken or wK captured */
int wk, bk, p;
for(wk=0; wk<64; wk++) for(p=0; p<64; p++) for(bk=0; bk<64; bk++) {
if(bk == wk) dtc[BTM][wk][p][bk] = 3, dtc[WTM][wk][p][bk] = -1; else
if(wk == p) dtc[BTM][wk][p][bk] = dtc[WTM][wk][p][bk] = -1; else
if(bk == p) dtc[BTM][wk][p][bk] = -3; else // with WTM black just captured P
if(p >= 56) dtc[WTM][wk][p][bk] = 1; // promoted and on move = win
}
}
void
Pass (int stm, int first, int def, int esc)
{
int wk, bk, p, d;
for(wk=0; wk<64; wk++) for(p=0; p<64; p++) for(bk=0; bk<64; bk++) {
int k = stm == WTM ? wk : bk;
int O88 = (k & 070) + k; // 0x88 square number of King
if(dtc[stm][wk][p][bk]) continue; // already decided
dtc[stm][wk][p][bk] = def; // assume lost if btm, undecided if wtm
if(bk != p && stm == WTM) { // pawn not captured, so can move
if(p+8 != bk && (dtc[BTM][wk][p+8 ][bk] > first || p < 16 &&
p+16 != bk && dtc[BTM][wk][p+16][bk] > first)) { dtc[WTM][wk][p][bk] = esc; continue; }
if(p+7 == bk && (p & 7) != 0) { dtc[WTM][wk][p][bk] = 2; continue; }
if(p+9 == bk && (p & 7) != 7) { dtc[WTM][wk][p][bk] = 2; continue; }
}
for(d=0; d<8; d++) {
int to = O88 + steps[d];
if(to & 0x88) continue; // off board
to -= to>>1 & 070;
if(stm == WTM ? dtc[BTM][to][p][bk] > first : dtc[WTM][wk][p][to] <= first)
{ dtc[stm][wk][p][bk] = esc; break; }
}
}
}
void
Pack ()
{
int wk, bk, p, i;
for(wk=0; wk<64; wk++) for(p=8; p<56; p+=2) for(bk=0; bk<64; bk++) { // calculate probe address and bit
int index = (bk >> 3) + 8*wk + 8*64*(p-8>>1);
bitbase[2*index] |= (dtc[WTM][wk][p][bk] > 0) << (bk & 7);
bitbase[2*index+1] |= (dtc[BTM][wk][p][bk] > 0) << (bk & 7);
}
}
void
Build ()
{
int i;
Init();
Pass(WTM, 2, 0, 2); Pass(BTM, 1, -2, 0); // calculate King captures and
stalemates
for(i=0; i<25; i++) // won't take more than 25 moves
Pass(WTM, 0, 0, 1), Pass(BTM, 0, 1, 0);
Pack();
}
Code: Select all
white to move: 124960+, 41044=, 6096-, 0 stalemate, 24508 K-capture
black to move: 97604+, 89866=, 6066-, 18 stalemate, 0 K-capture
-
- Posts: 27808
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: How to implement KPK ?
From probing a few hundred randomly generated positions I could not find an error. So it seems the code is OK.