-
Notifications
You must be signed in to change notification settings - Fork 0
/
td3_launcher_step_study.py
279 lines (223 loc) · 9.33 KB
/
td3_launcher_step_study.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
# !/usr/bin/env python3
import argparse
from copy import deepcopy
import gym
import gym.spaces
import numpy as np
import pandas as pd
import torch.multiprocessing as mp
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
from td3 import TD3
from models import RLNN
from random_process import *
from util import *
from memory import Memory, SharedMemory
save_average_mu_in_csv = True #enregistrer les différents mu average de l'actor dans un csv pendant l'apprentissage
USE_CUDA = torch.cuda.is_available()
if USE_CUDA:
FloatTensor = torch.cuda.FloatTensor
else:
FloatTensor = torch.FloatTensor
def evaluate(actor, env, memory=None, n_episodes=1, random=False, noise=None, render=False):
"""
Computes the score of an actor on a given number of runs
"""
if not random:
def policy(state):
state = FloatTensor(state.reshape(-1))
action = actor(state).cpu().data.numpy().flatten()
if noise is not None:
action += noise.sample()
return np.clip(action, -max_action, max_action)
else:
def policy(state):
return env.action_space.sample()
scores = []
steps = 0
for _ in range(n_episodes):
score = 0
obs = deepcopy(env.reset())
done = False
while not done:
# get next action and act
action = policy(obs)
n_obs, reward, done, info = env.step(action)
done_bool = 0 if steps + \
1 == env._max_episode_steps else float(done)
score += reward
steps += 1
# adding in memory
if memory is not None:
memory.add((obs, n_obs, action, reward, done_bool))
obs = n_obs
# render if needed
if render:
env.render()
# reset when done
if done:
env.reset()
scores.append(score)
return np.mean(scores), steps
def train(run, n_episodes, output=None, debug=False, render=False):
"""
Train the whole process
"""
total_steps = 0
step_cpt = 0
n = 0
plot_y_axis_train = []
plot_x_axis_train = []
plot_y_axis_eval = []
plot_x_axis_eval = []
df = pd.DataFrame(columns=["total_steps", "average_score", "best_score"] +
["score_{}".format(i) for i in range(args.n_actor)])
while total_steps < args.max_steps:
random = total_steps < args.start_steps
actor_steps = 0
# training the agent
f, s = evaluate(agent.actor, env, n_episodes=n_episodes,
noise=a_noise, random=random, memory=memory, render=render)
actor_steps += s
total_steps += s
step_cpt += s
plot_y_axis_train.append(f)
plot_x_axis_train.append(total_steps)
# print score
prCyan('noisy RL agent fitness:{}'.format(f))
agent.train(actor_steps)
# saving models and scores
if step_cpt >= args.period:
step_cpt = 0
f, _ = evaluate(agent.actor, env, n_episodes=args.n_eval)
prRed('Actor Fitness:{}'.format(f))
#save fitness :
if(save_average_mu_in_csv):
#on sauvegarde au fur-et-à-mesure les Mu moyen obtenus pendant l'apprentissage dans un csv
save_mu = open("pendulum_td3.csv", 'a')
save_mu.write(str(total_steps)+","+str(f)+"\r\n")#+ " : "+str(es.mu))
save_mu.close
plot_y_axis_eval.append(f)
plot_x_axis_eval.append(total_steps)
df.to_pickle(output + "/log.pkl")
res = {"total_steps": total_steps, "score": f}
#if args.save_all_models:
#os.makedirs(output + "/td3_run_{}_{}_steps".format(str(1+run),total_steps),exist_ok=True)
agent.actor.save_model("actors", "actor_td3_"+str(args.env)+"_"+str(run)+"_"+str(total_steps)+"_"+str(f))
step_cpt = 0
print(res)
# printing iteration resume
if debug:
prPurple('Iteration#{}: Total steps:{} \n'.format(
n, total_steps))
plt.figure()
plt.plot(plot_x_axis_train, plot_y_axis_train, c='b')
plt.plot(plot_x_axis_eval, plot_y_axis_eval, c='r')
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--mode', default='train', type=str,)
parser.add_argument('--env', default='Pendulum-v0', type=str)
parser.add_argument('--start_steps', default=1000, type=int)
# DDPG parameters
parser.add_argument('--actor_lr', default=0.01, type=float)
parser.add_argument('--critic_lr', default=0.02, type=float)
parser.add_argument('--batch_size', default=100, type=int)
parser.add_argument('--discount', default=1, type=float)
parser.add_argument('--reward_scale', default=1., type=float)
parser.add_argument('--tau', default=0.005, type=float)
parser.add_argument('--layer_norm', dest='layer_norm', action='store_true')
# TD3 parameters
parser.add_argument('--use_td3', dest='use_td3', action='store_false')
parser.add_argument('--policy_noise', default=0.2, type=float)
parser.add_argument('--noise_clip', default=0.5, type=float)
parser.add_argument('--policy_freq', default=2, type=int)
# Gaussian noise parameters
parser.add_argument('--gauss_sigma', default=0.1, type=float)
# OU process parameters
parser.add_argument('--ou_noise', dest='ou_noise', action='store_true')
parser.add_argument('--ou_theta', default=0.15, type=float)
parser.add_argument('--ou_sigma', default=0.2, type=float)
parser.add_argument('--ou_mu', default=0.0, type=float)
# Parameter noise parameters
parser.add_argument('--param_init_std', default=0.01, type=float)
parser.add_argument('--param_scale', default=0.2, type=float)
parser.add_argument('--param_adapt', default=1.01, type=float)
# Training parameters
parser.add_argument('--n_actor', default=1, type=int)
parser.add_argument('--n_episodes', default=1, type=int)
parser.add_argument('--n_eval', default=10, type=int)
parser.add_argument('--period', default=5000, type=int)
parser.add_argument('--max_steps', default=40000, type=int)#1000000
parser.add_argument('--mem_size', default=100000, type=int)
parser.add_argument('--env_max_steps', default=-1, type=int)# -1 for env default value
parser.add_argument('--nbRuns', default=50,type=int)
# Testing parameters
parser.add_argument('--filename', default="", type=str)
parser.add_argument('--n_test', default=1, type=int)
# misc
parser.add_argument('--output', default="actors", type=str)
parser.add_argument('--save_all_models',
dest="save_all_models", action="store_true")
parser.add_argument('--debug', dest='debug', action='store_true')
parser.add_argument('--seed', default=-1, type=int)
parser.add_argument('--render', dest='render', action='store_false')
parser.add_argument('--video_max_steps', default=1000, type=int)
args = parser.parse_args()
args.output = get_output_folder(args.output, args.env)
with open(args.output + "/parameters.txt", 'w') as file:
for key, value in vars(args).items():
file.write("{} = {}\n".format(key, value))
# The environment
env = gym.make(args.env)
if args.env_max_steps > 0 :
env._max_episode_steps = args.env_max_steps
state_dim = env.observation_space.shape[0]
action_dim = env.action_space.shape[0]
max_action = int(env.action_space.high[0])
# Random seed
if args.seed > 0:
np.random.seed(args.seed)
env.seed(args.seed)
torch.manual_seed(args.seed)
# replay buffer
memory = Memory(args.mem_size, state_dim, action_dim)
# action noise
if args.ou_noise:
a_noise = OrnsteinUhlenbeckProcess(
action_dim, mu=args.ou_mu, theta=args.ou_theta, sigma=args.ou_sigma)
else:
a_noise = GaussianNoise(action_dim, sigma=args.gauss_sigma)
for run in range(0,1):#args.nbRuns):
memory = Memory(args.mem_size, state_dim, action_dim)
# agent
agent = TD3(state_dim, action_dim, max_action, memory, args)
if args.mode == 'train':
train(run,n_episodes=args.n_episodes, output=args.output, debug=args.debug, render=False)#modif en brut
else:
raise RuntimeError('undefined mode {}'.format(args.mode))
env.close()
"""
#########################################
###### TEST DE L'AGENT (vidéo) : #####
#########################################
#création d'un environnement
env = gym.make(args.env)
#ajout d'un moniteur pour l'enregistrement vidéo sur l'environnement :
env = gym.wrappers.Monitor(env, './video',force=True)
#itialisation de l'environnement :
state = env.reset()
#itérations sur les actions choisies par l'acteur entrainté :
for _ in range(args.video_max_steps):
#choix de l'action par l'acteur entrainté :
action = agent.select_action(state)
#on effectue cette action et on récupère le nouvel état :
state, reward, done, _ = env.step(action)
#on recommence un nouvel épisode si on a complété le précédent :
if(done):
state = env.reset()
#fermeture de l'environnement :
env.close()
"""