pythonartificial-intelligencechessiterative-deepening

Iterative deepening and move ordering


As I understand, when implementing iterative deepening the best move at one depth should be used for ordering moves at higher depths. I have one issue with this: say I got the move m as my best move at the depth n, then when searching at the depth n + 1 should the move orderer only prioritize m at the highest level of search or at every level where move m is legal?

My current implementation of iterative deepening:

Search:

pvLine = None
for depth in range(1, self.maxDepth):
    self.auxSearch(board, depth, initalHash)
    
    # find the principal variation from the TT
    pvLine = self.getPVLine(board, initalHash)

    bestMove = pvLine[0][0] 
    bestValue = pvLine[0][1]

    self.ordering.setBestMove(bestMove, depth + 1)
    print(f'{depth=} | {bestValue=} | {bestMove=} | {pvLine=}')

return pvLine 

Move ordering:

if((move, depth) == self.bestMove):
    priority += self.BESTMOVE_BONUS

setBestMove function:

def setBestMove(self, move: chess.Move, depth: int) -> None:
    self.bestMove = (move, depth)

self.BESTMOVE_BONUS is a very big number, so the move will have the highest priority.

Currently, I am making sure that the move orderer only prioritizes the best move from previous shallower search at the highest level of the current search. I am not sure if my approach is correct or not?


Solution

  • Move ordering will give you a much faster algorithm than without and is fairly easy to implement. You can read more about it here: https://www.chessprogramming.org/Move_Ordering.

    I suggest you do as of now and put the best move from previous iteration first. The best move (or the best move sequence, "principal variation") is always the move from previous depths. So if you get a sequence of moves a1, b1, and c1 from depth 3, then at depth 4 you will try a1 at depth 1, b1 at depth 2, and c1 at depth 3 first.

    Second you should try good capture moves, often found with MVV-LVA. Capturing a queen with a pawn is usually a good move, but the other way around could be bad if the pawn is protected.

    Other easy to implement techniques are Killer moves and History moves, also found in the link above.