Another way of evaluating material imbalances

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
Volker Annuss
Posts: 181
Joined: Mon Sep 03, 2007 9:15 am

Another way of evaluating material imbalances

Post by Volker Annuss »

Material can be encoded in a single 64 bit number with a four bit counter for every piece type separated by a bit which is always 0. X is used as a special piece type that stands for having the bishop pair. (A single bit would be sufficient.)

Code: Select all

00000QQQQ0RRRR0BBBB0NNNN0PPPP0XXXX0qqqq0rrrr0bbbb0nnnn0pppp0xxxx
For the position on the board, there are two numbers, one for the total material on the board and one just for the imbalance which is what one side has more than the other.

Example: KRBBPPP-krnnpppp is encoded as

Code: Select all

           0000 0QQQQ 0RRRR 0BBBB 0NNNN 0PPPP 0XXXX 0qqqq 0rrrr 0bbbb 0nnnn 0pppp 0xxxx
material:  0000 00000 00001 00010 00000 00011 00001 00000 00001 00000 00010 00100 00000
imbalance: 0000 00000 00000 00010 00000 00000 00001 00000 00000 00000 00010 00001 00000
Material evaluation is made by a list of rules. Every rule has material, imbalance and a value. To test if a rule can be applied, just a subtraction is needed. The trick is that when a rule cannot be applied to some material, there is an overflow into one of the bits that should be 0.

Pseudocode for material eval:

Code: Select all

const mask = 0000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000;
board.material = material_encoding();
board.imbalance = imbalance_encoding();

for every rule do
{
	if ( ((board.material - rule.material) & mask) == 0 )
	{
		while ( ((board.imbalance - rule.imbalance) & mask) == 0 )
		{
			board.imbalance -= rule.imbalance;
			eval            += rule.value;
		}
	}
}
Theoretically it is possible to do the whole material evaluation with a list of rules. Special rules come first, general rules for a single piece last. In practice I still have classical material eval in Arminius and just some rules in the latest version coming soon.
BeyondCritics
Posts: 415
Joined: Sat May 05, 2012 2:48 pm
Full name: Oliver Roese

Re: Another way of evaluating material imbalances

Post by BeyondCritics »

Volker Annuss wrote: ...
Pseudocode for material eval:

Code: Select all

const mask = 0000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000 10000;
board.material = material_encoding();
board.imbalance = imbalance_encoding();

for every rule do
{
	if ( ((board.material - rule.material) & mask) == 0 )
	{
		while ( ((board.imbalance - rule.imbalance) & mask) == 0 )
		{
			board.imbalance -= rule.imbalance;
			eval            += rule.value;
		}
	}
}
...
I don't understand at all what you are doing here.
A rule fires, iff board.material >= rule.material for every piecetype i.
When a rule has fired you effectively calculate
r := min(floor(board.imbalance / rule.imbalance), i=0,1,...)
and finally you add eval += r*rule.value. I assume i misreading something...
User avatar
Volker Annuss
Posts: 181
Joined: Mon Sep 03, 2007 9:15 am

Re: Another way of evaluating material imbalances

Post by Volker Annuss »

You did not misread it. But I suggest to interpret it in a different way.

A rule fires when board.material >= rule.material and board.imbalance >= rule.imbalance for every piecetype i. A rule may fire more than once.

Every time a rule fires, it contributes to the evaluation and the imbalance is taken away from the board.

For example take a rule with an imbalance B-n and material PPPPP-ppppp. This evaluates the value of bishop vs. knight but only if both sides have at least 5 pawns.

Another example: A rule with an imbalance of 1 Pawn but material 2 pawns followed by another rule with the imbalance of 1 pawn and no material. Normally the first rule will fire but when it is the last pawn on the board, the second rule will fire.
BeyondCritics
Posts: 415
Joined: Sat May 05, 2012 2:48 pm
Full name: Oliver Roese

Re: Another way of evaluating material imbalances

Post by BeyondCritics »

OK, i think i understand your posting now better.
I believe your system works and could be of interest to others. Generalizing the (material|imbalance).counts to some feature.counts,
this could even be used in a broader context.
It is true that this method can express every finite function of material.counts, on the other hand the natural material equivalence is nicely expressed.
There are two questions still:
How do you find efficient rules? Given the rules, how do you estimate the parameters?
On problem with the rules is, that two rules with exactly overlapping material.imbalance are ambigous, since you can only use one of them.
Therefore you must either choose one of them to fire first or place a third rule before both of them, to handle the interaction.
User avatar
Volker Annuss
Posts: 181
Joined: Mon Sep 03, 2007 9:15 am

Re: Another way of evaluating material imbalances

Post by Volker Annuss »

BeyondCritics wrote:How do you find efficient rules? Given the rules, how do you estimate the parameters?
I did a kind of reverse tuning. I took some fixed values in a fixed order and have an optimizer choose the rules that match them best.

In more details, I made a list of 64 rules (better pairs of rules with colours reversed) with 2 vs. 1 or 2 pieces with a difference of at most the value of a piece. I did not use rules with the bishop pair. That is still evaluated the classical way. I also did not use rules with the material part, just with imbalance. Then I did population based incremental learning to choose 4 rules. After that I tuned the values to better fit the rules.

There is still much room for improvement. Ideas welcome.