forked from wboag/Obstacles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolicyIterationAgent.py
More file actions
133 lines (95 loc) · 3.7 KB
/
policyIterationAgent.py
File metadata and controls
133 lines (95 loc) · 3.7 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#
# Policy Iteration
#
# Purpose: Solve MDP using value iteration
#
# Author: Willie Boag
#
from collections import defaultdict
import random
class PolicyIterationAgent:
def __init__(self, mdp, discount=0.5, iterations=20):
# parameters
self.discount = discount
# MDP
self.mdp = mdp
# Initial policy is random
values = defaultdict(lambda:0)
policy = { state:random.choice(self.mdp.getPossibleActions(state)) for state in self.mdp.getStates() }
# Converged early?
streak = 0
for _ in range(iterations):
#print '\tit: ', _
# step one: policy evaluation
newValues = self.policyEvaluation(policy, values, iterations=50)
# step two one-step lookahead of expectimax
newPolicy = {}
for state in self.mdp.getStates():
actions = self.mdp.getPossibleActions(state)
newPolicy[state] = max(self.mdp.getPossibleActions(state),
key=lambda a: self.computeQValue(newValues, state, a))
diffs = 0
for state in self.mdp.getStates():
if policy[state] != newPolicy[state]:
diffs += 1
# for next iteration
values = newValues
policy = newPolicy
# Convergence?
if diffs == 0:
streak += 1
else:
streak = 0
if streak >= 15: break
#print 'CONVERGED'
#for state in self.mdp.getStates():
# if policy[state] != 'east':
# print state, '\t', policy[state], '\t', self.mdp.getPossibleActions(state)
#print
#for k,v in values.items():
# print k, '\t', v
#exit()
#print '\n\n'
#state = save
#for a in self.mdp.getPossibleActions(state):
# qVal = self.computeQValue(values, state, a)
# print '\t', state, '\t', a, '\t', qVal
#print policy[state]
#exit()
# Policy
self.policy = policy
def computeQValue(self, values, state, action):
"""
Compute the Q-value of action in state from the
value function stored in self.values.
"""
# Q-value is expected value you get by committing to an action & then acting optimally
qVal = 0
for sPrime,prob in self.mdp.getTransitionStatesAndProbs(state, action):
#print '\t\t', sPrime, '\t', self.mdp.getReward(state,action,sPrime), '\t', values[sPrime], '\t(', prob, ')'
qVal += prob * (self.mdp.getReward(state,action,sPrime) + self.discount*values[sPrime])
return qVal
def policyEvaluation(self, policy, values, iterations=50):
# Simplified value iteration
for _ in range(iterations):
#print '\t\tjt: ', _
newValues = defaultdict(lambda:0)
diffs = 0
# Get new value for each state
for state in self.mdp.getStates():
#if self.mdp.isTerminal(state): continue
# follow policy
action = policy[state]
# Compute qValue
qVal = 0
for sPrime,prob in self.mdp.getTransitionStatesAndProbs(state, action):
qVal += prob * (self.mdp.getReward(state,action,sPrime) + self.discount*values[sPrime])
newValues[state] = qVal
# Convergence test
diffs += abs(values[state] - newValues[state])
values = newValues
# Convergence?
if diffs < .1: break
return values
def getAction(self, state):
return self.policy[state]