2016-04-27 08:00:58 -07:00
|
|
|
import numpy as np
|
2022-03-31 12:50:38 -07:00
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
from gym import utils
|
2022-07-06 11:18:03 -04:00
|
|
|
from gym.envs.mujoco import MuJocoPyEnv
|
2022-06-30 10:59:59 -04:00
|
|
|
from gym.spaces import Box
|
2016-04-27 08:00:58 -07:00
|
|
|
|
|
|
|
|
2022-07-06 11:18:03 -04:00
|
|
|
class Walker2dEnv(MuJocoPyEnv, utils.EzPickle):
|
2022-06-19 21:50:31 +01:00
|
|
|
metadata = {
|
|
|
|
"render_modes": [
|
|
|
|
"human",
|
|
|
|
"rgb_array",
|
|
|
|
"depth_array",
|
|
|
|
"single_rgb_array",
|
|
|
|
"single_depth_array",
|
|
|
|
],
|
|
|
|
"render_fps": 125,
|
|
|
|
}
|
|
|
|
|
2022-06-16 18:29:50 +02:00
|
|
|
def __init__(self, **kwargs):
|
2022-06-30 10:59:59 -04:00
|
|
|
observation_space = Box(low=-np.inf, high=np.inf, shape=(17,), dtype=np.float64)
|
2022-07-06 11:18:03 -04:00
|
|
|
MuJocoPyEnv.__init__(
|
|
|
|
self, "walker2d.xml", 4, observation_space=observation_space, **kwargs
|
2022-05-24 08:47:51 -04:00
|
|
|
)
|
2022-08-16 18:05:36 +02:00
|
|
|
utils.EzPickle.__init__(self, **kwargs)
|
2016-04-27 08:00:58 -07:00
|
|
|
|
Cleanup, removal of unmaintained code (#836)
* add dtype to Box
* remove board_game, debugging, safety, parameter_tuning environments
* massive set of breaking changes
- remove python logging module
- _step, _reset, _seed, _close => non underscored method
- remove benchmark and scoring folder
* Improve render("human"), now resizable, closable window.
* get rid of default step and reset in wrappers, so it doesn’t silently fail for people with underscore methods
* CubeCrash unit test environment
* followup fixes
* MemorizeDigits unit test envrionment
* refactored spaces a bit
fixed indentation
disabled test_env_semantics
* fix unit tests
* fixes
* CubeCrash, MemorizeDigits tested
* gym backwards compatibility patch
* gym backwards compatibility, followup fixes
* changelist, add spaces to main namespaces
* undo_logger_setup for backwards compat
* remove configuration.py
2018-01-25 18:20:14 -08:00
|
|
|
def step(self, a):
|
2018-01-24 15:42:29 -08:00
|
|
|
posbefore = self.sim.data.qpos[0]
|
2016-04-27 08:00:58 -07:00
|
|
|
self.do_simulation(a, self.frame_skip)
|
2018-01-24 15:42:29 -08:00
|
|
|
posafter, height, ang = self.sim.data.qpos[0:3]
|
2022-06-08 00:20:56 +02:00
|
|
|
|
|
|
|
self.renderer.render_step()
|
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
alive_bonus = 1.0
|
2021-07-29 02:26:34 +02:00
|
|
|
reward = (posafter - posbefore) / self.dt
|
2016-04-27 08:00:58 -07:00
|
|
|
reward += alive_bonus
|
|
|
|
reward -= 1e-3 * np.square(a).sum()
|
2022-07-10 02:18:06 +05:30
|
|
|
terminated = not (height > 0.8 and height < 2.0 and ang > -1.0 and ang < 1.0)
|
2016-04-27 08:00:58 -07:00
|
|
|
ob = self._get_obs()
|
2022-07-10 02:18:06 +05:30
|
|
|
return ob, reward, terminated, False, {}
|
2016-04-27 08:00:58 -07:00
|
|
|
|
|
|
|
def _get_obs(self):
|
2018-01-24 15:42:29 -08:00
|
|
|
qpos = self.sim.data.qpos
|
|
|
|
qvel = self.sim.data.qvel
|
2017-02-22 17:24:27 -08:00
|
|
|
return np.concatenate([qpos[1:], np.clip(qvel, -10, 10)]).ravel()
|
2016-04-27 08:00:58 -07:00
|
|
|
|
2016-04-30 22:47:51 -07:00
|
|
|
def reset_model(self):
|
|
|
|
self.set_state(
|
2021-07-29 15:39:42 -04:00
|
|
|
self.init_qpos
|
|
|
|
+ self.np_random.uniform(low=-0.005, high=0.005, size=self.model.nq),
|
|
|
|
self.init_qvel
|
|
|
|
+ self.np_random.uniform(low=-0.005, high=0.005, size=self.model.nv),
|
2016-04-30 22:47:51 -07:00
|
|
|
)
|
2016-04-27 08:00:58 -07:00
|
|
|
return self._get_obs()
|
|
|
|
|
|
|
|
def viewer_setup(self):
|
2022-07-04 18:19:25 +01:00
|
|
|
assert self.viewer is not None
|
2016-04-27 08:00:58 -07:00
|
|
|
self.viewer.cam.trackbodyid = 2
|
|
|
|
self.viewer.cam.distance = self.model.stat.extent * 0.5
|
2018-09-24 14:53:23 -07:00
|
|
|
self.viewer.cam.lookat[2] = 1.15
|
2016-04-27 08:00:58 -07:00
|
|
|
self.viewer.cam.elevation = -20
|