/
run.py
369 lines (246 loc) · 8.4 KB
/
run.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import numpy.random as nr
import matplotlib.pyplot as plt
import matplotlib.animation as ani
import tensorflow as tf
import time
import filter as fil
import DDPG as ddpg
import numpy as np
import gym
import os
import pylab as pyl
num_episode=500
num_experience=4
total_state =500
MemorySize=100000
start=5000
batch_size=32
replay_transition=100
test=1
initial= True
Mean_reward=[]
max_reward=[]
min_reward=[]
Mean_q=[]
print ("version Tensorflow is")
print(tf.__version__)
def main():
env = gym.make('Pendulum-v0');
env_ren = gym.make('Pendulum-v0');
current_state = env.reset();
#env.monitor.start('tmp/Pendulum-v0-experiment-1')
validation_env(env)
newEnv = fil.makeFilteredEnv(env);
act_dim = newEnv.action_space.shape[0];
obs_dim = newEnv.observation_space.shape[0];
print ("DDPG REINFORCEMENT LEARNING ")
print "env" ,env.action_space.shape ," and " ,[1]+list(newEnv.action_space.shape), act_dim
print "env", env.observation_space.shape, " and ", newEnv.observation_space.shape, obs_dim
env = newEnv;
session = tf.Session();
session.run(tf.initialize_all_variables())
print "ini", session.run(tf.initialize_all_variables())
#saver = tf.train.Saver()
#writer = tf.train.SummaryWriter("/tmp/mnist_logs", session.graph_def)
#saver.restore(session, "tmp/model.ckpt")
# - Initialize the agent-
agent = ddpg.DDPG(newEnv)
avg_reward = []
after = 0
print("---avarage_reward---\n", len(avg_reward))
start_time = time.time()
session.run(tf.initialize_all_variables())
for i_episode in xrange(num_episode):
print "-----------\n Episode=", i_episode,"/",num_episode,
current_state = env.reset()
env.step(env.action_space.sample())
# env.reset()
action = env.action_space.sample()#agent.get_action(session, current_state)
#action = action + noise_act(act_dim)
for t in xrange(total_state):#1000 total state
if agent.get_buffer_size() > start:
agent.q_actor_critic(session, batch_size)
if t % 1000 == 0:
print "----", t
else:
next_state, reward, done, info = env.step(action[0])
action = agent.get_action(session, next_state)
action = action + agent.noise_act(action.shape)
priotity= 1
experience = [current_state, action, reward, next_state, done, info,priotity]
agent.replay_buffer.add(experience)
current_state = next_state
if done:
break
print ("\n apply_policy_gradient")
agent.apply_policy_gradient(session, 62)
print ("\n start test")
current_state = env.reset()
test_env(env,action,agent,session,avg_reward)
# Save the variables to disk.
#save_path = saver.save(session, "tmp/model.ckpt")
#print("Model saved in file: %s" % save_path)
if(i_episode%100==0 and i_episode!=1):
plot_reward()
plot_perfomance(avg_reward)
def test_env(env,action, agent,session,avg_reward):
rewards=[]
q_value = []
values=[]
Z=[]
for t in xrange(test):
total_reward = 0
current_state = env.reset()
for j in range(100):
env.render()
action = agent.get_action(session, current_state)
#q_v= agent.critic.get_Q_Value_critic( session,current_state[0],action)
#q_value.append(q_v)ne
current_state, reward, done, info = env.step(action[0])
#print "current state",current_state
rewards.append(reward)
if done:
print "done bei j=", j
print "done"
env.monitor.close()
print "--Mean reward ",np.mean(rewards)," ", np.max(rewards)," ",np.min(rewards)
Mean_reward.append(np.mean(rewards))
max_reward.append(np.max(rewards))
min_reward.append(np.min(rewards))
print("---Avarage_reward---", np.mean(rewards))
def plot_policy(agent,session,episode):
x = np.arange(-1, 1, 0.025)
y = np.arange(-1, 1, 0.025)
X, Y = pyl.meshgrid(x, y)
sample = np.ones([80, 80])
a = []
print "sample", sample
for i in range(len(X)):
for j in range(len(X)):
a.append([X[i][j], Y[i][j], X[i][j]])
sample = agent.get_sample_action(session, a)
sample = agent.get_critic_Q(session, a, sample)
sample = np.reshape(sample, [80, 80])
print "---size a", sample.shape
plot_function_countur2D(-1, 1.0, sample, episode)
plot_function_countur3D(-1, 1.0, sample, episode)
def validation_env( env ):
act_space = env.action_space;
obs_space = env.observation_space;
if not type(act_space) == gym.spaces.box.Box:
raise RuntimeError('Environment is no continous action space ');
if not type(obs_space) == gym.spaces.box.Box:
raise RuntimeError('Environment is no continous observation space ');
def noise_act(act_dim):
mu = 0;
sigma = 0.2;
theta = 0.15;
x = np.ones(act_dim) * mu;
dx = theta * (mu - x) + sigma * nr.randn(len(x))
noise = x + dx
return noise
def plot_reward():
l=len(Mean_reward)
x = np.arange(l)
print "Mean Reward ", Mean_reward
#plt.subplots(2, 1)
plt.figure()
plt.plot(x, Mean_reward, 'b-')
plt.ion()
plt.show(False)
time.sleep(1)
plt.draw()
#plt.plot(x, max_reward,'b--')
#plt.plot(x, min_reward,'b--' )
#plt.plot(x, Mean_q, 'g--')
def plot_perfomance(num):
l = len(num)
x = np.arange(l)
plt.figure(2)
plt.ion()
plt.plot(x, num, 'bo')
plt.show()
time.sleep(1)
def z_function(x,y):
return ((x**2+y**3)*np.exp(-(x**2+y**1)/2))
from mpl_toolkits.mplot3d import Axes3D
def plot_function_countur2D(min,max,action,episode):
x = np.arange(min, max, 0.025)
y = np.arange(min, max, 0.025)
# meshgrid create a matrix von min to max with 0.1 as distance
# the output is 2 matrix where one is the transport of the other one
X, Y = pyl.meshgrid(x, y)
# Z=z_function(X,np.transpose(X))
Z = action
print Y
# ==========
# 2D contur
# ==========
plt.figure()
im = plt.imshow(action, cmap=plt.cm.RdBu)
cset = plt.contour(action, x, linewidths=2, cmap=plt.cm.Set2)
plt.colorbar(im)
plt.clabel(cset, inline=True, fmt='%1.1f', fontsize=10)
plt.clabel(cset, inline=True, fmt='%1.1f', fontsize=10)
plt.ion()
plt.show(False)
time.sleep(2)
initial = False
plt.draw()
name_file='exp/episode2D'+str(episode) +'.png'
print name_file.__str__()
plt.savefig(name_file)
plt.close()
def plot_function_countur3D(min,max,action,episode):
x=np.arange(min,max,0.025)
y=np.arange(min,max,0.025)
# meshgrid create a matrix von min to max with 0.1 as distance
# the output is 2 matrix where one is the transport of the other one
X, Y = pyl.meshgrid(x, y)
#Z=z_function(X,np.transpose(X))
Z=action
print Y
#==========
# 3D
#==========
fig=plt.figure()
#plt.ion()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(Y, X, Z, rstride=1, cstride=1,
cmap=plt.cm.RdBu, linewidth=0, antialiased=False)
ax.set_xlim([min, max])
ax.set_ylim([min, max])
ax.zaxis.set_major_locator(plt.LinearLocator(3))
ax.zaxis.set_major_formatter(plt.FormatStrFormatter('%.02f'))
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show(False)
plt.draw()
name_file = 'exp/episode3D' + str(episode) + '.png'
print name_file.__str__()
plt.savefig(name_file.__str__())
plt.close()
# ==========
# 2D contur
# ==========
#im = plt.imshow(action, cmap=plt.cm.RdBu)
#cset = plt.contour(action, x, linewidths=2, cmap=plt.cm.Set2)
#plt.clabel(cset, inline=True, fmt='%1.1f', fontsize=10)
#plt.clabel(cset, inline=True, fmt='%1.1f', fontsize=10)
#plt.colorbar(im)
#fig=plt.figure(1)
#plt.ion()
#plt.show(False)
##time.sleep(2)
#initial=False
#plt.draw()
#time.sleep(2)
#plt.close()
if __name__ == '__main__':
main()
# env.monitor.close()
# filter = fil.filter(env);
# self.env = filter_env.makeFilteredEnv()
# print all variable into Enviroment
# Create agend DDPG and gob per paramter obs and action
# run a for with total training time
# run a for with