-
Notifications
You must be signed in to change notification settings - Fork 3
/
robot_env.py
265 lines (214 loc) · 8.91 KB
/
robot_env.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
"""Robot_env copy from the robot environments in OpenAI's gym.
Minor changes have been made for our use case.
See https://github.com/Farama-Foundation/Gym-Robotics.
"""
import os
import copy
import numpy as np
from typing import Optional, Union, Dict, Tuple, Any, List
import gym
from gym import error, spaces
from gym.utils import seeding
try:
import mujoco_py as mjpy
except ImportError as e:
raise error.DependencyNotInstalled(
"{}. (HINT: you need to install mujoco_py, and also perform the setup instructions here: \
https://github.com/openai/mujoco-py/.)".format(e))
DEFAULT_SIZE = 500
class RobotEnv(gym.GoalEnv):
"""Superclass for robot environments."""
def __init__(self, model_path: str, initial_qpos: dict, n_actions: int, n_substeps: int):
"""Load assets, models and start the Mujoco sim.
Args:
model_path: Path to the sim description xml file.
initial_qpos: Initial pose information for objects in the sim.
n_actions: Action dimension.
n_substeps: Number of internal Mujoco steps for one single gym step.
"""
if model_path.startswith("/"):
fullpath = model_path
else:
fullpath = os.path.join(os.path.dirname(__file__), "assets", model_path)
if not os.path.exists(fullpath):
raise IOError("File {} does not exist".format(fullpath))
model = mjpy.load_model_from_path(fullpath)
self.sim = mjpy.MjSim(model, nsubsteps=n_substeps)
self.viewer = None
self._viewers = {}
self._use_info = True
self._use_contact_info = False
self._use_step_reward = True
self.metadata = {
"render.modes": ["human", "rgb_array"],
"video.frames_per_second": int(np.round(1.0 / self.dt)),
}
self.seed(1)
self._env_setup(initial_qpos=initial_qpos)
self.initial_state = copy.deepcopy(self.sim.get_state())
self.goal = self._sample_goal()
obs = self._get_obs()
self.action_space = spaces.Box(-1.0, 1.0, shape=(n_actions,), dtype="float32")
self.observation_space = spaces.Dict(
dict(
desired_goal=spaces.Box(-np.inf,
np.inf,
shape=obs["achieved_goal"].shape,
dtype="float32"),
achieved_goal=spaces.Box(-np.inf,
np.inf,
shape=obs["achieved_goal"].shape,
dtype="float32"),
observation=spaces.Box(-np.inf,
np.inf,
shape=obs["observation"].shape,
dtype="float32"),
))
self.seed()
@property
def dt(self) -> float:
"""Time delta property."""
return self.sim.model.opt.timestep * self.sim.nsubsteps
# Env methods
# ----------------------------
def seed(self, seed: Optional[float] = None) -> List[float]:
"""Set the random seed for the environment.
Args:
seed: The random seed.
"""
self.np_random, seed = seeding.np_random(seed)
return [seed]
def step(self, action: np.ndarray) -> Tuple[dict, float, bool, Any]:
"""Take a step in the environment with the given action.
Args:
action: Agent action.
"""
action = np.clip(action, self.action_space.low, self.action_space.high)
self._set_action(action)
self.sim.step()
self._step_callback()
obs = self._get_obs()
done = False
if self._use_step_reward:
reward = self.compute_reward(obs["achieved_goal"], self.goal, None)
else:
reward = np.nan
if self._use_info:
info = {"is_success": self._is_success(obs["achieved_goal"], obs["desired_goal"])}
else:
info = {}
if self._use_contact_info:
info["contact_info"] = self._get_contact_info()
info["gripper_info"] = self._get_gripper_info()
info["object_info"] = self._get_object_info()
return obs, reward, done, info
def reset(self) -> Dict[str, np.ndarray]:
"""Attempt to reset the simulator.
Since we randomize initial conditions, it is possible to get into a state with numerical
issues (e.g. due to penetration or Gimbel lock) or we may not achieve an initial condition
(e.g. an object is within the hand). In this case, we just keep randomizing until we
eventually achieve a valid initial configuration.
"""
super(RobotEnv, self).reset()
did_reset_sim = False
while not did_reset_sim:
did_reset_sim = self._reset_sim()
self.goal = self._sample_goal().copy()
obs = self._get_obs()
return obs
def close(self):
"""Close the viewer."""
if self.viewer is not None:
# self.viewer.finish()
self.viewer = None
self._viewers = {}
def render(self,
mode: str = "human",
width: int = DEFAULT_SIZE,
height: int = DEFAULT_SIZE) -> Optional[np.ndarray]:
"""Render the current sim state.
Args:
mode: Render mode.
width: Render window width.
heights: Render window height.
"""
self._render_callback()
if mode == "rgb_array":
self._get_viewer(mode).render(width, height)
# window size used for old mujoco-py:
data = self._get_viewer(mode).read_pixels(width, height, depth=False)
# original image is upside-down, so flip it
return data[::-1, :, :]
elif mode == "human":
self._get_viewer(mode).render()
def use_info(self, val: bool = True):
"""Enable info calculation.
Args:
val: Flag to enable or disable info.
"""
self._use_info = val
def use_contact_info(self, val: bool = True):
"""Enable contact information between the gripper and the object in the info step return.
Has to be a function since gym wraps the environment in a `TimeLimit` object which does not
forward attribute changes.
Args:
val: Flag to enable or disable contact information. Default is True.
"""
self._use_contact_info = val
def use_step_reward(self, val: bool = True):
"""Use reward computation during ``step``.
Args:
val: Flag to disable or enable step rewards.
"""
self._use_step_reward = val
def _get_viewer(self, mode: str) -> Union[mjpy.MjViewer, mjpy.MjRenderContextOffscreen]:
self.viewer = self._viewers.get(mode)
if self.viewer is None:
if mode == "human":
self.viewer = mjpy.MjViewer(self.sim)
elif mode == "rgb_array":
self.viewer = mjpy.MjRenderContextOffscreen(self.sim, device_id=-1)
self._viewer_setup()
self._viewers[mode] = self.viewer
return self.viewer
# Extension methods
# ----------------------------
def _reset_sim(self) -> bool:
"""Reset a simulation and indicate whether or not it was successful.
If a reset was unsuccessful (e.g. if a randomized state caused an error in the
simulation), this method should indicate such a failure by returning False.
In such a case, this method will be called again to attempt a the reset again.
"""
self.sim.set_state(self.initial_state)
self.sim.forward()
return True
def _get_obs(self):
"""Return the observation."""
raise NotImplementedError()
def _set_action(self, action: np.ndarray):
"""Apply the given action to the simulation."""
raise NotImplementedError()
def _is_success(self, achieved_goal: np.ndarray, desired_goal: np.ndarray):
"""Indicate whether or not the achieved goal successfully achieved the desired goal."""
raise NotImplementedError()
def _sample_goal(self):
"""Sample a new goal and return it."""
raise NotImplementedError()
def _env_setup(self, initial_qpos: dict):
"""Configure the environment.
Can be used to configure initial state and extract information from the simulation.
"""
pass
def _viewer_setup(self):
"""Configure the viewer, e.g. set the camera position."""
pass
def _render_callback(self):
"""Execute callback before rendering.
Can be used to implement custom visualizations.
"""
pass
def _step_callback(self):
"""Execute callback after stepping once in the simulation.
Can be used to enforce additional constraints on the simulation state.
"""
pass