forked from Neo-X/RL-Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleActionSpace.py
349 lines (303 loc) · 13.7 KB
/
SampleActionSpace.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import random
import numpy as np
import math
import cPickle
import json
import os
import sys
# Networks
from model import RLLogisticRegression.RLLogisticRegression
from model import NeuralNet.NeuralNet
from RLNeuralNetwork import RLNeuralNetwork
from RLNeuralNetworkDQ import RLNeuralNetworkDQ
from RLDeepNet import RLDeepNet
from DeepCACLA import DeepCACLA
from DeepDPG import DeepDPG
from model import ForwardDynamicsNetwork.ForwardDynamicsNetwork
# Games
from MapGame import Map
from BallGame1D import BallGame1D
from BallGame1DFuture import BallGame1DFuture
from BallGame1DChoice import BallGame1DChoice
from RL_visualizing import *
from RLVisualize import RLVisualize
from NNVisualize import NNVisualize
from model import ExperienceMemory.ExperienceMemory
from RunGame import *
class Sampler(object):
"""
A simple method to sample the space of actions.
"""
def __init__(self, game):
self._x=[]
self._samples=[]
# action, value
self._bestSample=([0],[-10000000])
self._game=game
def sample(self, game, state):
self._samples = []
self._bestSample=([0],[-10000000])
xi = np.linspace(-2.0, 2.0, 100)
for i in xi:
y = game._reward(game._computeHeight(i+state[1]))
# print i, y
self._samples.append([[i],[y]])
if y > self._bestSample[1][0]:
self._bestSample[1][0] = y
self._bestSample[0][0] = i
def sampleModel(self, model, forwardDynamics, current_state, state_bounds, action_bounds):
self._samples = []
self._bestSample=([0],[-10000000])
pa = model.predict([norm_state(current_state, state_bounds)])
action = scale_action(pa, action_bounds)
# print "Suggested Action: " + str(action) + " for state: " + str(current_state)
xi = np.linspace(-0.5+action[0], 0.15+action[0], 100)
for i in xi:
pa = [i]
# prediction = scale_state(forwardDynamics.predict(state=norm_state(current_state, state_bounds), action=norm_action(pa, action_bounds)), state_bounds)
# y = model.q_value([norm_state(prediction, state_bounds)])
y = self._game._reward(self._game._computeHeight(i+current_state[1]))
# print i, y
self._samples.append([[i],[y]])
if y > self._bestSample[1][0]:
self._bestSample[1][0] = y
self._bestSample[0][0] = i
def getBestSample(self):
return self._bestSample
def setBestSample(self, samp):
self._bestSample = samp
def predict(self, state):
"""
Returns the best action
"""
return np.random.rand(1,1)
def q_value(self, state):
"""
Returns the expected value of the state
"""
return np.random.rand(1,1)[0]
def simpleSampling():
# make a color map of fixed colors
#try:
file = open(sys.argv[1])
settings = json.load(file)
file.close()
map = loadMap()
# Normalization constants for data
max_reward = settings['max_reward']
# max_reward = 1.0
state_bounds = np.array(settings['state_bounds'])
state_length = len(state_bounds[0])
print "Max Reward: " + str(max_reward)
print "State Bounds: " + str(state_bounds)
# game = Map(map)
game = None
game_type = settings['game_type']
if game_type == 'BallGame1DFuture':
print "Creating Game type: " + str(game_type)
game = BallGame1DFuture()
elif game_type == 'BallGame1D':
print "Creating Game type: " + str(game_type)
game = BallGame1D()
elif game_type == 'BallGame1DChoice':
print "Creating Game type: " + str(game_type)
game = BallGame1DChoice()
else:
print "Unrecognized game: " + str(game_type)
sys.exit()
game.enableRender()
game._simulate=True
# game._saveVideo=True
game.setMovieName(str(settings['agent_name']) + "_on_" + str(game_type))
action_bounds = np.array(settings['action_bounds'])
action_length = len(action_bounds[0])
data_folder = settings['data_folder']
states = np.array([state_bounds])
action_space_continuous=True
# file_name=data_folder+"navigator_agent_"+str(settings['agent_name'])+".pkl"
model = Sampler()
file_name_dynamics=data_folder+"forward_dynamics_"+str(settings['agent_name'])+".pkl"
forwardDynamicsModel = cPickle.load(open(file_name_dynamics))
if action_space_continuous:
# X, Y, U, V, Q = get_continuous_policy_visual_data(model, state_bounds, game)
X, Y, U, V, Q = get_continuous_policy_visual_data1D(model, state_bounds, game)
else:
X, Y, U, V, Q = get_policy_visual_data(model, state_bounds, game)
print "U: " + str(U)
print "V: " + str(V)
print "Q: " + str(Q)
game.init(U, V, Q)
game.reset()
if not os.path.exists(data_folder):
os.makedirs(data_folder)
num_actions = 20
scaling = 1.0
game._box.state[0][1] = 0.0
actions = (np.random.rand(num_actions,1)-0.5) * 2.0 * scaling
reward_sum=0
for action_ in actions:
# ballGame.resetTarget()
game.resetTarget()
game.resetHeight()
game._box.state[0][1] = 0.0
state = game.getState()
print "State: " + str(state)
model.sample(game, state)
# reward = game.actContinuous(action_)
# print "Action: " + str(action_)
# print "Verify State: " + str(state) + " with " + str(scale_state(norm_state(state, state_bounds=state_bounds), state_bounds=state_bounds))
if action_space_continuous:
# X, Y, U, V, Q = get_continuous_policy_visual_data(model, state_bounds, game)
X, Y, U, V, Q = get_continuous_policy_visual_data1D(model, state_bounds, game)
else:
X, Y, U, V, Q = get_policy_visual_data(model, state_bounds, game)
game.updatePolicy(U, V, Q)
# pa = model.predict([norm_state(state, state_bounds)])
if action_space_continuous:
# action = scale_action(pa, action_bounds)
action = model.getBestSample()[:1]
print "Action: " + str(action)
# prediction = scale_state(forwardDynamicsModel.predict(state=norm_state(state, state_bounds), action=norm_action(action, action_bounds)), state_bounds)
# print "Next State Prediction: " + str(prediction)
# predicted_height = game._computeHeight(prediction[1]) # This is dependent on the network shape
# game.setPrediction([2,predicted_height])
# print "Next Height Prediction: " + str(predicted_height)
reward = game.actContinuous(action)
# print "Height difference: " + str(math.fabs(predicted_height - game._max_y))
elif not action_space_continuous:
# print "Action: " + str(pa)
reward = game.act(action)
reward_sum+=reward
# print "Reward: " + str(reward)
print "Average reward: " + str(reward_sum/num_actions)
#except Exception, e:
# print "Error: " + str(e)
# raise e
def modelSampling():
# make a color map of fixed colors
#try:
file = open(sys.argv[1])
settings = json.load(file)
file.close()
map = loadMap()
# Normalization constants for data
max_reward = settings['max_reward']
state_bounds = np.array(settings['state_bounds'])
state_length = len(state_bounds[0])
print "Max Reward: " + str(max_reward)
print "State Bounds: " + str(state_bounds)
game_has_choices=False
# game = Map(map)
game = None
game_type = settings['game_type']
if game_type == 'BallGame1DFuture':
print "Creating Game type: " + str(game_type)
game = BallGame1DFuture()
elif game_type == 'BallGame1D':
print "Creating Game type: " + str(game_type)
game = BallGame1D()
elif game_type == 'BallGame1DChoice':
print "Creating Game type: " + str(game_type)
game = BallGame1DChoice()
game_has_choices=True
else:
print "Unrecognized game: " + str(game_type)
sys.exit()
# game.enableRender()
# game._simulate=True
# game._saveVideo=True
game.setMovieName(str(settings['agent_name']) + "_on_" + str(game_type))
action_bounds = np.array(settings['action_bounds'])
action_length = len(action_bounds[0])
data_folder = settings['data_folder']
states = np.array([state_bounds])
action_space_continuous=True
sampler = Sampler(game)
file_name=data_folder+"navigator_agent_"+str(settings['agent_name'])+".pkl"
model = cPickle.load(open(file_name))
file_name_dynamics=data_folder+"forward_dynamics_"+str(settings['agent_name'])+".pkl"
forwardDynamicsModel = cPickle.load(open(file_name_dynamics))
if action_space_continuous:
# X, Y, U, V, Q = get_continuous_policy_visual_data(model, state_bounds, game)
X, Y, U, V, Q = get_continuous_policy_visual_data1D(model, state_bounds, game)
else:
X, Y, U, V, Q = get_policy_visual_data(model, state_bounds, game)
# print "U: " + str(U)
# print "V: " + str(V)
# print "Q: " + str(Q)
game.init(U, V, Q)
game.reset()
if not os.path.exists(data_folder):
os.makedirs(data_folder)
num_actions = 200
scaling = 1.0
game._box.state[0][1] = 0.0
actions = (np.random.rand(num_actions,1)-0.5) * 2.0 * scaling
reward_sum=0
for action_ in actions:
# ballGame.resetTarget()
game._box.state[0][1] = 0.0
state = game.getState()
if (game_has_choices):
action_v=-1000000000
best_target=0
best_action=0
for i in range(len(state)):
# for i in range(1):
state_ = state[i]
# val=np.array([2,state_[1]])
# game.setTarget(val)
game.setTargetChoice(i)
sampler.sampleModel(model=model, forwardDynamics=forwardDynamicsModel, current_state=state_, state_bounds=state_bounds,
action_bounds=action_bounds)
action__ = sampler.getBestSample()
# print "Found action: " + str(action__) + " for state: " +str(state_)
if (action__[1][0] > action_v):
action_v = action__[1][0]
best_action = action__
state__ = state_
best_target=i
sampler.setBestSample(best_action)
state = state__
game.setTargetChoice(best_target)
# val=np.array([2,state[1]])
# game.setTarget(val)
else:
sampler.sampleModel(model=model, forwardDynamics=forwardDynamicsModel, current_state=state, state_bounds=state_bounds,
action_bounds=action_bounds)
print "State: " + str(state)
# reward = game.actContinuous(action_)
# print "Action: " + str(action_)
# print "Verify State: " + str(state) + " with " + str(scale_state(norm_state(state, state_bounds=state_bounds), state_bounds=state_bounds))
if action_space_continuous:
# X, Y, U, V, Q = get_continuous_policy_visual_data(model, state_bounds, game)
X, Y, U, V, Q = get_continuous_policy_visual_data1D(model, state_bounds, game)
else:
X, Y, U, V, Q = get_policy_visual_data(model, state_bounds, game)
game.updatePolicy(U, V, Q)
# pa = model.predict([norm_state(state, state_bounds)])
if action_space_continuous:
# action = scale_action(pa, action_bounds)
action = sampler.getBestSample()
print "Action: " + str(action)
action = action[0]
prediction = scale_state(forwardDynamicsModel.predict(state=norm_state(state, state_bounds), action=norm_action(action, action_bounds)), state_bounds)
# print "Next State Prediction: " + str(prediction)
predicted_height = game._computeHeight(prediction[1]) # This is dependent on the network shape
game.setPrediction([2,predicted_height])
# print "Next Height Prediction: " + str(predicted_height)
reward = game.actContinuous(action)
# print "Height difference: " + str(math.fabs(predicted_height - game._max_y))
elif not action_space_continuous:
# print "Action: " + str(pa)
reward = game.act(action)
reward_sum+=reward
game.resetTarget()
game.resetHeight()
# print "Reward: " + str(reward)
print "Average reward: " + str(reward_sum/num_actions)
#except Exception, e:
# print "Error: " + str(e)
# raise e
if __name__ == "__main__":
modelSampling()