我正在用Python制作一个连四人工智能,并且我正在使用迭代加深和Alpha-Beta剪枝的最小化最大算法。在更深的深度上,它仍然很慢,所以我想实现一个置换表。经过阅读,我认为我明白了一般的想法,但我还没有能够使它完全运作。这是我的代码的一部分(最小化最大算法中的最大化部分):
if(isMaximizing):
maxEval = -99999999999
bestMove = None
# cache.get(hash(board)) Here's where i'd check to see if the hash is already in the table
# if so i searched for the best move that was given to that board before.
# loop through possible moves
for move in [3,2,4,1,5,0,6]:
if moves[move] > -1:
# check if time limit has been reached for iterative deepening
if startTime - time.time() <= -10:
timeout = True
return (maxEval, bestMove, timeout)
if timeout == False:
board = makeMove((moves[move],move), True, board) # make the move
eval = minimax(depth - 1, board, False, alpha, beta, cache, zobTable, startTime, timeout)[0]
if eval > maxEval:
maxEval = eval
bestMove = (moves[move]+1,move)
board[moves[move] + 1][move] = '_' # undo the move on the board
moves[move] = moves[move] + 1 # undo the move in the list of legal moves
alpha = max(alpha, maxEval)
if alpha >= beta:
break
# cache.set(hash(board), (eval, value)) Here's where i would set the value and bestmove for the current boardstate
return (maxEval, bestMove, timeout)
我现在正在使用Zobrist哈希方法来进行棋盘的哈希,并使用有序字典将哈希棋盘添加到其中。对于这个哈希键,我已经添加了棋盘值和该棋盘的最佳移动值。不幸的是,这似乎导致算法选择了不好的移动(之前它有用)。请问有人知道应该将棋盘状态放在缓存中的哪里,以及从缓存中获取它们的位置吗?