evaluating color agnostic terms

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Cardoso
Posts: 362
Joined: Thu Mar 16, 2006 7:39 pm
Location: Portugal
Full name: Alvaro Cardoso

evaluating color agnostic terms

Post by Cardoso »

Hi everybody :),

suppose I got convinced my engine does better in very complex positions (wich it does not)
and I want my engine to prefer positions with many queens for example.
since the root_ply=1, I do the following in the eval:

//some_weight could be 10 for example (pawn=100, queen=1000),
//WQ and BQ are the queens bitboards of their respective color

Code: Select all

if (ply&1) {
   int qc = __popcount(WQ | BQ); 
   score += qc * some_weight; 
} else {
  // do nothing (wich might as well go without the "else" statement
  // this means the opponent don't evaluate/care this feature
}
is the above code correct ?
Will my engine prefer positions with many queens with the above code?

best regards,
Alvaro
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: evaluating color agnostic terms

Post by AlvaroBegue »

Why would you do this only for odd values of `ply'? You probably want to do it all the time, making sure the sign goes in the direction it has to go.
Cardoso
Posts: 362
Joined: Thu Mar 16, 2006 7:39 pm
Location: Portugal
Full name: Alvaro Cardoso

Re: evaluating color agnostic terms

Post by Cardoso »

AlvaroBegue wrote:Why would you do this only for odd values of `ply'? You probably want to do it all the time, making sure the sign goes in the direction it has to go.
Hi Álvaro, thanks for your reply.
The reason I do it is because only my engine (at odd plies) cares about this particular feature, the opponent engine (at even plies) isn't supposed to evaluate it. So whatever color my engine plays it will allways be at odd plies, since my root ply starts at 1.
Of course I'm not sure of my logic, that's why I'm asking.

best regards,
Alvaro
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: evaluating color agnostic terms

Post by AlvaroBegue »

You seem to be confused about how the evaluation function is used by a minimax searcher. It might very well be that odd plies are those where it is your engine's turn to move, but in your search you have to evaluate positions with either player to move, and whether you are happier with more queens on the board or not has nothing to do with whose turn it is.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: evaluating color agnostic terms

Post by Sven »

Cardoso wrote:
AlvaroBegue wrote:Why would you do this only for odd values of `ply'? You probably want to do it all the time, making sure the sign goes in the direction it has to go.
Hi Álvaro, thanks for your reply.
The reason I do it is because only my engine (at odd plies) cares about this particular feature, the opponent engine (at even plies) isn't supposed to evaluate it. So whatever color my engine plays it will allways be at odd plies, since my root ply starts at 1.
Of course I'm not sure of my logic, that's why I'm asking.

best regards,
Alvaro
If your engine is to move (odd ply in your case) then "many queens" gets a positive bonus. If the opponent is to move (even ply) then you still want the bonus for "many queens" for your engine but now it must be negative (a penalty) since you have the opponent's viewpoint and eval returns a value relative to the moving side. Your example misses the second part. It is important to have a symmetric evaluation function since anything else will confuse your search.

Whether using this type of evaluation feature is actually useful is another point which is out of scope here :-)

Sven
Cardoso
Posts: 362
Joined: Thu Mar 16, 2006 7:39 pm
Location: Portugal
Full name: Alvaro Cardoso

Re: evaluating color agnostic terms

Post by Cardoso »

Sven Schüle wrote:
Cardoso wrote:
AlvaroBegue wrote:Why would you do this only for odd values of `ply'? You probably want to do it all the time, making sure the sign goes in the direction it has to go.
Hi Álvaro, thanks for your reply.
The reason I do it is because only my engine (at odd plies) cares about this particular feature, the opponent engine (at even plies) isn't supposed to evaluate it. So whatever color my engine plays it will allways be at odd plies, since my root ply starts at 1.
Of course I'm not sure of my logic, that's why I'm asking.

best regards,
Alvaro
If your engine is to move (odd ply in your case) then "many queens" gets a positive bonus. If the opponent is to move (even ply) then you still want the bonus for "many queens" for your engine but now it must be negative (a penalty) since you have the opponent's viewpoint and eval returns a value relative to the moving side. Your example misses the second part. It is important to have a symmetric evaluation function since anything else will confuse your search.

Whether using this type of evaluation feature is actually useful is another point which is out of scope here :-)

Sven
Thanks Sven for the explanation, it really makes sense now.

best regards,
Alvaro
Jose Carlos
Posts: 151
Joined: Wed Mar 08, 2006 10:09 pm
Location: Murcia (Spain)

Re: evaluating color agnostic terms

Post by Jose Carlos »

Cardoso wrote:Hi everybody :),

suppose I got convinced my engine does better in very complex positions (wich it does not)
and I want my engine to prefer positions with many queens for example.
since the root_ply=1, I do the following in the eval:

//some_weight could be 10 for example (pawn=100, queen=1000),
//WQ and BQ are the queens bitboards of their respective color

Code: Select all

if (ply&1) {
   int qc = __popcount(WQ | BQ); 
   score += qc * some_weight; 
} else {
  // do nothing (wich might as well go without the "else" statement
  // this means the opponent don't evaluate/care this feature
}
is the above code correct ?
Will my engine prefer positions with many queens with the above code?

best regards,
Alvaro
If you want complex positions, you might want to try something like:

Code: Select all

  bitboard AttackedPieces = (WhiteAttacks & BlackPieces) | (BlackAttacks & WhitePieces);
  int NumberOfAttackedPieces = popcount(AttackedPieces);

  // assume score from white point of view
  score += some_weight * (programIsWhite ? NumberOfAttackedPieces : -NumberOfAttackedPieces);
__________________________
José Carlos Martínez Galán
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: evaluating color agnostic terms

Post by Don »

Cardoso wrote:Hi everybody :),

suppose I got convinced my engine does better in very complex positions (wich it does not)
and I want my engine to prefer positions with many queens for example.
since the root_ply=1, I do the following in the eval:

//some_weight could be 10 for example (pawn=100, queen=1000),
//WQ and BQ are the queens bitboards of their respective color

Code: Select all

if (ply&1) {
   int qc = __popcount(WQ | BQ); 
   score += qc * some_weight; 
} else {
  // do nothing (wich might as well go without the "else" statement
  // this means the opponent don't evaluate/care this feature
}
is the above code correct ?
Will my engine prefer positions with many queens with the above code?

best regards,
Alvaro
This probably is not how you would want to handle this. Normally you would want to add incentives and bonus only at evaluation time.

So at evaluation time you would want something like this:

int x = pop_count( wq | bq );

score[computers_color] += x * some_weight;

It's neater to keep 2 separate scores, one for white and one for black and then when you return from the evaluation function you sum them - and of course make sure you are expressing the score from the proper side.

Basically this is called asymmetrical evaluation, where you score the computers side differently.

Don
Capital punishment would be more effective as a preventive measure if it were administered prior to the crime.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: evaluating color agnostic terms

Post by bob »

Cardoso wrote:Hi everybody :),

suppose I got convinced my engine does better in very complex positions (wich it does not)
and I want my engine to prefer positions with many queens for example.
since the root_ply=1, I do the following in the eval:

//some_weight could be 10 for example (pawn=100, queen=1000),
//WQ and BQ are the queens bitboards of their respective color

Code: Select all

if (ply&1) {
   int qc = __popcount(WQ | BQ); 
   score += qc * some_weight; 
} else {
  // do nothing (wich might as well go without the "else" statement
  // this means the opponent don't evaluate/care this feature
}
is the above code correct ?
Will my engine prefer positions with many queens with the above code?

best regards,
Alvaro
Since you asked me to look, first an assumption... namely that you want the queens to stay on to lead to more complications? If that is the case, you need an asymmetric evaluation term. The ply&1 doesn't seem like the right way.

In your evaluation, you probably want something like this, using pseudo-code:

q_bonus = 0;
if (program_is_playing_white) {
q_bonus = PopCnt(white_queens) * queen_bonus;
} else
q_bonus = -PopCnt(black_queens) * queen_bonus;

Later:

score += q_bonus;

This assumes you compute score from white POV and then at the end you do something like:

return (wtm) ? score : -score;

If I didn't blow the above, it should add a bonus if your program has one or more queens, anywhere that an evaluation is done.

I wouldn't write the code quite as above, but it is easier to read than trying to condense it using a conditional assignment. The "q_bonus" is relative to the side your program is playing. If white, the bonus is + for each white queen, if black, the bonus is - for each black queen (making the bonus for white POV.