forked from facebookresearch/gala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmujoco.py
117 lines (82 loc) · 3.67 KB
/
mujoco.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
import gym
import numpy as np
class RandomisedMuJoCo(gym.Wrapper):
""" A gym environment wrapper that randomises parameters for MuJoCo Environments. """
def __init__(self, env: gym.Env):
super().__init__(env)
def sample_parameters(self, seed: int):
""" Sample the environments parameters for wind and scaling mass and apply them """
random = np.random.RandomState(seed)
# Wind
# Changing viscosity and density of medium from 0 to that of air so wind has an effect.
self.model.opt.viscosity = 0.0002
self.model.opt.density = 1.2
# We consider wind in only the xy plane.
self.model.opt.wind[:2] = 30 * (random.beta(0.4, 0.4, size=2) - 0.5)
# Mass
self.model.body_mass[:] = self.model.body_mass * (1 + 1.9 * (random.beta(0.1, 0.1) - 0.5))
self.sim.forward()
class MuJoCoWithAir(gym.Wrapper):
"""
A gym environment wrapping a MuJoCo environment to make the medium have the properties of air.
"""
def __init__(self, env: gym.Env):
super().__init__(env)
self.model.opt.viscosity = 0.0002
self.model.opt.density = 1.2
self.sim.forward()
def sample_parameters(self, *args):
pass
class RandomisedMassMuJoCo(gym.Wrapper):
""" A gym environment wrapper which randomises the agent's mass in a MuJoCo environment """
def __init__(self, env: gym.Env):
super().__init__(env)
def sample_parameters(self, seed: int):
""" Sample the environment's parameters for scaling the agent's mass and apply them """
random = np.random.RandomState(seed)
# Changing viscosity and density of medium from 0 to that of air.
self.model.opt.viscosity = 0.0002
self.model.opt.density = 1.2
# Mass
self.model.body_mass[:] = self.model.body_mass * (1 + 1.9 * (random.beta(0.1, 0.1) - 0.5))
self.sim.forward()
class RandomisedWindMuJoCo(gym.Wrapper):
def __init__(self, env: gym.Env):
super().__init__(env)
def sample_parameters(self, seed: int):
""" Sample the environment's parameters for wind in the xy plane and apply them """
random = np.random.RandomState(seed)
# Wind
# Changing viscosity and density of medium from 0 to that of air so wind has an effect.
self.model.opt.viscosity = 0.0002
self.model.opt.density = 1.2
# We consider wind in only the xy plane.
wind = 30 * (random.beta(0.4, 0.4, size=2) - 0.5)
self.model.opt.wind[:2] = wind
self.sim.forward()
return wind
# Environment buiding functions used to easily register environments in `register.py`
def make_random_hopper():
standard_hopper = gym.make('Hopper-v2')
return RandomisedMuJoCo(standard_hopper)
def make_random_half_cheetah():
standard_half_cheetah = gym.make('HalfCheetah-v2')
return RandomisedMuJoCo(standard_half_cheetah)
def make_hopper_with_air():
standard_hopper = gym.make('Hopper-v2')
return MuJoCoWithAir(standard_hopper)
def make_half_cheetah_with_air():
standard_half_cheetah = gym.make('HalfCheetah-v2')
return MuJoCoWithAir(standard_half_cheetah)
def make_random_wind_hopper():
standard_hopper = gym.make('Hopper-v2')
return RandomisedWindMuJoCo(standard_hopper)
def make_random_mass_hopper():
standard_hopper = gym.make('Hopper-v2')
return RandomisedMassMuJoCo(standard_hopper)
def make_random_wind_half_cheetah():
standard_half_cheetah = gym.make('HalfCheetah-v2')
return RandomisedWindMuJoCo(standard_half_cheetah)
def make_random_mass_half_cheetah():
standard_half_cheetah = gym.make('HalfCheetah-v2')
return RandomisedMassMuJoCo(standard_half_cheetah)