私は Python で Connect 4 AI を作成しています。これには、反復深化とアルファ ベータ プルーニングを備えたミニマックスを使用しています。それ以上の深さではまだかなり遅いので、転置テーブルを実装したいと思いました。それを読んだ後、私は一般的なアイデアを得ると思いますが、それをうまく機能させることができませんでした. これが私のコードの一部です:(ミニマックスの最大化部分):
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 ハッシュ法でハッシュしており、順序付けられた dict を使用してハッシュされたボードを追加しています。このハッシュキーに、ボードの値とそのボードの bestMove を追加しました。残念ながら、これによりアルゴリズムが悪い動きを選択するようです (以前は機能していました)。ボードステートをキャッシュのどこに配置し、キャッシュからどこに取得する必要があるか知っている人はいますか?