2016-04-27 08:00:58 -07:00
|
|
|
import numpy as np
|
2018-11-29 02:27:27 +01:00
|
|
|
|
2019-01-30 22:39:55 +01:00
|
|
|
from .space import Space
|
2016-04-27 08:00:58 -07:00
|
|
|
|
2019-01-30 22:39:55 +01:00
|
|
|
|
|
|
|
class Box(Space):
|
2019-03-25 00:39:32 +01:00
|
|
|
"""A box in R^n, i.e.each coordinate is bounded.
|
|
|
|
|
|
|
|
There are two common use cases:
|
|
|
|
|
|
|
|
* Identical bound for each dimension::
|
|
|
|
>>> Box(low=-1.0, high=2.0, shape=(3, 4), dtype=np.float32)
|
|
|
|
Box(3, 4)
|
|
|
|
|
|
|
|
* Independent bound for each dimension::
|
|
|
|
>>> Box(low=np.array([-1.0, -2.0]), high=np.array([2.0, 4.0]), dtype=np.float32)
|
|
|
|
Box(2,)
|
2016-06-11 23:10:58 -07:00
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
"""
|
2019-03-25 00:39:32 +01:00
|
|
|
def __init__(self, low, high, shape=None, dtype=np.float32):
|
|
|
|
assert dtype is not None, 'dtype must be explicitly provided. '
|
|
|
|
self.dtype = np.dtype(dtype)
|
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
if shape is None:
|
|
|
|
assert low.shape == high.shape
|
2019-03-25 00:39:32 +01:00
|
|
|
self.shape = low.shape
|
|
|
|
self.low = low
|
|
|
|
self.high = high
|
2016-04-27 08:00:58 -07:00
|
|
|
else:
|
|
|
|
assert np.isscalar(low) and np.isscalar(high)
|
2019-03-25 00:39:32 +01:00
|
|
|
self.shape = tuple(shape)
|
|
|
|
self.low = np.full(self.shape, low)
|
|
|
|
self.high = np.full(self.shape, high)
|
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
|
|
|
low = low + np.zeros(shape)
|
|
|
|
high = high + np.zeros(shape)
|
2019-03-25 00:39:32 +01:00
|
|
|
self.low = self.low.astype(self.dtype)
|
|
|
|
self.high = self.high.astype(self.dtype)
|
|
|
|
super(Box, self).__init__(self.shape, self.dtype)
|
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
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
def sample(self):
|
2019-02-05 17:49:29 -08:00
|
|
|
high = self.high if self.dtype.kind == 'f' else self.high.astype('int64') + 1
|
2019-03-25 00:39:32 +01:00
|
|
|
return self.np_random.uniform(low=self.low, high=high, size=self.shape).astype(self.dtype)
|
2018-09-24 20:11:03 +02:00
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
def contains(self, x):
|
2019-04-08 19:17:33 -07:00
|
|
|
if isinstance(x, list):
|
|
|
|
x = np.array(x) # Promote list to array for contains check
|
2019-03-25 00:39:32 +01:00
|
|
|
return x.shape == self.shape and np.all(x >= self.low) and np.all(x <= self.high)
|
2016-04-27 08:00:58 -07:00
|
|
|
|
|
|
|
def to_jsonable(self, sample_n):
|
|
|
|
return np.array(sample_n).tolist()
|
2018-09-24 20:11:03 +02:00
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
def from_jsonable(self, sample_n):
|
|
|
|
return [np.asarray(sample) for sample in sample_n]
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Box" + str(self.shape)
|
2018-11-29 02:27:27 +01:00
|
|
|
|
2016-04-27 08:00:58 -07:00
|
|
|
def __eq__(self, other):
|
2019-06-07 17:18:04 -04:00
|
|
|
return isinstance(other, Box) and (self.shape == other.shape) and np.allclose(self.low, other.low) and np.allclose(self.high, other.high)
|