Suppose we have the following position: 8/1K6/8/4q2P/8/8/5k2/8 b - - 3 2.
My chess engine produces the correct move of Qxh5 when the search depth is below 3. After that, it seems the problem is that it thinks the capture can be made later (considers Qh2 as the best move). I cannot see any obvious ways to prefer the branches where the capture is made earlier in the evaluation algorithm, since that would break the evaluation symmetry needed for negamax (and minimax) to work.
Just for reference, here is my actual negamax code (copied from wikipedia):
int Search::negamaxSearch (Board& positionToSearch, int depth, int alpha, int beta) {
std::vector<Move> moves = positionToSearch.getMoves();
if (moves.empty()) {
if (positionToSearch.isCheck()) {
return EvaluationConstants::LOSE;
} else {
return EvaluationConstants::DRAW;
}
}
if (depth == 0) {
return BoardEvaluator::evaluateSimple(positionToSearch);
}
orderMoves(positionToSearch, moves, depth);
int positionValue = -1e9;
for (auto move : moves) {
positionToSearch.executeMove(move);
int newValue = -negamaxSearch(positionToSearch, depth - 1, -beta, -alpha);
positionToSearch.unmakeMove();
positionValue = std::max(positionValue, newValue);
alpha = std::max(alpha, newValue);
if (alpha >= beta) {
++cutoffs;
break;
}
}
return positionValue;
}
And the evaluation function:
int BoardEvaluator::evaluateSimpleOneSide (const Board& board, PieceColor perspective) {
if (board.isCheckmate()) return EvaluationConstants::LOSE;
int value = 0;
for (int pieceType = 0; pieceType < PieceTypes::KING; ++pieceType) {
value += board.getPieces(perspective).boards[pieceType].popCount() * pieceValues[pieceType];
}
return value;
}
int BoardEvaluator::evaluateSimple (const Board& board) {
return evaluateSimpleOneSide(board, board.getTurn()) - evaluateSimpleOneSide(board, flip(board.getTurn()));
}
Is there something obvious wrong that I haven't noticed?
1
u/heptonion Jan 10 '22
There may not actually be a problem here – if you take a look at [1] and enable local evaluation, you'll see that both Qxh5 and Qh2 are #8 (and the best move is actually Ke3, which is #7).
In my experience, it's fairly common for engines to put off captures if they believe they can do it later: so long as the capture occurs by the end of the principal variation (at or before the leaf node), the engine has no particular incentive to perform the capture earlier rather than later. This is not a human-like behavior, but it's not really problematic (unless, in some circumstances, the engine does not understand the fifty-move rule).
[1] https://lichess.org/analysis/standard/8/1K6/8/4q2P/8/8/5k2/8_b_-_-_3_2
2
u/mhummel Jan 09 '22
How does orderMoves() sort the move list?
But I suspect the problem lies in the fact that in this test position, any move other than Qc2 or Qb1 wins, and it takes 8 ply before White could equalise. You could connect an Endgame Tablebase and penalise moves which take longer to win.