-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplanner.py
331 lines (238 loc) · 8.66 KB
/
planner.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import numpy as np
from pulp import *
import sys
# Get command line arguments
mdp = sys.argv[1] # MDP File
algorithm = sys.argv[2] # Algorithm
batchsize = int(sys.argv[3]) # Batchsize
randomseed = int(sys.argv[4]) # Randomseed
# Check if invoked from get_results
if len(sys.argv) == 6:
mode = sys.argv[5]
else:
mode = "na"
# Set the randomseed to specified value, only matters for RPI
np.random.seed(randomseed)
def read_mdp(mdp):
"""Function to read MDP file"""
f = open(mdp)
S = int(f.readline())
A = int(f.readline())
# Initialize Transition and Reward arrays
R = np.zeros((S, A, S))
T = np.zeros((S, A, S))
# Update the Reward Function
for s in range(S):
for a in range(A):
line = f.readline().split()
for sPrime in range(S):
R[s][a][sPrime] = line[sPrime]
# Update the Transition Function
for s in range(S):
for a in range(A):
line = f.readline().split()
for sPrime in range(S):
T[s][a][sPrime] = line[sPrime]
# Read the value of gamma
gamma = float(f.readline().rstrip())
f.close()
return S, A, R, T, gamma
def print_mdp(S, A, R, T, gamma):
"""Function to print the data read from MDP file"""
print "States: " + str(S)
print "Actions: " + str(A)
print "Reward Function:"
for s in range(S):
for a in range(A):
for sPrime in range(S):
print str(R[s][a][sPrime]) + "\t",
print "\n",
print "Transition Function:"
for s in range(S):
for a in range(A):
for sPrime in range(S):
print str(T[s][a][sPrime]) + "\t",
print "\n",
print "Gamma: " + str(gamma)
return
def find_v(T, R, gamma, policy):
"""Function to find value function V"""
# Initialize arrays of zeros for Value function after and before update
V1 = np.zeros(T.shape[0])
V0 = np.zeros(T.shape[0])
while(1):
# Until the V1 and V0 are close enough element wise
for s in range(T.shape[0]):
# Find the V1
V1[s] = np.sum(T[s, policy[s], :] * R[s, policy[s], :] +
gamma * T[s, policy[s], :] * V0)
# If V1 and V0 are close enough
if np.allclose(V1, V0, rtol=1e-13, atol=1e-15):
break
else:
# Update V0 with V1
np.copyto(V0, V1)
return V1
def find_q(V, T, R, gamma):
"""Function to find action value function Q"""
# Initialize arrays of zeros for Value function after and before update
Q = np.zeros((T.shape[0], T.shape[1]))
for s in range(T.shape[0]):
# Find action value for each state action pair
Q[s] = np.sum(T[s] * R[s] + gamma * T[s] * V, axis=1)
return Q
def hpi(T, R, gamma):
"""Implementation of Howard's PI"""
# Initialise policy to all zeros
policy = [0 for i in range(T.shape[0])]
# Set the flag
changed = 1
iterations = 0
while changed == 1:
# While flag is set
iterations += 1
# Find V and Q
V = find_v(T, R, gamma, policy)
Q = find_q(V, T, R, gamma)
# Find improvable states
improvable_states = []
for s in range(T.shape[0]):
# Check for improvablity and add to improvable states
if (Q[s][policy[s]] < np.amax(Q[s][:])):
improvable_states.append(s)
# If there are improvable states,
# switch the action for each improvable state
if len(improvable_states) > 0:
for k in improvable_states:
policy[k] = 1 - policy[k]
else:
# If no improvable states, reset the flag
changed = 0
# Print iterations if invoked from get_results.sh
if mode == "gen":
print iterations, policy
return V, policy
def rpi(T, R, gamma):
"""Implementation of Randomised PI"""
# Initialise policy to all zeros
policy = [0 for i in range(T.shape[0])]
# Set the flag
changed = 1
iterations = 0
while changed == 1:
# While flag is set
iterations += 1
# Find V and Q
V = find_v(T, R, gamma, policy)
Q = find_q(V, T, R, gamma)
# Find improvable states
improvable_states = []
for s in range(T.shape[0]):
# Check for improvablity and add to improvable states
if Q[s][policy[s]] < np.amax(Q[s][:]):
improvable_states.append(s)
if len(improvable_states) > 0:
# From the set of improving states, pick random non empty subset
# and switch actions of all these states
for i in range(np.random.choice(range(
len(improvable_states))) + 1):
j = np.random.choice(improvable_states)
policy[j] = 1 - policy[j]
improvable_states.remove(j)
else:
# If no improvable states, reset the flag
changed = 0
# Print iterations if invoked from get_results.sh
if mode == "gen":
print iterations, policy
return V, policy
def bspi(T, R, gamma, batch_size):
"""Implementation of Howard's PI"""
# Initialise policy to all zeros
policy = [0 for i in range(T.shape[0])]
iterations = 0
# Partition the states into batches; and improve rightmost batch first
# then the one on left to it and so on
for i in reversed(range(0, T.shape[0], batch_size)):
if i + batch_size - 1 < T.shape[0]:
batch = range(i, i + batch_size)
else:
batch = range(i, T.shape[0])
# Set the flag
changed = 1
while changed == 1:
# While flag is set
iterations += 1
# Find V and Q
V = find_v(T, R, gamma, policy)
Q = find_q(V, T, R, gamma)
# Find improvable states
improvable_states = []
for s in batch:
# Check for improvablity and add to improvable states
if (Q[s][policy[s]] < np.amax(Q[s][:])):
improvable_states.append(s)
# If there are improvable states,
# switch the action for each improvable state
if len(improvable_states) > 0:
for k in improvable_states: # For each improvable state
policy[k] = 1 - policy[k] # Switch the action
else:
# If no improvable states, reset the flag
changed = 0
# Print iterations if invoked from get_results.sh
if mode == "gen":
print iterations, policy
# Find final Vstar
V = find_v(T, R, gamma, policy)
return V, policy
def solve_lp(T, R, gamma):
"""Function to solve Linear Programming using PuLP"""
# Setting up problem and decision variables
prob = pulp.LpProblem('mdp_lp', pulp.LpMinimize)
decision_variables = pulp.LpVariable.dicts('v', range(T.shape[0]))
# Objective function
prob += np.sum(decision_variables.values())
for s in range(T.shape[0]):
for a in range(T.shape[1]):
# Add constraint to LP for each state and action
formula = 0.0
for sPrime in range(T.shape[2]):
formula += (T[s, a, sPrime] * (R[s, a, sPrime] +
gamma * decision_variables[sPrime]))
prob += decision_variables[s] >= formula
# Solve the LP Problem and get results in V
prob.solve() # solvers.PULP_CBC_CMD(fracGap=0.000000001)
V = np.array([v.varValue for v in prob.variables()])
return V
def lp(T, R, gamma):
"""Implementation of LP"""
# Initialise policy to all zeros
policy = [0 for i in range(T.shape[0])]
# Find V and Q
V = solve_lp(T, R, gamma)
Q = find_q(V, T, R, gamma)
# For each state, if action_0 value is less than action_1 value,
# change its action to action_1
for s in range(T.shape[0]):
if (Q[s][0] < Q[s][1]) and (policy[s] != 1):
policy[s] = 1
return V, policy
def print_output(V, pi):
"""Function to print results of any PI Method"""
for i in range(V.shape[0]):
print str.format("{0:.15f}", V[i]) + "\t" + str(pi[i])
return
# Read the MDP file
S1, A1, R1, T1, gamma1 = read_mdp(mdp)
# Invoke appropriate Algorithm according to Command Line Parameter
if algorithm == "hpi":
V_star, pi_star = hpi(T1, R1, gamma1)
elif algorithm == "rpi":
V_star, pi_star = rpi(T1, R1, gamma1)
elif algorithm == "bspi":
V_star, pi_star = bspi(T1, R1, gamma1, batchsize)
elif algorithm == "lp":
V_star, pi_star = lp(T1, R1, gamma1)
# Print the results to STDOUT
print_output(V_star, pi_star)