Python 3.x 一个perft函数搜索一个3节点深的国际象棋位置需要多长时间?
我制作了一个perft函数来测试我的象棋引擎的速度,该函数需要205秒(>2分钟)来深入3层并评估所有的叶子(~42000片叶子,因为35^3=42000)。这种缓慢应该发生吗?它只有42000个节点,这是非常少的。我希望有国际象棋引擎经验的人能帮我找出问题所在。国际象棋的性能结果: perft函数的运行根本不涉及使用minimax或alpha-beta修剪,因此不会是那些导致缓慢的操作 我的性能函数:Python 3.x 一个perft函数搜索一个3节点深的国际象棋位置需要多长时间?,python-3.x,performance,chess,minimax,alpha-beta-pruning,Python 3.x,Performance,Chess,Minimax,Alpha Beta Pruning,我制作了一个perft函数来测试我的象棋引擎的速度,该函数需要205秒(>2分钟)来深入3层并评估所有的叶子(~42000片叶子,因为35^3=42000)。这种缓慢应该发生吗?它只有42000个节点,这是非常少的。我希望有国际象棋引擎经验的人能帮我找出问题所在。国际象棋的性能结果: perft函数的运行根本不涉及使用minimax或alpha-beta修剪,因此不会是那些导致缓慢的操作 我的性能函数: a = GameState() def perft(game_state, nodes,
a = GameState()
def perft(game_state, nodes, counter):
if nodes == 0:
return 1
internal_count = 0
if game_state.player_turn == 'w':
for piece in game_state.w_pieces:
# I call .copy() after game_state because I don't want the set holding the pieces in
# game_state to change when I call all_possible_moves() on it. .copy() in GameState
# calls deepcopy on the internal sets and dicts in game_state. Every piece in the
# game_state set has a method called .all_possible_moves(GameState) that
# generates all the possible moves for that piece.
for move in piece.all_possible_moves(game_state.copy()):
counter += perft(game_state.copy().make_move(piece.coor, move), nodes - 1, 0)
else:
for piece in game_state.b_pieces:
for move in piece.all_possible_moves(game_state.copy()):
counter += perft(game_state.copy().make_move(piece.coor, move), nodes - 1, 0)
return counter
counter = perft(a, 3, 0)
print(counter)
我的perft函数不应该有任何明显的错误,对吗?我不知道为什么会跑这么长时间。我期望在一秒钟内找到42000个节点,但正如您在这里看到的,这花费了2分钟以上:
对于那些想看我的完整代码的人,这里是:我投票结束这个问题,因为它属于205秒太多了,我同意。我们没有所有相关的代码可以查看,但在国际象棋中,使用UndoMove()方法(即存储在Move()中更改的内容以便可以撤消)通常比为每个探测器复制游戏状态更快。