Ejemplo n.º 1
0
    def __init__(self, init_space, nb_bins):
        if not isinstance(init_space, Box):
            raise RuntimeError(
                "Impossible to convert a gym space of type {} to a discrete space"
                " (it should be of "
                "type space.Box)"
                "".format(type(init_space)))
        if nb_bins < 2:
            raise RuntimeError(
                "This do not work with less that 1 bin (if you want to ignored some part "
                "of the action_space or observation_space please use the "
                "\"gym_space.ignore_attr\" or \"gym_space.keep_only_attr\"")

        min_ = init_space.low
        max_ = init_space.high
        self._ignored = min_ == max_  # which component are ignored
        self._res = min_
        self._values = np.linspace(min_, max_, num=nb_bins + 2)
        self._values = self._values[
            1:-1, :]  # the values that will be used when using #gym_to_glop

        # TODO there might a cleaner approach here
        self._bins_size = np.linspace(min_, max_, num=2 * nb_bins + 1)
        self._bins_size = self._bins_size[
            2:-1:2, :]  # the values defining the "cuts"

        self._gen_idx = np.arange(self._bins_size.shape[-1])
        n_bins = np.ones(min_.shape[0]) * nb_bins
        n_bins[
            self.
            _ignored] = 1  # if min and max are equal, i don't want to have multiple variable
        BaseGymAttrConverter.__init__(
            self,
            space=MultiDiscrete(n_bins),
        )
Ejemplo n.º 2
0
    def __init__(self, init_space=None):
        self.size = None
        BaseGymAttrConverter.__init__(self, space=None)
        if init_space is not None:
            self.initialize_space(init_space)

        self.previous_fun = self._previous_fun
        self.after_fun = self._after_fun
Ejemplo n.º 3
0
 def __init__(self, substract, divide, dtype=None, init_space=None):
     BaseGymAttrConverter.__init__(self,
                                   g2op_to_gym=None,
                                   gym_to_g2op=None,
                                   space=None)
     self._substract = np.array(substract)
     self._divide = np.array(divide)
     self.dtype = dtype if dtype is not None else dt_float
     if init_space is not None:
         self.initialize_space(init_space)
Ejemplo n.º 4
0
 def __init__(self, init_space):
     if not isinstance(init_space, (MultiBinary, MultiDiscrete)):
         raise RuntimeError("Impossible to convert a gym space of type {} to a Tuple (it should be of "
                            "type space.MultiBinary or space.MultiDiscrete)"
                            "".format(type(init_space)))
     self.size = init_space.n
     li = []
     for i in range(self.size):
         tmp_sz = 2
         if isinstance(init_space, MultiDiscrete):
             tmp_sz = init_space.nvec[i]
         li.append(Discrete(tmp_sz))
     BaseGymAttrConverter.__init__(self, space=Tuple(li))
Ejemplo n.º 5
0
    def __init__(self, nb_bins, init_space=None):
        BaseGymAttrConverter.__init__(self,
                                      g2op_to_gym=None,
                                      gym_to_g2op=None,
                                      space=None)
        if nb_bins < 2:
            raise RuntimeError("This do not work with less that 1 bin (if you want to ignored some part "
                               "of the action_space or observation_space please use the "
                               "\"gym_space.ignore_attr\" or \"gym_space.keep_only_attr\"")

        self._nb_bins = nb_bins

        self._ignored = None
        self._res = None
        self._values = None
        self._bins_size = None
        self._gen_idx = None

        if init_space is not None:
            self.initialize_space(init_space)
Ejemplo n.º 6
0
 def __init__(self, substract, divide, init_space, dtype=None):
     self._substract = np.array(substract)
     self._divide = np.array(divide)
     self.dtype = dtype if dtype is not None else dt_float
     if not isinstance(init_space, Box):
         raise RuntimeError(
             "Impossible to scale a converter if this one is not from type space.Box"
         )
     tmp_space = copy.deepcopy(init_space)
     tmp_space.low = self.scale(tmp_space.low)
     tmp_space.high = self.scale(tmp_space.high)
     if dtype is not None:
         tmp_space.dtype = dtype
         tmp_space.low = tmp_space.low.astype(self.dtype)
         tmp_space.high = tmp_space.high.astype(self.dtype)
     BaseGymAttrConverter.__init__(self,
                                   g2op_to_gym=self.scale,
                                   gym_to_g2op=self.unscale,
                                   space=tmp_space)
     self.dtype = self.my_space.dtype
     self._substract = self._substract.astype(self.dtype)
     self._divide = self._divide.astype(self.dtype)