-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSERL-I.py
339 lines (281 loc) · 13.6 KB
/
SERL-I.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
import numpy as np, os, time, random
from core import mod_neuro_evo as utils_ne
from core import mod_utils as utils
import gym, torch
from core import replay_memory
from core import ddpg as ddpg
import argparse
from copy import deepcopy
# Parsers
render = False
parser = argparse.ArgumentParser()
parser.add_argument('--env', help='Environment Choices: (HalfCheetah-v2) (Ant-v2) (Reacher-v2) (Walker2d-v2) (Swimmer-v2) (Hopper-v2)', default='Hopper-v2')
parser.add_argument('--seed', help='seed', default=1)
parser.add_argument('--alpha', help='Control factor for the candidate population size', type=float, default=1.0)
parser.add_argument('--deviceid', help='GPU device ID', default=1)
env_tag = vars(parser.parse_args())['env']
seed = int(vars(parser.parse_args())['seed'])
alpha = float(vars(parser.parse_args())['alpha'])
# Set GPU
device = str(vars(parser.parse_args())['deviceid'])
os.environ["CUDA_VISIBLE_DEVICES"] = device
# Print
print("---------------------------------------------------")
print("Env name: ", env_tag)
print("Seed: ", seed)
print("Alpha: ", alpha)
print("GPU device: ", os.environ["CUDA_VISIBLE_DEVICES"] )
print("---------------------------------------------------")
# ERL Parameters
class Parameters:
def __init__(self):
# Number of Frames to Run
if env_tag == 'Ant-v2' or env_tag == 'HalfCheetah-v2':
self.num_frames = 6000000
else:
self.num_frames = 3000000
# Use CUDA
self.is_cuda = True; self.is_memory_cuda = True
# Set the device to run on CUDA or CPU
if self.is_cuda and torch.cuda.is_available():
self.device = torch.device('cuda')
else:
self.device = torch.device('cpu')
# Sync Period
if env_tag == "Swimmer-v2":
self.synch_period = 10
else:
self.synch_period = 1
# DDPG params
self.use_ln = True
self.gamma = 0.99; self.tau = 0.001
self.seed = seed
self.batch_size = 128
self.buffer_size = 1000000
self.frac_frames_train = 1.0
self.use_done_mask = True
###### NeuroEvolution Params ########
# Num of trials
if env_tag == 'Hopper-v2' or env_tag == 'Reacher-v2': self.num_evals = 5
elif env_tag == 'Walker2d-v2': self.num_evals = 3
else: self.num_evals = 1
# Elitism Rate
if env_tag == 'Hopper-v2' or env_tag == 'Ant-v2': self.elite_fraction = 0.3
elif env_tag == 'Reacher-v2' or env_tag == 'Walker2d-v2': self.elite_fraction = 0.2
else: self.elite_fraction = 0.1
self.pop_size = 10
self.crossover_prob = 0.0
self.mutation_prob = 0.9
# Individual-based control
self.sigma = 0.01
self.mutactor=None
self.evaluation_memory_size = 50000
self.surro_batch_size = 1024
# Logs folder
self.state_dim = None; self.action_dim = None #Simply instantiate them here, will be initialized later
self.save_foldername = env_tag +'/'+ f'SERL-I-{alpha}-{seed}/'
if not os.path.exists(self.save_foldername):
os.makedirs(self.save_foldername)
# SERL Agent
class Agent:
def __init__(self, args, env):
self.args = args
self.env = env
self.evolver = utils_ne.SSNE(self.args)
self.best_actor = ddpg.Actor(args)
# Parameters of SC
self.eval_memory_size = self.args.evaluation_memory_size
self.alpha = alpha
self.surro_critic = None
# Init population
self.pop = []
for _ in range(args.pop_size):
self.pop.append(ddpg.Actor(args))
# Turn off gradients and put in eval mode
for actor in self.pop: actor.eval()
# Init RL Agent
self.rl_agent = ddpg.DDPG(args)
self.replay_buffer = replay_memory.ReplayMemory(args.buffer_size)
self.ounoise = ddpg.OUNoise(args.action_dim)
self.dna_length = self.rl_agent.actor.count_parameters()
# Trackers
self.num_games = 0; self.num_frames = 0; self.gen_frames = None
# Functions of Surrogate-assisted Controller
# --------------------------------------------------------
def exchange_para(self, source_net, target_net):
for target_param, param in zip(target_net.parameters(), source_net.parameters()):
target_param.data.copy_(param.data)
def surro_evaluate(self, net, memory):
fitness = 0
datas = replay_memory.Transition(*zip(*memory))
states = torch.cat(datas.state)
n = len(states)
arr = np.arange(n)
for i in range(n // self.args.surro_batch_size):
batch_index = arr[self.args.surro_batch_size * i: self.args.surro_batch_size * (i + 1)]
batch_index = torch.LongTensor(batch_index)
inputs = states[batch_index]
actions = net(inputs)
surro_rewards = self.surro_critic(inputs, actions)
fitness += np.sum(surro_rewards.data.cpu().numpy())
return fitness / n
def add_experience(self, state, action, next_state, reward, done):
reward = utils.to_tensor(np.array([reward])).unsqueeze(0)
if self.args.is_cuda: reward = reward.cuda()
if self.args.use_done_mask:
done = utils.to_tensor(np.array([done]).astype('uint8')).unsqueeze(0)
if self.args.is_cuda: done = done.cuda()
action = utils.to_tensor(action)
if self.args.is_cuda: action = action.cuda()
self.replay_buffer.push(state, action, next_state, reward, done)
def evaluate(self, net, is_render=False, is_action_noise=False, store_transition=True):
total_reward = 0.0
state = self.env.reset()
state = utils.to_tensor(state).unsqueeze(0)
if self.args.is_cuda: state = state.cuda()
done = False
while not done:
if store_transition: self.num_frames += 1; self.gen_frames += 1
if render and is_render: self.env.render()
action = net.forward(state)
action.clamp(-1,1)
action = utils.to_numpy(action.cpu())
if is_action_noise: action += self.ounoise.noise()
next_state, reward, done, info = self.env.step(action.flatten()) #Simulate one step in environment
next_state = utils.to_tensor(next_state).unsqueeze(0)
if self.args.is_cuda:
next_state = next_state.cuda()
total_reward += reward
if store_transition:
self.add_experience(state, action, next_state, reward, done)
state = next_state
if store_transition: self.num_games += 1
return total_reward
def rl_to_evo(self, rl_net, evo_net):
for target_param, param in zip(evo_net.parameters(), rl_net.parameters()):
target_param.data.copy_(param.data)
def mutate_actor(self, base_para):
surro_actor = deepcopy(self.rl_agent.actor)
gauss_noise = np.random.randn(self.dna_length)
theta = base_para + self.args.sigma * gauss_noise
surro_actor.inject_parameters(torch.tensor(theta).to(self.args.device))
return surro_actor
def train(self):
self.gen_frames = 0
####################### EVOLUTION #####################
all_fitness = []
# Pre-selection
if self.num_games>2:
pre_pop = []
if env_tag == 'Swimmer-v2':
base_para = self.best_actor.extract_parameters()
base_para = base_para.cpu().numpy()
else:
base_para = self.rl_agent.actor.extract_parameters()
base_para = base_para.cpu().numpy()
# Mutate
for idx in range(int(self.alpha*self.args.pop_size)):
pre_actor = self.mutate_actor(base_para=base_para)
self.pop.append(pre_actor)
print("######## Preselection ###########")
pre_fitness = []
self.surro_critic = deepcopy(self.rl_agent.critic)
if len(self.replay_buffer) < self.eval_memory_size:
evaluation_memory = self.replay_buffer.memory
else:
evaluation_memory = self.replay_buffer.memory[-self.eval_memory_size::]
for net in self.pop:
fitness = self.surro_evaluate(net, memory=evaluation_memory)
pre_fitness.append(fitness)
# Get candidates
pre_select_index = np.argsort(pre_fitness)[-(self.args.pop_size - 1):]
print("Preselection index:", pre_select_index)
if self.evolver.rl_policy is not None:
if self.evolver.rl_policy in pre_select_index:
self.evolver.rl_policy = list(pre_select_index).index(self.evolver.rl_policy)
else:
self.evolver.rl_policy = None
for index in pre_select_index:
pre_pop.append(self.pop[index])
pre_pop.append(self.best_actor)
self.pop = pre_pop
print("######## Real Fitness Evalution ###########")
for net in self.pop:
fitness = 0.0
for eval in range(self.args.num_evals): fitness += self.evaluate(net, is_render=False, is_action_noise=False)
all_fitness.append(fitness/self.args.num_evals)
# Validation test
champ_index = all_fitness.index(max(all_fitness))
best_train_fitness = max(all_fitness)
worst_index = all_fitness.index(min(all_fitness))
test_score = 0.0
# For Elite protection
self.best_actor = deepcopy(self.pop[champ_index])
# Report best fitness
for eval in range(5): test_score += self.evaluate(self.best_actor, is_render=False, is_action_noise=False, store_transition=False)/5.0
# NeuroEvolution's probabilistic selection and recombination step
elite_index, all_elites, unselects = self.evolver.epoch(self.pop, all_fitness)
####################### DDPG Part#########################
#DDPG Experience Collection
rl_score = self.evaluate(self.rl_agent.actor, is_render=False, is_action_noise=True) #Train
# Validation test for RL agent
testr = 0
for eval in range(5):
testr += self.evaluate(self.rl_agent.actor, is_render=False, store_transition=False, is_action_noise=False)/5
# DDPG learning step
if len(self.replay_buffer) > self.args.batch_size * 5:
for _ in range(int(self.gen_frames*self.args.frac_frames_train)):
transitions = self.replay_buffer.sample(self.args.batch_size)
batch = replay_memory.Transition(*zip(*transitions))
self.rl_agent.update_parameters(batch)
# Synch RL Agent to NE
# Elite protection
if self.num_games % self.args.synch_period == 0:
if worst_index not in all_elites:
print("worst_index not in all_elites, RL change to population, index:", worst_index)
self.rl_to_evo(self.rl_agent.actor, self.pop[worst_index])
self.evolver.rl_policy = worst_index
self.evolver.best_real_policy = champ_index
print('Synch from RL --> Nevo')
else:
if len(unselects) > 0:
print("RL change to population, index:", unselects[-1])
self.rl_to_evo(self.rl_agent.actor, self.pop[unselects[-1]])
self.evolver.rl_policy = unselects[-1]
print('Synch from RL --> Nevo')
else:
print("RL change to population, all elite index:", all_elites[-1])
self.rl_to_evo(self.rl_agent.actor, self.pop[all_elites[-1]])
self.evolver.rl_policy = all_elites[-1]
print('Synch from RL --> Nevo')
return best_train_fitness, test_score, elite_index
if __name__ == "__main__":
parameters = Parameters()
tracker = utils.Tracker(parameters, ['serl'], '_score.csv')
frame_tracker = utils.Tracker(parameters, ['frame_serl'], '_score.csv')
time_tracker = utils.Tracker(parameters, ['time_serl'], '_score.csv')
# Create Env
env = utils.NormalizedActions(gym.make(env_tag))
parameters.action_dim = env.action_space.shape[0]
parameters.state_dim = env.observation_space.shape[0]
# Seed
env.seed(parameters.seed)
torch.manual_seed(parameters.seed); np.random.seed(parameters.seed); random.seed(parameters.seed)
# Create SERL Agent
agent = Agent(parameters, env)
next_save = 100; time_start = time.time()
generation = 0
# Begin training
while agent.num_frames <= parameters.num_frames:
best_train_fitness, erl_score, elite_index = agent.train()
generation +=1
if best_train_fitness != 0 and erl_score != 0:
print('#Games:', agent.num_games, '#Frames:', agent.num_frames, ' Epoch_Max:', '%.2f'%best_train_fitness if best_train_fitness != None else None, ' Test_Score:','%.2f'%erl_score if erl_score != None else None, ' Avg:','%.2f'%tracker.all_tracker[0][1], 'ENV '+env_tag)
tracker.update([erl_score], agent.num_games)
frame_tracker.update([erl_score], agent.num_frames)
time_tracker.update([erl_score], time.time()-time_start)
#Save Policy
if agent.num_games > next_save:
next_save += 100
if elite_index != None: torch.save(agent.pop[elite_index].state_dict(), parameters.save_foldername + 'evo_net.pkl')
print("Progress Saved !!!!!")