问题
I tried to programme a minimax algorithm in python. But it is so confusing. I am new to recursion functions. My mind structure has some error at somewhere but I could not solve it. My minimax tree returns with '-100' that must be 100 to achieve true answer. If anything is missing or not clear, please just let me know. Thank you
def startposition():
return 2, 'max'
def terminalstate(state):
if state == (0, 'min') or state == (0, 'max'):
return True
else:
return False
def minimax(state):
if terminalstate(state):
return utilitystatic(state)
else:
if state[1] == 'min':
value = -250
for x in successorsgenerator(state):
value = max(value, minimax(x))
elif state[1] == 'max':
value = 250
for x in successorsgenerator(state):
value = min(value, minimax(x))
return value
def utilitystatic(state):
assert terminalstate(state)
if state[1] == 'max':
return -100
elif state[1] == 'min':
return 100
assert False
def successorsgenerator(state):
successors = []
state = toggle(state)
newstate = decrease(state)
i = 0
while newstate[0] >= 0 and i < 3:
successors.append(newstate)
i += 1
newstate = decrease(newstate)
print('successors:', successors)
return successors
def toggle(state):
state = list(state)
state[1] = 'min' if state[1] == 'max' else 'max'
state = tuple(state)
return state
def decrease(state):
state = state[:0] + (state[0] - 1,) + state[1:2]
return state
stick = startposition()
exit = minimax(stick)
print('last result', exit)
回答1:
The code is correct from the perspective of the max player, if the min player goes first. The way minimax works, the min layer should return the minimum of all its possible states (because the min player is also optimizing their move). So you shouldn't switch your min and max calls, but rather which player goes first.
Here is your state tree visualized: https://imgur.com/a/0iRFc.jpg (I apparently don't have enough rep to display the image). The top layer of your recursion will take
max(-250, -100)
and return -100. Because the game starts with the max player finishing his move with 2 in the stack, this makes sense. If you want to switch the return value to 100, you need to change the game such that the max player goes first (because in this game scenario, whoever goes first wins).
回答2:
I solved my problem. I needed to change value = min(value, minimax(x)) to value = max(value, minimax(x)) and 250 to -250. Problem solved.
来源:https://stackoverflow.com/questions/48957432/how-can-i-programme-a-minimax-algorithm-for-nim-game-python