-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpruning_agent.py
83 lines (72 loc) · 3.14 KB
/
pruning_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
71
72
73
74
75
76
77
78
79
80
from agent import Agent
import random
import util
class PruningAgent(Agent):
"""
A mix of RL and pruning agent.
"""
def __init__(self, index=0, depth = 1):
self.index = index
self.numActionsToPick = 5
self.keys = []
#added a depth and a evaluation function.
self.depth = int(depth)
def getAction(self, state):
def recurse(state, depth, agentIndex, alpha, beta):
assert state.player == agentIndex
choices = []
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:
actions = state.getLegalActions()
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:
actions = self.maximizeProbActions(state, self.numActionsToPick)
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:
actions = self.maximizeProbActions(state, self.numActionsToPick)
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 maximizeProbActions(self,state,N):
actions = state.getLegalActions()
actionProbs = []
for a in actions:
actionProbs.append(util.getLearnedTransProbabilities(state,a))
prioList=[x for _, x in sorted(zip(actionProbs, actions),
key=lambda pair: -pair[0])]
return prioList[:N]
def evaluationFunction(self, currentState):
features = util.stateFeatureExtractor(currentState)
weights = util.loadWeights('rl_weights.txt')
optimalScore = util.dot(weights,features)
return optimalScore