-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_halfcheetah.py
154 lines (126 loc) · 5.09 KB
/
dynamic_halfcheetah.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
import numpy as np
from gymnasium import utils
from gymnasium.spaces import Box
from gymnasium.envs.mujoco import MujocoEnv
from ray.tune.registry import register_env
from gymnasium.envs.registration import register
DEFAULT_CAMERA_CONFIG = {
"distance": 4.0,
}
class CustomHalfCheetahEnv(MujocoEnv, utils.EzPickle):
metadata = {
"render_modes": [
"human",
"rgb_array",
"depth_array",
],
"render_fps": 20,
}
def __init__(
self,
forward_reward_weight=1.0,
ctrl_cost_weight=0.1,
reset_noise_scale=0.1,
exclude_current_positions_from_observation=True,
**kwargs,
):
utils.EzPickle.__init__(
self,
forward_reward_weight,
ctrl_cost_weight,
reset_noise_scale,
exclude_current_positions_from_observation,
**kwargs,
)
self._current_step = 0
self._total_step = 0
self._max_episode_time = 1000
self._forward_reward_weight = forward_reward_weight
self._ctrl_cost_weight = ctrl_cost_weight
self._reset_noise_scale = reset_noise_scale
self._exclude_current_positions_from_observation = (
exclude_current_positions_from_observation
)
if exclude_current_positions_from_observation:
observation_space = Box(
low=-np.inf, high=np.inf, shape=(17,), dtype=np.float64
)
else:
observation_space = Box(
low=-np.inf, high=np.inf, shape=(18,), dtype=np.float64
)
MujocoEnv.__init__(
self,
"half_cheetah.xml",
5,
observation_space=observation_space,
default_camera_config=DEFAULT_CAMERA_CONFIG,
**kwargs,
)
def control_cost(self, action):
control_cost = self._ctrl_cost_weight * np.sum(np.square(action))
return control_cost
def step(self, action):
self._current_step += 1
self._total_step += 1
x_position_before = self.data.qpos[0]
self.do_simulation(action, self.frame_skip)
x_position_after = self.data.qpos[0]
x_velocity = (x_position_after - x_position_before) / self.dt
ctrl_cost = self.control_cost(action)
forward_reward = self._forward_reward_weight * x_velocity
observation = self._get_obs()
reward = forward_reward - ctrl_cost
terminated = False
info = {
"x_position": x_position_after,
"x_velocity": x_velocity,
"reward_run": forward_reward,
"reward_ctrl": -ctrl_cost,
}
if self.render_mode == "human":
self.render()
terminated = self._current_step >= self._max_episode_time
# change the dynamics of reward after ~50M timesteps (after stabilization)
# could also change a physical attribute, but seems a little more difficult
# will look into this if the reward weight doesn't do much
# update: reward weight didn't really work, so now implemented a decrease in joint stiffness
# joint stiffness: how much the joint resists movement from its rest position (i.e. higher values indicate more rigidity -- helping it find balance while standing)
# joint damping coefficient: the amount of resistance against the joint's motion which slows down rapid movements and prevents overshooting (similar to "D" in PID)
if self._total_step % 750_000 == 0:
# or can use self.model.dof_damping
self.model.jnt_stiffness[3] /= 5
print(f"changing back joint stiffness to {self.model.jnt_stiffness[3]}")
return observation, reward, terminated, False, info
def reset(self, *, seed=None, options=None):
return self.reset_model(), {}
def _get_obs(self):
position = self.data.qpos.flat.copy()
velocity = self.data.qvel.flat.copy()
if self._exclude_current_positions_from_observation:
position = position[1:]
observation = np.concatenate((position, velocity)).ravel()
return observation
def reset_model(self):
noise_low = -self._reset_noise_scale
noise_high = self._reset_noise_scale
qpos = self.init_qpos + self.np_random.uniform(
low=noise_low, high=noise_high, size=self.model.nq
)
qvel = (
self.init_qvel
+ self._reset_noise_scale * self.np_random.standard_normal(self.model.nv)
)
self.set_state(qpos, qvel)
observation = self._get_obs()
self._current_step = 0
return observation
# custom env. needs registered within gymnasium AND also RLLIB (below)
register(
id='CustomHalfCheetah-v0',
entry_point=CustomHalfCheetahEnv,
)
# RLLIB registration
def env_creator(config):
return CustomHalfCheetahEnv(**config)
register_env("CustomHalfCheetahEnv", env_creator)