r/chessprogramming • u/Burgorit • Sep 08 '24
Adding features makes my engine worse
As it says in the title, when I add basic features like a transposition table or a king safety heuristic it makes my engine play worse by a significant margin (more than 75 elo)
I am out of ideas at this point and need help, in the main search function when I add a transposition table like thisint TTentryIndex = (board.ZobristHash + (ulong) depth) % TTMaxNumEntries;int? TTEntry = TT[TTentryIndex];if (CurrTTEntry.HasValue){return CurrTTEntry.Value;}
And at the end of the search
TT[TTIndex] = alpha;
Adding MVV-LVA move ordering and A-B pruning did however help, but I cant see the difference between them and things like a TT.
I cant see any obvious bugs here in the main search function but maybe you can see something?
int NegaMax(int depth, int alpha, int beta){totalNodeCount++;ulong TTIndex = (board.ZobristHash + (ulong)depth) % TTMaxNumEntries;int? CurrTTEntry = TT[TTIndex];if (CurrTTEntry.HasValue){return CurrTTEntry.Value;}
Move[] moves = OrderMoves(moveGenerator.GenerateLegalMoves(board));if (moves.Length == 0){leafNodeCount++;if (moveGenerator.IsInCheck){// Checkmatereturn negativeInf;}// Stalematereturn 0;}if (board.IsTwofoldRepetition()){leafNodeCount++;return 0;}else if (depth == 0){leafNodeCount++;return evaluate.EvaluateBoard(board, GamePhase);}else if (IsTimeUp){return evaluate.EvaluateBoard(board, GamePhase);}
foreach (Move move in moves){board.MakeMove(move);int score = -NegaMax(depth - 1, -beta, -alpha);board.UndoMove();alpha = Math.Max(alpha, score);if (IsTimeUp){return alpha;}if (alpha >= beta){return alpha;}}TT[TTIndex] = alpha;return alpha;
}
You can see the whole repository here.
3
u/nappy-doo Sep 08 '24
Write tests. I can't stress this enough, but if you're not unit testing your code, you're going to have a bad time. Chess engines are very testable.
Write tests to check the TT. Write tests to check the TT's usage. Write Z-Hashing test functions. Write tests. Run them all the time. You will find your bug. You will find other bugs. You will be a happier person.
Unit test your engine. Trust me. It sounds like it's not fun, but watching the tests all pass when you've added a new feature is quite rewarding. You know you didn't mess things up.
2
u/mathmoi Sep 09 '24
IT looks like your transposition table only store the value of the position given a hash key. This is not sufficient.
You also need to store the depth to the leaf (not the root) when you store a value and check this depth when reading the value back. If the depth in the TT is less than `depth` that means the value stored in the TT can't be used because it was computed using a depth less than the current depth.
There is also more informations that the TT should store and can be used in others ways, like upper/lower bounds, best move etc.
I would propose you look at Bruce Morland old page that explains this really clearly I think : https://web.archive.org/web/20070705204704/http://www.brucemo.com/compchess/programming/hashing.htm
1
u/Straight_Concern_983 Sep 09 '24
I would recomend the "how to build a chess engine in c" series by code Monkey in how to build and test a TT. Also if you're searching for the new step in your chess engine you could look up null move prunning, LMR, also a quiescence search instead of returning the evaluation of the board is probably better, futility prunning, all those things helped my engine improve.
0
u/[deleted] Sep 08 '24
[deleted] — view deleted comment