【发布时间】:2019-09-19 18:19:39
【问题描述】:
我正在阅读《深度学习和围棋游戏》这本书,但我在书中没有走多远;我编写了基础(规则、辅助类)和 Qt GUI 界面。所有的作品,我决定写极小极大程序的例子,看看我能不能打败它;-) 但它太慢了:下一个动作需要几分钟,初始棋盘为 9x9。默认深度为 3 步,我认为第一步的计算需要 (9x9)x(9x9-1)x(9x9-2)~ 500 000 个位置。好的,它是 python,而不是 C,但我认为这可以在最多一分钟内计算出来。
我删除了一个对 copy.deepcopy() 的调用,这似乎消耗了很多时间。但是速度太慢了。
这里有一些东西: 计算线程:
class BotPlay(QThread):
"""
Thread de calcul du prochain coup par le bot
"""
def __init__(self, bot, bots, game):
"""
constructeur, pour le prochain coup à jouer
:param bot: le bot qui doit jouer
:param bots: l'ensemble des 2
:param game: l'état actuel du jeu (avant le coup à jouer)
"""
QThread.__init__(self)
self.bot = bot
self.bots = bots
self.game = game
played = pyqtSignal(Move, dict, GameState)
def __del__(self):
self.wait()
def run(self):
self.msleep(300)
bot_move = self.bot.select_move(self.game)
self.played.emit(bot_move, self.bots, self.game)
选择移动方法及其类:
class DepthPrunedMinimaxAgent(Agent):
@bot_thinking(associated_name="minimax prof. -> LONG")
def select_move(self, game_state: GameState):
PonderedMove = namedtuple('PonderedMove', 'move outcome')
best_move_so_far = None
for possible_move in game_state.legal_moves():
next_state = game_state.apply_move(possible_move)
our_best_outcome = -1 * self.best_result(next_state, capture_diff)
if best_move_so_far is None or our_best_outcome > best_move_so_far.outcome:
best_move_so_far = PonderedMove(possible_move, our_best_outcome)
return best_move_so_far.move
def best_result(self, game_state: GameState, eval_fn, max_depth: int = 2):
if game_state.is_over():
if game_state.next_player == game_state.winner():
return sys.maxsize
else:
return -sys.maxsize
if max_depth == 0:
return eval_fn(game_state)
best_so_far = -sys.maxsize
for candidate_move in game_state.legal_moves():
next_state = game_state.apply_move(candidate_move)
opponent_best_result = self.best_result(next_state, eval_fn, max_depth - 1)
our_result = -opponent_best_result
if our_result > best_so_far:
best_so_far = our_result
return best_so_far
我几乎可以肯定问题不是来自 GUI,因为本书给出的程序的初始版本完全处于控制台模式,和我的一样慢。
我的要求是什么?好吧,要确定这种缓慢的行为是不正常的,也许是为了知道出了什么问题。 minimax算法来自书本,所以没问题。
谢谢
【问题讨论】:
标签: python algorithm artificial-intelligence