Beispiel #1
0
def create_model(name="discrete"):
    if name == "discrete":
        return lambda: DiscreteUniform(n_actions=10)
    elif name == "continuous":
        bs = Bounds(low=-1, high=1, shape=(3, ))
        return lambda: ContinuousUniform(bounds=bs)
    elif name == "random_normal":
        bs = Bounds(low=-1, high=1, shape=(3, ))
        return lambda: NormalContinuous(loc=0, scale=1, bounds=bs)
    raise ValueError("Invalid param `name`.")
Beispiel #2
0
    def test_sample(self):
        bounds = Bounds(low=-1, high=3, shape=(3, ))
        model = ContinuousUniform(bounds=bounds)
        actions = model.predict(batch_size=100).actions
        assert actions.min() >= -1
        assert actions.max() <= 3

        bounds = Bounds(low=-1, high=3, shape=(3, 10))
        model = ContinuousUniform(bounds=bounds)
        actions = model.predict(batch_size=100).actions
        assert actions.min() >= -1
        assert actions.max() <= 3
Beispiel #3
0
    def test_sample(self):
        bounds = Bounds(low=-5, high=5, shape=(3, ))
        model = NormalContinuous(bounds=bounds)
        actions = model.predict(batch_size=10000).actions
        assert actions.min() >= -5
        assert actions.max() <= 5
        assert numpy.allclose(actions.mean(), 0, atol=0.05)
        assert numpy.allclose(actions.std(), 1, atol=0.05)

        bounds = Bounds(low=-10, high=30, shape=(3, 10))
        model = NormalContinuous(bounds=bounds, loc=5, scale=2)
        actions = model.predict(batch_size=10000).actions
        assert actions.min() >= -10
        assert actions.max() <= 30
        assert numpy.allclose(actions.mean(), 5, atol=0.05), actions.mean()
        assert numpy.allclose(actions.std(), 2, atol=0.05), actions.std()
Beispiel #4
0
    def from_bounds_params(
        cls,
        function: Callable,
        shape: tuple = None,
        high: Union[int, float, numpy.ndarray] = numpy.inf,
        low: Union[int, float, numpy.ndarray] = numpy.NINF,
        custom_domain_check: Callable[[numpy.ndarray], numpy.ndarray] = None,
    ) -> "Function":
        """
        Initialize a function defining its shape and bounds without using a :class:`Bounds`.

        Args:
            function: Callable that takes a batch of vectors (batched across \
                      the first dimension of the array) and returns a vector of \
                      scalar. This function is applied to a batch of walker \
                      observations.
            shape: Input shape of the solution vector without taking into account \
                    the batch dimension. For example, a two dimensional function \
                    applied to a batch of 5 walkers will have shape=(2,), even though
                    the observations will have shape (5, 2)
            high: Upper bound of the function domain. If it's an scalar it will \
                  be the same for all dimensions. If its a numpy array it will \
                  be the upper bound for each dimension.
            low: Lower bound of the function domain. If it's an scalar it will \
                  be the same for all dimensions. If its a numpy array it will \
                  be the lower bound for each dimension.
            custom_domain_check: Callable that checks points inside the bounds \
                    to know if they are in a custom domain when it is not just \
                    a set of rectangular bounds.

        Returns:
            :class:`Function` with its :class:`Bounds` created from the provided arguments.

        """
        if (not isinstance(high, numpy.ndarray)
                and not isinstance(low, numpy.ndarray) is None
                and shape is None):
            raise TypeError(
                "Need to specify shape or high or low must be a numpy array.")
        bounds = Bounds(high=high, low=low, shape=shape)
        return Function(function=function,
                        bounds=bounds,
                        custom_domain_check=custom_domain_check)
Beispiel #5
0
 def test_get_params_dict(self):
     bounds = Bounds(low=-1, high=3, shape=(3, ))
     model = ContinuousModel(bounds=bounds)
     params = model.get_params_dict()
     assert params["actions"]["size"] == model.shape
Beispiel #6
0
 def test_attributes(self):
     bounds = Bounds(low=-1, high=3, shape=(3, ))
     model = ContinuousModel(bounds=bounds)
     assert model.shape == (3, )
     assert model.n_dims == 3