-
Notifications
You must be signed in to change notification settings - Fork 5
/
RL_visualizing.py
191 lines (165 loc) · 5.85 KB
/
RL_visualizing.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
import matplotlib.pyplot as plt
# from matplotlib import mpl
import numpy as np
# import matplotlib.animation as animation
import random
import dill
import sys
from game.MapGame import Map
def loadMap():
dataset="map.json"
import json
dataFile = open(dataset)
s = dataFile.read()
data = json.loads(s)
dataFile.close()
return data["map"]
def norm_reward(reward, max_reward):
return (reward+(max_reward/2.0))/(max_reward*0.5)
def norm_state(state, max_state):
return norm_action(action_=state, action_bounds_=max_state)
def scale_state(state, max_state):
return scale_action(normed_action_=state, action_bounds_=max_state)
def norm_state2(state, max_state):
"""
For when the origin is in the centre of the environment
"""
return (state)/max_state
def clampAction(actionV, bounds):
"""
bounds[0] is lower bounds
bounds[1] is upper bounds
"""
for i in range(len(actionV)):
if actionV[i] < bounds[0][i]:
actionV[i] = bounds[0][i]
elif actionV[i] > bounds[1][i]:
actionV[i] = bounds[1][i]
return actionV
def norm_action(action_, action_bounds_):
"""
Normalizes the action
Where the middle of the action bounds are mapped to 0
upper bound will correspond to 1 and -1 to the lower
from environment space to normalized space
"""
avg = (action_bounds_[0] + action_bounds_[1])/2
return (action_ - (avg)) / (action_bounds_[1]-avg)
def scale_action(normed_action_, action_bounds_):
"""
from normalize space back to environment space
Normalizes the action
Where 0 in the action will be mapped to the middle of the action bounds
1 will correspond to the upper bound and -1 to the lower
"""
avg = (action_bounds_[0] + action_bounds_[1])/2.0
return normed_action_ * (action_bounds_[1] - avg) + avg
def get_policy_visual_data(model_, max_state, game):
X,Y = np.mgrid[0:16,0:16]
U = []
V = []
Q = []
for i in range(16):
t_u = []
t_v = []
t_q = []
for j in range(16):
state = np.array([X[i][j],Y[i][j]])
pa = model_.predict([norm_state(state,max_state)])
# pa = model_.predict([norm_state(state,max_state)])
q = np.max(model_.q_values([norm_state(state,max_state)]))
# q=0
move = game.move(pa)
t_u.append(move[0])
t_v.append(move[1])
t_q.append(q)
U.append(t_u)
V.append(t_v)
Q.append(t_q)
U = np.array(U)*1.0
V = np.array(V)*1.0
return (X, Y, U, V, Q)
def get_continuous_policy_visual_data(model_, max_state, game):
X,Y = np.mgrid[0:16,0:16]
U = []
V = []
Q = []
for i in range(16):
t_u = []
t_v = []
t_q = []
for j in range(16):
state = np.array([X[i][j],Y[i][j]])
pa = model_.predict([norm_state(state,max_state[:2])])
# pa = model_.predict([norm_state(state,max_state)])
q = (model_.q_value([norm_state(state,max_state[:2])]))
# q=0
move = pa
t_u.append(move[0])
t_v.append(move[1])
t_q.append(q)
U.append(t_u)
V.append(t_v)
Q.append(t_q)
U = np.array(U)*1.0
V = np.array(V)*1.0
return (X, Y, U, V, Q)
def get_continuous_policy_visual_data1D(model_, state_bounds, game):
"""
For policies with one output action parameter
"""
size_=16
X,Y = game.getGrid()
U = np.array(X)
V = np.array(X)
Q = np.array(X)
for i in range(size_):
for j in range(size_):
if len(state_bounds[0]) == 4:
state = np.array([X[i][j],Y[i][j], X[i][j], X[i][j]])
elif len(state_bounds[0]) == 2:
state = np.array([X[i][j],Y[i][j]])
pa = model_.predict([norm_state(state,state_bounds)])
# pa = model_.predict([norm_state(state,max_state)])
q = (model_.q_value([norm_state(state,state_bounds)]))
# q=0
move = pa
U[i,j]=move[0]
V[i,j]=0
Q[i,j]=q
U = np.array(U)*1.0
V = np.array(V)*1.0
Q = np.array(Q)
# Switch X and Y?
return (X, Y, U, V, Q)
class RLVisulize(object):
def __init__(self, map):
# self._fig, (self._map_ax, self._policy_ax) = plt.subplots(1, 2, sharey=False)
self._map = map
self._bounds = np.array([[0,0], [15,15]])
def final_policy(self, X, Y, U, V, Q):
X,Y = np.mgrid[0:self._bounds[1][0]+1,0:self._bounds[1][0]+1]
# print X,Y
# print U,V
# print Q
fig, ax = plt.subplots(1)
# self._policy = self._policy_ax.quiver(X[::2, ::2],Y[::2, ::2],U[::2, ::2],V[::2, ::2], linewidth=0.5, pivot='mid', edgecolor='k', headaxislength=5, facecolor='None')
ax.quiver(X,Y,U,V,Q, alpha=.75, linewidth=1.0, pivot='mid', angles='xy', linestyles='-', scale=25.0)
ax.quiver(X,Y,U,V, linewidth=0.5, pivot='mid', edgecolor='k', headaxislength=3, facecolor='None', angles='xy', linestyles='-', scale=25.0)
plt.title("Final Policy")
# these are matplotlib.patch.Patch properties
textstr = """$\max q=%.2f$\n$\min q=%.2f$"""%(np.max(Q), np.min(Q))
props = dict(boxstyle='round', facecolor='wheat', alpha=0.75)
# place a text box in upper left in axes coords
ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
verticalalignment='top', bbox=props)
plt.show()
if __name__ == "__main__":
max_state = 8.0
file_name=sys.argv[1]
model = dill.load(open(file_name))
map = loadMap()
rlv = RLVisulize(map)
game = Map(map)
X, Y, U, V, Q = get_policy_visual_data(model, max_state, game)
rlv.final_policy(X, Y, U, V, Q)