2017-04-22 20:57:36 -07:00
|
|
|
import numpy as np
|
|
|
|
from gym import utils
|
|
|
|
from gym.envs.mujoco import mujoco_env
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2017-04-22 20:57:36 -07:00
|
|
|
class ThrowerEnv(mujoco_env.MujocoEnv, utils.EzPickle):
|
|
|
|
def __init__(self):
|
|
|
|
utils.EzPickle.__init__(self)
|
|
|
|
self._ball_hit_ground = False
|
|
|
|
self._ball_hit_location = None
|
2021-07-29 02:26:34 +02:00
|
|
|
mujoco_env.MujocoEnv.__init__(self, "thrower.xml", 5)
|
2017-04-22 20:57:36 -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):
|
2017-04-22 20:57:36 -07:00
|
|
|
ball_xy = self.get_body_com("ball")[:2]
|
2017-04-23 23:14:28 -07:00
|
|
|
goal_xy = self.get_body_com("goal")[:2]
|
2017-04-22 20:57:36 -07:00
|
|
|
|
|
|
|
if not self._ball_hit_ground and self.get_body_com("ball")[2] < -0.25:
|
|
|
|
self._ball_hit_ground = True
|
|
|
|
self._ball_hit_location = self.get_body_com("ball")
|
|
|
|
|
|
|
|
if self._ball_hit_ground:
|
|
|
|
ball_hit_xy = self._ball_hit_location[:2]
|
|
|
|
reward_dist = -np.linalg.norm(ball_hit_xy - goal_xy)
|
|
|
|
else:
|
|
|
|
reward_dist = -np.linalg.norm(ball_xy - goal_xy)
|
2021-07-29 02:26:34 +02:00
|
|
|
reward_ctrl = -np.square(a).sum()
|
2017-04-22 20:57:36 -07:00
|
|
|
|
|
|
|
reward = reward_dist + 0.002 * reward_ctrl
|
|
|
|
self.do_simulation(a, self.frame_skip)
|
|
|
|
ob = self._get_obs()
|
|
|
|
done = False
|
2021-07-29 02:26:34 +02:00
|
|
|
return ob, reward, done, dict(reward_dist=reward_dist, reward_ctrl=reward_ctrl)
|
2017-04-22 20:57:36 -07:00
|
|
|
|
|
|
|
def viewer_setup(self):
|
|
|
|
self.viewer.cam.trackbodyid = 0
|
|
|
|
self.viewer.cam.distance = 4.0
|
|
|
|
|
|
|
|
def reset_model(self):
|
|
|
|
self._ball_hit_ground = False
|
|
|
|
self._ball_hit_location = None
|
|
|
|
|
|
|
|
qpos = self.init_qpos
|
2021-07-29 02:26:34 +02:00
|
|
|
self.goal = np.array(
|
|
|
|
[
|
|
|
|
self.np_random.uniform(low=-0.3, high=0.3),
|
|
|
|
self.np_random.uniform(low=-0.3, high=0.3),
|
|
|
|
]
|
|
|
|
)
|
2017-04-22 20:57:36 -07:00
|
|
|
|
|
|
|
qpos[-9:-7] = self.goal
|
2021-07-29 02:26:34 +02:00
|
|
|
qvel = self.init_qvel + self.np_random.uniform(
|
|
|
|
low=-0.005, high=0.005, size=self.model.nv
|
|
|
|
)
|
2017-04-22 20:57:36 -07:00
|
|
|
qvel[7:] = 0
|
|
|
|
self.set_state(qpos, qvel)
|
|
|
|
return self._get_obs()
|
|
|
|
|
|
|
|
def _get_obs(self):
|
2021-07-29 02:26:34 +02:00
|
|
|
return np.concatenate(
|
|
|
|
[
|
|
|
|
self.sim.data.qpos.flat[:7],
|
|
|
|
self.sim.data.qvel.flat[:7],
|
|
|
|
self.get_body_com("r_wrist_roll_link"),
|
|
|
|
self.get_body_com("ball"),
|
|
|
|
self.get_body_com("goal"),
|
|
|
|
]
|
|
|
|
)
|