OpenAI Gym custom environment: Discrete observation space with real values

Viewed 2411

I would like to create custom openai gym environment that has discrete state space, but with float values. To be more precise, it should be a range of values with 0.25 step: 10.0, 10.25, 10.5, 10.75, 11.0, ..., 19.75, 20.0

Is there a way to do this in openai gym custom environment, using spaces like Discrete, Box, MultiDiscrete or some others? Discrete requires an integer, and Box doesn't seem to have some kind of a step parameter.

1 Answers

You could implement your own space using np.linspace (considering e.g. spaces.Box as a guideline):

from gym.spaces.space import Space
import numpy as np

class Incremental(Space):
    def __init__(self, start, stop, num, **kwargs):
        self.values = np.linspace(start, stop, num, **kwargs)
        super().__init__(self.values.shape, self.values.dtype)

    def sample(self):
        return np.random.choice(self.values)

    def contains(self, x):
        return x in self.values

space = Incremental(10, 20, 41)
Related