MinMax 函数在深度 = 3 时崩溃
MinMax function crashes on depth = 3
我正在通过使用卷积神经网络评估特定的棋盘状态来编写国际象棋 AI,然后我使用该评估来应用 min_max 算法来获得 AI 的移动。当我的算法的深度超过 2 时,我收到有关将元组与标量进行比较的错误。
def NN_evaluate(board):
board3d = split_dims(board)
board3d = np.expand_dims(board3d, 0)
return model.predict(board3d)[0][0]
def minimax(board, depth, alpha, beta, maximizing_player):
if depth == 0 or board.is_game_over():
return NN_evaluate(board)
moves = board.legal_moves
if maximizing_player:
max_eval = -np.Inf
for move in moves:
board.push(move)
current_eval = minimax(board, depth-1, alpha, beta, False)
board.pop()
max_eval = max(max_eval, current_eval)
best_move = move
alpha = max(alpha, current_eval)
if beta <= alpha:
break
return max_eval
else:
min_eval = np.Inf
for move in moves:
board.push(move)
current_eval = minimax(board, depth-1, alpha, beta, True)
board.pop()
min_eval = min(min_eval, current_eval)
best_move = move
beta = min(beta, current_eval)
if beta <= alpha:
break
return min_eval
def get_ai_move(board, depth, maximizing_player):
max_move = None
max_eval = -np.inf
for move in board.legal_moves:
board.push(move)
current_eval = minimax(board, depth-1, -np.inf, np.inf, False)
board.pop()
if current_eval > max_eval:
max_eval = current_eval
max_move = move
return max_move
board = chess.Board()
with chess.engine.SimpleEngine.popen_uci('C:\Users\coope\Downloads\Python\Machine Learning\Chess AI\stockfish_15_win_x64_avx2\stockfish_15_x64_avx2.exe') as engine:
while True:
move = get_ai_move(board, 3, True)
board.push(move)
print(f'\n{board}')
if board.is_game_over():
break
move = engine.analyse(board, chess.engine.Limit(time=1), info=chess.engine.INFO_PV)['pv'][0]
board.push(move)
print(f'\n{board}')
if board.is_game_over():
break
错误如下
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_1108/561116885.py in <module>
3 with chess.engine.SimpleEngine.popen_uci('C:\Users\coope\Downloads\Python\Machine Learning\Chess AI\stockfish_15_win_x64_avx2\stockfish_15_x64_avx2.exe') as engine:
4 while True:
----> 5 move = get_ai_move(board, 3, True)
6 board.push(move)
7 print(f'\n{board}')
~\AppData\Local\Temp/ipykernel_1108/3382320008.py in get_ai_move(board, depth, maximizing_player)
40 for move in board.legal_moves:
41 board.push(move)
---> 42 current_eval = minimax(board, depth-1, -np.inf, np.inf, False)
43 board.pop()
44 if current_eval > max_eval:
~\AppData\Local\Temp/ipykernel_1108/3382320008.py in minimax(board, depth, alpha, beta, maximizing_player)
28 current_eval = minimax(board, depth-1, alpha, beta, True)
29 board.pop()
---> 30 min_eval = min(min_eval, current_eval)
31 best_move = move
32 beta = min(beta, current_eval)
TypeError: '>' not supported between instances of 'float' and 'NoneType'
这似乎是我的 min(min_eval, current_eval)
的问题,但我不确定如果 get_ai_move(board, 2, True)
不崩溃,如何解决它。
你的
return max_eval
在for循环里面,应该在外面。像这样写存在不执行的情况,因此整个函数returns None
(默认return value in python if you don't return 函数中的任何内容)
我正在通过使用卷积神经网络评估特定的棋盘状态来编写国际象棋 AI,然后我使用该评估来应用 min_max 算法来获得 AI 的移动。当我的算法的深度超过 2 时,我收到有关将元组与标量进行比较的错误。
def NN_evaluate(board):
board3d = split_dims(board)
board3d = np.expand_dims(board3d, 0)
return model.predict(board3d)[0][0]
def minimax(board, depth, alpha, beta, maximizing_player):
if depth == 0 or board.is_game_over():
return NN_evaluate(board)
moves = board.legal_moves
if maximizing_player:
max_eval = -np.Inf
for move in moves:
board.push(move)
current_eval = minimax(board, depth-1, alpha, beta, False)
board.pop()
max_eval = max(max_eval, current_eval)
best_move = move
alpha = max(alpha, current_eval)
if beta <= alpha:
break
return max_eval
else:
min_eval = np.Inf
for move in moves:
board.push(move)
current_eval = minimax(board, depth-1, alpha, beta, True)
board.pop()
min_eval = min(min_eval, current_eval)
best_move = move
beta = min(beta, current_eval)
if beta <= alpha:
break
return min_eval
def get_ai_move(board, depth, maximizing_player):
max_move = None
max_eval = -np.inf
for move in board.legal_moves:
board.push(move)
current_eval = minimax(board, depth-1, -np.inf, np.inf, False)
board.pop()
if current_eval > max_eval:
max_eval = current_eval
max_move = move
return max_move
board = chess.Board()
with chess.engine.SimpleEngine.popen_uci('C:\Users\coope\Downloads\Python\Machine Learning\Chess AI\stockfish_15_win_x64_avx2\stockfish_15_x64_avx2.exe') as engine:
while True:
move = get_ai_move(board, 3, True)
board.push(move)
print(f'\n{board}')
if board.is_game_over():
break
move = engine.analyse(board, chess.engine.Limit(time=1), info=chess.engine.INFO_PV)['pv'][0]
board.push(move)
print(f'\n{board}')
if board.is_game_over():
break
错误如下
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_1108/561116885.py in <module>
3 with chess.engine.SimpleEngine.popen_uci('C:\Users\coope\Downloads\Python\Machine Learning\Chess AI\stockfish_15_win_x64_avx2\stockfish_15_x64_avx2.exe') as engine:
4 while True:
----> 5 move = get_ai_move(board, 3, True)
6 board.push(move)
7 print(f'\n{board}')
~\AppData\Local\Temp/ipykernel_1108/3382320008.py in get_ai_move(board, depth, maximizing_player)
40 for move in board.legal_moves:
41 board.push(move)
---> 42 current_eval = minimax(board, depth-1, -np.inf, np.inf, False)
43 board.pop()
44 if current_eval > max_eval:
~\AppData\Local\Temp/ipykernel_1108/3382320008.py in minimax(board, depth, alpha, beta, maximizing_player)
28 current_eval = minimax(board, depth-1, alpha, beta, True)
29 board.pop()
---> 30 min_eval = min(min_eval, current_eval)
31 best_move = move
32 beta = min(beta, current_eval)
TypeError: '>' not supported between instances of 'float' and 'NoneType'
这似乎是我的 min(min_eval, current_eval)
的问题,但我不确定如果 get_ai_move(board, 2, True)
不崩溃,如何解决它。
你的
return max_eval
在for循环里面,应该在外面。像这样写存在不执行的情况,因此整个函数returns None
(默认return value in python if you don't return 函数中的任何内容)