forked from isaac-sim/IsaacGymEnvs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
adr_vec_task.py
1250 lines (951 loc) · 58.8 KB
/
adr_vec_task.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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2018-2023, NVIDIA Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import copy
from typing import Dict, Any, Tuple, List, Set
import gym
from gym import spaces
from isaacgym import gymtorch, gymapi
from isaacgymenvs.utils.dr_utils import get_property_setter_map, get_property_getter_map, \
get_default_setter_args, apply_random_samples, check_buckets, generate_random_samples
import torch
import numpy as np
import operator, random
from copy import deepcopy
from isaacgymenvs.utils.utils import nested_dict_get_attr, nested_dict_set_attr
from collections import deque
from enum import Enum
import sys
import abc
from abc import ABC
from omegaconf import ListConfig
class RolloutWorkerModes:
ADR_ROLLOUT = 0 # rollout with current ADR params
ADR_BOUNDARY = 1 # rollout with params on boundaries of ADR, used to decide whether to expand ranges
TEST_ENV = 2 # rollout wit default DR params, used to measure overall success rate. (currently unused)
from isaacgymenvs.tasks.base.vec_task import Env, VecTask
class EnvDextreme(Env):
def __init__(self, config: Dict[str, Any], rl_device: str, sim_device: str, graphics_device_id: int, headless: bool, use_dict_obs: bool):
Env.__init__(self, config, rl_device, sim_device, graphics_device_id, headless)
self.use_dict_obs = use_dict_obs
if self.use_dict_obs:
self.obs_dims = config["env"]["obsDims"]
self.obs_space = spaces.Dict(
{
k: spaces.Box(
np.ones(shape=dims) * -np.Inf, np.ones(shape=dims) * np.Inf
)
for k, dims in self.obs_dims.items()
}
)
else:
self.num_observations = config["env"]["numObservations"]
self.num_states = config["env"].get("numStates", 0)
self.obs_space = spaces.Box(np.ones(self.num_obs) * -np.Inf, np.ones(self.num_obs) * np.Inf)
self.state_space = spaces.Box(np.ones(self.num_states) * -np.Inf, np.ones(self.num_states) * np.Inf)
def get_env_state(self):
"""
Return serializable environment state to be saved to checkpoint.
Can be used for stateful training sessions, i.e. with adaptive curriculums.
"""
return None
def set_env_state(self, env_state):
pass
class VecTaskDextreme(EnvDextreme, VecTask):
def __init__(self, config, rl_device, sim_device, graphics_device_id, headless, use_dict_obs=False):
"""Initialise the `VecTask`.
Args:
config: config dictionary for the environment.
sim_device: the device to simulate physics on. eg. 'cuda:0' or 'cpu'
graphics_device_id: the device ID to render with.
headless: Set to False to disable viewer rendering.
"""
EnvDextreme.__init__(self, config, rl_device, sim_device, graphics_device_id, headless, use_dict_obs=use_dict_obs)
self.sim_params = self._VecTask__parse_sim_params(self.cfg["physics_engine"], self.cfg["sim"])
if self.cfg["physics_engine"] == "physx":
self.physics_engine = gymapi.SIM_PHYSX
elif self.cfg["physics_engine"] == "flex":
self.physics_engine = gymapi.SIM_FLEX
else:
msg = f"Invalid physics engine backend: {self.cfg['physics_engine']}"
raise ValueError(msg)
self.virtual_display = None
# optimization flags for pytorch JIT
torch._C._jit_set_profiling_mode(False)
torch._C._jit_set_profiling_executor(False)
self.gym = gymapi.acquire_gym()
self.first_randomization = True
self.randomize = self.cfg["task"]["randomize"]
self.randomize_obs_builtin = "observations" in self.cfg["task"].get("randomization_params", {})
self.randomize_act_builtin = "actions" in self.cfg["task"].get("randomization_params", {})
self.randomized_suffix = "randomized"
if self.use_dict_obs and self.randomize and self.randomize_obs_builtin:
self.randomisation_obs = set(self.obs_space.keys()).intersection(set(self.randomization_params['observations'].keys()))
for obs_name in self.randomisation_obs:
self.obs_space[f"{obs_name}_{self.randomized_suffix}"] = self.obs_space[obs_name]
self.obs_dims[f"{obs_name}_{self.randomized_suffix}"] = self.obs_dims[obs_name]
self.obs_randomizations = {}
elif self.randomize_obs_builtin:
self.obs_randomizations = None
self.action_randomizations = None
self.original_props = {}
self.actor_params_generator = None
self.extern_actor_params = {}
self.last_step = -1
self.last_rand_step = -1
for env_id in range(self.num_envs):
self.extern_actor_params[env_id] = None
# create envs, sim and viewer
self.sim_initialized = False
self.create_sim()
self.gym.prepare_sim(self.sim)
self.sim_initialized = True
self.set_viewer()
self.allocate_buffers()
def allocate_buffers(self):
"""Allocate the observation, states, etc. buffers.
These are what is used to set observations and states in the environment classes which
inherit from this one, and are read in `step` and other related functions.
"""
# allocate buffers
if self.use_dict_obs:
self.obs_dict = {
k: torch.zeros(
(self.num_envs, *dims), device=self.device, dtype=torch.float
)
for k, dims in self.obs_dims.items()
}
print("Obs dictinary: ")
print(self.obs_dims)
# print(self.obs_dict)
for k, dims in self.obs_dims.items():
print("1")
print(dims)
self.obs_dict_repeat = {
k: torch.zeros(
(self.num_envs, *dims), device=self.device, dtype=torch.float
)
for k, dims in self.obs_dims.items()
}
else:
self.obs_dict = {}
self.obs_buf = torch.zeros(
(self.num_envs, self.num_obs), device=self.device, dtype=torch.float)
self.states_buf = torch.zeros(
(self.num_envs, self.num_states), device=self.device, dtype=torch.float)
self.rew_buf = torch.zeros(
self.num_envs, device=self.device, dtype=torch.float)
self.reset_buf = torch.ones(
self.num_envs, device=self.device, dtype=torch.long)
self.timeout_buf = torch.zeros(
self.num_envs, device=self.device, dtype=torch.long)
self.progress_buf = torch.zeros(
self.num_envs, device=self.device, dtype=torch.long)
self.randomize_buf = torch.zeros(
self.num_envs, device=self.device, dtype=torch.long)
self.extras = {}
def create_sim(self, compute_device: int, graphics_device: int, physics_engine, sim_params: gymapi.SimParams):
"""Create an Isaac Gym sim object.
Args:
compute_device: ID of compute device to use.
graphics_device: ID of graphics device to use.
physics_engine: physics engine to use (`gymapi.SIM_PHYSX` or `gymapi.SIM_FLEX`)
sim_params: sim params to use.
Returns:
the Isaac Gym sim object.
"""
sim = self.gym.create_sim(compute_device, graphics_device, physics_engine, sim_params)
if sim is None:
print("*** Failed to create sim")
quit()
return sim
def get_state(self):
"""Returns the state buffer of the environment (the priviledged observations for asymmetric training)."""
if self.use_dict_obs:
raise NotImplementedError("No states in vec task when `use_dict_obs=True`")
return torch.clamp(self.states_buf, -self.clip_obs, self.clip_obs).to(self.rl_device)
@abc.abstractmethod
def pre_physics_step(self, actions: torch.Tensor):
"""Apply the actions to the environment (eg by setting torques, position targets).
Args:
actions: the actions to apply
"""
@abc.abstractmethod
def post_physics_step(self):
"""Compute reward and observations, reset any environments that require it."""
def step(self, actions: torch.Tensor) -> Tuple[Dict[str, torch.Tensor], torch.Tensor, torch.Tensor, Dict[str, Any]]:
"""Step the physics of the environment.
Args:
actions: actions to apply
Returns:
Observations, rewards, resets, info
Observations are dict of observations (currently only one member called 'obs')
"""
# randomize actions
if self.action_randomizations is not None and self.randomize_act_builtin:
actions = self.action_randomizations['noise_lambda'](actions)
action_tensor = torch.clamp(actions, -self.clip_actions, self.clip_actions)
# apply actions
self.pre_physics_step(action_tensor)
# step physics and render each frame
for i in range(self.control_freq_inv):
self.render()
self.gym.simulate(self.sim)
if self.device == 'cpu':
self.gym.fetch_results(self.sim, True)
# compute observations, rewards, resets, ...
self.post_physics_step()
# fill time out buffer: set to 1 if we reached the max episode length AND the reset buffer is 1. Timeout == 1 makes sense only if the reset buffer is 1.
self.timeout_buf = (self.progress_buf >= self.max_episode_length - 1) & (self.reset_buf != 0)
# randomize observations
# cannot randomise in the env because of missing suffix in the observation dict
if self.randomize and self.randomize_obs_builtin and self.use_dict_obs and len(self.obs_randomizations) > 0:
for obs_name, v in self.obs_randomizations.items():
self.obs_dict[f"{obs_name}_{self.randomized_suffix}"] = v['noise_lambda'](self.obs_dict[obs_name])
# Random cube pose
if hasattr(self, 'enable_random_obs') and self.enable_random_obs and obs_name == 'object_pose_cam':
self.obs_dict[f"{obs_name}_{self.randomized_suffix}"] \
= self.get_random_cube_observation(self.obs_dict[f"{obs_name}_{self.randomized_suffix}"])
if hasattr(self, 'enable_random_obs') and self.enable_random_obs:
relative_rot = self.get_relative_rot(self.obs_dict['object_pose_cam_'+ self.randomized_suffix][:, 3:7],
self.obs_dict['goal_pose'][:, 3:7])
v = self.obs_randomizations['goal_relative_rot_cam']
self.obs_dict["goal_relative_rot_cam_" + self.randomized_suffix] = v['noise_lambda'](relative_rot)
elif self.randomize and self.randomize_obs_builtin and not self.use_dict_obs and self.obs_randomizations is not None:
self.obs_buf = self.obs_randomizations['noise_lambda'](self.obs_buf)
self.extras["time_outs"] = self.timeout_buf.to(self.rl_device)
if self.use_dict_obs:
obs_dict_ret = {
k: torch.clone(torch.clamp(t, -self.clip_obs, self.clip_obs)).to(
self.rl_device
)
for k, t in self.obs_dict.items()
}
return obs_dict_ret, self.rew_buf.to(self.rl_device), self.reset_buf.to(self.rl_device), self.extras
else:
self.obs_dict["obs"] = torch.clamp(self.obs_buf, -self.clip_obs, self.clip_obs).to(self.rl_device)
# asymmetric actor-critic
if self.num_states > 0:
self.obs_dict["states"] = self.get_state()
return self.obs_dict, self.rew_buf.to(self.rl_device), self.reset_buf.to(self.rl_device), self.extras
def reset(self) -> torch.Tensor:
"""Reset the environment.
Returns:
Observation dictionary
"""
zero_actions = self.zero_actions()
# step the simulator
self.step(zero_actions)
if self.use_dict_obs:
obs_dict_ret = {
k: torch.clone(
torch.clamp(t, -self.clip_obs, self.clip_obs).to(self.rl_device)
)
for k, t in self.obs_dict.items()
}
return obs_dict_ret
else:
self.obs_dict["obs"] = torch.clamp(self.obs_buf, -self.clip_obs, self.clip_obs).to(self.rl_device)
# asymmetric actor-critic
if self.num_states > 0:
self.obs_dict["states"] = self.get_state()
return self.obs_dict
"""
Domain Randomization methods
"""
def get_env_state(self):
"""
Return serializable environment state to be saved to checkpoint.
Can be used for stateful training sessions, i.e. with adaptive curriculums.
"""
if self.use_adr:
return dict(adr_params=self.adr_params)
else:
return {}
def set_env_state(self, env_state):
if env_state is None:
return
for key in self.get_env_state().keys():
if key == "adr_params" and self.use_adr and not self.adr_load_from_checkpoint:
print("Skipping loading ADR params from checkpoint...")
continue
value = env_state.get(key, None)
if value is None:
continue
self.__dict__[key] = value
print(f'Loaded env state value {key}:{value}')
if self.use_adr:
print(f'ADR Params after loading from checkpoint: {self.adr_params}')
def get_randomization_dict(self, dr_params, obs_shape):
dist = dr_params["distribution"]
op_type = dr_params["operation"]
sched_type = dr_params["schedule"] if "schedule" in dr_params else None
sched_step = dr_params["schedule_steps"] if "schedule" in dr_params else None
op = operator.add if op_type == 'additive' else operator.mul
if not self.use_adr:
apply_white_noise_prob = dr_params.get("apply_white_noise", 0.5)
if sched_type == 'linear':
sched_scaling = 1.0 / sched_step * \
min(self.last_step, sched_step)
elif sched_type == 'constant':
sched_scaling = 0 if self.last_step < sched_step else 1
else:
sched_scaling = 1
if dist == 'gaussian':
mu, var = dr_params["range"]
mu_corr, var_corr = dr_params.get("range_correlated", [0., 0.])
if op_type == 'additive':
mu *= sched_scaling
var *= sched_scaling
mu_corr *= sched_scaling
var_corr *= sched_scaling
elif op_type == 'scaling':
var = var * sched_scaling # scale up var over time
mu = mu * sched_scaling + 1.0 * \
(1.0 - sched_scaling) # linearly interpolate
var_corr = var_corr * sched_scaling # scale up var over time
mu_corr = mu_corr * sched_scaling + 1.0 * \
(1.0 - sched_scaling) # linearly interpolate
local_params = {
'mu': mu, 'var': var, 'mu_corr': mu_corr, 'var_corr': var_corr,
'corr': torch.randn(self.num_envs, *obs_shape, device=self.device)
}
if not self.use_adr:
local_params['apply_white_noise_mask'] = (torch.rand(self.num_envs, device=self.device) < apply_white_noise_prob).float()
def noise_lambda(tensor, params=local_params):
corr = local_params['corr']
corr = corr * params['var_corr'] + params['mu_corr']
if self.use_adr:
return op(
tensor, corr + torch.randn_like(tensor) * params['var'] + params['mu'])
else:
return op(
tensor, corr + torch.randn_like(tensor) * params['apply_white_noise_mask'].view(-1, 1) * params['var'] + params['mu'])
elif dist == 'uniform':
lo, hi = dr_params["range"]
lo_corr, hi_corr = dr_params.get("range_correlated", [0., 0.])
if op_type == 'additive':
lo *= sched_scaling
hi *= sched_scaling
lo_corr *= sched_scaling
hi_corr *= sched_scaling
elif op_type == 'scaling':
lo = lo * sched_scaling + 1.0 * (1.0 - sched_scaling)
hi = hi * sched_scaling + 1.0 * (1.0 - sched_scaling)
lo_corr = lo_corr * sched_scaling + 1.0 * (1.0 - sched_scaling)
hi_corr = hi_corr * sched_scaling + 1.0 * (1.0 - sched_scaling)
local_params = {'lo': lo, 'hi': hi, 'lo_corr': lo_corr, 'hi_corr': hi_corr,
'corr': torch.rand(self.num_envs, *obs_shape, device=self.device)
}
if not self.use_adr:
local_params['apply_white_noise_mask'] = (torch.rand(self.num_envs, device=self.device) < apply_white_noise_prob).float()
def noise_lambda(tensor, params=local_params):
corr = params['corr']
corr = corr * (params['hi_corr'] - params['lo_corr']) + params['lo_corr']
if self.use_adr:
return op(tensor, corr + torch.rand_like(tensor) * (params['hi'] - params['lo']) + params['lo'])
else:
return op(tensor, corr + torch.rand_like(tensor) * params['apply_white_noise_mask'].view(-1, 1) * (params['hi'] - params['lo']) + params['lo'])
else:
raise NotImplementedError
# return {'lo': lo, 'hi': hi, 'lo_corr': lo_corr, 'hi_corr': hi_corr, 'noise_lambda': noise_lambda}
return {'noise_lambda': noise_lambda, 'corr_val': local_params['corr']}
class ADRVecTask(VecTaskDextreme):
def __init__(self, config, rl_device, sim_device, graphics_device_id, headless, use_dict_obs=False):
self.adr_cfg = self.cfg["task"].get("adr", {})
self.use_adr = self.adr_cfg.get("use_adr", False)
self.all_env_ids = torch.tensor(list(range(self.cfg["env"]["numEnvs"])), dtype=torch.long, device=sim_device)
if self.use_adr:
self.worker_adr_boundary_fraction = self.adr_cfg["worker_adr_boundary_fraction"]
self.adr_queue_threshold_length = self.adr_cfg["adr_queue_threshold_length"]
self.adr_objective_threshold_low = self.adr_cfg["adr_objective_threshold_low"]
self.adr_objective_threshold_high = self.adr_cfg["adr_objective_threshold_high"]
self.adr_extended_boundary_sample = self.adr_cfg["adr_extended_boundary_sample"]
self.adr_rollout_perf_alpha = self.adr_cfg["adr_rollout_perf_alpha"]
self.update_adr_ranges = self.adr_cfg["update_adr_ranges"]
self.adr_clear_other_queues = self.adr_cfg["clear_other_queues"]
self.adr_rollout_perf_last = None
self.adr_load_from_checkpoint = self.adr_cfg["adr_load_from_checkpoint"]
assert self.randomize, "Worker mode currently only supported when Domain Randomization is turned on"
# 0 = rollout worker
# 1 = ADR worker (see https://arxiv.org/pdf/1910.07113.pdf Section 5)
# 2 = eval worker
# rollout type is selected when an environment gets randomized
self.worker_types = torch.zeros(self.cfg["env"]["numEnvs"], dtype=torch.long, device=sim_device)
self.adr_tensor_values = {}
self.adr_params = self.adr_cfg["params"]
self.adr_params_keys = list(self.adr_params.keys())
# list of params which rely on patching the built in domain randomisation
self.adr_params_builtin_keys = []
for k in self.adr_params:
self.adr_params[k]["range"] = self.adr_params[k]["init_range"]
if "limits" not in self.adr_params[k]:
self.adr_params[k]["limits"] = [None, None]
if "delta_style" in self.adr_params[k]:
assert self.adr_params[k]["delta_style"] in ["additive", "multiplicative"]
else:
self.adr_params[k]["delta_style"] = "additive"
if "range_path" in self.adr_params[k]:
self.adr_params_builtin_keys.append(k)
else: # normal tensorised ADR param
param_type = self.adr_params[k].get("type", "uniform")
dtype = torch.long if param_type == "categorical" else torch.float
self.adr_tensor_values[k] = torch.zeros(self.cfg["env"]["numEnvs"], device=sim_device, dtype=dtype)
self.num_adr_params = len(self.adr_params)
# modes for ADR workers.
# there are 2n modes, where mode 2n is lower range and mode 2n+1 is upper range for DR parameter n
self.adr_modes = torch.zeros(self.cfg["env"]["numEnvs"], dtype=torch.long, device=sim_device)
self.adr_objective_queues = [deque(maxlen=self.adr_queue_threshold_length) for _ in range(2*self.num_adr_params)]
super().__init__(config, rl_device, sim_device, graphics_device_id, headless, use_dict_obs=use_dict_obs)
def get_current_adr_params(self, dr_params):
"""Splices the current ADR parameters into the requried ranges"""
current_adr_params = copy.deepcopy(dr_params)
for k in self.adr_params_builtin_keys:
nested_dict_set_attr(current_adr_params, self.adr_params[k]["range_path"], self.adr_params[k]["range"])
return current_adr_params
def get_dr_params_by_env_id(self, env_id, default_dr_params, current_adr_params):
"""Returns the (dictionary) DR params for a particular env ID.
(only applies to env randomisations, for tensor randomisations see `sample_adr_tensor`.)
Params:
env_id: which env ID to get the dict for.
default_dr_params: environment default DR params.
current_adr_params: current dictionary of DR params with current ADR ranges patched in.
Returns:
a patched dictionary with the env randomisations corresponding to the env ID.
"""
env_type = self.worker_types[env_id]
if env_type == RolloutWorkerModes.ADR_ROLLOUT: # rollout worker, uses current ADR params
return current_adr_params
elif env_type == RolloutWorkerModes.ADR_BOUNDARY: # ADR worker, substitute upper or lower bound as entire range for this env
adr_mode = int(self.adr_modes[env_id])
env_adr_params = copy.deepcopy(current_adr_params)
adr_id = adr_mode // 2 # which adr parameter
adr_bound = adr_mode % 2 # 0 = lower, 1 = upper
param_name = self.adr_params_keys[adr_id]
# this DR parameter is randomised as a tensor not through normal DR api
# if not "range_path" in self.adr_params[self.adr_params_keys[adr_id]]:
if not param_name in self.adr_params_builtin_keys:
return env_adr_params
if self.adr_extended_boundary_sample:
boundary_value = self.adr_params[param_name]["next_limits"][adr_bound]
else:
boundary_value = self.adr_params[param_name]["range"][adr_bound]
new_range = [boundary_value, boundary_value]
nested_dict_set_attr(env_adr_params, self.adr_params[param_name]["range_path"], new_range)
return env_adr_params
elif env_type == RolloutWorkerModes.TEST_ENV: # eval worker, uses default fixed params
return default_dr_params
else:
raise NotImplementedError
def modify_adr_param(self, param, direction, adr_param_dict, param_limit=None):
"""Modify an ADR param.
Args:
param: current value of the param.
direction: what direction to move the ADR parameter ('up' or 'down')
adr_param_dict: dictionary of ADR parameter, used to read delta and method of applying delta
param_limit: limit of the parameter (upper bound for 'up' and lower bound for 'down' mode)
Returns:
whether the param was updated
"""
op = adr_param_dict["delta_style"]
delta = adr_param_dict["delta"]
if direction == 'up':
if op == "additive":
new_val = param + delta
elif op == "multiplicative":
assert delta > 1.0, "Must have delta>1 for multiplicative ADR update."
new_val = param * delta
else:
raise NotImplementedError
if param_limit is not None:
new_val = min(new_val, param_limit)
changed = abs(new_val - param) > 1e-9
return new_val, changed
elif direction == 'down':
if op == "additive":
new_val = param - delta
elif op == "multiplicative":
assert delta > 1.0, "Must have delta>1 for multiplicative ADR update."
new_val = param / delta
else:
raise NotImplementedError
if param_limit is not None:
new_val = max(new_val, param_limit)
changed = abs(new_val - param) > 1e-9
return new_val, changed
else:
raise NotImplementedError
@staticmethod
def env_ids_from_mask(mask):
return torch.nonzero(mask, as_tuple=False).squeeze(-1)
def sample_adr_tensor(self, param_name, env_ids=None):
"""Samples the values for a particular ADR parameter as a tensor.
Sets the value as a side-effect in the dictionary of current adr tensors.
Args:
param_name: name of the parameter to sample
env_ids: env ids to sample
Returns:
(len(env_ids), tensor_dim) tensor of sampled parameter values,
where tensor_dim is the trailing dimension of the generated tensor as
specifide in the ADR conifg
"""
if env_ids is None:
env_ids = self.all_env_ids
sample_mask = torch.zeros(self.num_envs, dtype=torch.bool, device=self.device)
sample_mask[env_ids] = True
params = self.adr_params[param_name]
param_range = params["range"]
next_limits = params.get("next_limits", None)
param_type = params.get("type", "uniform")
n = self.adr_params_keys.index(param_name)
low_idx = 2*n
high_idx = 2*n + 1
adr_workers_low_mask = (self.worker_types == RolloutWorkerModes.ADR_BOUNDARY) & (self.adr_modes == low_idx) & sample_mask
adr_workers_high_mask = (self.worker_types == RolloutWorkerModes.ADR_BOUNDARY) & (self.adr_modes == high_idx) & sample_mask
rollout_workers_mask = (~adr_workers_low_mask) & (~adr_workers_high_mask) & sample_mask
rollout_workers_env_ids = self.env_ids_from_mask(rollout_workers_mask)
if param_type == "uniform":
result = torch.zeros((len(env_ids),), device=self.device, dtype=torch.float)
uniform_noise_rollout_workers = \
torch.rand((rollout_workers_env_ids.shape[0],), device=self.device, dtype=torch.float) \
* (param_range[1] - param_range[0]) + param_range[0]
result[rollout_workers_mask[env_ids]] = uniform_noise_rollout_workers
if self.adr_extended_boundary_sample:
result[adr_workers_low_mask[env_ids]] = next_limits[0]
result[adr_workers_high_mask[env_ids]] = next_limits[1]
else:
result[adr_workers_low_mask[env_ids]] = param_range[0]
result[adr_workers_high_mask[env_ids]] = param_range[1]
elif param_type == "categorical":
result = torch.zeros((len(env_ids), ), device=self.device, dtype=torch.long)
uniform_noise_rollout_workers = torch.randint(int(param_range[0]), int(param_range[1])+1, size=(rollout_workers_env_ids.shape[0], ), device=self.device)
result[rollout_workers_mask[env_ids]] = uniform_noise_rollout_workers
result[adr_workers_low_mask[env_ids]] = int(next_limits[0] if self.adr_extended_boundary_sample else param_range[0])
result[adr_workers_high_mask[env_ids]] = int(next_limits[1] if self.adr_extended_boundary_sample else param_range[1])
else:
raise NotImplementedError(f"Unknown distribution type {param_type}")
self.adr_tensor_values[param_name][env_ids] = result
return result
def get_adr_tensor(self, param_name, env_ids=None):
"""Returns the current value of an ADR tensor.
"""
if env_ids is None:
return self.adr_tensor_values[param_name]
else:
return self.adr_tensor_values[param_name][env_ids]
def recycle_envs(self, recycle_envs):
"""Recycle the workers that have finished their episodes or to be reassigned etc.
Args:
recycle_envs: env_ids of environments to be recycled
"""
worker_types_rand = torch.rand(len(recycle_envs), device=self.device, dtype=torch.float)
new_worker_types = torch.zeros(len(recycle_envs), device=self.device, dtype=torch.long)
# Choose new types for wokrers
new_worker_types[(worker_types_rand < self.worker_adr_boundary_fraction)] = RolloutWorkerModes.ADR_ROLLOUT
new_worker_types[(worker_types_rand >= self.worker_adr_boundary_fraction)] = RolloutWorkerModes.ADR_BOUNDARY
self.worker_types[recycle_envs] = new_worker_types
# resample the ADR modes (which boundary values to sample) for the given environments (only applies to ADR_BOUNDARY mode)
self.adr_modes[recycle_envs] = torch.randint(0, self.num_adr_params * 2, (len(recycle_envs),), dtype=torch.long, device=self.device)
def adr_update(self, rand_envs, adr_objective):
"""Performs ADR update step (implements algorithm 1 from https://arxiv.org/pdf/1910.07113.pdf).
"""
rand_env_mask = torch.zeros(self.num_envs, dtype=torch.bool, device=self.device)
rand_env_mask[rand_envs] = True
total_nats = 0.0 # measuring entropy
if self.update_adr_ranges:
adr_params_iter = list(enumerate(self.adr_params))
random.shuffle(adr_params_iter)
# only recycle once
already_recycled = False
for n, adr_param_name in adr_params_iter:
# mode index for environments evaluating lower ADR bound
low_idx = 2*n
# mode index for environments evaluating upper ADR bound
high_idx = 2*n+1
adr_workers_low = (self.worker_types == RolloutWorkerModes.ADR_BOUNDARY) & (self.adr_modes == low_idx)
adr_workers_high = (self.worker_types == RolloutWorkerModes.ADR_BOUNDARY) & (self.adr_modes == high_idx)
# environments which will be evaluated for ADR (finished the episode) and which are evaluating performance at the
# lower and upper boundaries
adr_done_low = rand_env_mask & adr_workers_low
adr_done_high = rand_env_mask & adr_workers_high
# objective value at environments which have been evaluating the lower bound of ADR param n
objective_low_bounds = adr_objective[adr_done_low]
# objective value at environments which have been evaluating the upper bound of ADR param n
objective_high_bounds = adr_objective[adr_done_high]
# add the success of objectives to queues
self.adr_objective_queues[low_idx].extend(objective_low_bounds.cpu().numpy().tolist())
self.adr_objective_queues[high_idx].extend(objective_high_bounds.cpu().numpy().tolist())
low_queue = self.adr_objective_queues[low_idx]
high_queue = self.adr_objective_queues[high_idx]
mean_low = np.mean(low_queue) if len(low_queue) > 0 else 0.
mean_high = np.mean(high_queue) if len(high_queue) > 0 else 0.
current_range = self.adr_params[adr_param_name]["range"]
range_lower = current_range[0]
range_upper = current_range[1]
range_limits = self.adr_params[adr_param_name]["limits"]
init_range = self.adr_params[adr_param_name]["init_range"]
# one step beyond the current ADR values
[next_limit_lower, next_limit_upper] = self.adr_params[adr_param_name].get("next_limits", [None, None])
changed_low, changed_high = False, False
if len(low_queue) >= self.adr_queue_threshold_length:
changed_low = False
if mean_low < self.adr_objective_threshold_low:
# increase lower bound
range_lower, changed_low = self.modify_adr_param(
range_lower, 'up', self.adr_params[adr_param_name], param_limit=init_range[0]
)
elif mean_low > self.adr_objective_threshold_high:
# reduce lower bound
range_lower, changed_low = self.modify_adr_param(
range_lower, 'down', self.adr_params[adr_param_name], param_limit=range_limits[0]
)
# if the ADR boundary is changed, workers working from the old paremeters become invalid.
# Therefore, while we use the data from them to train, we can no longer use them to evaluate DR at the boundary
if changed_low:
print(f'Changing {adr_param_name} lower bound. Queue length {len(self.adr_objective_queues[low_idx])}. Mean perf: {mean_low}. Old val: {current_range[0]}. New val: {range_lower}')
self.adr_objective_queues[low_idx].clear()
self.worker_types[adr_workers_low] = RolloutWorkerModes.ADR_ROLLOUT
if len(high_queue) >= self.adr_queue_threshold_length:
if mean_high < self.adr_objective_threshold_low:
# reduce upper bound
range_upper, changed_high = self.modify_adr_param(
range_upper, 'down', self.adr_params[adr_param_name], param_limit=init_range[1]
)
elif mean_high > self.adr_objective_threshold_high:
# increase upper bound
range_upper, changed_high = self.modify_adr_param(
range_upper, 'up', self.adr_params[adr_param_name], param_limit=range_limits[1]
)
# if the ADR boundary is changed, workers working from the old paremeters become invalid.
# Therefore, while we use the data from them to train, we can no longer use them to evaluate DR at the boundary
if changed_high:
print(f'Changing upper bound {adr_param_name}. Queue length {len(self.adr_objective_queues[high_idx])}. Mean perf {mean_high}. Old val: {current_range[1]}. New val: {range_upper}')
self.adr_objective_queues[high_idx].clear()
self.worker_types[adr_workers_high] = RolloutWorkerModes.ADR_ROLLOUT
if changed_low or next_limit_lower is None:
next_limit_lower, _ = self.modify_adr_param(range_lower, 'down', self.adr_params[adr_param_name], param_limit=range_limits[0])
if changed_high or next_limit_upper is None:
next_limit_upper, _ = self.modify_adr_param(range_upper, 'up', self.adr_params[adr_param_name], param_limit=range_limits[1])
self.adr_params[adr_param_name]["range"] = [range_lower, range_upper]
if not self.adr_params[adr_param_name]["delta"] < 1e-9: # disabled
upper_lower_delta = range_upper - range_lower
if upper_lower_delta < 1e-3:
upper_lower_delta = 1e-3
nats = np.log(upper_lower_delta)
total_nats += nats
# print(f'nats {nats} delta {upper_lower_delta} range lower {range_lower} range upper {range_upper}')
self.adr_params[adr_param_name]["next_limits"] = [next_limit_lower, next_limit_upper]
if hasattr(self, 'extras') and ((changed_high or changed_low) or self.last_step % 100 == 0): # only log so often to prevent huge log files with ADR vars
self.extras[f'adr/params/{adr_param_name}/lower'] = range_lower
self.extras[f'adr/params/{adr_param_name}/upper'] = range_upper
self.extras[f'adr/objective_perf/boundary/{adr_param_name}/lower/value'] = mean_low
self.extras[f'adr/objective_perf/boundary/{adr_param_name}/lower/queue_len'] = len(low_queue)
self.extras[f'adr/objective_perf/boundary/{adr_param_name}/upper/value'] = mean_high
self.extras[f'adr/objective_perf/boundary/{adr_param_name}/upper/queue_len'] = len(high_queue)
if self.adr_clear_other_queues and (changed_low or changed_high):
for q in self.adr_objective_queues:
q.clear()
recycle_envs = torch.nonzero((self.worker_types == RolloutWorkerModes.ADR_BOUNDARY), as_tuple=False).squeeze(-1)
self.recycle_envs(recycle_envs)
already_recycled = True
break
if hasattr(self, 'extras') and self.last_step % 100 == 0: # only log so often to prevent huge log files with ADR vars
mean_perf = adr_objective[rand_env_mask & (self.worker_types == RolloutWorkerModes.ADR_ROLLOUT)].mean()
if self.adr_rollout_perf_last is None:
self.adr_rollout_perf_last = mean_perf
else:
self.adr_rollout_perf_last = self.adr_rollout_perf_last * self.adr_rollout_perf_alpha + mean_perf * (1-self.adr_rollout_perf_alpha)
self.extras[f'adr/objective_perf/rollouts'] = self.adr_rollout_perf_last
self.extras[f'adr/npd'] = total_nats / len(self.adr_params)
if not already_recycled:
self.recycle_envs(rand_envs)
else:
self.worker_types[rand_envs] = RolloutWorkerModes.ADR_ROLLOUT
# ensure tensors get re-sampled before new episode
for k in self.adr_tensor_values:
self.sample_adr_tensor(k, rand_envs)
def apply_randomizations(self, dr_params, randomize_buf, adr_objective=None, randomisation_callback=None):
"""Apply domain randomizations to the environment.
Note that currently we can only apply randomizations only on resets, due to current PhysX limitations
Args:
dr_params: parameters for domain randomization to use.
randomize_buf: selective randomisation of environments
adr_objective: consecutive successes scalar
randomisation_callback: callbacks we may want to use from the environment class
"""
# If we don't have a randomization frequency, randomize every step
rand_freq = dr_params.get("frequency", 1)
# First, determine what to randomize:
# - non-environment parameters when > frequency steps have passed since the last non-environment
# - physical environments in the reset buffer, which have exceeded the randomization frequency threshold
# - on the first call, randomize everything
self.last_step = self.gym.get_frame_count(self.sim)
# for ADR
if self.use_adr:
if self.first_randomization:
adr_env_ids = list(range(self.num_envs))
else:
adr_env_ids = torch.nonzero(randomize_buf, as_tuple=False).squeeze(-1).tolist()
self.adr_update(adr_env_ids, adr_objective)
current_adr_params = self.get_current_adr_params(dr_params)
if self.first_randomization:
do_nonenv_randomize = True
env_ids = list(range(self.num_envs))
else:
do_nonenv_randomize = (self.last_step - self.last_rand_step) >= rand_freq
env_ids = torch.nonzero(randomize_buf, as_tuple=False).squeeze(-1).tolist()
if do_nonenv_randomize:
self.last_rand_step = self.last_step
# For Manual DR
if not self.use_adr:
if self.first_randomization:
do_nonenv_randomize = True
env_ids = list(range(self.num_envs))
else:
# randomise if the number of steps since the last randomization is greater than the randomization frequency
do_nonenv_randomize = (self.last_step - self.last_rand_step) >= rand_freq
rand_envs = torch.where(self.randomize_buf >= rand_freq, torch.ones_like(self.randomize_buf), torch.zeros_like(self.randomize_buf))
rand_envs = torch.logical_and(rand_envs, self.reset_buf)
env_ids = torch.nonzero(rand_envs, as_tuple=False).squeeze(-1).tolist()
self.randomize_buf[rand_envs] = 0
if do_nonenv_randomize:
self.last_rand_step = self.last_step
# We don't use it for ADR(!)
if self.randomize_act_builtin:
self.action_randomizations = self.get_randomization_dict(dr_params['actions'], (self.num_actions,))
if self.use_dict_obs and self.randomize_obs_builtin:
for nonphysical_param in self.randomisation_obs:
self.obs_randomizations[nonphysical_param] = self.get_randomization_dict(dr_params['observations'][nonphysical_param],
self.obs_space[nonphysical_param].shape)
elif self.randomize_obs_builtin:
self.observation_randomizations = self.get_randomization_dict(dr_params['observations'], self.obs_space.shape)
param_setters_map = get_property_setter_map(self.gym)
param_setter_defaults_map = get_default_setter_args(self.gym)
param_getters_map = get_property_getter_map(self.gym)
# On first iteration, check the number of buckets
if self.first_randomization:
check_buckets(self.gym, self.envs, dr_params)
# Randomize non-environment parameters e.g. gravity, timestep, rest_offset etc.