0

I've recently been improving my Negamax algorithm for chess, adding a transposition table. The algorithm was working well before this addition. After this addition I can't see no big improvements in speed, and sometimes I even see some regressions. I tested it with a depth of 5 and 6.

Is there any error in my code? (P.S.: tell me if the question is not clear or incomplete, I will edit it) Thank you!

Here's the code, the new pieces are the commented lines at the top and at the bottom:

private int negaMax(Board board, Move[] legalMoves, int depth, int turnMultiplier, int alpha, int beta) {

    //Search in transposition table
    /*int initialAlpha = alpha;
    int transpositionDepth = 0;
    String transpositionFlag = "";
    int transpositionEvaluation = 0;
    String fenString = board.GetFenString().Split(" ")[0];
    if (transpositionTable.ContainsKey(fenString)) {
        transpositionDepth = Int32.Parse(transpositionTable[fenString].Split(" ")[0]);
        transpositionFlag = transpositionTable[fenString].Split(" ")[1];
        transpositionEvaluation = Int32.Parse(transpositionTable[fenString].Split(" ")[2]);

        if(transpositionDepth >= depth) {
            if(transpositionFlag.Equals("Exact")) { return transpositionEvaluation; }
            else if (transpositionFlag.Equals("Lowerbound")) { alpha = Math.Max(alpha, transpositionEvaluation); }
            else if (transpositionFlag.Equals("Upperbound")) { beta = Math.Min(beta, transpositionEvaluation); }

            if(alpha >= beta) { return transpositionEvaluation; }

        }
    }*/

    //Base case
    if (depth == 0) { return evaluatePosition(board) * turnMultiplier; }

    //Recursion
    int bestEvaluation = -infinity;

    foreach(Move move in board.GetLegalMoves()) {

        board.MakeMove(move);
        legalMoves = board.GetLegalMoves();
        orderMoves(legalMoves);
        int evaluation = -negaMax(board, legalMoves, depth - 1, -turnMultiplier, -beta, -alpha);

        if(evaluation > bestEvaluation) {
            bestEvaluation = evaluation;
            if (depth == searchDepth) { bestMove = move; }
        }

        board.UndoMove(move);

        //Pruning
        alpha = Math.Max(alpha, bestEvaluation);
        if(alpha >= beta) {
            break;
        }
    }

    //Transposition table update
    /*transpositionEvaluation = bestEvaluation;
    if (bestEvaluation <= initialAlpha) { transpositionFlag = "Upperbound"; }
    else if (bestEvaluation >= beta) { transpositionFlag = "Lowerbound"; }
    else { transpositionFlag = "Exact"; }
    transpositionDepth = depth;
    if (!transpositionTable.ContainsKey(fenString)) {
        transpositionTable.Add(fenString, transpositionDepth + " " + transpositionFlag + " " + transpositionEvaluation);
    }*/

    return bestEvaluation;
}
SKAE
  • 83
  • 1
  • 10
  • There's some obvious stuff like doing a bunch of string operations and your TT being some kind of dictionary would make it incredibly slow. Updating your alpha/beta values with the valus retrieved from TT doesn't seem safe either. If you have checkmate scores that are calculated based on the distance from root position, those need to be handled specifically in your TT as well. TT is notoriously tricky to get correct, i suggest checking out the chess programming wiki and specifically looking through the forum discussions linked there, many basic questions are already answered there. – Jane Doe Jul 26 '23 at 11:38
  • Take a look here : https://web.archive.org/web/20071031100051/http://www.brucemo.com/compchess/programming/hashing.htm – LittleCoder Aug 25 '23 at 10:26

0 Answers0