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;
}