-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimax.py
executable file
·36 lines (29 loc) · 1.07 KB
/
minimax.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Minimax Search (from AIMA textbook)
#file based on recursion on of functions
from utils import *
def minimax_decision(state, game):
"""Given a state in a game, calculate the best move by searching
forward all the way to the terminal states. [Fig. 6.4]"""
player = game.to_move(state)
def max_value(state):
if game.terminal_test(state):
return game.utility(state, player)
#same as min value function except v=-infinity
v = -infinity
for (a, s) in game.successors(state):
v = max(v, min_value(s))
return v
def min_value(state):
#base case
if game.terminal_test(state):
return game.utility(state, player)
#infinity is best that this can do
v = infinity
#return list of a,s
for (a, s) in game.successors(state):
v = min(v, max_value(s))
return v
# Body of minimax_decision starts here:
action, state = argmax(game.successors(state),
lambda ((a, s)): min_value(s))
return action