forked from AI4Finance-Foundation/ElegantRL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helloworld_SAC_TD3_DDPG_single_file.py
788 lines (629 loc) Β· 37 KB
/
helloworld_SAC_TD3_DDPG_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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
import os
import sys
import time
from copy import deepcopy
from typing import Tuple, List, Optional
import numpy as np
import torch as th
import torch.nn as nn
import gymnasium as gym
ARY = np.ndarray
TEN = th.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 = 6e-5 # 2 ** -14 ~= 6e-5
self.soft_update_tau = 5e-3 # 2 ** -8 ~= 5e-3
self.state_update_tau = 1e-2 # 1e-1 ~ 1e-6
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):
def __init__(self, state_dim: int, action_dim: int):
super().__init__()
self.state_dim = state_dim
self.action_dim = action_dim
self.ActionDist = th.distributions.normal.Normal
self.state_avg = nn.Parameter(th.zeros((state_dim,)), requires_grad=False)
self.state_std = nn.Parameter(th.ones((state_dim,)), requires_grad=False)
def state_norm(self, state: TEN) -> TEN:
return (state - self.state_avg) / self.state_std
class Actor(ActorBase):
def __init__(self, net_dims: List[int], state_dim: int, action_dim: int):
super().__init__(state_dim=state_dim, action_dim=action_dim)
self.net = build_mlp(dims=[state_dim, *net_dims, action_dim])
layer_init_with_orthogonal(self.net[-1], std=0.5)
def forward(self, state: TEN) -> TEN:
state = self.state_norm(state)
action = self.net(state)
return action.tanh()
def get_action(self, state: TEN, action_std: float) -> TEN: # for exploration
state = self.state_norm(state)
action_avg = self.net(state).tanh()
dist = self.ActionDist(action_avg, action_std)
action = dist.sample()
return action.clip(-1.0, 1.0)
class ActorSAC(ActorBase):
def __init__(self, net_dims: List[int], state_dim: int, action_dim: int):
super().__init__(state_dim=state_dim, action_dim=action_dim)
self.encoder_s = build_mlp(dims=[state_dim, *net_dims]) # encoder of state
self.decoder_a_avg = build_mlp(dims=[net_dims[-1], action_dim]) # decoder of action mean
self.decoder_a_std = build_mlp(dims=[net_dims[-1], action_dim]) # decoder of action log_std
self.soft_plus = nn.Softplus()
def forward(self, state: TEN) -> TEN:
state = self.state_norm(state)
state_tmp = self.encoder_s(state) # temporary tensor of state
return self.decoder_a_avg(state_tmp).tanh() # action
def get_action(self, state: TEN, **_kwargs) -> TEN: # for exploration
state = self.state_norm(state)
state_tmp = self.encoder_s(state) # temporary tensor of state
action_avg = self.decoder_a_avg(state_tmp)
action_std = self.decoder_a_std(state_tmp).clamp(-20, 2).exp()
noise = th.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: TEN) -> Tuple[TEN, TEN]:
state = self.state_norm(state)
state_tmp = self.encoder_s(state) # temporary tensor of state
action_log_std = self.decoder_a_std(state_tmp).clamp(-20, 2)
action_std = action_log_std.exp()
action_avg = self.decoder_a_avg(state_tmp)
noise = th.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):
def __init__(self, state_dim: int, action_dim: int):
super().__init__()
self.state_dim = state_dim
self.action_dim = action_dim
self.state_avg = nn.Parameter(th.zeros((state_dim,)), requires_grad=False)
self.state_std = nn.Parameter(th.ones((state_dim,)), requires_grad=False)
def state_norm(self, state: TEN) -> TEN:
return (state - self.state_avg) / self.state_std
class Critic(CriticBase):
def __init__(self, net_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 + action_dim, *net_dims, 1])
def forward(self, state: TEN, action: TEN) -> TEN:
state = self.state_norm(state)
value = self.net(th.cat((state, action), dim=1))
return value # Q value
class CriticTwin(CriticBase):
def __init__(self, net_dims: List[int], state_dim: int, action_dim: int, num_ensembles: int = 8):
super().__init__(state_dim=state_dim, action_dim=action_dim)
self.net = build_mlp(dims=[state_dim + action_dim, *net_dims, num_ensembles])
layer_init_with_orthogonal(self.net[-1], std=0.5)
def forward(self, state: TEN, action: TEN) -> TEN:
values = self.get_q_values(state=state, action=action)
value = values.mean(dim=-1, keepdim=True)
return value # Q value
def get_q_values(self, state: TEN, action: TEN) -> TEN:
state = self.state_norm(state)
values = self.net(th.cat((state, action), dim=1))
return values # Q values
class CriticEnsemble(CriticBase):
def __init__(self, net_dims: List[int], state_dim: int, action_dim: int, num_ensembles: int = 8):
super().__init__(state_dim=state_dim, action_dim=action_dim)
self.encoder_sa = build_mlp(dims=[state_dim + action_dim, net_dims[0]]) # encoder of state and action
self.decoder_qs = []
for net_i in range(num_ensembles):
decoder_q = build_mlp(dims=[*net_dims, 1])
layer_init_with_orthogonal(decoder_q[-1], std=0.5)
self.decoder_qs.append(decoder_q)
setattr(self, f"decoder_q{net_i:02}", decoder_q)
def forward(self, state: TEN, action: TEN) -> TEN:
values = self.get_q_values(state=state, action=action)
value = values.mean(dim=-1, keepdim=True)
return value # Q value
def get_q_values(self, state: TEN, action: TEN) -> TEN:
state = self.state_norm(state)
tensor_sa = self.encoder_sa(th.cat((state, action), dim=1))
values = th.concat([decoder_q(tensor_sa) for decoder_q in self.decoder_qs], dim=-1)
return values # Q values
def layer_init_with_orthogonal(layer, std=1.0, bias_const=1e-6):
th.nn.init.orthogonal_(layer.weight, std)
th.nn.init.constant_(layer.bias, bias_const)
def build_mlp(dims: List[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, env_args: dict):
if env_class.__module__ == 'gymnasium.envs.registration': # special rule
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.max_size = max_size
self.device = th.device(f"cuda:{gpu_id}" if (th.cuda.is_available() and (gpu_id >= 0)) else "cpu")
self.states = th.empty((max_size, state_dim), dtype=th.float32, device=self.device)
self.actions = th.empty((max_size, action_dim), dtype=th.float32, device=self.device)
self.rewards = th.empty((max_size, 1), dtype=th.float32, device=self.device)
self.undones = th.empty((max_size, 1), dtype=th.float32, device=self.device)
self.unmasks = th.empty((max_size, 1), dtype=th.float32, device=self.device)
def update(self, items: Tuple[TEN, TEN, TEN, TEN, TEN]):
states, actions, rewards, undones, unmasks = items
p = self.p + rewards.shape[0] # 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:]
self.unmasks[p0:p1], self.unmasks[0:p] = unmasks[:p2], unmasks[-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.unmasks[self.p:p] = unmasks
self.p = p
self.cur_size = self.max_size if self.if_full else self.p
def sample(self, batch_size: int) -> Tuple[TEN, TEN, TEN, TEN, TEN, TEN]:
ids = th.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.unmasks[ids],
self.states[ids + 1],
)
class AgentBase:
def __init__(self, net_dims: List[int], state_dim: int, action_dim: int, gpu_id: int = 0, args: Config = Config()):
self.net_dims: List[int] = net_dims
self.state_dim: int = state_dim
self.action_dim: int = action_dim
self.gamma: float = args.gamma
self.batch_size: int = args.batch_size
self.horizon_len: int = args.horizon_len
self.repeat_times: float = args.repeat_times
self.reward_scale: float = args.reward_scale
self.learning_rate: float = args.learning_rate
self.soft_update_tau: float = args.soft_update_tau
self.state_update_tau: float = args.state_update_tau
self.explore_noise_std = getattr(args, 'explore_noise_std', 0.05) # standard deviation of exploration noise
self.last_state: Optional[ARY] = None # state of the trajectory for training. `shape == (state_dim)`
self.device = th.device(f"cuda:{gpu_id}" if (th.cuda.is_available() and (gpu_id >= 0)) else "cpu")
self.act: Optional[ActorBase] = None
self.cri: Optional[CriticBase] = None
self.act_target = self.act
self.cri_target = self.cri
self.act_optimizer: Optional[th.optim] = None
self.cri_optimizer: Optional[th.optim] = None
self.criterion = th.nn.SmoothL1Loss()
def get_random_action(self) -> TEN:
return th.rand(self.action_dim) * 2 - 1.0
def get_policy_action(self, state: TEN) -> TEN:
return self.act.get_action(state.unsqueeze(0), action_std=self.explore_noise_std)[0]
def explore_env(self, env, horizon_len: int, if_random: bool = False) -> Tuple[TEN, TEN, TEN, TEN, TEN]:
self.horizon_len = horizon_len # update horizon_len for update_net()
states = th.zeros((horizon_len, self.state_dim), dtype=th.float32).to(self.device)
actions = th.zeros((horizon_len, self.action_dim), dtype=th.float32).to(self.device)
rewards = th.zeros(horizon_len, dtype=th.float32).to(self.device)
terminals = th.zeros(horizon_len, dtype=th.bool).to(self.device)
truncates = th.zeros(horizon_len, dtype=th.bool).to(self.device)
ary_state = self.last_state
for i in range(horizon_len):
state = th.as_tensor(ary_state, dtype=th.float32, device=self.device)
action = self.get_random_action() if if_random \
else self.get_policy_action(state)
ary_action = action.detach().cpu().numpy()
ary_state, reward, terminal, truncate, _ = env.step(ary_action)
if terminal or truncate:
ary_state, info_dict = env.reset()
states[i] = state
actions[i] = action
rewards[i] = reward
terminals[i] = terminal
truncates[i] = truncate
self.last_state = ary_state
rewards = rewards.unsqueeze(1)
undones = th.logical_not(terminals).unsqueeze(1)
unmasks = th.logical_not(truncates).unsqueeze(1)
return states, actions, rewards, undones, unmasks
def update_critic_net(self, buffer: ReplayBuffer, batch_size: int) -> Tuple[TEN, TEN]:
with th.no_grad():
state, action, reward, undone, unmask, next_state = buffer.sample(batch_size)
next_action = self.act(next_state) # deterministic policy
next_q = self.cri_target(next_state, next_action)
q_label = reward + undone * self.gamma * next_q
q_value = self.cri(state, action) * unmask
obj_critic = self.criterion(q_value, q_label)
return obj_critic, state
def update_actor_net(self, state: TEN, update_t: int, if_skip: bool = False) -> Optional[TEN]:
if if_skip:
return None
action_pg = self.act(state) # action to policy gradient
obj_actor = self.cri(state, action_pg).mean()
return obj_actor
def update_net(self, buffer, if_skip_actor: bool = False) -> Tuple[float, float]:
states = buffer.states[-self.horizon_len:]
state_update_tau = 1.0 if if_skip_actor else self.state_update_tau
self.update_avg_std_for_state_norm(states=states, tau=state_update_tau)
obj_critics = []
obj_actors = []
th.set_grad_enabled(True)
update_times = int(buffer.cur_size * self.repeat_times / self.batch_size)
assert update_times >= 1
for update_t in range(update_times):
obj_critic, state = self.update_critic_net(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.append(obj_critic.item())
obj_actor = self.update_actor_net(state, update_t, if_skip_actor)
if isinstance(obj_actor, TEN):
self.optimizer_update(self.act_optimizer, -obj_actor)
self.soft_update(self.act_target, self.act, self.soft_update_tau)
obj_actors.append(obj_actor.item())
th.set_grad_enabled(False)
obj_critic_avg = np.array(obj_critics).mean() if len(obj_critics) else 0.0
obj_actor_avg = np.array(obj_actors).mean() if len(obj_actors) else 0.0
return obj_critic_avg, obj_actor_avg
@staticmethod
def optimizer_update(optimizer, objective: TEN):
optimizer.zero_grad()
objective.backward()
optimizer.step()
@staticmethod
def soft_update(target_net: th.nn.Module, current_net: th.nn.Module, tau: float):
if target_net is current_net:
return
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_norm(self, states: TEN, tau: float):
if tau == 0 or self.state_update_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).clamp_min(1e-4)
self.cri.state_avg[:] = self.act.state_avg
self.cri.state_std[:] = self.cri.state_std
class AgentDDPG(AgentBase):
def __init__(self, net_dims: List[int], state_dim: int, action_dim: int, gpu_id: int = 0, args: Config = Config()):
super().__init__(net_dims, state_dim, action_dim, gpu_id, args)
self.explore_noise_std = getattr(args, 'explore_noise', 0.05) # set for `self.get_policy_action()`
self.act = Actor(net_dims=net_dims, state_dim=state_dim, action_dim=action_dim).to(self.device)
self.cri = Critic(net_dims=net_dims, state_dim=state_dim, action_dim=action_dim).to(self.device)
self.act_target = deepcopy(self.act)
self.cri_target = deepcopy(self.cri)
self.act_optimizer = th.optim.Adam(self.act.parameters(), self.learning_rate)
self.cri_optimizer = th.optim.Adam(self.cri.parameters(), self.learning_rate)
def get_policy_action(self, state: TEN) -> TEN:
return self.act.get_action(state.unsqueeze(0), action_std=self.explore_noise_std)[0]
class AgentTD3(AgentBase):
def __init__(self, net_dims: List[int], state_dim: int, action_dim: int, gpu_id: int = 0, args: Config = Config()):
super().__init__(net_dims, state_dim, action_dim, gpu_id, args)
self.update_freq = getattr(args, 'update_freq', 2) # standard deviation of exploration noise
self.num_ensembles = getattr(args, 'num_ensembles', 8) # the number of critic networks
self.policy_noise_std = getattr(args, 'policy_noise_std', 0.10) # standard deviation of exploration noise
self.explore_noise_std = getattr(args, 'explore_noise_std', 0.05) # standard deviation of exploration noise
self.act = Actor(net_dims, state_dim, action_dim).to(self.device)
self.cri = CriticTwin(net_dims, state_dim, action_dim, num_ensembles=self.num_ensembles).to(self.device)
self.act_target = deepcopy(self.act)
self.cri_target = deepcopy(self.cri)
self.act_optimizer = th.optim.Adam(self.act.parameters(), self.learning_rate)
self.cri_optimizer = th.optim.Adam(self.cri.parameters(), self.learning_rate)
def update_critic_net(self, buffer: ReplayBuffer, batch_size: int) -> Tuple[TEN, TEN]:
with th.no_grad():
state, action, reward, undone, unmask, next_state = buffer.sample(batch_size)
next_action = self.act.get_action(next_state, action_std=self.policy_noise_std) # deterministic policy
next_q = self.cri_target.get_q_values(next_state, next_action).min(dim=1, keepdim=True)[0]
q_label = reward + undone * self.gamma * next_q
q_values = self.cri.get_q_values(state, action) * unmask
q_labels = q_label.repeat(1, q_values.shape[1])
obj_critic = self.criterion(q_values, q_labels)
return obj_critic, state
def update_actor_net(self, state: TEN, update_t: int = 0, if_skip: bool = False) -> Optional[TEN]:
if if_skip:
return None
action_pg = self.act(state) # action to policy gradient
obj_actor = self.cri_target.get_q_values(state, action_pg).mean()
return obj_actor
class AgentSAC(AgentBase):
def __init__(self, net_dims: List[int], state_dim: int, action_dim: int, gpu_id: int = 0, args: Config = Config()):
super().__init__(net_dims, state_dim, action_dim, gpu_id, args)
self.num_ensembles = getattr(args, 'num_ensembles', 8) # the number of critic networks
self.act = ActorSAC(net_dims, state_dim, action_dim).to(self.device)
self.cri = CriticEnsemble(net_dims, state_dim, action_dim, num_ensembles=self.num_ensembles).to(self.device)
self.act_target = deepcopy(self.act)
self.cri_target = deepcopy(self.cri)
self.act_optimizer = th.optim.Adam(self.act.parameters(), self.learning_rate)
self.cri_optimizer = th.optim.Adam(self.cri.parameters(), self.learning_rate)
self.alpha_log = th.tensor(-1, dtype=th.float32, requires_grad=True, device=self.device) # trainable var
self.alpha_optim = th.optim.Adam((self.alpha_log,), lr=args.learning_rate)
self.target_entropy = -np.log(action_dim)
def get_policy_action(self, state: TEN) -> TEN:
return self.act.get_action(state.unsqueeze(0))[0] # stochastic policy for exploration
def update_critic_net(self, buffer: ReplayBuffer, batch_size: int) -> Tuple[TEN, TEN]:
with th.no_grad():
state, action, reward, undone, unmask, next_state = buffer.sample(batch_size)
next_action, next_logprob = self.act.get_action_logprob(next_state) # stochastic policy
next_q = th.min(self.cri_target.get_q_values(next_state, next_action), dim=1, keepdim=True)[0]
alpha = self.alpha_log.exp()
q_label = reward + undone * self.gamma * (next_q - next_logprob * alpha)
q_values = self.cri.get_q_values(state, action) * unmask
q_labels = q_label.repeat(1, q_values.shape[1])
obj_critic = self.criterion(q_values, q_labels)
return obj_critic, state
def update_actor_net(self, state: TEN, update_t: int = 0, if_skip: bool = False) -> Optional[TEN]:
if if_skip:
return None
action_pg, 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_pg) - logprob * alpha).mean()
return obj_actor
class PendulumEnv(gym.Wrapper): # a demo of custom env
def __init__(self):
gym_env_name = '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, **kwargs) -> Tuple[ARY, dict]: # reset the agent in env
state, info_dict = self.env.reset()
return state, info_dict
def step(self, action: ARY) -> Tuple[ARY, float, bool, 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, terminated, truncated, info_dict = self.env.step(action * 2)
state = state.reshape(self.state_dim)
return state, float(reward) * 0.5, terminated, truncated, info_dict
def train_agent(args: Config):
args.init_before_training()
th.set_grad_enabled(False)
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,
)
env = build_env(args.env_class, args.env_args)
agent = args.agent_class(args.net_dims, args.state_dim, args.action_dim, gpu_id=args.gpu_id, args=args)
agent.last_state, info_dict = env.reset()
buffer = ReplayBuffer(
gpu_id=args.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
agent.update_net(buffer, if_skip_actor=False)
while True: # start training
buffer_items = agent.explore_env(env, args.horizon_len)
buffer.update(buffer_items)
logging_tuple = agent.update_net(buffer)
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: ActorBase, 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: ActorBase, if_render: bool = False) -> (float, int):
device = next(actor.parameters()).device # net.parameters() is a Python generator.
state, info_dict = env.reset()
episode_steps = 0
cumulative_returns = 0.0 # sum of rewards in an episode
for episode_steps in range(12345):
tensor_state = th.as_tensor(state, dtype=th.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, terminated, truncated, _ = env.step(action)
cumulative_returns += reward
if if_render:
env.render()
if terminated or truncated:
break
cumulative_returns = getattr(env.unwrapped, 'cumulative_returns', cumulative_returns)
return cumulative_returns, episode_steps + 1
def valid_agent(env_class, env_args: dict, net_dims: List[int], agent_class, actor_path: str, render_times: int = 8):
env = build_env(env_class, env_args)
state_dim = env_args['state_dim']
action_dim = env_args['action_dim']
agent = agent_class(net_dims, state_dim, action_dim, gpu_id=-1)
actor = agent.act
print(f"| render and load actor from: {actor_path}")
actor.load_state_dict(th.load(actor_path, map_location=lambda storage, loc: storage))
for i in range(render_times):
cumulative_reward, episode_step = get_rewards_and_steps(env, actor, if_render=True)
print(f"|{i:4} cumulative_reward {cumulative_reward:9.3f} episode_step {episode_step:5.0f}")
def train_sac_td3_ddpg_for_pendulum(gpu_id: int = 0, drl_id: int = 0):
agent_class = [AgentSAC, AgentTD3, AgentDDPG][drl_id] # DRL algorithm name
print(f"agent_class {agent_class.__name__}")
env_class = PendulumEnv # run a custom env: PendulumEnv, which based on OpenAI pendulum
env_args = {
'env_name': 'Pendulum-v1', # 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=agent_class, env_class=env_class, env_args=env_args) # see `Config` for explanation
args.break_step = int(1e5) # 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.gpu_id = gpu_id # the ID of single GPU, -1 means CPU
train_agent(args)
if input("| Press 'y' to load actor.pth and render:"):
actor_name = sorted([s for s in os.listdir(args.cwd) if s[-4:] == '.pth'])[-1]
actor_path = f"{args.cwd}/{actor_name}"
valid_agent(env_class, env_args, args.net_dims, agent_class, actor_path)
"""
cumulative returns range: -1000 < -700 < -100 < -50
AgentSAC env_name Pendulum-v1
| step time | avgR stdR avgS | objC objA
| 1.02e+04 37 | -623.17 114.43 200 | 0.65 -62.60
| 2.05e+04 103 | -482.81 27.75 200 | 0.94 -91.89
| 3.07e+04 190 | -213.40 97.32 200 | 0.78 -77.40
| 4.10e+04 296 | -77.75 41.87 200 | 0.62 -46.33
| 5.12e+04 427 | -70.72 22.34 200 | 0.47 -32.85
AgentTD3 env_name Pendulum-v1
| step time | avgR stdR avgS | objC objA
| 1.02e+04 34 | -732.08 44.68 200 | 0.70 -63.95
| 2.05e+04 92 | -418.86 32.07 200 | 0.69 -90.68
| 3.07e+04 173 | -237.52 50.73 200 | 0.67 -73.77
| 4.10e+04 280 | -61.79 2.99 200 | 0.58 -49.62
| 5.12e+04 415 | -79.03 40.78 200 | 0.44 -33.22
"""
def train_sac_td3_ddpg_for_lunar_lander(gpu_id: int = 0, drl_id: int = 0):
agent_class = [AgentSAC, AgentTD3, AgentDDPG][drl_id] # DRL algorithm name
print(f"agent_class {agent_class.__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=agent_class, env_class=env_class, env_args=env_args) # see `Config` for explanation
args.break_step = int(2e5) # break training if 'total_step > break_step'
args.net_dims = (128, 128) # the middle layer dimension of MultiLayer Perceptron
args.horizon_len = 256 # 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_update_tau = 1e-2 # do rolling normalization on state using soft update tau
args.batch_size = 256 # do rolling normalization on state using soft update tau
args.gamma = 0.98
args.eval_times = 32
args.eval_per_step = int(2e4)
args.gpu_id = gpu_id # the ID of single GPU, -1 means CPU
train_agent(args)
if input("| Press 'y' to load actor.pth and render:"):
actor_name = sorted([s for s in os.listdir(args.cwd) if s[-4:] == '.pth'])[-1]
actor_path = f"{args.cwd}/{actor_name}"
valid_agent(env_class, env_args, args.net_dims, agent_class, actor_path)
"""
cumulative returns range: -1500 < -140 < 200 < 280
AgentSAC env_name LunarLanderContinuous-v2
| step time | avgR stdR avgS | objC objA
| 2.02e+04 190 | -24.88 99.83 854 | 1.93 2.25
| 4.04e+04 557 | -27.52 43.49 995 | 2.19 14.83
| 6.07e+04 1104 | 10.36 45.61 997 | 1.70 14.53
| 8.09e+04 1834 | 104.48 65.05 916 | 1.54 11.82
| 1.01e+05 2743 | 160.15 67.38 783 | 1.56 10.33
| 1.21e+05 3832 | 146.69 57.42 824 | 1.30 10.22
| 1.42e+05 5105 | 162.57 53.90 799 | 1.45 9.00
| 1.62e+05 6552 | 139.77 79.76 787 | 1.42 8.45
| 1.82e+05 8191 | 121.63 79.18 869 | 1.46 6.80
AgentTD3 env_name LunarLanderContinuous-v2
| step time | avgR stdR avgS | objC objA
| 2.02e+04 116 | -11.51 75.17 272 | 1.51 35.14
| 4.04e+04 335 | -73.55 75.77 556 | 1.20 37.81
| 6.07e+04 658 | -83.49 80.27 964 | 1.12 17.95
| 8.09e+04 1084 | 184.33 80.48 543 | 1.13 36.24
| 1.01e+05 1616 | 142.96 96.74 744 | 0.90 27.96
| 1.21e+05 2254 | 62.56 102.60 795 | 1.05 27.80
| 1.42e+05 2998 | -24.59 60.55 987 | 0.95 25.17
| 1.62e+05 3827 | 137.89 160.10 483 | 1.09 20.72
| 1.82e+05 4776 | 48.25 110.73 722 | 0.96 31.02
"""
if __name__ == '__main__':
GPU_ID = int(sys.argv[1]) if len(sys.argv) > 1 else 0
DRL_ID = int(sys.argv[2]) if len(sys.argv) > 1 else 0
train_sac_td3_ddpg_for_pendulum(gpu_id=GPU_ID, drl_id=DRL_ID)
train_sac_td3_ddpg_for_lunar_lander(gpu_id=GPU_ID, drl_id=DRL_ID)