Code: Select all
static void eval_loose(const Board *B, eval_t *e)
// detect loose pieces and attribute small bonuses for attacking them
{
assert(B->initialized && e);
for (unsigned color = White; color <= Black; color++) {
const unsigned us = color, them = opp_color(us);
// loose pawns = undefended pawns
uint64_t loose_pawns = B->b[them][Pawn] & ~B->st->attacks[them][NoPiece];
// loose pieces = pieces either attacked by a pawn or not defended by a pawn
uint64_t enemy_pieces = B->all[them] & ~B->b[them][Pawn];
uint64_t loose_pieces = enemy_pieces & (~B->st->attacks[them][Pawn] | B->st->attacks[us][Pawn]);
// hanging = loose and attacked
uint64_t hanging = (loose_pawns | loose_pieces) & B->st->attacks[us][NoPiece];
// scoring (a bit simplistic at the moment, at least it's fast)
while (hanging) {
unsigned sq = next_bit(&hanging), victim = B->piece_on[sq];
e->op[us] += 5 + Material[Opening][victim]/32;
e->eg[us] += 10 + Material[EndGame][victim]/32;
}
}
}
So what I'm doing is basically:
1/ loose pawn = pawn that is not defended
2/ loose piece = piece that is not defended by a pawn, or piece that is attacked by a pawn (regardless of whether it's defended or not).
3/ hanging = loose + attacked
And the scoring function is very simplistic. There is a base score and a linear part proportional to the hanging piece (victim) value.
This approach seems rather stupid when I think about it, but in self-testing it scored almost +48 elo (1000 games) to my surprise. I suppose this kind of feature will never give a huge elo increase anyway, so a compromise has to be found between slowing down the eval and doing something intelligent but complex, and having an over simplistic fast code.
I wonder how others have done this, what they've tried, and with what kind of results.
Intuitively, my thought of hanging pieces is the following:
1/ the quiescent search will grab hanging pieces if it can. but it will choose a stand pat score when the side to move has pieces hanging. so if your opponent has a hanging piece and the quiescent search doesn't chose to take it, there's generally a good reason for it (losing SEE, checks involved, or other complications such as discovering new attacks etc.). However, even if the opponent hanging piece is SEE defended, or defended by other considerations (discovering other attacks etc.) it also means that in a way some opponent pieces are not free of movement and are tied to maintain these defensive tactics
2/ when you have only one hanging piece, it shouldn't be penalized too strongly, and the penalty should typically be the cost of a tempo (unless the piece is actually trapped and a tempo won't save it, but this would be hard to code in the eval in an efficient way).
3/ when you have several hanging pieces, there is no 100% rule nor even a 90% rule, but there are some significant chances that you'll end up losing one. Of course tactical complications such as forks, checks, passed pawn pushes can allow you to leave hanging pieces, so it's hard to know in the eval whether you're going to lose a piece or not.
4/ when you have many hanging pieces, the chances of being able to defend all of them by means of tactical threats becomes smaller and smaller. Perhaps I should start increasing the hanging piece penalty with the number of hanging pieces ?
Anyway, thoughts, suggestions welcome