2019-12-07 21:08:40 +01:00
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
import gym
|
|
|
|
import numpy as np
|
|
|
|
from gym.spaces import Discrete, Dict, Box
|
|
|
|
|
|
|
|
|
2022-05-18 09:58:25 +02:00
|
|
|
class CartPoleSparseRewards(gym.Env):
|
|
|
|
"""Wrapper for gym CartPole environment where reward is accumulated to the end."""
|
2019-12-07 21:08:40 +01:00
|
|
|
|
|
|
|
def __init__(self, config=None):
|
|
|
|
self.env = gym.make("CartPole-v0")
|
|
|
|
self.action_space = Discrete(2)
|
|
|
|
self.observation_space = Dict(
|
|
|
|
{
|
|
|
|
"obs": self.env.observation_space,
|
|
|
|
"action_mask": Box(low=0, high=1, shape=(self.action_space.n,)),
|
2022-01-29 18:41:57 -08:00
|
|
|
}
|
2019-12-07 21:08:40 +01:00
|
|
|
)
|
|
|
|
self.running_reward = 0
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.running_reward = 0
|
[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
|
|
|
return {
|
|
|
|
"obs": self.env.reset(),
|
|
|
|
"action_mask": np.array([1, 1], dtype=np.float32),
|
|
|
|
}
|
2019-12-07 21:08:40 +01:00
|
|
|
|
|
|
|
def step(self, action):
|
|
|
|
obs, rew, done, info = self.env.step(action)
|
|
|
|
self.running_reward += rew
|
|
|
|
score = self.running_reward if done else 0
|
[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
|
|
|
return (
|
|
|
|
{"obs": obs, "action_mask": np.array([1, 1], dtype=np.float32)},
|
|
|
|
score,
|
|
|
|
done,
|
|
|
|
info,
|
2022-01-29 18:41:57 -08:00
|
|
|
)
|
2019-12-07 21:08:40 +01:00
|
|
|
|
|
|
|
def set_state(self, state):
|
|
|
|
self.running_reward = state[1]
|
|
|
|
self.env = deepcopy(state[0])
|
|
|
|
obs = np.array(list(self.env.unwrapped.state))
|
[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
|
|
|
return {"obs": obs, "action_mask": np.array([1, 1], dtype=np.float32)}
|
2019-12-07 21:08:40 +01:00
|
|
|
|
|
|
|
def get_state(self):
|
|
|
|
return deepcopy(self.env), self.running_reward
|