def test_creation(self): with warnings.catch_warnings(): warnings.filterwarnings("ignore") with make("l2rpn_wcci_2020", test=True) as env: # test i can create converter = self.init_converter(env) act_space = GymActionSpace(env=env, converter=converter) act_space.sample()
def test_json(self): with warnings.catch_warnings(): warnings.filterwarnings("ignore") with make("l2rpn_wcci_2020", test=True) as env: # test i can create obs_space = GymObservationSpace(env) act_space = GymActionSpace(env) obs_space.seed(0) act_space.seed(0) self._aux_test_json(obs_space) self._aux_test_json(act_space)
def test_creation(self): with warnings.catch_warnings(): warnings.filterwarnings("ignore") with make(self.get_env_name(), test=True) as env: # test i can create obs_space = GymObservationSpace(env) act_space = GymActionSpace(env)
def _init_gym_converter(self): if self.__gym_action_space is None: # lazy import from gym import spaces from grid2op.gym_compat import GymActionSpace # i do that not to duplicate the code of the low / high bounds gym_action_space = GymActionSpace(self.init_action_space) low = tuple() high = tuple() order_gym = [] dtypes = [] shapes = [] sizes = [] prev = 0 for k, v in gym_action_space.spaces.items(): order_gym.append(k) dtypes.append(v.dtype) if isinstance(v, spaces.MultiBinary): low += tuple([0 for _ in range(v.n)]) high += tuple([1 for _ in range(v.n)]) my_size = v.n elif isinstance(v, spaces.Box): low += tuple(v.low) high += tuple(v.high) my_size = v.low.shape[0] else: raise RuntimeError( "Impossible to convert this converter to gym. Type {} of data " "encountered while only MultiBinary and Box are supported for now." ) shapes.append(my_size) sizes.append(np.arange(my_size) + prev) prev += my_size self.__gym_action_space = gym_action_space my_type = spaces.Box(low=np.array(low), high=np.array(high), dtype=dt_float) order_me = [] _order_gym_2_me = np.zeros(my_type.shape[0], dtype=dt_int) - 1 _order_me_2_gym = np.zeros(my_type.shape[0], dtype=dt_int) - 1 for el in self.init_action_space.attr_list_vect: order_me.append(GymActionSpace.keys_grid2op_2_human[el]) prev = 0 order_gym = list(gym_action_space.spaces.keys()) for id_me, nm_attr in enumerate(order_me): id_gym = order_gym.index(nm_attr) index_me = np.arange(shapes[id_gym]) + prev _order_gym_2_me[sizes[id_gym]] = index_me _order_me_2_gym[index_me] = sizes[id_gym] # self.__order_gym_2_me[this_gym_ind] = sizes[id_me] prev += shapes[id_gym] self.__order_gym_2_me = _order_gym_2_me self.__order_me_2_gym = _order_me_2_gym self.__dict_space = {"action": my_type} self.__order_gym = order_gym self.__dtypes_gym = dtypes self.__shapes_gym = shapes
def test_creation(self): with warnings.catch_warnings(): warnings.filterwarnings("ignore") if self.get_env_path() is not None: env_path_or_name = os.path.join(self.get_env_path(), self.get_env_name()) else: env_path_or_name = self.get_env_name() with make(env_path_or_name, test=True) as env: # test i can create obs_space = GymObservationSpace(env) act_space = GymActionSpace(env)
def test_to_from_gym_act(self): with warnings.catch_warnings(): warnings.filterwarnings("ignore") with make("l2rpn_wcci_2020", test=True) as env: converter = self.init_converter(env) act_space = GymActionSpace(env=env, converter=converter) act_space.seed(0) converter.seed(0) gym_act = act_space.sample() act = act_space.from_gym(gym_act) self._aux_test_json(act_space, gym_act) gym_act2 = act_space.to_gym(act) act2 = act_space.from_gym(gym_act2) g2op_act = converter.convert_act(act) g2op_act2 = converter.convert_act(act2) assert g2op_act == g2op_act2 act_space.seed(0) for i in range(10): gym_act = act_space.sample() act = act_space.from_gym(gym_act) self._aux_test_json(act_space, gym_act) gym_act2 = act_space.to_gym(act) act2 = act_space.from_gym(gym_act2) g2op_act = converter.convert_act(act) g2op_act2 = converter.convert_act(act2) assert g2op_act == g2op_act2
def test_to_from_gym_act(self): with warnings.catch_warnings(): warnings.filterwarnings("ignore") with make("l2rpn_wcci_2020", test=True) as env: act_space = GymActionSpace(env) act = env.action_space() gym_act = act_space.to_gym(act) self._aux_test_json(act_space, gym_act) assert act_space.contains(gym_act) act2 = act_space.from_gym(gym_act) assert act == act2 act_space.seed(0) for i in range(10): gym_act = act_space.sample() act = act_space.from_gym(gym_act) self._aux_test_json(act_space, gym_act) gym_act2 = act_space.to_gym(act) act2 = act_space.from_gym(gym_act2) assert act == act2