-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasic_minimax_agent.py
73 lines (58 loc) · 2.44 KB
/
basic_minimax_agent.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from agent import Agent
import random
class BasicMinimaxAgent(Agent):
"""
A minimax agent.
"""
def __init__(self, index=0, depth = 1):
self.index = index
self.keys = []
#added a depth and a evaluation function.
self.depth = int(depth)
def getAction(self, state):
def recurse(state, depth, agentIndex, alpha, beta):
choices = []
actions = state.getLegalActions()
nextAgentIndex = (agentIndex + 1) % state.numPlayers
if state.isEnd():
return state.Utility()
if depth == 0:
return self.evaluationFunction(state)
# if is the other agent, tries to minimize
if agentIndex == self.index:
for action in actions:
choices.append(recurse(state.getSuccessor(action), depth, nextAgentIndex, alpha, beta))
if alpha >= beta:
break
if min(choices) > alpha:
alpha = min(choices)
return max(choices)
elif agentIndex != state.numPlayers-1:
for action in actions:
choices.append(recurse(state.getSuccessor(action), depth, nextAgentIndex, alpha, beta))
if alpha >= beta:
break
if max(choices) < beta:
beta = max(choices)
return min(choices)
else:
for action in actions:
choices.append(recurse(state.getSuccessor(action), depth-1, nextAgentIndex, alpha, beta))
if alpha >= beta:
break
if max(choices) < beta:
beta = max(choices)
return min(choices)
actions = state.getLegalActions()
values = [recurse(state.getSuccessor(action), self.depth, (self.index+1)%2,float('-inf'), float('+inf')) \
for action in actions]
value = max(values)
#chose a random action from one of the bests.
bestIndices = [index for index in range(len(values)) if values[index] == value]
chosenIndex = random.choice(bestIndices)
best = actions[chosenIndex]
return best
def evaluationFunction(self, currentState):
numberOfCards = currentState.getHandSize()
optimalScore = -numberOfCards
return optimalScore