forked from AI4Finance-Foundation/ElegantRL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelloworld_SAC_TD3_single_file.py
656 lines (533 loc) Β· 31.4 KB
/
helloworld_SAC_TD3_single_file.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
import os
import sys
import time
from copy import deepcopy
import gym
import numpy as np
import torch
import torch.nn as nn
from torch import Tensor
class Config: # for off-policy
def __init__(self, agent_class=None, env_class=None, env_args=None):
self.agent_class = agent_class # agent = agent_class(...)
self.if_off_policy = True # whether off-policy or on-policy of DRL algorithm
self.env_class = env_class # env = env_class(**env_args)
self.env_args = env_args # env = env_class(**env_args)
if env_args is None: # dummy env_args
env_args = {'env_name': None, 'state_dim': None, 'action_dim': None, 'if_discrete': None}
self.env_name = env_args['env_name'] # the name of environment. Be used to set 'cwd'.
self.state_dim = env_args['state_dim'] # vector dimension (feature number) of state
self.action_dim = env_args['action_dim'] # vector dimension (feature number) of action
self.if_discrete = env_args['if_discrete'] # discrete or continuous action space
'''Arguments for reward shaping'''
self.gamma = 0.99 # discount factor of future rewards
self.reward_scale = 1.0 # an approximate target reward usually be closed to 256
'''Arguments for training'''
self.net_dims = (64, 32) # the middle layer dimension of MLP (MultiLayer Perceptron)
self.learning_rate = 1e-4 # 2 ** -14 ~= 6e-5
self.soft_update_tau = 5e-3 # 2 ** -8 ~= 5e-3
self.state_value_tau = 0.1 # 0.05 ~ 0.50
self.batch_size = int(64) # num of transitions sampled from replay buffer.
self.horizon_len = int(256) # collect horizon_len step while exploring, then update network
self.buffer_size = int(1e6) # ReplayBuffer size. First in first out for off-policy.
self.repeat_times = 1.0 # repeatedly update network using ReplayBuffer to keep critic's loss small
'''Arguments for device'''
self.gpu_id = int(0) # `int` means the ID of single GPU, -1 means CPU
self.thread_num = int(8) # cpu_num for pytorch, `torch.set_num_threads(self.num_threads)`
self.random_seed = int(0) # initialize random seed in self.init_before_training()
'''Arguments for evaluate'''
self.cwd = None # current working directory to save model. None means set automatically
self.if_remove = True # remove the cwd folder? (True, False, None:ask me)
self.break_step = +np.inf # break training if 'total_step > break_step'
self.eval_times = int(16) # number of times that get episodic cumulative return
self.eval_per_step = int(1e4) # evaluate the agent per training steps
def init_before_training(self):
if self.cwd is None: # set cwd (current working directory) for saving model
self.cwd = f'./{self.env_name}_{self.agent_class.__name__[5:]}'
os.makedirs(self.cwd, exist_ok=True)
class ActorBase(nn.Module): # todo state_norm
def __init__(self, state_dim: int, action_dim: int):
super().__init__()
self.state_dim = state_dim
self.action_dim = action_dim
self.net = None # build_mlp(dims=[state_dim, *dims, action_dim])
self.ActionDist = torch.distributions.normal.Normal
self.action_std = None
self.state_avg = nn.Parameter(torch.zeros((state_dim,)), requires_grad=False)
self.state_std = nn.Parameter(torch.ones((state_dim,)), requires_grad=False)
def state_norm(self, state: Tensor) -> Tensor:
return (state - self.state_avg) / self.state_std # todo state_norm
class Actor(ActorBase):
def __init__(self, dims: [int], state_dim: int, action_dim: int):
super().__init__(state_dim=state_dim, action_dim=action_dim)
self.net = build_mlp(dims=[state_dim, *dims, action_dim])
def forward(self, state: Tensor) -> Tensor:
state = self.state_norm(state)
action = self.net(state)
return action.tanh()
def get_action(self, state: Tensor) -> Tensor: # for exploration
state = self.state_norm(state)
action_avg = self.net(state).tanh()
dist = self.ActionDist(action_avg, self.action_std)
action = dist.sample()
return action.clip(-1.0, 1.0)
class ActorSAC(ActorBase):
def __init__(self, dims: [int], state_dim: int, action_dim: int):
super().__init__(state_dim=state_dim, action_dim=action_dim)
self.enc_s = build_mlp(dims=[state_dim, *dims]) # encoder of state
self.dec_a_avg = build_mlp(dims=[dims[-1], action_dim]) # decoder of action mean
self.dec_a_std = build_mlp(dims=[dims[-1], action_dim]) # decoder of action log_std
self.soft_plus = nn.Softplus()
def forward(self, state: Tensor) -> Tensor:
state = self.state_norm(state)
state_tmp = self.enc_s(state) # temporary tensor of state
return self.dec_a_avg(state_tmp).tanh() # action
def get_action(self, state: Tensor) -> Tensor: # for exploration
state = self.state_norm(state)
state_tmp = self.enc_s(state) # temporary tensor of state
action_avg = self.dec_a_avg(state_tmp)
action_std = self.dec_a_std(state_tmp).clamp(-20, 2).exp()
noise = torch.randn_like(action_avg, requires_grad=True)
action = action_avg + action_std * noise
return action.tanh() # action (re-parameterize)
def get_action_logprob(self, state: Tensor) -> [Tensor, Tensor]:
state = self.state_norm(state)
state_tmp = self.enc_s(state) # temporary tensor of state
action_log_std = self.dec_a_std(state_tmp).clamp(-20, 2)
action_std = action_log_std.exp()
action_avg = self.dec_a_avg(state_tmp)
noise = torch.randn_like(action_avg, requires_grad=True)
action = action_avg + action_std * noise
logprob = -action_log_std - noise.pow(2) * 0.5 - np.log(np.sqrt(2 * np.pi))
# dist = self.Normal(action_avg, action_std)
# action = dist.sample()
# logprob = dist.log_prob(action)
'''fix logprob by adding the derivative of y=tanh(x)'''
logprob -= (np.log(2.) - action - self.soft_plus(-2. * action)) * 2. # better than below
# logprob -= (1.000001 - action.tanh().pow(2)).log()
return action.tanh(), logprob.sum(1, keepdim=True)
class CriticBase(nn.Module): # todo state_norm, value_norm
def __init__(self, state_dim: int, action_dim: int):
super().__init__()
self.state_dim = state_dim
self.action_dim = action_dim
self.net = None # build_mlp(dims=[state_dim + action_dim, *dims, 1])
self.state_avg = nn.Parameter(torch.zeros((state_dim,)), requires_grad=False)
self.state_std = nn.Parameter(torch.ones((state_dim,)), requires_grad=False)
self.value_avg = nn.Parameter(torch.zeros((1,)), requires_grad=False)
self.value_std = nn.Parameter(torch.ones((1,)), requires_grad=False)
def state_norm(self, state: Tensor) -> Tensor:
return (state - self.state_avg) / self.state_std # todo state_norm
def value_re_norm(self, value: Tensor) -> Tensor:
return value * self.value_std + self.value_avg # todo value_norm
class CriticTwin(CriticBase):
def __init__(self, dims: [int], state_dim: int, action_dim: int):
super().__init__(state_dim=state_dim, action_dim=action_dim)
self.enc_sa = build_mlp(dims=[state_dim + action_dim, *dims]) # encoder of state and action
self.dec_q1 = build_mlp(dims=[dims[-1], 1]) # decoder of Q value 1
self.dec_q2 = build_mlp(dims=[dims[-1], 1]) # decoder of Q value 2
def forward(self, state: Tensor, action: Tensor) -> Tensor:
state = self.state_norm(state)
sa_tmp = self.enc_sa(torch.cat((state, action), dim=1))
value = self.dec_q1(sa_tmp)
value = self.value_re_norm(value)
return value # Q value
def get_q1_q2(self, state, action):
state = self.state_norm(state)
sa_tmp = self.enc_sa(torch.cat((state, action), dim=1))
value1 = self.value_re_norm(self.dec_q1(sa_tmp))
value2 = self.value_re_norm(self.dec_q2(sa_tmp))
return value1, value2 # two Q values
def build_mlp(dims: [int]) -> nn.Sequential: # MLP (MultiLayer Perceptron)
net_list = []
for i in range(len(dims) - 1):
net_list.extend([nn.Linear(dims[i], dims[i + 1]), nn.ReLU()])
del net_list[-1] # remove the activation of output layer
return nn.Sequential(*net_list)
def get_gym_env_args(env, if_print: bool) -> dict:
if {'unwrapped', 'observation_space', 'action_space', 'spec'}.issubset(dir(env)): # isinstance(env, gym.Env):
env_name = env.unwrapped.spec.id
state_shape = env.observation_space.shape
state_dim = state_shape[0] if len(state_shape) == 1 else state_shape # sometimes state_dim is a list
if_discrete = isinstance(env.action_space, gym.spaces.Discrete)
action_dim = env.action_space.n if if_discrete else env.action_space.shape[0]
else:
env_name = env.env_name
state_dim = env.state_dim
action_dim = env.action_dim
if_discrete = env.if_discrete
env_args = {'env_name': env_name, 'state_dim': state_dim, 'action_dim': action_dim, 'if_discrete': if_discrete}
print(f"env_args = {repr(env_args)}") if if_print else None
return env_args
def kwargs_filter(function, kwargs: dict) -> dict:
import inspect
sign = inspect.signature(function).parameters.values()
sign = {val.name for val in sign}
common_args = sign.intersection(kwargs.keys())
return {key: kwargs[key] for key in common_args} # filtered kwargs
def build_env(env_class=None, env_args=None):
if env_class.__module__ == 'gym.envs.registration': # special rule
assert '0.18.0' <= gym.__version__ <= '0.25.2' # pip3 install gym==0.24.0
env = env_class(id=env_args['env_name'])
else:
env = env_class(**kwargs_filter(env_class.__init__, env_args.copy()))
for attr_str in ('env_name', 'state_dim', 'action_dim', 'if_discrete'):
setattr(env, attr_str, env_args[attr_str])
return env
class ReplayBuffer: # for off-policy
def __init__(self, max_size: int, state_dim: int, action_dim: int, gpu_id: int = 0):
self.p = 0 # pointer
self.if_full = False
self.cur_size = 0
self.add_size = 0
self.max_size = max_size
self.device = torch.device(f"cuda:{gpu_id}" if (torch.cuda.is_available() and (gpu_id >= 0)) else "cpu")
self.states = torch.empty((max_size, state_dim), dtype=torch.float32, device=self.device)
self.actions = torch.empty((max_size, action_dim), dtype=torch.float32, device=self.device)
self.rewards = torch.empty((max_size, 1), dtype=torch.float32, device=self.device)
self.undones = torch.empty((max_size, 1), dtype=torch.float32, device=self.device)
def update(self, items: [Tensor]):
states, actions, rewards, undones = items
add_size = rewards.shape[0]
p = self.p + add_size # pointer
if p > self.max_size:
self.if_full = True
p0 = self.p
p1 = self.max_size
p2 = self.max_size - self.p
p = p - self.max_size
self.states[p0:p1], self.states[0:p] = states[:p2], states[-p:]
self.actions[p0:p1], self.actions[0:p] = actions[:p2], actions[-p:]
self.rewards[p0:p1], self.rewards[0:p] = rewards[:p2], rewards[-p:]
self.undones[p0:p1], self.undones[0:p] = undones[:p2], undones[-p:]
else:
self.states[self.p:p] = states
self.actions[self.p:p] = actions
self.rewards[self.p:p] = rewards
self.undones[self.p:p] = undones
self.p = p
self.add_size = add_size
self.cur_size = self.max_size if self.if_full else self.p
def sample(self, batch_size: int) -> [Tensor]:
ids = torch.randint(self.cur_size - 1, size=(batch_size,), requires_grad=False)
return self.states[ids], self.actions[ids], self.rewards[ids], self.undones[ids], self.states[ids + 1]
def slice(self, data: Tensor, slice_size: int) -> Tensor:
slice_data = data[self.p - slice_size:self.p] if slice_size >= self.p \
else torch.vstack((data[slice_size - self.p:], data[:self.p]))
return slice_data
class AgentBase:
def __init__(self, net_dims: [int], state_dim: int, action_dim: int, gpu_id: int = 0, args: Config = Config()):
self.state_dim = state_dim
self.action_dim = action_dim
self.gamma = args.gamma
self.batch_size = args.batch_size
self.repeat_times = args.repeat_times
self.reward_scale = args.reward_scale
self.learning_rate = args.learning_rate
self.if_off_policy = args.if_off_policy
self.soft_update_tau = args.soft_update_tau
self.state_value_tau = args.state_value_tau
self.last_state = None # save the last state of the trajectory for training. `last_state.shape == (state_dim)`
self.device = torch.device(f"cuda:{gpu_id}" if (torch.cuda.is_available() and (gpu_id >= 0)) else "cpu")
act_class = getattr(self, "act_class", None)
cri_class = getattr(self, "cri_class", None)
self.act = self.act_target = act_class(net_dims, state_dim, action_dim).to(self.device)
self.cri = self.cri_target = cri_class(net_dims, state_dim, action_dim).to(self.device) \
if cri_class else self.act
self.act_optimizer = torch.optim.Adam(self.act.parameters(), self.learning_rate)
self.cri_optimizer = torch.optim.Adam(self.cri.parameters(), self.learning_rate) \
if cri_class else self.act_optimizer
self.criterion = torch.nn.SmoothL1Loss()
def explore_env(self, env, horizon_len: int, if_random: bool = False) -> [Tensor]:
states = torch.zeros((horizon_len, self.state_dim), dtype=torch.float32).to(self.device)
actions = torch.zeros((horizon_len, self.action_dim), dtype=torch.float32).to(self.device)
rewards = torch.zeros(horizon_len, dtype=torch.float32).to(self.device)
dones = torch.zeros(horizon_len, dtype=torch.bool).to(self.device)
state = self.last_state
get_action = self.act.get_action
for i in range(horizon_len):
action = torch.rand(self.action_dim) * 2 - 1.0 if if_random else get_action(state.unsqueeze(0))[0]
states[i] = state
ary_action = action.detach().cpu().numpy()
ary_state, reward, done, _ = env.step(ary_action)
state = torch.as_tensor(env.reset() if done else ary_state,
dtype=torch.float32, device=self.device)
actions[i] = action
rewards[i] = reward
dones[i] = done
self.last_state = state
rewards = rewards.unsqueeze(1)
undones = (1.0 - dones.type(torch.float32)).unsqueeze(1)
return states, actions, rewards, undones
@staticmethod
def optimizer_update(optimizer, objective: Tensor):
optimizer.zero_grad()
objective.backward()
optimizer.step()
@staticmethod
def soft_update(target_net: torch.nn.Module, current_net: torch.nn.Module, tau: float):
# assert target_net is not current_net
for tar, cur in zip(target_net.parameters(), current_net.parameters()):
tar.data.copy_(cur.data * tau + tar.data * (1.0 - tau))
def update_avg_std_for_state_value_norm(self, states: Tensor, returns: Tensor):
tau = self.state_value_tau
if tau == 0:
return
state_avg = states.mean(dim=0, keepdim=True)
state_std = states.std(dim=0, keepdim=True)
self.act.state_avg[:] = self.act.state_avg * (1 - tau) + state_avg * tau
self.act.state_std[:] = self.cri.state_std * (1 - tau) + state_std * tau + 1e-4
self.cri.state_avg[:] = self.act.state_avg
self.cri.state_std[:] = self.act.state_std
returns_avg = returns.mean(dim=0)
returns_std = returns.std(dim=0)
self.cri.value_avg[:] = self.cri.value_avg * (1 - tau) + returns_avg * tau
self.cri.value_std[:] = self.cri.value_std * (1 - tau) + returns_std * tau + 1e-4
class AgentTD3(AgentBase):
def __init__(self, net_dims: [int], state_dim: int, action_dim: int, gpu_id: int = 0, args: Config = Config()):
self.act_class = getattr(self, 'act_class', Actor) # get the attribute of object `self`
self.cri_class = getattr(self, 'cri_class', CriticTwin) # get the attribute of object `self`
super().__init__(net_dims, state_dim, action_dim, gpu_id, args)
self.cri_target = deepcopy(self.cri)
self.act_target = deepcopy(self.act)
self.explore_noise_std = getattr(args, 'explore_noise_std', 0.06) # standard deviation of exploration noise
self.policy_noise_std = getattr(args, 'policy_noise_std', 0.12) # standard deviation of exploration noise
self.act.action_std = self.explore_noise_std
self.update_freq = getattr(args, 'update_freq', 2) # standard deviation of exploration noise
self.horizon_len = 0
def update_net(self, buffer: ReplayBuffer) -> [float]:
self.act.action_std = self.act_target.action_std = self.policy_noise_std
with torch.no_grad():
add_states = buffer.slice(buffer.states, buffer.add_size)
add_actions = buffer.slice(buffer.actions, buffer.add_size)
add_returns = self.cri_target(add_states, add_actions)
self.update_avg_std_for_state_value_norm(states=add_states, returns=add_returns)
del add_states, add_actions, add_returns
obj_critics = obj_actors = 0.0
update_times = int(buffer.cur_size * self.repeat_times / self.batch_size)
for t in range(update_times):
obj_critic, state = self.get_obj_critic(buffer, self.batch_size)
self.optimizer_update(self.cri_optimizer, obj_critic)
self.soft_update(self.cri_target, self.cri, self.soft_update_tau)
obj_critics += obj_critic.item()
if t % self.update_freq == 0:
action = self.act(state) # policy gradient
obj_actor = (self.cri(state, action)).mean()
self.optimizer_update(self.act_optimizer, -obj_actor)
self.soft_update(self.act_target, self.act, self.soft_update_tau)
obj_actors += obj_actor.item()
self.act.action_std = self.act_target.action_std = self.explore_noise_std
return obj_critics / update_times, obj_actors / (update_times / self.update_freq)
def get_obj_critic(self, buffer, batch_size: int) -> (Tensor, Tensor):
with torch.no_grad():
state, action, reward, undone, next_state = buffer.sample(batch_size)
next_action = self.act_target.get_action(next_state) # stochastic policy
next_q = torch.min(*self.cri_target.get_q1_q2(next_state, next_action)) # twin critics
q_label = reward + undone * self.gamma * next_q
q1, q2 = self.cri.get_q1_q2(state, action)
obj_critic = (self.criterion(q1, q_label) + self.criterion(q2, q_label)) / 2.
return obj_critic, state
class AgentSAC(AgentBase):
def __init__(self, net_dims: [int], state_dim: int, action_dim: int, gpu_id: int = 0, args: Config = Config()):
self.act_class = getattr(self, 'act_class', ActorSAC) # get the attribute of object `self`
self.cri_class = getattr(self, 'cri_class', CriticTwin) # get the attribute of object `self`
super().__init__(net_dims, state_dim, action_dim, gpu_id, args)
self.cri_target = deepcopy(self.cri)
self.alpha_log = torch.tensor(-1, dtype=torch.float32, requires_grad=True, device=self.device) # trainable var
self.alpha_optim = torch.optim.Adam((self.alpha_log,), lr=args.learning_rate)
self.target_entropy = -np.log(action_dim)
def update_net(self, buffer: ReplayBuffer) -> [float]:
with torch.no_grad():
add_states = buffer.slice(buffer.states, buffer.add_size)
add_actions = buffer.slice(buffer.actions, buffer.add_size)
add_returns = self.cri_target(add_states, add_actions)
self.update_avg_std_for_state_value_norm(states=add_states, returns=add_returns)
del add_states, add_actions, add_returns
obj_critics = obj_actors = 0.0
update_times = int(buffer.cur_size * self.repeat_times / self.batch_size)
for i in range(update_times):
obj_critic, state = self.get_obj_critic(buffer, self.batch_size)
self.optimizer_update(self.cri_optimizer, obj_critic)
self.soft_update(self.cri_target, self.cri, self.soft_update_tau)
obj_critics += obj_critic.item()
action, logprob = self.act.get_action_logprob(state) # policy gradient
obj_alpha = (self.alpha_log * (-logprob + self.target_entropy).detach()).mean()
self.optimizer_update(self.alpha_optim, obj_alpha)
alpha = self.alpha_log.exp().detach()
obj_actor = (self.cri(state, action) - logprob * alpha).mean()
self.optimizer_update(self.act_optimizer, -obj_actor)
obj_actors += obj_actor.item()
return obj_critics / update_times, obj_actors / update_times
def get_obj_critic(self, buffer, batch_size: int) -> (Tensor, Tensor):
with torch.no_grad():
state, action, reward, undone, next_state = buffer.sample(batch_size)
next_action, next_logprob = self.act.get_action_logprob(next_state) # stochastic policy
next_q = torch.min(*self.cri_target.get_q1_q2(next_state, next_action)) # twin critics
alpha = self.alpha_log.exp()
q_label = reward + undone * self.gamma * (next_q - next_logprob * alpha)
q1, q2 = self.cri.get_q1_q2(state, action)
obj_critic = (self.criterion(q1, q_label) + self.criterion(q2, q_label)) / 2.
return obj_critic, state
class PendulumEnv(gym.Wrapper): # a demo of custom gym env
def __init__(self, gym_env_name=None):
gym.logger.set_level(40) # Block warning
assert '0.18.0' <= gym.__version__ <= '0.25.2' # pip3 install gym==0.24.0
if gym_env_name is None:
gym_env_name = "Pendulum-v0" if gym.__version__ < '0.18.0' else "Pendulum-v1"
super().__init__(env=gym.make(gym_env_name))
'''the necessary env information when you design a custom env'''
self.env_name = gym_env_name # the name of this env.
self.state_dim = self.observation_space.shape[0] # feature number of state
self.action_dim = self.action_space.shape[0] # feature number of action
self.if_discrete = False # discrete action or continuous action
def reset(self) -> np.ndarray: # reset the agent in env
return self.env.reset()
def step(self, action: np.ndarray) -> (np.ndarray, float, bool, dict): # agent interacts in env
# OpenAI Pendulum env set its action space as (-2, +2). It is bad.
# We suggest that adjust action space to (-1, +1) when designing a custom env.
state, reward, done, info_dict = self.env.step(action * 2)
state = state.reshape(self.state_dim)
return state, float(reward * 0.5), done, info_dict
def train_agent(args: Config):
args.init_before_training()
gpu_id = args.gpu_id
env = build_env(args.env_class, args.env_args)
agent = args.agent_class(args.net_dims, args.state_dim, args.action_dim, gpu_id=gpu_id, args=args)
agent.last_state = torch.as_tensor(env.reset(), dtype=torch.float32, device=agent.device)
buffer = ReplayBuffer(gpu_id=gpu_id, max_size=args.buffer_size,
state_dim=args.state_dim, action_dim=1 if args.if_discrete else args.action_dim, )
buffer_items = agent.explore_env(env, args.horizon_len * args.eval_times, if_random=True)
buffer.update(buffer_items) # warm up for ReplayBuffer
evaluator = Evaluator(eval_env=build_env(args.env_class, args.env_args),
eval_per_step=args.eval_per_step, eval_times=args.eval_times, cwd=args.cwd)
torch.set_grad_enabled(False)
while True: # start training
buffer_items = agent.explore_env(env, args.horizon_len)
buffer.update(buffer_items)
torch.set_grad_enabled(True)
logging_tuple = agent.update_net(buffer)
torch.set_grad_enabled(False)
evaluator.evaluate_and_save(agent.act, args.horizon_len, logging_tuple)
if (evaluator.total_step > args.break_step) or os.path.exists(f"{args.cwd}/stop"):
break # stop training when reach `break_step` or `mkdir cwd/stop`
class Evaluator:
def __init__(self, eval_env, eval_per_step: int = 1e4, eval_times: int = 8, cwd: str = '.'):
self.cwd = cwd
self.env_eval = eval_env
self.eval_step = 0
self.total_step = 0
self.start_time = time.time()
self.eval_times = eval_times # number of times that get episodic cumulative return
self.eval_per_step = eval_per_step # evaluate the agent per training steps
self.recorder = list()
print("\n| `step`: Number of samples, or total training steps, or running times of `env.step()`."
"\n| `time`: Time spent from the start of training to this moment."
"\n| `avgR`: Average value of cumulative rewards, which is the sum of rewards in an episode."
"\n| `stdR`: Standard dev of cumulative rewards, which is the sum of rewards in an episode."
"\n| `avgS`: Average of steps in an episode."
"\n| `objC`: Objective of Critic network. Or call it loss function of critic network."
"\n| `objA`: Objective of Actor network. It is the average Q value of the critic network."
f"\n| {'step':>8} {'time':>8} | {'avgR':>8} {'stdR':>6} {'avgS':>6} | {'objC':>8} {'objA':>8}")
def evaluate_and_save(self, actor, horizon_len: int, logging_tuple: tuple):
self.total_step += horizon_len
if self.eval_step + self.eval_per_step > self.total_step:
return
self.eval_step = self.total_step
rewards_steps_ary = [get_rewards_and_steps(self.env_eval, actor) for _ in range(self.eval_times)]
rewards_steps_ary = np.array(rewards_steps_ary, dtype=np.float32)
avg_r = rewards_steps_ary[:, 0].mean() # average of cumulative rewards
std_r = rewards_steps_ary[:, 0].std() # std of cumulative rewards
avg_s = rewards_steps_ary[:, 1].mean() # average of steps in an episode
used_time = time.time() - self.start_time
self.recorder.append((self.total_step, used_time, avg_r))
print(f"| {self.total_step:8.2e} {used_time:8.0f} "
f"| {avg_r:8.2f} {std_r:6.2f} {avg_s:6.0f} "
f"| {logging_tuple[0]:8.2f} {logging_tuple[1]:8.2f}")
def get_rewards_and_steps(env, actor, if_render: bool = False) -> (float, int): # cumulative_rewards and episode_steps
device = next(actor.parameters()).device # net.parameters() is a Python generator.
state = env.reset()
episode_steps = 0
cumulative_returns = 0.0 # sum of rewards in an episode
for episode_steps in range(12345):
tensor_state = torch.as_tensor(state, dtype=torch.float32, device=device).unsqueeze(0)
tensor_action = actor(tensor_state)
action = tensor_action.detach().cpu().numpy()[0] # not need detach(), because using torch.no_grad() outside
state, reward, done, _ = env.step(action)
cumulative_returns += reward
if if_render:
env.render()
if done:
break
return cumulative_returns, episode_steps + 1
def train_sac_td3_for_pendulum():
agent_class = [AgentSAC, AgentTD3][0] # DRL algorithm name
env_class = PendulumEnv # run a custom env: PendulumEnv, which based on OpenAI pendulum
env_args = {
'env_name': 'Pendulum', # Apply torque on the free end to swing a pendulum into an upright position
'state_dim': 3, # the x-y coordinates of the pendulum's free end and its angular velocity.
'action_dim': 1, # the torque applied to free end of the pendulum
'if_discrete': False # continuous action space, symbols β direction, value β force
}
get_gym_env_args(env=PendulumEnv(), if_print=True) # return env_args
args = Config(agent_class, env_class, env_args) # see `config.py Arguments()` for hyperparameter explanation
args.break_step = int(4e4) # break training if 'total_step > break_step'
args.net_dims = (64, 32) # the middle layer dimension of MultiLayer Perceptron
args.gamma = 0.97 # discount factor of future rewards
args.horizon_len = 64 # collect horizon_len step while exploring, then update network
args.repeat_times = 1.0 # repeatedly update network using ReplayBuffer to keep critic's loss small
args.state_value_tau = 0.02
args.explore_noise_std = 0.10
args.policy_noise_std = 0.15
train_agent(args)
"""
cumulative returns range: -2000 < -1000 < -200 < -80
SAC
| step time | avgR stdR avgS | objC objA
| 1.00e+04 135 | -211.21 55.50 200 | 0.88 -69.34
| 2.01e+04 479 | -74.14 56.91 200 | 0.62 -22.68
| 3.01e+04 1029 | -69.16 36.39 200 | 0.36 -16.79
TD3
| step time | avgR stdR avgS | objC objA
| 1.00e+04 103 | -771.30 38.15 200 | 1.03 -98.23
| 2.01e+04 380 | -89.88 62.76 200 | 0.73 -50.82
| 3.01e+04 813 | -91.69 42.66 200 | 0.45 -30.01
"""
def train_sac_td3_for_lunar_lander():
agent_class = [AgentSAC, AgentTD3][1] # DRL algorithm name
env_class = gym.make
env_args = {
'env_name': 'LunarLanderContinuous-v2', # A lander learns to land on a landing pad
'state_dim': 8, # coordinates xy, linear velocities xy, angle, angular velocity, two booleans
'action_dim': 2, # fire main engine or side engine.
'if_discrete': False # continuous action space, symbols β direction, value β force
}
get_gym_env_args(env=gym.make('LunarLanderContinuous-v2'), if_print=True) # return env_args
args = Config(agent_class, env_class, env_args) # see `config.py Arguments()` for hyperparameter explanation
args.break_step = int(8e4) # break training if 'total_step > break_step'
args.net_dims = (128, 128) # the middle layer dimension of MultiLayer Perceptron
args.horizon_len = 128 # collect horizon_len step while exploring, then update network
args.repeat_times = 1.0 # repeatedly update network using ReplayBuffer to keep critic's loss small
args.state_value_tau = 0.1 # todo
args.state_value_tau = 0.01 # todo
# args.state_value_tau = 0.001 # todo
# args.state_value_tau = 0.000 # todo
# todo YonV1943 2022-10-31 15:34:34 something wrong with the state_std and value_std !!!!!!!!!!
args.gpu_id = GPU_ID
args.random_seed = GPU_ID
train_agent(args)
"""
cumulative returns range: -1500 < -140 < 200 < 280
SAC
| step time | avgR stdR avgS | objC objA
| 1.01e+04 88 | 19.53 148.64 362 | 1.93 23.59
| 2.02e+04 294 | -60.15 120.83 805 | 2.59 60.84
| 3.03e+04 617 | -50.82 46.35 965 | 3.53 104.68
| 4.04e+04 1051 | -55.18 22.74 972 | 2.58 90.86
| 5.06e+04 1560 | 172.70 84.48 664 | 2.06 66.80
| 6.07e+04 2175 | 211.03 90.33 511 | 2.07 55.08
TD3
"""
if __name__ == '__main__':
GPU_ID = int(sys.argv[1]) # todo
# train_sac_td3_for_pendulum()
train_sac_td3_for_lunar_lander()