KPK

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
hgm
Posts: 28354
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: KPK

Post by hgm »

mar wrote:Do you do this automatically (as I suspect) or manually for each piece configuration?
I never use non-trivial indexing (as if accessing a multidimensional array indexed by the square numbers). Indexes outside the canonical symmetry sector are mapped into it by manipulation of the index, however, to exploit symmetry. For 8x8 boards this easy (e.g. XORing with 070707070).
Karlo Bala
Posts: 373
Joined: Wed Mar 22, 2006 10:17 am
Location: Novi Sad, Serbia
Full name: Karlo Balla

Re: KPK

Post by Karlo Bala »

BubbaTough wrote:
Sven Schüle wrote:
hgm wrote:
mar wrote:Safe promotions to queen are considered a win,
I suppose you do test for stalemate after the promotion, not just for recapture of the Queen. Having a Queen and the move is always a win.
Note that there are also few KPK positions where queening draws due to stalemate but underpromoting to a rook wins, e.g.:
[d]8/k1P5/2K5/8/8/8/8/8 w - - 0 1

Sven
Just out of curiosity, are there any where you need to know about under promotion to judge them accurately, or are they all positions where not knowing about under promotion just causes you to mate a few moves later (like this one)?

-Sam
Probably.

[d]8/k1P5/8/K7/8/8/8/8 w - - 0 1
Best Regards,
Karlo Balla Jr.
mar
Posts: 2656
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: KPK

Post by mar »

hgm wrote:I never use non-trivial indexing (as if accessing a multidimensional array indexed by the square numbers). Indexes outside the canonical symmetry sector are mapped into it by manipulation of the index, however, to exploit symmetry. For 8x8 boards this easy (e.g. XORing with 070707070).
I see, so you mirror horizontally, vertically or both.
I thought that in pawnless positions (8x8 boards), one could try 90-degrees rotations as well (I hope it's not nonsense).
User avatar
hgm
Posts: 28354
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: KPK

Post by hgm »

Yes, diagonal mirroring is slightly more expensive:

address = index>>3 & 0707070707 | index<<3 & 0x7070707070

If the board is not 8x8 it is not so straightforward, and the only generator I have for arbitrary boards does not use symmetry at all. It should be possible, however, by keeping the contribution of file and rank coords to the index separately. E.g. on a 10x10 board you could do

indexh = file1 + 100* file2 + 10000*file3 ...
indexv = rank1 + 100*rank2 + 10000*rank3 ...
index = indexh + 10*indexv

Then you can use 90909 - indexh for left-right flip, and 90909 - indexv for top-bottom flip. And index = 10*indexh + indexv for a diagonal reflection. indexh and indexv can then be each updated incrementally in the scanning and moving routines. On non-square boards there is no diagonal symmetry, and you could directly update 10*indexv incrementally.
BubbaTough
Posts: 1154
Joined: Fri Jun 23, 2006 5:18 am

Re: KPK

Post by BubbaTough »

Karlo Bala wrote:
BubbaTough wrote:
Sven Schüle wrote:
hgm wrote:
mar wrote:Safe promotions to queen are considered a win,
I suppose you do test for stalemate after the promotion, not just for recapture of the Queen. Having a Queen and the move is always a win.
Note that there are also few KPK positions where queening draws due to stalemate but underpromoting to a rook wins, e.g.:
[d]8/k1P5/2K5/8/8/8/8/8 w - - 0 1

Sven
Just out of curiosity, are there any where you need to know about under promotion to judge them accurately, or are they all positions where not knowing about under promotion just causes you to mate a few moves later (like this one)?

-Sam
Probably.

[d]8/k1P5/8/K7/8/8/8/8 w - - 0 1
Nice, I like it :).

-Sam
mar
Posts: 2656
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: KPK

Post by mar »

hgm wrote:Yes, diagonal mirroring is slightly more expensive:

address = index>>3 & 0707070707 | index<<3 & 0x7070707070

If the board is not 8x8 it is not so straightforward, and the only generator I have for arbitrary boards does not use symmetry at all. It should be possible, however, by keeping the contribution of file and rank coords to the index separately. E.g. on a 10x10 board you could do

indexh = file1 + 100* file2 + 10000*file3 ...
indexv = rank1 + 100*rank2 + 10000*rank3 ...
index = indexh + 10*indexv

Then you can use 90909 - indexh for left-right flip, and 90909 - indexv for top-bottom flip. And index = 10*indexh + indexv for a diagonal reflection. indexh and indexv can then be each updated incrementally in the scanning and moving routines. On non-square boards there is no diagonal symmetry, and you could directly update 10*indexv incrementally.
I see, clever, so only one diagonal mirroring (or transposition) will do.
So the rotations then become HD, HV and VD.
lucasart
Posts: 3241
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: KPK

Post by lucasart »

Sven Schüle wrote:
hgm wrote:
mar wrote:Safe promotions to queen are considered a win,
I suppose you do test for stalemate after the promotion, not just for recapture of the Queen. Having a Queen and the move is always a win.
Note that there are also few KPK positions where queening draws due to stalemate but underpromoting to a rook wins, e.g.:
[d]8/k1P5/2K5/8/8/8/8/8 w - - 0 1

Sven
Good spot.

But that is not a problem. In practice the KPK bitbase only tells you if it's a draw or not (1 bit per position). It doesn't tell you that you should do a queen promotion. So it's not a problem.

I don't think there are positions where white can promote (w/o being captured) and it's draw regardless of the promotion piece he chooses (or even if he chooses a king move instead).
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
User avatar
hgm
Posts: 28354
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: KPK

Post by hgm »

lucasart wrote:I don't think there are positions where white can promote (w/o being captured) and it's draw regardless of the promotion piece he chooses (or even if he chooses a king move instead).
[d]8/1P6/8/8/8/K7/8/k7 w

But it is won after a King move, so still no problem.
lucasart
Posts: 3241
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: KPK

Post by lucasart »

hgm wrote:
lucasart wrote:I don't think there are positions where white can promote (w/o being captured) and it's draw regardless of the promotion piece he chooses (or even if he chooses a king move instead).
[d]8/1P6/8/8/8/K7/8/k7 w

But it is won after a King move, so still no problem.
Nice one! Anyway, the Stockfish code is correct to assume that in all cases where white can promote (w/o losing the pawn) it's a win. KPK bitbases don't tell what move you should play to win (but that's trivial for the search to find), they just tell you if it's draw or not. So unless we can find a position where white can promote and it's still a draw, regardless of what white plays, then we're fine with the plain and simple promotion condition.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: KPK

Post by Sven »

lucasart wrote:
Sven Schüle wrote:
hgm wrote:
mar wrote:Safe promotions to queen are considered a win,
I suppose you do test for stalemate after the promotion, not just for recapture of the Queen. Having a Queen and the move is always a win.
Note that there are also few KPK positions where queening draws due to stalemate but underpromoting to a rook wins, e.g.:
[d]8/k1P5/2K5/8/8/8/8/8 w - - 0 1

Sven
Good spot.

But that is not a problem. In practice the KPK bitbase only tells you if it's a draw or not (1 bit per position). It doesn't tell you that you should do a queen promotion. So it's not a problem.

I don't think there are positions where white can promote (w/o being captured) and it's draw regardless of the promotion piece he chooses (or even if he chooses a king move instead).
My point was different. The bitbase generation algorithm "check whether queening creates stalemate; if so => draw, otherwise => won" would misevaluate the position above (provided there is no king move creating a known win), and some similar ones. Of course a full EGTB generation would not suffer from that problem since it would be based on already generated KQK, KRK databases. But as I understood the "short cut" way of generating the KPK bitbase should include to statically evaluate those positions where promotion is possible, and I only wanted to point out that doing this correctly would require more than just checking queen promotions and a possible stalemate.

Sven