python - Minmax 改变了当前状态,我不知道如何避免这个问题
问题描述
我已经实现了 Dynamic Connect 的修改版本,但我无法解决我面临的问题,显然 minimax 在搜索树时会改变当前状态。minimax 返回正确的移动,但它没有正确应用它。
def humanPolicy(game, state, player=-1):
instructions= input('Input action:')
action = game.instructions_to_actions(state, instructions, player)
return actin
def minimax(game, st, depth, player):
def recurse(st, depth):
if game.isEnd(state, player) or depth == 0:
return (game.utility(st, player), 0)
choices = [(recurse(game.action_succ(st, action, player), depth - 1)[0], action) for action in game.actions(st, player)]
if player ==1:
return (max(choices))
elif player == -1:
return min(choices)
value, action = recurse(st, depth)
print('minimax says action={}, value = {}'.format(action, value))
return action
# stt = {}
w_list = [(5, 1), (1, 3), (1, 4), (7, 4), (7, 5), (3, 7)]
b_list = [(4, 1), (3, 1), (7, 2), (1, 6), (5, 7), (6, 7)]
policies = {1: humanPolicy, -1: minimax}
game = DynamicConnect(w_list, b_list)
state = game.InitialState()
st = state
player = 1
depth = 3
while True:
board = [['.' for i in range(7)] for j in range(7)]
if player == 1:
print('Player: White: O')
else:
print('Player: Black: X')
print(state)
for (x, y) in state[0]:
board[y-1][x-1] = 'O'
for (x, y) in state[1]:
board[y-1][x-1] = 'X'
print(*(' '.join(row) for row in board), sep='\n')
print('='*10, state)
# policy = policies[player]
if player == 1:
action = humanPolicy(game, state, player)
else:
action = minimax(game, st,depth, player)
print(action)
state = game.action_succ(st, action, player)
st = state
player = -player
if game.isEnd(state, player):
print('Game over')
print('utility = {}'.format(game.utility(state, player)))
print(state)
这是一个动作后继函数,它返回一个新的状态,但是当它接收到来自极小最大值的动作时,它不能正常工作。
def action_succ(self, state, action, player):
initial_pos = action[0]
new_pos = action[1]
if player == -1:
if initial_pos in state[1]:
state[1][state[1].index(initial_pos)] = new_pos
else:
if initial_pos in state[0]:
state[0][state[0].index(initial_pos)] = new_pos
return (state)
解决方案
推荐阅读
- scala - 将 Dstream rdd 转换为数据帧时出现 Nullpointer 异常
- python - 使用嵌套循环访问字典 - Python
- json - 重建字典列表
- java - 强制更新已安装的安卓应用
- cookies - 通过 websocket 从客户端发送 httpOnly cookie
- r - 如何使用 R plotly 在条形图中的条形末尾(而不是居中)放置刻度线和轴标签(或刻度文本)?
- c# - Entity Framework Core 数据库-优先选择特定列
- gremlin - Gremlin - AWS Neptune - 当按顶点上不存在的属性排序时发出问题
- python - 我是否需要包含 Python PDF Toolkit 或 pypdftk 作为 Lambda 层?
- r - 在模型中交互时,使用“caret::train()”构建的线性模型上的“predict.lm()”出错