-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathenvs.py
882 lines (750 loc) · 31.6 KB
/
envs.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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
import os
import time
import numpy as np
import torch
from tensorboardX import SummaryWriter
from common.memories import SharedReplayBuffer
# import imageio
import gym
from gym import spaces
def _t2n(x):
"""Convert torch tensor to a numpy array."""
return x.detach().cpu().numpy()
class Runner(object):
"""
Base class for training recurrent policies.
:param config: (dict) Config dictionary containing parameters for training.
"""
def __init__(self, config):
self.all_args = config['all_args']
self.envs = config['envs']
self.eval_envs = config['eval_envs']
self.device = config['device']
self.num_agents = config['num_agents']
if config.__contains__("render_envs"):
self.render_envs = config['render_envs']
# parameters
self.env_name = self.all_args.env_name
self.algorithm_name = self.all_args.algorithm_name
self.experiment_name = self.all_args.experiment_name
self.use_centralized_V = self.all_args.use_centralized_V
self.use_obs_instead_of_state = self.all_args.use_obs_instead_of_state
self.num_env_steps = self.all_args.num_env_steps
self.episode_length = self.all_args.episode_length
self.n_rollout_threads = self.all_args.n_rollout_threads
self.n_eval_rollout_threads = self.all_args.n_eval_rollout_threads
self.n_render_rollout_threads = self.all_args.n_render_rollout_threads
self.use_linear_lr_decay = self.all_args.use_linear_lr_decay
self.hidden_size = self.all_args.hidden_size
self.use_render = self.all_args.use_render
self.recurrent_N = self.all_args.recurrent_N
# interval
self.save_interval = self.all_args.save_interval
self.use_eval = self.all_args.use_eval
self.eval_interval = self.all_args.eval_interval
self.log_interval = self.all_args.log_interval
# dir
self.model_dir = self.all_args.model_dir
self.run_dir = config["run_dir"]
self.log_dir = str(self.run_dir / 'logs')
if not os.path.exists(self.log_dir):
os.makedirs(self.log_dir)
self.writter = SummaryWriter(self.log_dir)
self.save_dir = str(self.run_dir / 'models')
if not os.path.exists(self.save_dir):
os.makedirs(self.save_dir)
from agent import RMAPPO as TrainAlgo
from agent import Agent as Policy
share_observation_space = self.envs.share_observation_space[0] if self.use_centralized_V else self.envs.observation_space[0]
# policy network
self.policy = Policy(self.all_args,
self.envs.observation_space[0],
share_observation_space,
self.envs.action_space[0],
device = self.device)
if self.model_dir is not None:
self.restore()
# algorithm
self.trainer = TrainAlgo(self.all_args, self.policy, device = self.device)
# buffer
self.buffer = SharedReplayBuffer(self.all_args,
self.num_agents,
self.envs.observation_space[0],
share_observation_space,
self.envs.action_space[0])
def run(self):
"""Collect training data, perform training updates, and evaluate policy."""
raise NotImplementedError
def warmup(self):
"""Collect warmup pre-training data."""
raise NotImplementedError
def collect(self, step):
"""Collect rollouts for training."""
raise NotImplementedError
def insert(self, data):
"""
Insert data into buffer.
:param data: (Tuple) data to insert into training buffer.
"""
raise NotImplementedError
@torch.no_grad()
def compute(self):
"""Calculate returns for the collected data."""
self.trainer.prep_rollout()
next_values = self.trainer.policy.get_values(np.concatenate(self.buffer.share_obs[-1]),
np.concatenate(self.buffer.rnn_states_critic[-1]),
np.concatenate(self.buffer.masks[-1]))
next_values = np.array(np.split(_t2n(next_values), self.n_rollout_threads))
self.buffer.compute_returns(next_values, self.trainer.value_normalizer)
def train(self):
"""Train policies with data in buffer. """
self.trainer.prep_training()
train_infos = self.trainer.train(self.buffer)
self.buffer.after_update()
return train_infos
def save(self):
"""Save policy's actor and critic networks."""
policy_actor = self.trainer.policy.actor
torch.save(policy_actor.state_dict(), str(self.save_dir) + "/actor.pt")
policy_critic = self.trainer.policy.critic
torch.save(policy_critic.state_dict(), str(self.save_dir) + "/critic.pt")
def restore(self):
"""Restore policy's networks from a saved model."""
policy_actor_state_dict = torch.load(str(self.model_dir) + '/actor.pt')
self.policy.actor.load_state_dict(policy_actor_state_dict)
if not self.all_args.use_render:
policy_critic_state_dict = torch.load(str(self.model_dir) + '/critic.pt')
self.policy.critic.load_state_dict(policy_critic_state_dict)
def log_train(self, train_infos, total_num_steps):
"""
Log training info.
:param train_infos: (dict) information about training update.
:param total_num_steps: (int) total number of training env steps.
"""
for k, v in train_infos.items():
self.writter.add_scalars(k, {k: v}, total_num_steps)
def log_env(self, env_infos, total_num_steps):
"""
Log env info.
:param env_infos: (dict) information about env state.
:param total_num_steps: (int) total number of training env steps.
"""
for k, v in env_infos.items():
if len(v)>0:
self.writter.add_scalars(k, {k: np.mean(v)}, total_num_steps)
def _t2n(x):
return x.detach().cpu().numpy()
class EnvRunner(Runner):
"""Runner class to perform training, evaluation. and data collection for the MPEs. See parent class for details."""
def __init__(self, config):
super(EnvRunner, self).__init__(config)
def run(self):
self.warmup()
start = time.time()
episodes = (
int(self.num_env_steps) // self.episode_length // self.n_rollout_threads
)
for episode in range(episodes):
if self.use_linear_lr_decay:
self.trainer.policy.lr_decay(episode, episodes)
for step in range(self.episode_length):
# Sample actions
(
values,
actions,
action_log_probs,
rnn_states,
rnn_states_critic,
actions_env,
) = self.collect(step)
# Obser reward and next obs
obs, rewards, dones, infos = self.envs.step(actions_env)
data = (
obs,
rewards,
dones,
infos,
values,
actions,
action_log_probs,
rnn_states,
rnn_states_critic,
)
# insert data into buffer
self.insert(data)
# compute return and update network
self.compute()
train_infos = self.train()
# post process
total_num_steps = (
(episode + 1) * self.episode_length * self.n_rollout_threads
)
# save model
if episode % self.save_interval == 0 or episode == episodes - 1:
self.save()
# log information
if episode % self.log_interval == 0:
end = time.time()
print(
"\n Scenario {} Algo {} Exp {} updates {}/{} episodes, total num timesteps {}/{}, FPS {}.\n".format(
self.all_args.scenario_name,
self.algorithm_name,
self.experiment_name,
episode,
episodes,
total_num_steps,
self.num_env_steps,
int(total_num_steps / (end - start)),
)
)
# if self.env_name == "MPE":
# env_infos = {}
# for agent_id in range(self.num_agents):
# idv_rews = []
# for info in infos:
# if 'individual_reward' in info[agent_id].keys():
# idv_rews.append(info[agent_id]['individual_reward'])
# agent_k = 'agent%i/individual_rewards' % agent_id
# env_infos[agent_k] = idv_rews
train_infos["average_episode_rewards"] = (
np.mean(self.buffer.rewards) * self.episode_length
)
print(
"average episode rewards is {}".format(
train_infos["average_episode_rewards"]
)
)
self.log_train(train_infos, total_num_steps)
# self.log_env(env_infos, total_num_steps)
# eval
if episode % self.eval_interval == 0 and self.use_eval:
self.eval(total_num_steps)
def warmup(self):
# reset env
obs = self.envs.reset() # shape = (5, 2, 14)
# replay buffer
if self.use_centralized_V:
share_obs = obs.reshape(self.n_rollout_threads, -1) # shape = (5, 28)
share_obs = np.expand_dims(share_obs, 1).repeat(
self.num_agents, axis=1
) # shape = (5, 2, 28)
else:
share_obs = obs
self.buffer.share_obs[0] = share_obs.copy()
self.buffer.obs[0] = obs.copy()
@torch.no_grad()
def collect(self, step):
self.trainer.prep_rollout()
(
value,
action,
action_log_prob,
rnn_states,
rnn_states_critic,
) = self.trainer.policy.get_actions(
np.concatenate(self.buffer.share_obs[step]),
np.concatenate(self.buffer.obs[step]),
np.concatenate(self.buffer.rnn_states[step]),
np.concatenate(self.buffer.rnn_states_critic[step]),
np.concatenate(self.buffer.masks[step]),
)
# [self.envs, agents, dim]
values = np.array(np.split(_t2n(value), self.n_rollout_threads))
actions = np.array(np.split(_t2n(action), self.n_rollout_threads))
action_log_probs = np.array(
np.split(_t2n(action_log_prob), self.n_rollout_threads)
)
rnn_states = np.array(np.split(_t2n(rnn_states), self.n_rollout_threads))
rnn_states_critic = np.array(
np.split(_t2n(rnn_states_critic), self.n_rollout_threads)
)
# rearrange action
if self.envs.action_space[0].__class__.__name__ == "MultiDiscrete":
for i in range(self.envs.action_space[0].shape):
uc_actions_env = np.eye(self.envs.action_space[0].high[i] + 1)[
actions[:, :, i]
]
if i == 0:
actions_env = uc_actions_env
else:
actions_env = np.concatenate((actions_env, uc_actions_env), axis=2)
elif self.envs.action_space[0].__class__.__name__ == "Discrete":
# actions --> actions_env : shape:[10, 1] --> [5, 2, 5]
actions_env = np.squeeze(np.eye(self.envs.action_space[0].n)[actions], 2)
else:
# TODO 这里改造成自己环境需要的形式即可
# TODO Here, you can change the shape of actions_env to fit your environment
actions_env = actions
# raise NotImplementedError
return (
values,
actions,
action_log_probs,
rnn_states,
rnn_states_critic,
actions_env,
)
def insert(self, data):
(
obs,
rewards,
dones,
infos,
values,
actions,
action_log_probs,
rnn_states,
rnn_states_critic,
) = data
rnn_states[dones == True] = np.zeros(
((dones == True).sum(), self.recurrent_N, self.hidden_size),
dtype=np.float32,
)
rnn_states_critic[dones == True] = np.zeros(
((dones == True).sum(), *self.buffer.rnn_states_critic.shape[3:]),
dtype=np.float32,
)
masks = np.ones((self.n_rollout_threads, self.num_agents, 1), dtype=np.float32)
masks[dones == True] = np.zeros(((dones == True).sum(), 1), dtype=np.float32)
if self.use_centralized_V:
share_obs = obs.reshape(self.n_rollout_threads, -1)
share_obs = np.expand_dims(share_obs, 1).repeat(self.num_agents, axis=1)
else:
share_obs = obs
self.buffer.insert(
share_obs,
obs,
rnn_states,
rnn_states_critic,
actions,
action_log_probs,
values,
rewards,
masks,
)
@torch.no_grad()
def eval(self, total_num_steps):
eval_episode_rewards = []
eval_obs = self.eval_envs.reset()
eval_rnn_states = np.zeros(
(self.n_eval_rollout_threads, *self.buffer.rnn_states.shape[2:]),
dtype=np.float32,
)
eval_masks = np.ones(
(self.n_eval_rollout_threads, self.num_agents, 1), dtype=np.float32
)
for eval_step in range(self.episode_length):
self.trainer.prep_rollout()
eval_action, eval_rnn_states = self.trainer.policy.act(
np.concatenate(eval_obs),
np.concatenate(eval_rnn_states),
np.concatenate(eval_masks),
deterministic=True,
)
eval_actions = np.array(
np.split(_t2n(eval_action), self.n_eval_rollout_threads)
)
eval_rnn_states = np.array(
np.split(_t2n(eval_rnn_states), self.n_eval_rollout_threads)
)
if self.eval_envs.action_space[0].__class__.__name__ == "MultiDiscrete":
for i in range(self.eval_envs.action_space[0].shape):
eval_uc_actions_env = np.eye(
self.eval_envs.action_space[0].high[i] + 1
)[eval_actions[:, :, i]]
if i == 0:
eval_actions_env = eval_uc_actions_env
else:
eval_actions_env = np.concatenate(
(eval_actions_env, eval_uc_actions_env), axis=2
)
elif self.eval_envs.action_space[0].__class__.__name__ == "Discrete":
eval_actions_env = np.squeeze(
np.eye(self.eval_envs.action_space[0].n)[eval_actions], 2
)
else:
raise NotImplementedError
# Obser reward and next obs
eval_obs, eval_rewards, eval_dones, eval_infos = self.eval_envs.step(
eval_actions_env
)
eval_episode_rewards.append(eval_rewards)
eval_rnn_states[eval_dones == True] = np.zeros(
((eval_dones == True).sum(), self.recurrent_N, self.hidden_size),
dtype=np.float32,
)
eval_masks = np.ones(
(self.n_eval_rollout_threads, self.num_agents, 1), dtype=np.float32
)
eval_masks[eval_dones == True] = np.zeros(
((eval_dones == True).sum(), 1), dtype=np.float32
)
eval_episode_rewards = np.array(eval_episode_rewards)
eval_env_infos = {}
eval_env_infos["eval_average_episode_rewards"] = np.sum(
np.array(eval_episode_rewards), axis=0
)
eval_average_episode_rewards = np.mean(
eval_env_infos["eval_average_episode_rewards"]
)
print(
"eval average episode rewards of agent: "
+ str(eval_average_episode_rewards)
)
self.log_env(eval_env_infos, total_num_steps)
@torch.no_grad()
def render(self):
"""Visualize the env."""
envs = self.envs
all_frames = []
for episode in range(self.all_args.render_episodes):
obs = envs.reset()
if self.all_args.save_gifs:
image = envs.render("rgb_array")[0][0]
all_frames.append(image)
else:
envs.render("human")
rnn_states = np.zeros(
(
self.n_rollout_threads,
self.num_agents,
self.recurrent_N,
self.hidden_size,
),
dtype=np.float32,
)
masks = np.ones(
(self.n_rollout_threads, self.num_agents, 1), dtype=np.float32
)
episode_rewards = []
for step in range(self.episode_length):
calc_start = time.time()
self.trainer.prep_rollout()
action, rnn_states = self.trainer.policy.act(
np.concatenate(obs),
np.concatenate(rnn_states),
np.concatenate(masks),
deterministic=True,
)
actions = np.array(np.split(_t2n(action), self.n_rollout_threads))
rnn_states = np.array(
np.split(_t2n(rnn_states), self.n_rollout_threads)
)
if envs.action_space[0].__class__.__name__ == "MultiDiscrete":
for i in range(envs.action_space[0].shape):
uc_actions_env = np.eye(envs.action_space[0].high[i] + 1)[
actions[:, :, i]
]
if i == 0:
actions_env = uc_actions_env
else:
actions_env = np.concatenate(
(actions_env, uc_actions_env), axis=2
)
elif envs.action_space[0].__class__.__name__ == "Discrete":
actions_env = np.squeeze(np.eye(envs.action_space[0].n)[actions], 2)
else:
raise NotImplementedError
# Obser reward and next obs
obs, rewards, dones, infos = envs.step(actions_env)
episode_rewards.append(rewards)
rnn_states[dones == True] = np.zeros(
((dones == True).sum(), self.recurrent_N, self.hidden_size),
dtype=np.float32,
)
masks = np.ones(
(self.n_rollout_threads, self.num_agents, 1), dtype=np.float32
)
masks[dones == True] = np.zeros(
((dones == True).sum(), 1), dtype=np.float32
)
if self.all_args.save_gifs:
image = envs.render("rgb_array")[0][0]
all_frames.append(image)
calc_end = time.time()
elapsed = calc_end - calc_start
if elapsed < self.all_args.ifi:
time.sleep(self.all_args.ifi - elapsed)
else:
envs.render("human")
print(
"average episode rewards is: "
+ str(np.mean(np.sum(np.array(episode_rewards), axis=0)))
)
# if self.all_args.save_gifs:
# imageio.mimsave(str(self.gif_dir) + '/render.gif', all_frames, duration=self.all_args.ifi)
# Modified from OpenAI Baselines code to work with multi-agent envs
class DummyVecEnv():
def __init__(self, env_fns):
self.envs = [fn() for fn in env_fns]
env = self.envs[0]
self.num_envs = len(env_fns)
self.observation_space = env.observation_space
self.share_observation_space = env.share_observation_space
self.action_space = env.action_space
self.actions = None
def step(self, actions):
"""
Step the environments synchronously.
This is available for backwards compatibility.
"""
self.step_async(actions)
return self.step_wait()
def step_async(self, actions):
self.actions = actions
def step_wait(self):
results = [env.step(a) for (a, env) in zip(self.actions, self.envs)]
obs, rews, dones, infos = map(np.array, zip(*results))
for (i, done) in enumerate(dones):
if 'bool' in done.__class__.__name__:
if done:
obs[i] = self.envs[i].reset()
else:
if np.all(done):
obs[i] = self.envs[i].reset()
self.actions = None
return obs, rews, dones, infos
def reset(self):
obs = [env.reset() for env in self.envs]
return np.array(obs)
def close(self):
for env in self.envs:
env.close()
def render(self, mode="human"):
if mode == "rgb_array":
return np.array([env.render(mode=mode) for env in self.envs])
elif mode == "human":
for env in self.envs:
env.render(mode=mode)
else:
raise NotImplementedError
class EnvCore(object):
"""
# 环境中的智能体
"""
def __init__(self):
self.agent_num = 2 # 设置智能体(小飞机)的个数,这里设置为两个 # set the number of agents(aircrafts), here set to two
self.obs_dim = 14 # 设置智能体的观测维度 # set the observation dimension of agents
self.action_dim = 5 # 设置智能体的动作维度,这里假定为一个五个维度的 # set the action dimension of agents, here set to a five-dimensional
def reset(self):
"""
# self.agent_num设定为2个智能体时,返回值为一个list,每个list里面为一个shape = (self.obs_dim, )的观测数据
# When self.agent_num is set to 2 agents, the return value is a list, each list contains a shape = (self.obs_dim, ) observation data
"""
sub_agent_obs = []
for i in range(self.agent_num):
sub_obs = np.random.random(size=(14,))
sub_agent_obs.append(sub_obs)
return sub_agent_obs
def step(self, actions):
"""
# self.agent_num设定为2个智能体时,actions的输入为一个2纬的list,每个list里面为一个shape = (self.action_dim, )的动作数据
# 默认参数情况下,输入为一个list,里面含有两个元素,因为动作维度为5,所里每个元素shape = (5, )
# When self.agent_num is set to 2 agents, the input of actions is a 2-dimensional list, each list contains a shape = (self.action_dim, ) action data
# The default parameter situation is to input a list with two elements, because the action dimension is 5, so each element shape = (5, )
"""
sub_agent_obs = []
sub_agent_reward = []
sub_agent_done = []
sub_agent_info = []
for i in range(self.agent_num):
sub_agent_obs.append(np.random.random(size=(14,)))
sub_agent_reward.append([np.random.rand()])
sub_agent_done.append(False)
sub_agent_info.append({})
return [sub_agent_obs, sub_agent_reward, sub_agent_done, sub_agent_info]
class ContinuousActionEnv(object):
"""
对于连续动作环境的封装
Wrapper for continuous action environment.
"""
def __init__(self):
self.env = EnvCore()
self.num_agent = self.env.agent_num
self.signal_obs_dim = self.env.obs_dim
self.signal_action_dim = self.env.action_dim
# if true, action is a number 0...N, otherwise action is a one-hot N-dimensional vector
self.discrete_action_input = False
self.movable = True
# configure spaces
self.action_space = []
self.observation_space = []
self.share_observation_space = []
share_obs_dim = 0
total_action_space = []
for agent in range(self.num_agent):
# physical action space
u_action_space = spaces.Box(
low=-np.inf,
high=+np.inf,
shape=(self.signal_action_dim,),
dtype=np.float32,
)
if self.movable:
total_action_space.append(u_action_space)
# total action space
self.action_space.append(total_action_space[0])
# observation space
share_obs_dim += self.signal_obs_dim
self.observation_space.append(
spaces.Box(
low=-np.inf,
high=+np.inf,
shape=(self.signal_obs_dim,),
dtype=np.float32,
)
) # [-inf,inf]
self.share_observation_space = [
spaces.Box(
low=-np.inf, high=+np.inf, shape=(share_obs_dim,), dtype=np.float32
)
for _ in range(self.num_agent)
]
def step(self, actions):
"""
输入actions维度假设:
# actions shape = (5, 2, 5)
# 5个线程的环境,里面有2个智能体,每个智能体的动作是一个one_hot的5维编码
Input actions dimension assumption:
# actions shape = (5, 2, 5)
# 5 threads of environment, there are 2 agents inside, and each agent's action is a 5-dimensional one_hot encoding
"""
results = self.env.step(actions)
obs, rews, dones, infos = results
return np.stack(obs), np.stack(rews), np.stack(dones), infos
def reset(self):
obs = self.env.reset()
return np.stack(obs)
def close(self):
pass
def render(self, mode="rgb_array"):
pass
def seed(self, seed):
pass
class DiscreteActionEnv(object):
"""
对于离散动作环境的封装
Wrapper for discrete action environment.
"""
def __init__(self):
self.env = EnvCore()
self.num_agent = self.env.agent_num
self.signal_obs_dim = self.env.obs_dim
self.signal_action_dim = self.env.action_dim
# if true, action is a number 0...N, otherwise action is a one-hot N-dimensional vector
self.discrete_action_input = False
self.movable = True
# configure spaces
self.action_space = []
self.observation_space = []
self.share_observation_space = []
share_obs_dim = 0
total_action_space = []
for agent in range(self.num_agent):
# physical action space
u_action_space = spaces.Discrete(self.signal_action_dim) # 5个离散的动作
if self.movable:
total_action_space.append(u_action_space)
# total action space
if len(total_action_space) > 1:
# all action spaces are discrete, so simplify to MultiDiscrete action space
if all(
[
isinstance(act_space, spaces.Discrete)
for act_space in total_action_space
]
):
act_space = MultiDiscrete(
[[0, act_space.n - 1] for act_space in total_action_space]
)
else:
act_space = spaces.Tuple(total_action_space)
self.action_space.append(act_space)
else:
self.action_space.append(total_action_space[0])
# observation space
share_obs_dim += self.signal_obs_dim
self.observation_space.append(
spaces.Box(
low=-np.inf,
high=+np.inf,
shape=(self.signal_obs_dim,),
dtype=np.float32,
)
) # [-inf,inf]
self.share_observation_space = [
spaces.Box(
low=-np.inf, high=+np.inf, shape=(share_obs_dim,), dtype=np.float32
)
for _ in range(self.num_agent)
]
def step(self, actions):
"""
输入actions维度假设:
# actions shape = (5, 2, 5)
# 5个线程的环境,里面有2个智能体,每个智能体的动作是一个one_hot的5维编码
Input actions dimension assumption:
# actions shape = (5, 2, 5)
# 5 threads of the environment, with 2 intelligent agents inside, and each intelligent agent's action is a 5-dimensional one_hot encoding
"""
results = self.env.step(actions)
obs, rews, dones, infos = results
return np.stack(obs), np.stack(rews), np.stack(dones), infos
def reset(self):
obs = self.env.reset()
return np.stack(obs)
def close(self):
pass
def render(self, mode="rgb_array"):
pass
def seed(self, seed):
pass
class MultiDiscrete:
"""
- The multi-discrete action space consists of a series of discrete action spaces with different parameters
- It can be adapted to both a Discrete action space or a continuous (Box) action space
- It is useful to represent game controllers or keyboards where each key can be represented as a discrete action space
- It is parametrized by passing an array of arrays containing [min, max] for each discrete action space
where the discrete action space can take any integers from `min` to `max` (both inclusive)
Note: A value of 0 always need to represent the NOOP action.
e.g. Nintendo Game Controller
- Can be conceptualized as 3 discrete action spaces:
1) Arrow Keys: Discrete 5 - NOOP[0], UP[1], RIGHT[2], DOWN[3], LEFT[4] - params: min: 0, max: 4
2) Button A: Discrete 2 - NOOP[0], Pressed[1] - params: min: 0, max: 1
3) Button B: Discrete 2 - NOOP[0], Pressed[1] - params: min: 0, max: 1
- Can be initialized as
MultiDiscrete([ [0,4], [0,1], [0,1] ])
"""
def __init__(self, array_of_param_array):
super().__init__()
self.low = np.array([x[0] for x in array_of_param_array])
self.high = np.array([x[1] for x in array_of_param_array])
self.num_discrete_space = self.low.shape[0]
self.n = np.sum(self.high) + 2
def sample(self):
"""Returns a array with one sample from each discrete action space"""
# For each row: round(random .* (max - min) + min, 0)
random_array = np.random.rand(self.num_discrete_space)
return [
int(x)
for x in np.floor(
np.multiply((self.high - self.low + 1.0), random_array) + self.low
)
]
def contains(self, x):
return (
len(x) == self.num_discrete_space
and (np.array(x) >= self.low).all()
and (np.array(x) <= self.high).all()
)
@property
def shape(self):
return self.num_discrete_space
def __repr__(self):
return "MultiDiscrete" + str(self.num_discrete_space)
def __eq__(self, other):
return np.array_equal(self.low, other.low) and np.array_equal(
self.high, other.high
)
if __name__ == "__main__":
DiscreteActionEnv().step(actions=None)