2020-11-12 10:30:41 -08:00
|
|
|
from gym.envs.classic_control import PendulumEnv, CartPoleEnv
|
2020-08-02 09:12:09 -07:00
|
|
|
import numpy as np
|
|
|
|
|
2020-10-06 20:28:16 +02:00
|
|
|
# MuJoCo may not be installed.
|
|
|
|
HalfCheetahEnv = HopperEnv = None
|
2021-02-11 18:58:46 +01:00
|
|
|
|
2020-10-06 20:28:16 +02:00
|
|
|
try:
|
|
|
|
from gym.envs.mujoco import HalfCheetahEnv, HopperEnv
|
2021-02-11 18:58:46 +01:00
|
|
|
except Exception:
|
2020-10-06 20:28:16 +02:00
|
|
|
pass
|
2020-08-02 09:12:09 -07:00
|
|
|
|
|
|
|
|
2020-11-12 10:30:41 -08:00
|
|
|
class CartPoleWrapper(CartPoleEnv):
|
|
|
|
"""Wrapper for the Cartpole-v0 environment.
|
|
|
|
|
|
|
|
Adds an additional `reward` method for some model-based RL algos (e.g.
|
|
|
|
MB-MPO).
|
|
|
|
"""
|
|
|
|
|
|
|
|
def reward(self, obs, action, obs_next):
|
|
|
|
# obs = batch * [pos, vel, angle, rotation_rate]
|
|
|
|
x = obs_next[:, 0]
|
|
|
|
theta = obs_next[:, 2]
|
|
|
|
|
2021-02-11 18:58:46 +01:00
|
|
|
# 1.0 if we are still on, 0.0 if we are terminated due to bounds
|
|
|
|
# (angular or x-axis) being breached.
|
2022-01-29 18:41:57 -08:00
|
|
|
rew = 1.0 - (
|
|
|
|
(x < -self.x_threshold)
|
|
|
|
| (x > self.x_threshold)
|
|
|
|
| (theta < -self.theta_threshold_radians)
|
|
|
|
| (theta > self.theta_threshold_radians)
|
|
|
|
).astype(np.float32)
|
2020-11-12 10:30:41 -08:00
|
|
|
|
|
|
|
return rew
|
|
|
|
|
|
|
|
|
2020-10-06 20:28:16 +02:00
|
|
|
class PendulumWrapper(PendulumEnv):
|
[RLlib] Upgrade gym version to 0.21 and deprecate pendulum-v0. (#19535)
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
* Reformatting
* Fixing tests
* Move atari-py install conditional to req.txt
* migrate to new ale install method
* Fix QMix, SAC, and MADDPA too.
* Unpin gym and deprecate pendulum v0
Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1
Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.
Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20
Move atari-py install conditional to req.txt
migrate to new ale install method
Make parametric_actions_cartpole return float32 actions/obs
Adding type conversions if obs/actions don't match space
Add utils to make elements match gym space dtypes
Co-authored-by: Jun Gong <jungong@anyscale.com>
Co-authored-by: sven1977 <svenmika1977@gmail.com>
2021-11-03 08:24:00 -07:00
|
|
|
"""Wrapper for the Pendulum-v1 environment.
|
2020-08-02 09:12:09 -07:00
|
|
|
|
2020-10-06 20:28:16 +02:00
|
|
|
Adds an additional `reward` method for some model-based RL algos (e.g.
|
|
|
|
MB-MPO).
|
|
|
|
"""
|
2020-08-02 09:12:09 -07:00
|
|
|
|
|
|
|
def reward(self, obs, action, obs_next):
|
2020-10-06 20:28:16 +02:00
|
|
|
# obs = [cos(theta), sin(theta), dtheta/dt]
|
|
|
|
# To get the angle back from obs: atan2(sin(theta), cos(theta)).
|
2022-01-29 18:41:57 -08:00
|
|
|
theta = np.arctan2(np.clip(obs[:, 1], -1.0, 1.0), np.clip(obs[:, 0], -1.0, 1.0))
|
2020-10-06 20:28:16 +02:00
|
|
|
# Do everything in (B,) space (single theta-, action- and
|
|
|
|
# reward values).
|
|
|
|
a = np.clip(action, -self.max_torque, self.max_torque)[0]
|
2022-01-29 18:41:57 -08:00
|
|
|
costs = (
|
|
|
|
self.angle_normalize(theta) ** 2 + 0.1 * obs[:, 2] ** 2 + 0.001 * (a ** 2)
|
|
|
|
)
|
2020-10-06 20:28:16 +02:00
|
|
|
return -costs
|
2020-08-02 09:12:09 -07:00
|
|
|
|
2020-10-06 20:28:16 +02:00
|
|
|
@staticmethod
|
|
|
|
def angle_normalize(x):
|
2022-01-29 18:41:57 -08:00
|
|
|
return ((x + np.pi) % (2 * np.pi)) - np.pi
|
2020-08-02 09:12:09 -07:00
|
|
|
|
2020-09-09 00:34:34 -07:00
|
|
|
|
2021-02-11 18:58:46 +01:00
|
|
|
class HalfCheetahWrapper(HalfCheetahEnv or object):
|
|
|
|
"""Wrapper for the MuJoCo HalfCheetah-v2 environment.
|
|
|
|
|
|
|
|
Adds an additional `reward` method for some model-based RL algos (e.g.
|
|
|
|
MB-MPO).
|
|
|
|
"""
|
|
|
|
|
|
|
|
def reward(self, obs, action, obs_next):
|
|
|
|
if obs.ndim == 2 and action.ndim == 2:
|
|
|
|
assert obs.shape == obs_next.shape
|
|
|
|
forward_vel = obs_next[:, 8]
|
|
|
|
ctrl_cost = 0.1 * np.sum(np.square(action), axis=1)
|
|
|
|
reward = forward_vel - ctrl_cost
|
|
|
|
return np.minimum(np.maximum(-1000.0, reward), 1000.0)
|
|
|
|
else:
|
|
|
|
forward_vel = obs_next[8]
|
|
|
|
ctrl_cost = 0.1 * np.square(action).sum()
|
|
|
|
reward = forward_vel - ctrl_cost
|
2020-10-06 20:28:16 +02:00
|
|
|
return np.minimum(np.maximum(-1000.0, reward), 1000.0)
|
2020-09-09 00:34:34 -07:00
|
|
|
|
|
|
|
|
2021-02-11 18:58:46 +01:00
|
|
|
class HopperWrapper(HopperEnv or object):
|
|
|
|
"""Wrapper for the MuJoCo Hopper-v2 environment.
|
|
|
|
|
|
|
|
Adds an additional `reward` method for some model-based RL algos (e.g.
|
|
|
|
MB-MPO).
|
|
|
|
"""
|
|
|
|
|
|
|
|
def reward(self, obs, action, obs_next):
|
|
|
|
alive_bonus = 1.0
|
|
|
|
assert obs.ndim == 2 and action.ndim == 2
|
2022-01-29 18:41:57 -08:00
|
|
|
assert obs.shape == obs_next.shape and action.shape[0] == obs.shape[0]
|
2021-02-11 18:58:46 +01:00
|
|
|
vel = obs_next[:, 5]
|
|
|
|
ctrl_cost = 1e-3 * np.sum(np.square(action), axis=1)
|
|
|
|
reward = vel + alive_bonus - ctrl_cost
|
|
|
|
return np.minimum(np.maximum(-1000.0, reward), 1000.0)
|
|
|
|
|
|
|
|
|
2020-08-02 09:12:09 -07:00
|
|
|
if __name__ == "__main__":
|
2020-10-06 20:28:16 +02:00
|
|
|
env = PendulumWrapper()
|
2020-08-02 09:12:09 -07:00
|
|
|
env.reset()
|
2020-10-06 20:28:16 +02:00
|
|
|
for _ in range(100):
|
2020-08-02 09:12:09 -07:00
|
|
|
env.step(env.action_space.sample())
|
2020-10-06 20:28:16 +02:00
|
|
|
env.render()
|