0

I'm working on a chess engine and have implemented Negamax with alpha beta pruning. I have noticed that fewer nodes are being searched (depth 5 in the starting position goes from 4,865,609 to 701,028 nodes searched), but I am not sure if it is pruning as much as it should. Is there a way I can test the validity of my pruning?

I have seen that other peoples implementation of alpha beta pruning with no move ordering searches around 30,000 nodes at depth 5 on the starting position. Even when I implement MVV-LVA move ordering, I'm still searching 454,727 nodes.

const INITIAL_ALPHA: i32 = std::i32::MIN + 1;
const INITIAL_BETA: i32 = std::i32::MAX - 1;

pub fn best_move_negamax_ab(&self, board: &Board, depth: u8) -> (i32, Option<Move>) {
    let mut moves = self.move_gen.generate_moves(board);
    let mut best_move = None;
    let mut best_score = std::i32::MIN + 1;

    mvv_lva_sort_moves(board, &mut moves);

    for mv in moves {
        let new_board = board.clone_with_move(&mv);
        let score = -self.negamax_alpha_beta(&new_board, INITIAL_ALPHA, INITIAL_BETA, depth - 1);
        if score > best_score {
            best_move = Some(mv);
            best_score = score;
        }
    }

    (best_score, best_move)
}

fn negamax_alpha_beta(&self, board: &Board, alpha: i32, beta: i32, depth: u8) -> i32 {
    if depth == 0 {
        return evaluate(board) as i32;
    }

    let mut moves = self.move_gen.generate_moves(board);
    let mut alpha = alpha;

    mvv_lva_sort_moves(board, &mut moves);

    for mv in moves {
        let new_board = board.clone_with_move(&mv);
        let score = -self.negamax_alpha_beta(&new_board, -beta, -alpha, depth - 1);
        if score >= beta {
            return beta;
        }
        if score > alpha {
            alpha = score;
        }
    }
    return alpha;
}
Gwoodz
  • 23
  • 4
  • 1
    Try looking at the [Stockfish's docs](https://github.com/official-stockfish/Stockfish/wiki) I know you can get the total number of moves from a position, maybe there is a way to get the pruned tree as well – IcyTv Jun 03 '23 at 22:44

0 Answers0