Number of unique piece counts in chess

Discussion of chess software programming and technical issues.

Moderator: Ras

dangi12012
Posts: 1062
Joined: Tue Apr 28, 2020 10:03 pm
Full name: Daniel Infuehr

Number of unique piece counts in chess

Post by dangi12012 »

The question is how many unique piece counts can exist.

This will boil down a single number to a corresponding number of pieces per color so a perfect compression of all 12 std::popcount(PieceType)
Could be used to unroll each and every loop that usually is happening over the BB of a piece.

Useful for: templated loop unrolling at compiletime - a graph of functions that are templated and all call downward towards kK.

Code: Select all

Wpawn    |   Bpawn     |   0..8 -> each of those can exist as nbrq
Wknight  |   Bknight   |   0..2
Wbishop  |   Bbishop   |   0..2
Wrook    |   Brook     |   0..2
Wqueen   |   Bqueen    |   0..1
Wking    |   Bking     |   1
Since the number of kings cannot change they are not part of this calculation (fixed at 1,1)
Quick implementation:

Code: Select all

#define loop(var, cnt) for(int var = 0; var < cnt; var++) {
#define loop2(var, cnt) loop(B##var, cnt) loop(W##var, cnt)
#define loopEnd }}}}}}}}}}

struct PieceConfig
{
	int Wpawn, Wknight, Wbishop, Wrook, Wqueen;
	int Bpawn, Bknight, Bbishop, Brook, Bqueen;

	bool valid(int pawns, int knights, int bishops, int rooks, int queens)
	{
		int total = pawns + knights + bishops + rooks + queens;
		if (total > 15) return false;
		int c_knigths = std::max(knights - 2, 0);
		int c_bishops = std::max(bishops - 2, 0);
		int c_rooks =   std::max(rooks - 2, 0);
		int c_queens =  std::max(queens - 1, 0);
		int c_total = c_knigths + c_bishops + c_rooks + c_queens;
		int c_available = std::max(8 - pawns, 0);

		if (c_total > c_available) return false;
		return true;
	}

	bool valid()
	{
		return valid(Wpawn, Wknight, Wbishop, Wrook, Wqueen) && valid(Bpawn, Bknight, Bbishop, Brook, Bqueen);
	}

	auto operator<=> (const PieceConfig& cmp) const = default;
};


uint64_t PieceNums()
{
	std::set<PieceConfig> cfgs;
	loop2(knight, 10)
	loop2(bishop, 10)
	loop2(rook, 10)
	loop2(queen, 9)
	loop2(pawn, 8)
	//Each pawn can also exist as nbrq
	PieceConfig cfg 
	{
		Wpawn, Wknight, Wbishop, Wrook, Wqueen,
		Bpawn, Bknight, Bbishop, Brook, Bqueen
	};
	if (cfg.valid())
	{
		cfgs.insert(cfg);
	}

	loopEnd
	return cfgs.size();
}

int main()
{
	std::cout << PieceNums() << "\n";
}
Result:
73256481

I did a statistic of how many 2000 Elo or above games actually contain many pieces. The answer is this:
2 queens, 2 bishops, 2 rooks, 2 knight 99,9828215%

Result max 2 queens:
16384

So a single 14 bit number would be able to resolve all popcounts of the 12 pieces that can exist on the board for 99,98% of all games in a single lookup.
Now I did not find 73256481 referenced anyhwere so is the math correct here?
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
User avatar
Ajedrecista
Posts: 2101
Joined: Wed Jul 13, 2011 9:04 pm
Location: Madrid, Spain.

Re: Number of unique piece counts in chess.

Post by Ajedrecista »

Hello Daniel:

I think that this number was already computed and your result is different than the other:

https://kirill-kryukov.com/chess/nulp/n ... ieces.html

There is a link in that page to an even older post at CCC (the former name of TalkChess) dating from 1999, where the same number 75,585,636 appears:

Subject: Counting & Encoding Any Chess Position in 157 bits

However, I am not sure if that number is exact or an upper limit, so I do not discard that we are comparing apples to oranges. You have interesting links at NULP to further investigate. Good luck!

Regards from Spain.

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

Re: Number of unique piece counts in chess

Post by hgm »

Not sure what you are calculating. I would say that per color there are 2 x 3 x 3 x 3 x 9 = 486 possible compositions, and the square of that is 236,196. Or are you also counting compositions with super-numerary pieces?
Uri Blass
Posts: 10793
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Number of unique piece counts in chess

Post by Uri Blass »

I calculated an upper bound for structures of one side by hand without a computer program.

Let the number of white pawns be n1
number of white knights be n2
number of white bishops be n3
number of white rooks be n4
number of white queens be n5
We have
n1+n2+n3+n4+n5<=15
n1>=0,n2>=0,n3>=0,n4>=0,n5>=0

basically the sequence
n1+1,n1+n2+2,n1+n2+n3+3,n1+n2+n3+n4+4,n1+n2+n3+n4+n5+5 is a monotonic sequence when
1<=n1+1<n1+n2+2<n1+n2+n3+3<n1+n2+n3+n4+4<n1+n2+n3+n4+n5+5<=20

number of options for this sequence is number of ways to choose 5 different numbers out of 20 that is 20!/(5!*15!)=15504

Now I need to substract impossible structures.
structures can be impossible exactly for one of the reasons(not for more than one):
1)having at least 9 white pawns
2)having at least 10 white queens
3)having at least 11 white rooks
4)having at least 11 white bishops
5)having at least 11 white knights

I Count Forbidden Structures with at least 9 pawns.
Let the number of white pawns be n1
number of white knights be n2
number of white bishops be n3
number of white rooks be n4
number of white queens be n5
basically we have now
10<=n1+1<n1+n2+2<n1+n2+n3+3<n1+n2+n3+n4+4<n1+n2+n3+n4+n5+5<=20


11!/(5!*6!)=462 to substract.
Forbidden Structures with at least 10 queens same as structures with at least 10 pawns and we get
10!/(5!*5!)=252
Forbidden Structures with at least 11 rooks same as structures with at least 11 pawns and we get
9!/(5!*4!)=126 same for 11 bishops or 11 knights and I get
15504-462-252-126*3=14412

Now 14412^2>73254681=8559^2 so it seems that you got 8559 for one side and I wonder what is the mistake.

Edit:I see that I did not substract part of the impossible structures for example
9 queens and 3 rooks and maybe this is the reason that I got a bigger number.
Last edited by Uri Blass on Wed Jul 27, 2022 7:39 pm, edited 1 time in total.
Uri Blass
Posts: 10793
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Number of unique piece counts in chess

Post by Uri Blass »

hgm wrote: Wed Jul 27, 2022 7:20 pm Not sure what you are calculating. I would say that per color there are 2 x 3 x 3 x 3 x 9 = 486 possible compositions, and the square of that is 236,196. Or are you also counting compositions with super-numerary pieces?
I think that you do not consider promotions
486 is the number of piece structures for one side without promotions.
Uri Blass
Posts: 10793
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Number of unique piece counts in chess

Post by Uri Blass »

Note that I do not claim that X^2 is the number of possible structures for both sides when X is the number of possible structure for one side because there are many impossible structures that are possible if you look only at white or only at black for example:
White has 16 pieces and black has 16 pieces that include one promoted piece instead of a pawn(impossible because you need to make some capture to promote a piece).
Uri Blass
Posts: 10793
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Number of unique piece counts in chess.

Post by Uri Blass »

Ajedrecista wrote: Wed Jul 27, 2022 7:12 pm Hello Daniel:

I think that this number was already computed and your result is different than the other:

https://kirill-kryukov.com/chess/nulp/n ... ieces.html

There is a link in that page to an even older post at CCC (the former name of TalkChess) dating from 1999, where the same number 75,585,636 appears:

Subject: Counting & Encoding Any Chess Position in 157 bits

However, I am not sure if that number is exact or an upper limit, so I do not discard that we are comparing apples to oranges. You have interesting links at NULP to further investigate. Good luck!

Regards from Spain.

Ajedrecista.
I see in the first link a number that is equal to 8694^2 that is different than 8559^2 for the number of material configuration for chess pieces when you allow every starting position so one has to be wrong even if you look at material configuration only for one side.
dangi12012
Posts: 1062
Joined: Tue Apr 28, 2020 10:03 pm
Full name: Daniel Infuehr

Re: Number of unique piece counts in chess

Post by dangi12012 »

Closer Look - Normal case:
Maximum of two pieces of each type. Calculation is simple: 3 cases - none, one, two - King is always there: 1
Result is squared because the situation is symmetrical

K*N*B*R*Q*P
1*3*3*3*3*9 = 729^2 = 531441 (Same with code with valid check disabled)

Warning: You cannot have 2 queens and 8 pawns.
So the rule applies here (maximum number of promoted pieces = 8 - pawns). So you cannot have 2 queens and 8 pawns.

Code: Select all

int c_knigths = std::max(knights - 2, 0);
int c_bishops = std::max(bishops - 2, 0);
int c_rooks =   std::max(rooks - 2, 0);
int c_queens =  std::max(queens - 1, 0);
int c_total = c_knigths + c_bishops + c_rooks + c_queens;
int c_available = std::max(8 - pawns, 0);

if (c_total > c_available) return false;
Final Normal case Result:
702^2 = 492804



General case
Up to: 10 RBN, 9Q, 8P
Second rules applies here additionally: The total number of pieces cannot be more than 15. (King excluded)

Code: Select all

int total = pawns + knights + bishops + rooks + queens;
		if (total > 15) return false;
General Result:
8694 ^ 2 = 75585636


Final code used:

Code: Select all


#define loop(var, cnt) for(int var = 0; var <= cnt; var++) {
#define loopEnd }}}}}

struct PieceConfig
{
	int pawns, knights, bishops, rooks, queens;

	bool valid()
	{
		int total = pawns + knights + bishops + rooks + queens;
		if (total > 15) return false;
		int c_knigths = std::max(knights - 2, 0);
		int c_bishops = std::max(bishops - 2, 0);
		int c_rooks =   std::max(rooks - 2, 0);
		int c_queens =  std::max(queens - 1, 0);
		int c_total = c_knigths + c_bishops + c_rooks + c_queens;
		int c_available = std::max(8 - pawns, 0);

		if (c_total > c_available) return false;
		return true;
	}


	auto operator<=> (const PieceConfig& cmp) const = default;
};


uint64_t PieceNums()
{
	std::set<PieceConfig> cfgs;
	loop(knight, 10)
	loop(bishop, 10)
	loop(rook, 10)
	loop(queen, 9)
	loop(pawn, 8)
	//Each pawn can also exist as nbrq
	PieceConfig cfg 
	{
		pawn, knight, bishop, rook, queen
	};
	if (cfg.valid())
	{
		cfgs.insert(cfg);
	}

	loopEnd
	return cfgs.size();
}

int main()
{
	std::cout << PieceNums() << "\n";
}
The results should be correct (very high confidence) - because of the simplicity of the code and the symmetry argument of squaring one side.
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
dangi12012
Posts: 1062
Joined: Tue Apr 28, 2020 10:03 pm
Full name: Daniel Infuehr

Re: Number of unique piece counts in chess

Post by dangi12012 »

Exchaustive list:

Code: Select all

K
QK
QQK
RK
RQK
RQQK
RRK
RRQK
RRQQK
BK
BQK
BQQK
BRK
BRQK
BRQQK
BRRK
BRRQK
BRRQQK
BBK
BBQK
BBQQK
BBRK
BBRQK
BBRQQK
BBRRK
BBRRQK
BBRRQQK
NK
NQK
NQQK
NRK
NRQK
NRQQK
NRRK
NRRQK
NRRQQK
NBK
NBQK
NBQQK
NBRK
NBRQK
NBRQQK
NBRRK
NBRRQK
NBRRQQK
NBBK
NBBQK
NBBQQK
NBBRK
NBBRQK
N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PPNRK
PPPPPPPPNRQK
PPPPPPPPNRRK
PPPPPPPPNRRQK
PPPPPPPPNBK
PPPPPPPPNBQK
PPPPPPPPNBRK
PPPPPPPPNBRQK
PPPPPPPPNBRRK
PPPPPPPPNBRRQK
PPPPPPPPNBBK
PPPPPPPPNBBQK
PPPPPPPPNBBRK
PPPPPPPPNBBRQK
PPPPPPPPNBBRRK
PPPPPPPPNBBRRQK
PPPPPPPPNNK
PPPPPPPPNNQK
PPPPPPPPNNRK
PPPPPPPPNNRQK
PPPPPPPPNNRRK
PPPPPPPPNNRRQK
PPPPPPPPNNBK
PPPPPPPPNNBQK
PPPPPPPPNNBRK
PPPPPPPPNNBRQK
PPPPPPPPNNBRRK
PPPPPPPPNNBRRQK
PPPPPPPPNNBBK
PPPPPPPPNNBBQK
PPPPPPPPNNBBRK
PPPPPPPPNNBBRQK
PPPPPPPPNNBBRRK
PPPPPPPPNNBBRRQK
Total: 702 per side
Good for: 99,98% of all positions reached in high level games.

So having bitloop fallback code for the 0.02% (Bitloop(X) for (; X; X &= X - 1)).
For every other case a single number between 0 and 702 per side suffices to know how often any of the 6 pieces occur in legal games.

Two things to note in above array:
This can be sorted such that you can only ever go downwards towards entry 0 like walking a tree.
This can be done at compiletime and totally replace every loop over the number of pieces.

Results appear in literature too: (as # of loops which would be absolutely correct if I wanted to know the number of board positions)
http://www.nwchess.com/articles/misc/Ch ... rticle.pdf

Closely related topic: http://www.talkchess.com/forum3/viewtop ... =7&t=80149
Worlds-fastest-Bitboard-Chess-Movegenerator
Daniel Inführ - Software Developer
jhaglund2
Posts: 66
Joined: Mon Jan 16, 2017 6:28 pm

Re: Number of unique piece counts in chess

Post by jhaglund2 »

While that is certainly interesting, I think how many unique chess notations would be more valuable to store, and way smaller.
E.g.:

Code: Select all

const string castle[6] = { "e1g1", "e1c1", "e8g8", "e8c8", "O-O", "O-O-O" };
ep_w[14]
ep_b[14]
w_pawn[100]
b_pawn[100]
promote_w[88]
promote_b[88]
knight[336]
bishop_b[280]
bishop_w[280]
rook[896]
queen[1456] 
king[420]
4078 notations (includes duplicates)
1970 notations (without duplicates)
Then, create a bool flag for each notation, if legal.

Just my 2 cents.