-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_gridworld.py
More file actions
374 lines (316 loc) · 12.6 KB
/
plot_gridworld.py
File metadata and controls
374 lines (316 loc) · 12.6 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colors
def plot_dashed_arrow(state, width, ax, direction):
#print("plotting dashed arrow", direction)
h_length = 0.15
shaft_length = 0.4
#convert state to coords where (0,0) is top left
x_coord = state % width
y_coord = state // width
#print(x_coord, y_coord)
if direction == 'down':
x_end = 0
y_end = shaft_length - h_length
elif direction == 'up':
x_end = 0
y_end = -shaft_length + h_length
elif direction == 'left':
x_end = -shaft_length + h_length
y_end = 0
elif direction == 'right':
x_end = shaft_length - h_length
y_end = 0
else:
print("ERROR: ", direction, " is not a valid action")
return
print(x_end, y_end)
ax.arrow(x_coord, y_coord, x_end, y_end, head_width=None, head_length=None, fc='k', ec='k',linewidth=4, linestyle=':',fill=False)
#convert state to coords where (0,0) is top left
x_coord = state % width
y_coord = state // width
#print(x_coord, y_coord)
if direction == 'down':
x_end = 0
y_end = h_length
y_coord += shaft_length - h_length
elif direction == 'up':
x_end = 0
y_end = -h_length
y_coord += -shaft_length + h_length
elif direction == 'left':
x_end = -h_length
y_end = 0
x_coord += -shaft_length + h_length
elif direction == 'right':
x_end = h_length
y_end = 0
x_coord += shaft_length - h_length
else:
print("ERROR: ", direction, " is not a valid action")
return
print(x_end, y_end)
ax.arrow(x_coord, y_coord, x_end, y_end, head_width=0.2, head_length=h_length, fc='k', ec='k',linewidth=4, fill=False,length_includes_head = True)
def plot_arrow(state, width, ax, direction, prob=None):
#print("plotting arrow", direction)
# if prob:
# h_length = 0.15 * min(prob*4,1)
# shaft_length = 0.4 * min(prob*4,1)
# else:
h_length = 0.15
shaft_length = 0.4
#convert state to coords where (0,0) is top left
x_coord = state % width
y_coord = state // width
#print(x_coord, y_coord)
if direction == 'down':
x_end = 0
y_end = shaft_length - h_length
elif direction == 'up':
x_end = 0
y_end = -shaft_length + h_length
elif direction == 'left':
x_end = -shaft_length + h_length
y_end = 0
elif direction == 'right':
x_end = shaft_length - h_length
y_end = 0
else:
print("ERROR: ", direction, " is not a valid action")
return
# print(x_end, y_end)
if prob is not None:
if prob > 0.005:
ax.arrow(x_coord, y_coord, x_end, y_end, head_width=0.2*prob, head_length=h_length*prob, fc='k', ec='k',linewidth=4*prob)
else:
ax.arrow(x_coord, y_coord, x_end, y_end, head_width=0.2, head_length=h_length, fc='k', ec='k',linewidth=4)
def plot_dot(state, width, ax):
ax.plot(state % width, state // width, 'ko',markersize=10)
#plot a stochastic policy and make arrows reflect the probability of action
def plot_optimal_policy_stochastic(pi_stoch, feature_list, num_rows, num_cols, filename = None):
#reformat into rows and cols
pi = []
feature_mat = []
cnt = 0
for r in range(num_rows):
pi_row = []
f_row = []
for c in range(num_cols):
pi_row.append(pi_stoch[cnt])
f_row.append(feature_list[cnt])
cnt += 1
pi.append(pi_row)
feature_mat.append(f_row)
plot_stochastic_policy(pi, feature_mat, filename)
def plot_optimal_policy_lists(pi_list, feature_list, num_rows, num_cols, filename = None):
#reformat into rows and cols
pi = []
feature_mat = []
cnt = 0
for r in range(num_rows):
pi_row = []
f_row = []
for c in range(num_cols):
pi_row.append(pi_list[cnt])
f_row.append(feature_list[cnt])
cnt += 1
pi.append(pi_row)
feature_mat.append(f_row)
plot_optimal_policy(pi, feature_mat, filename)
def get_policy_string_from_trajectory(traj, feature_list, mdp_env, filename = None):
pi_list = ["" for s in range(mdp_env.num_states)]
#go through trajectory and fill in entries
for s,a in traj:
if a is None:
pi_list[s] = "."
else:
pi_list[s] = mdp_env.get_readable_actions(a)
plot_optimal_policy_lists(pi_list, feature_list, mdp_env.num_rows, mdp_env.num_cols, filename)
def plot_stochastic_policy(pi, feature_mat, filename = None):
plt.figure()
ax = plt.axes()
count = 0
rows,cols = len(pi), len(pi[0])
for line in pi:
for el_dict in line:
#print("optimal action", el)
# could be a stochastic policy with more than one optimal action
for char in el_dict:
char_prob = el_dict[char]
#print(char)
if char == "^":
plot_arrow(count, cols, ax, "up", prob=char_prob)
elif char == "v":
plot_arrow(count, cols, ax, "down", prob=char_prob)
elif char == ">":
plot_arrow(count, cols, ax, "right", prob=char_prob)
elif char == "<":
plot_arrow(count, cols, ax, "left", prob=char_prob)
elif char == ".":
plot_dot(count, cols, ax)
elif el == "w":
#wall
pass
count += 1
#use for wall states
#if walls:
mat = [[0 if fvec is None else fvec.index(1)+1 for fvec in row] for row in feature_mat]
#mat =[[0,0],[2,2]]
feature_set = set()
for mrow in mat:
for m in mrow:
feature_set.add(m)
num_features = len(feature_set)
print(mat)
all_colors = ['black','white','tab:red','tab:blue','tab:green','tab:purple', 'tab:orange', 'tab:gray', 'tab:cyan']
colors_to_use = []
for f in range(9):#hard coded to only have 9 features right now
if f in feature_set:
colors_to_use.append(all_colors[f])
cmap = colors.ListedColormap(colors_to_use)
# else:
# mat = [[fvec.index(1) for fvec in row] for row in feature_mat]
# cmap = colors.ListedColormap(['white','tab:red','tab:blue','tab:green','tab:purple', 'tab:orange', 'tab:gray', 'tab:cyan'])
#input()
#convert feature_mat into colors
#heatmap = plt.imshow(mat, cmap="Reds", interpolation='none', aspect='equal')
im = plt.imshow(mat, cmap=cmap, interpolation='none', aspect='equal')
ax = plt.gca()
ax.set_xticks(np.arange(-.5, cols, 1), minor=True);
ax.set_yticks(np.arange(-.5, rows, 1), minor=True);
#ax.grid(which='minor', axis='both', linestyle='-', linewidth=5, color='k')
# Gridlines based on minor ticks
ax.grid(which='minor', color='k', linestyle='-', linewidth=5)
ax.xaxis.set_major_formatter(plt.NullFormatter())
ax.yaxis.set_major_formatter(plt.NullFormatter())
ax.yaxis.set_major_locator(plt.NullLocator())
ax.xaxis.set_major_locator(plt.NullLocator())
#cbar = plt.colorbar(heatmap)
#cbar.ax.tick_params(labelsize=20)
plt.tight_layout()
if not filename:
plt.show()
else:
plt.savefig(filename)
def plot_optimal_policy(pi, feature_mat, filename = None):
plt.figure()
ax = plt.axes()
count = 0
rows,cols = len(pi), len(pi[0])
for line in pi:
for el in line:
#print("optimal action", el)
# could be a stochastic policy with more than one optimal action
for char in el:
#print(char)
if char == "^":
plot_arrow(count, cols, ax, "up")
elif char == "v":
plot_arrow(count, cols, ax, "down")
elif char == ">":
plot_arrow(count, cols, ax, "right")
elif char == "<":
plot_arrow(count, cols, ax, "left")
elif char == ".":
plot_dot(count, cols, ax)
elif el == "w":
#wall
pass
count += 1
#use for wall states
#if walls:
mat = [[0 if fvec is None else fvec.index(1)+1 for fvec in row] for row in feature_mat]
#mat =[[0,0],[2,2]]
feature_set = set()
for mrow in mat:
for m in mrow:
feature_set.add(m)
num_features = len(feature_set)
print(mat)
all_colors = ['black','white','tab:red','tab:blue','tab:green','tab:purple', 'tab:orange', 'tab:gray', 'tab:cyan']
colors_to_use = []
for f in range(9):#hard coded to only have 9 features right now
if f in feature_set:
colors_to_use.append(all_colors[f])
cmap = colors.ListedColormap(colors_to_use)
# else:
# mat = [[fvec.index(1) for fvec in row] for row in feature_mat]
# cmap = colors.ListedColormap(['white','tab:red','tab:blue','tab:green','tab:purple', 'tab:orange', 'tab:gray', 'tab:cyan'])
#input()
#convert feature_mat into colors
#heatmap = plt.imshow(mat, cmap="Reds", interpolation='none', aspect='equal')
im = plt.imshow(mat, cmap=cmap, interpolation='none', aspect='equal')
ax = plt.gca()
ax.set_xticks(np.arange(-.5, cols, 1), minor=True);
ax.set_yticks(np.arange(-.5, rows, 1), minor=True);
#ax.grid(which='minor', axis='both', linestyle='-', linewidth=5, color='k')
# Gridlines based on minor ticks
ax.grid(which='minor', color='k', linestyle='-', linewidth=5)
ax.xaxis.set_major_formatter(plt.NullFormatter())
ax.yaxis.set_major_formatter(plt.NullFormatter())
ax.yaxis.set_major_locator(plt.NullLocator())
ax.xaxis.set_major_locator(plt.NullLocator())
#cbar = plt.colorbar(heatmap)
#cbar.ax.tick_params(labelsize=20)
plt.tight_layout()
if not filename:
plt.show()
else:
plt.savefig(filename)
def plot_test_query(state, better_action, worse_action, feature_mat, equal_pref = False):
plt.figure()
ax = plt.axes()
count = 0
rows,cols = len(feature_mat), len(feature_mat[0])
if better_action == "^":
plot_arrow(state, cols, ax, "up")
elif better_action == "v":
plot_arrow(state, cols, ax, "down")
elif better_action == ">":
plot_arrow(state, cols, ax, "right")
elif better_action == "<":
plot_arrow(state, cols, ax, "left")
if equal_pref:
if worse_action == "^":
plot_arrow(state, cols, ax, "up")
elif worse_action == "v":
plot_arrow(state, cols, ax, "down")
elif worse_action == ">":
plot_arrow(state, cols, ax, "right")
elif worse_action == "<":
plot_arrow(state, cols, ax, "left")
else:
if worse_action == "^":
plot_dashed_arrow(state, cols, ax, "up")
elif worse_action == "v":
plot_dashed_arrow(state, cols, ax, "down")
elif worse_action == ">":
plot_dashed_arrow(state, cols, ax, "right")
elif worse_action == "<":
plot_dashed_arrow(state, cols, ax, "left")
mat = [[0 if fvec is None else fvec.index(1)+1 for fvec in row] for row in feature_mat]
print(mat)
#convert feature_mat into colors
#heatmap = plt.imshow(mat, cmap="Reds", interpolation='none', aspect='equal')
cmap = colors.ListedColormap(['black','white','tab:red', 'tab:blue','tab:green','tab:purple', 'tab:orange', 'tab:gray', 'tab:cyan'])
im = plt.imshow(mat, cmap=cmap, interpolation='none', aspect='equal')
ax = plt.gca()
ax.set_xticks(np.arange(-.5, cols, 1), minor=True);
ax.set_yticks(np.arange(-.5, rows, 1), minor=True);
#ax.grid(which='minor', axis='both', linestyle='-', linewidth=5, color='k')
# Gridlines based on minor ticks
ax.grid(which='minor', color='k', linestyle='-', linewidth=5)
ax.xaxis.set_major_formatter(plt.NullFormatter())
ax.yaxis.set_major_formatter(plt.NullFormatter())
ax.yaxis.set_major_locator(plt.NullLocator())
ax.xaxis.set_major_locator(plt.NullLocator())
#cbar = plt.colorbar(heatmap)
#cbar.ax.tick_params(labelsize=20)
plt.show()
if __name__=="__main__":
pi = [['', '^><','.'],['<>v','<','>'],['<>^v','v' ,'^']]
feature_mat = [[(1,0),(0,1),(0,1)],[(0,1),(0,1),(0,1)],[(0,1), (0,1),(1,0)] ]
plot_optimal_policy(pi, feature_mat)
pi = ['v', '^><','.','<>v','<','>','<>^v','v' ,'^']
feature_mat = [(1,0),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1), (0,1),(1,0) ]
plot_optimal_policy_lists(pi, feature_mat, 3,3)