2019-02-17 04:44:59 +08:00
|
|
|
import numpy as np
|
|
|
|
import gym
|
|
|
|
|
2020-06-06 03:22:19 -07:00
|
|
|
from ray.rllib.utils.annotations import PublicAPI
|
2019-02-17 04:44:59 +08:00
|
|
|
|
2020-06-06 03:22:19 -07:00
|
|
|
|
|
|
|
@PublicAPI
|
2019-02-17 04:44:59 +08:00
|
|
|
class Simplex(gym.Space):
|
|
|
|
"""Represents a d - 1 dimensional Simplex in R^d.
|
|
|
|
|
|
|
|
That is, all coordinates are in [0, 1] and sum to 1.
|
|
|
|
The dimension d of the simplex is assumed to be shape[-1].
|
|
|
|
|
|
|
|
Additionally one can specify the underlying distribution of
|
|
|
|
the simplex as a Dirichlet distribution by providing concentration
|
|
|
|
parameters. By default, sampling is uniform, i.e. concentration is
|
|
|
|
all 1s.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
self.action_space = spaces.Simplex(shape=(3, 4))
|
|
|
|
--> 3 independent 4d Dirichlet with uniform concentration
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, shape, concentration=None, dtype=np.float32):
|
|
|
|
assert type(shape) in [tuple, list]
|
[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
|
|
|
super().__init__(shape, dtype)
|
|
|
|
self.dim = self.shape[-1]
|
2019-02-17 04:44:59 +08:00
|
|
|
|
|
|
|
if concentration is not None:
|
|
|
|
assert concentration.shape == shape[:-1]
|
|
|
|
else:
|
|
|
|
self.concentration = [1] * self.dim
|
|
|
|
|
|
|
|
def sample(self):
|
2022-01-29 18:41:57 -08:00
|
|
|
return np.random.dirichlet(self.concentration, size=self.shape[:-1]).astype(
|
|
|
|
self.dtype
|
|
|
|
)
|
2019-02-17 04:44:59 +08:00
|
|
|
|
|
|
|
def contains(self, x):
|
|
|
|
return x.shape == self.shape and np.allclose(
|
2022-01-29 18:41:57 -08:00
|
|
|
np.sum(x, axis=-1), np.ones_like(x[..., 0])
|
|
|
|
)
|
2019-02-17 04:44:59 +08:00
|
|
|
|
|
|
|
def to_jsonable(self, sample_n):
|
|
|
|
return np.array(sample_n).tolist()
|
|
|
|
|
|
|
|
def from_jsonable(self, sample_n):
|
|
|
|
return [np.asarray(sample) for sample in sample_n]
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Simplex({}; {})".format(self.shape, self.concentration)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2022-01-29 18:41:57 -08:00
|
|
|
return (
|
|
|
|
np.allclose(self.concentration, other.concentration)
|
|
|
|
and self.shape == other.shape
|
|
|
|
)
|