forked from PKU-Alignment/safety-gymnasium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.py
342 lines (279 loc) · 12.8 KB
/
builder.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
# Copyright 2022-2023 OmniSafe Team. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Env builder."""
from __future__ import annotations
from dataclasses import asdict, dataclass
from typing import Any, ClassVar
import gymnasium
import numpy as np
from safety_gymnasium import tasks
from safety_gymnasium.bases.base_task import BaseTask
from safety_gymnasium.utils.common_utils import ResamplingError, quat2zalign
from safety_gymnasium.utils.task_utils import get_task_class_name
@dataclass
class RenderConf:
r"""Render options.
Attributes:
mode (str): render mode, can be 'human', 'rgb_array', 'depth_array'.
width (int): width of the rendered image.
height (int): height of the rendered image.
camera_id (int): camera id to render.
camera_name (str): camera name to render.
Note:
``camera_id`` and ``camera_name`` can only be set one of them.
"""
mode: str = None
width: int = 256
height: int = 256
camera_id: int = None
camera_name: str = None
# pylint: disable-next=too-many-instance-attributes
class Builder(gymnasium.Env, gymnasium.utils.EzPickle):
r"""An entry point to organize different environments, while showing unified API for users.
The Builder class constructs the basic control framework of environments, while
the details were hidden. There is another important parts, which is **task module**
including all task specific operation.
Methods:
- :meth:`_setup_simulation`: Set up mujoco the simulation instance.
- :meth:`_get_task`: Instantiate a task object.
- :meth:`set_seed`: Set the seed for the environment.
- :meth:`reset`: Reset the environment.
- :meth:`step`: Step the environment.
- :meth:`_reward`: Calculate the reward.
- :meth:`_cost`: Calculate the cost.
- :meth:`render`: Render the environment.
Attributes:
- :attr:`task_id` (str): Task id.
- :attr:`config` (dict): Pre-defined configuration of the environment, which is passed via
:meth:`safety_gymnasium.register()`.
- :attr:`render_parameters` (RenderConf): Render parameters.
- :attr:`action_space` (gymnasium.spaces.Box): Action space.
- :attr:`observation_space` (gymnasium.spaces.Dict): Observation space.
- :attr:`obs_space_dict` (dict): Observation space dictionary.
- :attr:`done` (bool): Whether the episode is done.
"""
metadata: ClassVar[dict[str, Any]] = {
'render_modes': [
'human',
'rgb_array',
'depth_array',
],
'render_fps': 30,
}
def __init__( # pylint: disable=too-many-arguments
self,
task_id: str,
config: dict | None = None,
render_mode: str | None = None,
width: int = 256,
height: int = 256,
camera_id: int | None = None,
camera_name: str | None = None,
) -> None:
"""Initialize the builder.
Note:
The ``camera_name`` parameter can be chosen from:
- **human**: The camera used for freely moving around and can get input
from keyboard real time.
- **vision**: The camera used for vision observation, which is fixed in front of the
agent's head.
- **track**: The camera used for tracking the agent.
- **fixednear**: The camera used for top-down observation.
- **fixedfar**: The camera used for top-down observation, but is further than **fixednear**.
Args:
task_id (str): Task id.
config (dict): Pre-defined configuration of the environment, which is passed via
:meth:`safety_gymnasium.register`.
render_mode (str): Render mode, can be 'human', 'rgb_array', 'depth_array'.
width (int): Width of the rendered image.
height (int): Height of the rendered image.
camera_id (int): Camera id to render.
camera_name (str): Camera name to render.
"""
gymnasium.utils.EzPickle.__init__(self, config=config)
self.task_id: str = task_id
self.config: dict = config
self._seed: int = None
self._setup_simulation()
self.first_reset: bool = None
self.steps: int = None
self.cost: float = None
self.terminated: bool = True
self.truncated: bool = False
self.render_parameters = RenderConf(render_mode, width, height, camera_id, camera_name)
def _setup_simulation(self) -> None:
"""Set up mujoco the simulation instance."""
self.task = self._get_task()
self.set_seed()
def _get_task(self) -> BaseTask:
"""Instantiate a task object."""
class_name = get_task_class_name(self.task_id)
assert hasattr(tasks, class_name), f'Task={class_name} not implemented.'
task_class = getattr(tasks, class_name)
task = task_class(config=self.config)
task.build_observation_space()
return task
def set_seed(self, seed: int | None = None) -> None:
"""Set internal random state seeds."""
self._seed = np.random.randint(2**32, dtype='int64') if seed is None else seed
self.task.random_generator.set_random_seed(self._seed)
def reset(
self,
*,
seed: int | None = None,
options: dict | None = None,
) -> tuple[np.ndarray, dict]: # pylint: disable=arguments-differ
"""Reset the environment and return observations."""
info = {}
if not self.task.mechanism_conf.randomize_layout:
assert seed is None, 'Cannot set seed if randomize_layout=False'
self.set_seed(0)
elif seed is not None:
self.set_seed(seed)
self.terminated = False
self.truncated = False
self.steps = 0 # Count of steps taken in this episode
self.task.reset()
self.task.specific_reset()
self.task.update_world() # refresh specific settings
self.task.agent.reset()
cost = self._cost()
assert cost['cost_sum'] == 0, f'World has starting cost! {cost}'
# Reset stateful parts of the environment
self.first_reset = False # Built our first world successfully
# Return an observation
return (self.task.obs(), info)
def step(self, action: np.ndarray) -> tuple[np.ndarray, float, float, bool, bool, dict]:
"""Take a step and return observation, reward, cost, terminated, truncated, info."""
assert not self.done, 'Environment must be reset before stepping.'
action = np.array(action, copy=False) # cast to ndarray
if action.shape != self.action_space.shape: # check action dimension
raise ValueError('Action dimension mismatch')
info = {}
exception = self.task.simulation_forward(action)
if exception:
self.truncated = True
reward = self.task.reward_conf.reward_exception
info['cost_exception'] = 1.0
else:
# Reward processing
reward = self._reward()
# Constraint violations
info.update(self._cost())
cost = info['cost_sum']
self.task.specific_step()
# Goal processing
if self.task.goal_achieved:
info['goal_met'] = True
if self.task.mechanism_conf.continue_goal:
# Update the internal layout
# so we can correctly resample (given objects have moved)
self.task.update_layout()
# Try to build a new goal, end if we fail
if self.task.mechanism_conf.terminate_resample_failure:
try:
self.task.update_world()
except ResamplingError:
# Normal end of episode
self.terminated = True
else:
# Try to make a goal, which could raise a ResamplingError exception
self.task.update_world()
else:
self.terminated = True
# termination of death processing
if not self.task.agent.is_alive():
self.terminated = True
# Timeout
self.steps += 1
if self.steps >= self.task.num_steps:
self.truncated = True # Maximum number of steps in an episode reached
if self.render_parameters.mode == 'human':
self.render()
return self.task.obs(), reward, cost, self.terminated, self.truncated, info
def _reward(self) -> float:
"""Calculate the current rewards.
Call exactly once per step.
"""
reward = self.task.calculate_reward()
# Intrinsic reward for uprightness
if self.task.reward_conf.reward_orientation:
zalign = quat2zalign(
self.task.data.get_body_xquat(self.task.reward_conf.reward_orientation_body),
)
reward += self.task.reward_conf.reward_orientation_scale * zalign
# Clip reward
reward_clip = self.task.reward_conf.reward_clip
if reward_clip:
in_range = -reward_clip < reward < reward_clip
if not in_range:
reward = np.clip(reward, -reward_clip, reward_clip)
print('Warning: reward was outside of range!')
return reward
def _cost(self) -> dict:
"""Calculate the current costs and return a dict.
Call exactly once per step.
"""
cost = self.task.calculate_cost()
# Optionally remove shaping from reward functions.
if self.task.cost_conf.constrain_indicator:
for k in list(cost.keys()):
cost[k] = float(cost[k] > 0.0) # Indicator function
self.cost = cost
return cost
def render(self) -> np.ndarray | None:
"""Call underlying :meth:`safety_gymnasium.bases.underlying.Underlying.render` directly.
Width and height in parameters are constant defaults for rendering
frames for humans. (not used for vision)
The set of supported modes varies per environment. (And some
third-party environments may not support rendering at all.)
By convention, if render_mode is:
- None (default): no render is computed.
- human: render return None.
The environment is continuously rendered in the current display or terminal. Usually for human consumption.
- rgb_array: return a single frame representing the current state of the environment.
A frame is a numpy.ndarray with shape (x, y, 3) representing RGB values for an x-by-y pixel image.
- rgb_array_list: return a list of frames representing the states of the environment since the last reset.
Each frame is a numpy.ndarray with shape (x, y, 3), as with `rgb_array`.
- depth_array: return a single frame representing the current state of the environment.
A frame is a numpy.ndarray with shape (x, y) representing depth values for an x-by-y pixel image.
- depth_array_list: return a list of frames representing the states of the environment since the last reset.
Each frame is a numpy.ndarray with shape (x, y), as with `depth_array`.
"""
assert self.render_parameters.mode, 'Please specify the render mode when you make env.'
assert (
not self.task.observe_vision
), 'When you use vision envs, you should not call this function explicitly.'
return self.task.render(cost=self.cost, **asdict(self.render_parameters))
@property
def action_space(self) -> gymnasium.spaces.Box:
"""Helper to get action space."""
return self.task.action_space
@property
def observation_space(self) -> gymnasium.spaces.Box | gymnasium.spaces.Dict:
"""Helper to get observation space."""
return self.task.observation_space
@property
def obs_space_dict(self) -> dict[str, gymnasium.spaces.Box]:
"""Helper to get observation space dictionary."""
return self.task.obs_info.obs_space_dict
@property
def done(self) -> bool:
"""Whether this episode is ended."""
return self.terminated or self.truncated
@property
def render_mode(self) -> str:
"""The render mode."""
return self.render_parameters.mode