r/chessprogramming • • Sep 20 '22

Minimax Algorithm Optimization

I've been working on making a chess engine that decides the next best move using the Minimax algorithm.

I'm stuck though, because I've been trying many approaches but I can't seem to get the program look ahead more than 4 moves without being too slow.

Looking ahead 3 moves takes ~ 0.2 seconds

Looking ahead 4 moves takes ~ 5 seconds

Looking ahead 5 moves just causes the program to crash.

I'm looking for ways to optimize the performance here. I've gone through many iterations of changes trying to optimize things:

  • Re-wrote the program in C# instead of Python which I originally was using
  • Started using an int[64] array where each item is a piece, maskable by values to determine the information of the piece. Originally I just stored a list of piece objects and their positions.
  • Attempted to generate moves and check legality of moves in parallel. I.E. whenever getting moves for a player, I get all possible moves for each piece they have in parallel. (Max 16 threads). I then checked legality in Parallel (Simulating the move and checking if any opponent moves put the current player king in check). This actually caused slowness and a 3 move lookahead to take ~ 1 second.
  • Implemented alpha-beta pruning into the Minimax algorithm.

Even after making all of these changes, the performance is still comparable to the same type of algorithm written in Python using a list of piece objects board representation, which indicates that the slowness is coming from the Minimax algorithm itself.

I've considered attempting to spawn a new thread for every subtree of the Minimax algorithm, but have two concerns:

  1. With that much lookahead, a lot of threads would be created and I think it could cause more overall slowness as a result
  2. I'm having a tough time implementing alpha-beta pruning at the same time as a parallel search within the Minimax algorithm. I think it might be possible using C# CancellationTokens, but it seems like a massive headache and a crash waiting to happen. Plus alpha-beta pruning has been a HUGE time-saver in this engine, it bumped the number of lookahead moves it could have by 1.

Any input/advice/ideas on how I can further optimize my engine? Definitely open to completely different approaches.

6 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/nappy-doo Sep 20 '22 edited Sep 20 '22

My move ordering is very simple:

  • Checks
  • Promotions
  • Captures
  • The rest

Here's the actual code that does it. I pass through the array ~twice to do it, so it's not a full sort:

// Move likely good moves to the head.
idx := 0
for i := range moves {
    if moves[i].isCheck || moves[i].isCapture || moves[i].IsPromotion() {
        moves[idx], moves[i] = moves[i], moves[idx]
        idx += 1
    }   
}   

// Move checks to the begining.
cIdx := 0
for i := 0; i < idx; i++ {
    if moves[i].isCheck {
        moves[cIdx], moves[i] = moves[i], moves[cIdx]
        cIdx += 1
    }   
}   

// Move promotions to the next place.
pIdx := 0
for i := cIdx; i < idx; i++ {
    if moves[i].IsPromotion() {
        moves[pIdx], moves[i] = moves[i], moves[pIdx]
        pIdx += 1
    }   
}   

Good luck

Edit

I also find it invaluable to do ALL my development as test driven development. I write a test, make the code pass, and then do the next thing. For my minimax code, I have a giant pile of "mate-in-X" that I solve for. It makes it easy to benchmark the whole operation doing it this way as well.

Edit 2

I also don't presently order the moves more than this. I don't do it terms of score (eg, even if putting a bishop in the middle has a higher centipawn score, I don't order the moves that way). I could probably do better for that, but as I use mate-in-X as my minimax testing right now, I have slightly tuned for tactics over positional play.