def test_json_loadable(self): dict_ = self.helper_action.to_dict() tmp = json.dumps(obj=dict_, indent=4, sort_keys=True) res = HelperAction.from_dict(json.loads(tmp)) assert np.all(res.name_gen == self.helper_action.name_gen) assert np.all(res.name_load == self.helper_action.name_load) assert np.all(res.name_line == self.helper_action.name_line) assert np.all(res.sub_info == self.helper_action.sub_info) assert np.all(res.load_to_subid == self.helper_action.load_to_subid) assert np.all(res.gen_to_subid == self.helper_action.gen_to_subid) assert np.all( res.line_or_to_subid == self.helper_action.line_or_to_subid) assert np.all( res.line_ex_to_subid == self.helper_action.line_ex_to_subid) assert np.all( res.load_to_sub_pos == self.helper_action.load_to_sub_pos) assert np.all(res.gen_to_sub_pos == self.helper_action.gen_to_sub_pos) assert np.all( res.line_or_to_sub_pos == self.helper_action.line_or_to_sub_pos) assert np.all( res.line_ex_to_sub_pos == self.helper_action.line_ex_to_sub_pos) assert np.all( res.load_pos_topo_vect == self.helper_action.load_pos_topo_vect) assert np.all( res.gen_pos_topo_vect == self.helper_action.gen_pos_topo_vect) assert np.all(res.line_or_pos_topo_vect == self.helper_action.line_or_pos_topo_vect) assert np.all(res.line_ex_pos_topo_vect == self.helper_action.line_ex_pos_topo_vect) assert np.all(res.actionClass == self.helper_action.actionClass)
def setUp(self): self.backend = PandaPowerBackend( detailed_infos_for_cascading_failures=True) self.path_matpower = PATH_DATA_TEST self.case_file = "test_case14.json" self.backend.load_grid(self.path_matpower, self.case_file) self.tolvect = 1e-2 self.tol_one = 1e-5 self.game_rules = GameRules() self.action_env = HelperAction( gridobj=self.backend, legal_action=self.game_rules.legal_action) self.lines_flows_init = np.array([ 638.28966637, 305.05042301, 17658.9674809, 26534.04334098, 10869.23856329, 4686.71726729, 15612.65903298, 300.07915572, 229.8060832, 169.97292682, 100.40192958, 265.47505664, 21193.86923911, 21216.44452327, 49701.1565287, 124.79684388, 67.59759985, 192.19424706, 666.76961936, 1113.52773632 ]) # _parameters for the environment self.env_params = Parameters() # used for init an env too self.chronics_handler = ChronicsHandler() self.id_first_line_disco = 8 # due to hard overflow self.id_2nd_line_disco = 11 # due to soft overflow
def test_from_dict(self): res = HelperAction.from_dict(self.res) assert np.all(res.name_gen == self.helper_action.name_gen) assert np.all(res.name_load == self.helper_action.name_load) assert np.all(res.name_line == self.helper_action.name_line) assert np.all(res.sub_info == self.helper_action.sub_info) assert np.all(res.load_to_subid == self.helper_action.load_to_subid) assert np.all(res.gen_to_subid == self.helper_action.gen_to_subid) assert np.all( res.line_or_to_subid == self.helper_action.line_or_to_subid) assert np.all( res.line_ex_to_subid == self.helper_action.line_ex_to_subid) assert np.all( res.load_to_sub_pos == self.helper_action.load_to_sub_pos) assert np.all(res.gen_to_sub_pos == self.helper_action.gen_to_sub_pos) assert np.all( res.line_or_to_sub_pos == self.helper_action.line_or_to_sub_pos) assert np.all( res.line_ex_to_sub_pos == self.helper_action.line_ex_to_sub_pos) assert np.all( res.load_pos_topo_vect == self.helper_action.load_pos_topo_vect) assert np.all( res.gen_pos_topo_vect == self.helper_action.gen_pos_topo_vect) assert np.all(res.line_or_pos_topo_vect == self.helper_action.line_or_pos_topo_vect) assert np.all(res.line_ex_pos_topo_vect == self.helper_action.line_ex_pos_topo_vect) # pdb.set_trace() assert np.all(res.actionClass == self.helper_action.actionClass)
def setUp(self): self.backend = PandaPowerBackend() self.path_matpower = PATH_DATA_TEST self.case_file = "test_case14.json" self.backend.load_grid(self.path_matpower, self.case_file) self.tolvect = 1e-2 self.tol_one = 1e-5 self.game_rules = GameRules() self.action_env = HelperAction(name_prod=self.backend.name_prods, name_load=self.backend.name_loads, name_line=self.backend.name_lines, subs_info=self.backend.subs_elements, load_to_subid=self.backend.load_to_subid, gen_to_subid=self.backend.gen_to_subid, lines_or_to_subid=self.backend.lines_or_to_subid, lines_ex_to_subid=self.backend.lines_ex_to_subid, ##### load_to_sub_pos=self.backend.load_to_sub_pos, gen_to_sub_pos=self.backend.gen_to_sub_pos, lines_or_to_sub_pos=self.backend.lines_or_to_sub_pos, lines_ex_to_sub_pos=self.backend.lines_ex_to_sub_pos, ##### load_pos_topo_vect=self.backend.load_pos_topo_vect, gen_pos_topo_vect=self.backend.gen_pos_topo_vect, lines_or_pos_topo_vect=self.backend.lines_or_pos_topo_vect, lines_ex_pos_topo_vect=self.backend.lines_ex_pos_topo_vect, game_rules=self.game_rules)
def setUp(self): self.backend = PandaPowerBackend() self.path_matpower = PATH_DATA_TEST self.case_file = "test_case14.json" self.backend.load_grid(self.path_matpower, self.case_file) self.tolvect = 1e-2 self.tol_one = 1e-5 self.game_rules = GameRules() self.action_env = HelperAction( gridobj=self.backend, legal_action=self.game_rules.legal_action)
def setUp(self): """ The case file is a representation of the case14 as found in the ieee14 powergrid. :return: """ # from ADNBackend import ADNBackend # self.backend = ADNBackend() # self.path_matpower = "/home/donnotben/Documents/RL4Grid/RL4Grid/data" # self.case_file = "ieee14_ADN.xml" # self.backend.load_grid(self.path_matpower, self.case_file) self.tolvect = 1e-2 self.tol_one = 1e-5 self.game_rules = GameRules() # pdb.set_trace() self.helper_action = HelperAction( name_prod=["gen_{}".format(i) for i in range(5)], name_load=["load_{}".format(i) for i in range(11)], name_line=["line_{}".format(i) for i in range(20)], subs_info=np.array([3, 6, 4, 6, 5, 6, 3, 2, 5, 3, 3, 3, 4, 3], dtype=np.int), load_to_subid=np.array([1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13]), gen_to_subid=np.array([0, 1, 2, 5, 7]), lines_or_to_subid=np.array( [0, 0, 1, 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 8, 8, 9, 11, 12]), lines_ex_to_subid=np.array([ 1, 4, 2, 3, 4, 3, 4, 6, 8, 5, 10, 11, 12, 7, 8, 9, 13, 10, 12, 13 ]), ##### load_to_sub_pos=np.array([4, 2, 5, 4, 4, 4, 1, 1, 1, 2, 1]), gen_to_sub_pos=np.array([2, 5, 3, 5, 1]), lines_or_to_sub_pos=np.array( [0, 1, 1, 2, 3, 1, 2, 3, 4, 3, 1, 2, 3, 1, 2, 2, 3, 0, 0, 1]), lines_ex_to_sub_pos=np.array( [0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 2, 2, 3, 0, 1, 2, 2, 0, 0, 0]), ##### load_pos_topo_vect=np.array( [7, 11, 18, 23, 28, 39, 41, 44, 47, 51, 54]), gen_pos_topo_vect=np.array([2, 8, 12, 29, 34]), lines_or_pos_topo_vect=np.array([ 0, 1, 4, 5, 6, 10, 15, 16, 17, 22, 25, 26, 27, 31, 32, 37, 38, 40, 46, 50 ]), lines_ex_pos_topo_vect=np.array([ 3, 19, 9, 13, 20, 14, 21, 30, 35, 24, 45, 48, 52, 33, 36, 42, 55, 43, 49, 53 ]), game_rules=self.game_rules)
def __init__(self, gridobj, controler_backend): """ Parameters ---------- gridobj: :class:`grid2op.Space.Space` Structure of the powergrid envbackend: :class:`grid2op.Backend.Backend` An instanciated backend to perform some computation on a powergrid, before taking some actions. """ legal_act = AllwaysLegal() self.action_space = HelperAction(gridobj=gridobj, actionClass=VoltageOnlyAction, legal_action=legal_act) self.backend = controler_backend.copy()
class Environment(_BasicEnv): """ Attributes ---------- logger: ``logger`` Use to store some information (currently in beta status) time_stamp: ``datetime.time`` Current time of the chronics nb_time_step: ``int`` Number of time steps played this episode parameters: :class:`grid2op.Parameters.Parameters` Parameters used for the game rewardClass: ``type`` Type of reward used. Should be a subclass of :class:`grid2op.Reward.Reward` init_grid_path: ``str`` The path where the description of the powergrid is located. backend: :class:`grid2op.Backend.Backend` The backend used to compute powerflows and cascading failures. game_rules: :class:`grid2op.GameRules.GameRules` The rules of the game (define which actions are legal and which are not) helper_action_player: :class:`grid2op.Action.HelperAction` Helper used to manipulate more easily the actions given to / provided by the :class:`grid2op.Agent` (player) helper_action_env: :class:`grid2op.Action.HelperAction` Helper used to manipulate more easily the actions given to / provided by the environment to the backend. helper_observation: :class:`grid2op.Observation.ObservationHelper` Helper used to generate the observation that will be given to the :class:`grid2op.Agent` current_obs: :class:`grid2op.Observation.Observation` The current observation (or None if it's not intialized) no_overflow_disconnection: ``bool`` Whether or not cascading failures are computed or not (TRUE = the powerlines above their thermal limits will not be disconnected). This is initialized based on the attribute :attr:`grid2op.Parameters.Parameters.NO_OVERFLOW_DISCONNECTION`. timestep_overflow: ``numpy.ndarray``, dtype: int Number of consecutive timesteps each powerline has been on overflow. nb_timestep_overflow_allowed: ``numpy.ndarray``, dtype: int Number of consecutive timestep each powerline can be on overflow. It is usually read from :attr:`grid2op.Parameters.Parameters.NB_TIMESTEP_POWERFLOW_ALLOWED`. hard_overflow_threshold: ``float`` Number of timestep before an :class:`grid2op.Agent.Agent` can reconnet a powerline that has been disconnected by the environment due to an overflow. env_dc: ``bool`` Whether the environment computes the powerflow using the DC approximation or not. It is usually read from :attr:`grid2op.Parameters.Parameters.ENV_DC`. chronics_handler: :class:`grid2op.ChronicsHandler.ChronicsHandler` Helper to get the modification of each time step during the episode. names_chronics_to_backend: ``dict`` Configuration file used to associated the name of the objects in the backend (both extremities of powerlines, load or production for example) with the same object in the data (:attr:`Environment.chronics_handler`). The idea is that, usually data generation comes from a different software that does not take into account the powergrid infrastructure. Hence, the same "object" can have a different name. This mapping is present to avoid the need to rename the "object" when providing data. A more detailed description is available at :func:`grid2op.ChronicsHandler.GridValue.initialize`. reward_helper: :class:`grid2p.Reward.RewardHelper` Helper that is called to compute the reward at each time step. action_space: :class:`grid2op.Action.HelperAction` Another name for :attr:`Environment.helper_action_player` for gym compatibility. observation_space: :class:`grid2op.Observation.ObservationHelper` Another name for :attr:`Environment.helper_observation` for gym compatibility. reward_range: ``(float, float)`` The range of the reward function metadata: ``dict`` For gym compatibility, do not use spec: ``None`` For Gym compatibility, do not use viewer: ``object`` Used to display the powergrid. Currently not supported. env_modification: :class:`grid2op.Action.Action` Representation of the actions of the environment for the modification of the powergrid. current_reward: ``float`` The reward of the current time step TODO update with maintenance, hazards etc. see below # store actions "cooldown" times_before_line_status_actionable max_timestep_line_status_deactivated times_before_topology_actionable max_timestep_topology_deactivated time_next_maintenance duration_next_maintenance hard_overflow_threshold time_remaining_before_reconnection # redispacthing target_dispatch actual_dispatch gen_activeprod_t: Should be initialized at 0. for "step" to properly recognize it's the first time step of the game """ def __init__(self, init_grid_path: str, chronics_handler, backend, parameters, names_chronics_to_backend=None, actionClass=TopologyAction, observationClass=CompleteObservation, rewardClass=FlatReward, legalActClass=AllwaysLegal, voltagecontrolerClass=ControlVoltageFromFile, thermal_limit_a=None, epsilon_poly=1e-2, tol_poly=1e-6): """ Initialize the environment. See the descirption of :class:`grid2op.Environment.Environment` for more information. Parameters ---------- init_grid_path: ``str`` Used to initailize :attr:`Environment.init_grid_path` chronics_handler backend parameters names_chronics_to_backend actionClass observationClass rewardClass legalActClass """ # TODO documentation!! _BasicEnv.__init__(self, parameters=parameters, thermal_limit_a=thermal_limit_a, epsilon_poly=epsilon_poly, tol_poly=tol_poly) # the voltage controler self.voltagecontrolerClass = voltagecontrolerClass self.voltage_controler = None # for gym compatibility (initialized below) self.action_space = None self.observation_space = None self.reward_range = None self.viewer = None self.metadata = None self.spec = None # for plotting self.graph_layout = None self.init_backend(init_grid_path, chronics_handler, backend, names_chronics_to_backend, actionClass, observationClass, rewardClass, legalActClass) def init_backend(self, init_grid_path, chronics_handler, backend, names_chronics_to_backend, actionClass, observationClass, rewardClass, legalActClass): if not isinstance(rewardClass, type): raise Grid2OpException( "Parameter \"rewardClass\" used to build the Environment should be a type (a class) " "and not an object (an instance of a class). " "It is currently \"{}\"".format(type(rewardClass))) if not issubclass(rewardClass, Reward): raise Grid2OpException( "Parameter \"rewardClass\" used to build the Environment should derived form the grid2op.Reward class, " "type provided is \"{}\"".format(type(rewardClass))) self.rewardClass = rewardClass self.actionClass = actionClass self.observationClass = observationClass # backend self.init_grid_path = os.path.abspath(init_grid_path) if not isinstance(backend, Backend): raise Grid2OpException( "Parameter \"backend\" used to build the Environment should derived form the grid2op.Backend class, " "type provided is \"{}\"".format(type(backend))) self.backend = backend self.backend.load_grid( self.init_grid_path) # the real powergrid of the environment self.backend.load_redispacthing_data( os.path.split(self.init_grid_path)[0]) self.backend.assert_grid_correct() self.init_grid(backend) self._has_been_initialized() if self._thermal_limit_a is None: self._thermal_limit_a = self.backend.thermal_limit_a else: self.backend.set_thermal_limit(self._thermal_limit_a) *_, tmp = self.backend.generators_info() # rules of the game if not isinstance(legalActClass, type): raise Grid2OpException( "Parameter \"legalActClass\" used to build the Environment should be a type " "(a class) and not an object (an instance of a class). " "It is currently \"{}\"".format(type(legalActClass))) if not issubclass(legalActClass, LegalAction): raise Grid2OpException( "Parameter \"legalActClass\" used to build the Environment should derived form the " "grid2op.LegalAction class, type provided is \"{}\"".format( type(legalActClass))) self.game_rules = GameRules(legalActClass=legalActClass) self.legalActClass = legalActClass # action helper if not isinstance(actionClass, type): raise Grid2OpException( "Parameter \"actionClass\" used to build the Environment should be a type (a class) " "and not an object (an instance of a class). " "It is currently \"{}\"".format(type(legalActClass))) if not issubclass(actionClass, Action): raise Grid2OpException( "Parameter \"actionClass\" used to build the Environment should derived form the " "grid2op.Action class, type provided is \"{}\"".format( type(actionClass))) if not isinstance(observationClass, type): raise Grid2OpException( "Parameter \"actionClass\" used to build the Environment should be a type (a class) " "and not an object (an instance of a class). " "It is currently \"{}\"".format(type(legalActClass))) if not issubclass(observationClass, Observation): raise Grid2OpException( "Parameter \"observationClass\" used to build the Environment should derived form the " "grid2op.Observation class, type provided is \"{}\"".format( type(observationClass))) # action affecting the grid that will be made by the agent self.helper_action_player = HelperAction( gridobj=self.backend, actionClass=actionClass, legal_action=self.game_rules.legal_action) # action that affect the grid made by the environment. self.helper_action_env = HelperAction( gridobj=self.backend, actionClass=Action, legal_action=self.game_rules.legal_action) self.helper_observation = ObservationHelper( gridobj=self.backend, observationClass=observationClass, rewardClass=rewardClass, env=self) # handles input data if not isinstance(chronics_handler, ChronicsHandler): raise Grid2OpException( "Parameter \"chronics_handler\" used to build the Environment should derived form the " "grid2op.ChronicsHandler class, type provided is \"{}\"". format(type(chronics_handler))) self.chronics_handler = chronics_handler self.chronics_handler.initialize( self.name_load, self.name_gen, self.name_line, self.name_sub, names_chronics_to_backend=names_chronics_to_backend) self.names_chronics_to_backend = names_chronics_to_backend # test to make sure the backend is consistent with the chronics generator self.chronics_handler.check_validity(self.backend) # reward function self.reward_helper = RewardHelper(rewardClass=rewardClass) self.reward_helper.initialize(self) # controler for voltage if not issubclass(self.voltagecontrolerClass, ControlVoltageFromFile): raise Grid2OpException( "Parameter \"voltagecontrolClass\" should derive from \"ControlVoltageFromFile\"." ) self.voltage_controler = self.voltagecontrolerClass( gridobj=self.backend, controler_backend=self.backend) # performs one step to load the environment properly (first action need to be taken at first time step after # first injections given) self._reset_maintenance() do_nothing = self.helper_action_env({}) *_, fail_to_start, _ = self.step(do_nothing) if fail_to_start: raise Grid2OpException( "Impossible to initialize the powergrid, the powerflow diverge at iteration 0." ) # test the backend returns object of the proper size self.backend.assert_grid_correct_after_powerflow() # for gym compatibility self.action_space = self.helper_action_player # this should be an action !!! self.observation_space = self.helper_observation # this return an observation. self.reward_range = self.reward_helper.range() self.viewer = None self.metadata = {'render.modes': ["human", "rgb_array"]} self.spec = None self.current_reward = self.reward_range[0] self.done = False self._reset_vectors_and_timings() def _voltage_control(self, agent_action, prod_v_chronics): """ Update the environment action "action_env" given a possibly new voltage setpoint for the generators. This function can be overide for a more complex handling of the voltages. It mush update (if needed) the voltages of the environment action :attr:`BasicEnv.env_modification` Parameters ---------- agent_action: :class:`grid2op.Action.Action` The action performed by the player (or do nothing is player action were not legal or ambiguous) prod_v_chronics: ``numpy.ndarray`` or ``None`` The voltages that has been specified in the chronics """ self.env_modification += self.voltage_controler.fix_voltage( self.current_obs, agent_action, self.env_modification, prod_v_chronics) def set_chunk_size(self, new_chunk_size): """ For an efficient data pipeline, it can be usefull to not read all part of the input data (for example for load_p, prod_p, load_q, prod_v). Grid2Op support the reading of large chronics by "chunk" of given size. Reading data in chunk can also reduce the memory footprint, useful in case of multiprocessing environment while large chronics. It is critical to set a small chunk_size in case of training machine learning algorithm (reinforcement learning agent) at the beginning when the agent performs poorly, the software might spend most of its time loading the data. **NB** this has no effect if the chronics does not support this feature. TODO see xxx for more information **NB** The environment need to be **reset** for this to take effect (it won't affect the chronics already loaded) Parameters ---------- new_chunk_size: ``int`` or ``None`` The new chunk size (positive integer) """ if new_chunk_size is None: self.chronics_handler.set_chunk_size(new_chunk_size) return try: new_chunk_size = int(new_chunk_size) except Exception as e: raise Grid2OpException( "Impossible to set the chunk size. It should be convertible a integer, and not" "{}".format(new_chunk_size)) if new_chunk_size <= 0: raise Grid2OpException( "Impossible to read less than 1 data at a time. Please make sure \"new_chunk_size\"" "is a positive integer.") self.chronics_handler.set_chunk_size(new_chunk_size) def set_id(self, id_): """ Set the id that will be used at the next call to :func:`Environment.reset`. **NB** this has no effect if the chronics does not support this feature. TODO see xxx for more information **NB** The environment need to be **reset** for this to take effect. Parameters ---------- id_: ``int`` the id of the chronics used. Examples -------- Here an example that will loop 10 times through the same chronics (always using the same injection then): .. code-block:: python import grid2op from grid2op import make from grid2op.Agent import DoNothingAgent env = make("case14_redisp") # create an environment agent = DoNothingAgent(env.action_space) # create an Agent for i in range(10): env.set_id(0) # tell the environment you simply want to use the chronics with ID 0 obs = env.reset() # it is necessary to perform a reset reward = env.reward_range[0] done = False while not done: act = agent.act(obs, reward, done) obs, reward, done, info = env.step(act) """ self.chronics_handler.tell_id(id_ - 1) def attach_renderer(self, graph_layout=None): if self.viewer is not None: return graph_layout = self.graph_layout if graph_layout is None else graph_layout if graph_layout is not None: self.viewer = Renderer(graph_layout, observation_space=self.helper_observation) self.viewer.reset(self) else: raise PlotError( "No layout are available for the powergrid. Renderer is not possible." ) def __str__(self): return '<{} instance>'.format(type(self).__name__) # TODO be closer to original gym implementation # if self.spec is None: # return '<{} instance>'.format(type(self).__name__) # else: # return '<{}<{}>>'.format(type(self).__name__, self.spec.id) def reset_grid(self): """ Reset the backend to a clean state by reloading the powergrid from the hard drive. This might takes some time. If the thermal has been modified, it also modify them into the new backend. """ self.backend.load_grid( self.init_grid_path) # the real powergrid of the environment self.backend.assert_grid_correct() if self._thermal_limit_a is not None: self.backend.set_thermal_limit(self._thermal_limit_a) do_nothing = self.helper_action_env({}) self.step(do_nothing) # test the backend returns object of the proper size self.backend.assert_grid_correct_after_powerflow() def add_text_logger(self, logger=None): """ Add a text logger to this :class:`Environment` Logging is for now an incomplete feature. It will get improved Parameters ---------- logger: The logger to use """ self.logger = logger return self def seed(self, seed=None): """ Set the seed of this :class:`Environment` for a better control and to ease reproducible experiments. This is not supported yet. Parameters ---------- seed: ``int`` The seed to set. """ try: seed = np.array(seed).astype('int64') except Exception as e: raise Grid2OpException( "Impossible to seed with the seed provided. Make sure it can be converted to a" "numpy 64 integer.") # example from gym # self.np_random, seed = seeding.np_random(seed) # TODO make that more clean, see example of seeding @ https://github.com/openai/gym/tree/master/gym/utils self.chronics_handler.seed(seed) self.helper_observation.seed(seed) self.helper_action_player.seed(seed) self.helper_action_env.seed(seed) return [seed] def reset(self): """ Reset the environment to a clean state. It will reload the next chronics if any. And reset the grid to a clean state. This triggers a full reloading of both the chronics (if they are stored as files) and of the powergrid, to ensure the episode is fully over. This method should be called only at the end of an episode. """ self.chronics_handler.next_chronics() self.chronics_handler.initialize( self.backend.name_load, self.backend.name_gen, self.backend.name_line, self.backend.name_sub, names_chronics_to_backend=self.names_chronics_to_backend) self.current_obs = None self._reset_maintenance() self._reset_redispatching() self.reset_grid() if self.viewer is not None: self.viewer.reset(self) # if True, then it will not disconnect lines above their thermal limits self._reset_vectors_and_timings() return self.get_obs() def render(self, mode='human'): err_msg = "Impossible to use the renderer, please set it up with \"env.init_renderer(graph_layout)\", " \ "graph_layout being the position of each substation of the powergrid that you must provide" self.attach_renderer() if mode == "human": if self.viewer is not None: has_quit = self.viewer.render(self.current_obs, reward=self.current_reward, timestamp=self.time_stamp, done=self.done) if has_quit: self.close() exit() else: raise Grid2OpException(err_msg) elif mode == "rgb_array": if self.viewer is not None: return np.array( self.viewer.get_rgb(self.current_obs, reward=self.current_reward, timestamp=self.time_stamp, done=self.done)) else: raise Grid2OpException(err_msg) else: raise Grid2OpException( "Renderer mode \"{}\" not supported.".format(mode)) def copy(self): """ performs a deep copy of the environment Returns ------- """ tmp_backend = self.backend self.backend = None res = copy.deepcopy(self) res.backend = tmp_backend.copy() if self._thermal_limit_a is not None: res.backend.set_thermal_limit(self._thermal_limit_a) self.backend = tmp_backend return res def get_kwargs(self): """ This function allows to make another Environment with the same parameters as the one that have been used to make this one. This is usefull especially in cases where Environment is not pickable (for example if some non pickable c++ code are used) but you still want to make parallel processing using "MultiProcessing" module. In that case, you can send this dictionnary to each child process, and have each child process make a copy of ``self`` Returns ------- res: ``dict`` A dictionnary that helps build an environment like ``self`` Examples -------- It should be used as follow: .. code-block:: python import grid2op from grid2op.Environment import Environment env = grid2op.make() # create the environment of your choice copy_of_env = Environment(**env.get_kwargs()) # And you can use this one as you would any other environment. """ res = {} res["init_grid_path"] = self.init_grid_path res["chronics_handler"] = copy.deepcopy(self.chronics_handler) res["parameters"] = copy.deepcopy(self.parameters) res["names_chronics_to_backend"] = copy.deepcopy( self.names_chronics_to_backend) res["actionClass"] = self.actionClass res["observationClass"] = self.observationClass res["rewardClass"] = self.rewardClass res["legalActClass"] = self.legalActClass res["epsilon_poly"] = self._epsilon_poly res["tol_poly"] = self._tol_poly res["thermal_limit_a"] = self._thermal_limit_a res["voltagecontrolerClass"] = self.voltagecontrolerClass return res def get_params_for_runner(self): """ This method is used to initialize a proper :class:`grid2op.Runner.Runner` to use this specific environment. Examples -------- It should be used as followed: .. code-block:: python import grid2op from grid2op.Runner import Runner env = grid2op.make() # create the environment of your choice agent = DoNothingAgent(env.actoin_space) # create the proper runner runner = Runner(**env.get_params_for_runner(), agentClass=DoNothingAgent) # now you can run runner.run(nb_episode=1) # run for 1 episode """ res = {} res["init_grid_path"] = self.init_grid_path res["path_chron"] = self.chronics_handler.path res["parameters_path"] = self.parameters.to_dict() res["names_chronics_to_backend"] = self.names_chronics_to_backend res["actionClass"] = self.actionClass res["observationClass"] = self.observationClass res["rewardClass"] = self.rewardClass res["legalActClass"] = self.legalActClass res["envClass"] = Environment res["gridStateclass"] = self.chronics_handler.chronicsClass res["backendClass"] = type(self.backend) # TODO res["verbose"] = False dict_ = copy.deepcopy(self.chronics_handler.kwargs) if 'path' in dict_: # path is handled elsewhere del dict_["path"] res["gridStateclass_kwargs"] = dict_ res["thermal_limit_a"] = self._thermal_limit_a res["voltageControlerClass"] = self.voltagecontrolerClass # TODO make a test for that return res
def init_backend(self, init_grid_path, chronics_handler, backend, names_chronics_to_backend, actionClass, observationClass, rewardClass, legalActClass): if not isinstance(rewardClass, type): raise Grid2OpException( "Parameter \"rewardClass\" used to build the Environment should be a type (a class) " "and not an object (an instance of a class). " "It is currently \"{}\"".format(type(rewardClass))) if not issubclass(rewardClass, Reward): raise Grid2OpException( "Parameter \"rewardClass\" used to build the Environment should derived form the grid2op.Reward class, " "type provided is \"{}\"".format(type(rewardClass))) self.rewardClass = rewardClass self.actionClass = actionClass self.observationClass = observationClass # backend self.init_grid_path = os.path.abspath(init_grid_path) if not isinstance(backend, Backend): raise Grid2OpException( "Parameter \"backend\" used to build the Environment should derived form the grid2op.Backend class, " "type provided is \"{}\"".format(type(backend))) self.backend = backend self.backend.load_grid( self.init_grid_path) # the real powergrid of the environment self.backend.load_redispacthing_data( os.path.split(self.init_grid_path)[0]) self.backend.assert_grid_correct() self.init_grid(backend) self._has_been_initialized() if self._thermal_limit_a is None: self._thermal_limit_a = self.backend.thermal_limit_a else: self.backend.set_thermal_limit(self._thermal_limit_a) *_, tmp = self.backend.generators_info() # rules of the game if not isinstance(legalActClass, type): raise Grid2OpException( "Parameter \"legalActClass\" used to build the Environment should be a type " "(a class) and not an object (an instance of a class). " "It is currently \"{}\"".format(type(legalActClass))) if not issubclass(legalActClass, LegalAction): raise Grid2OpException( "Parameter \"legalActClass\" used to build the Environment should derived form the " "grid2op.LegalAction class, type provided is \"{}\"".format( type(legalActClass))) self.game_rules = GameRules(legalActClass=legalActClass) self.legalActClass = legalActClass # action helper if not isinstance(actionClass, type): raise Grid2OpException( "Parameter \"actionClass\" used to build the Environment should be a type (a class) " "and not an object (an instance of a class). " "It is currently \"{}\"".format(type(legalActClass))) if not issubclass(actionClass, Action): raise Grid2OpException( "Parameter \"actionClass\" used to build the Environment should derived form the " "grid2op.Action class, type provided is \"{}\"".format( type(actionClass))) if not isinstance(observationClass, type): raise Grid2OpException( "Parameter \"actionClass\" used to build the Environment should be a type (a class) " "and not an object (an instance of a class). " "It is currently \"{}\"".format(type(legalActClass))) if not issubclass(observationClass, Observation): raise Grid2OpException( "Parameter \"observationClass\" used to build the Environment should derived form the " "grid2op.Observation class, type provided is \"{}\"".format( type(observationClass))) # action affecting the grid that will be made by the agent self.helper_action_player = HelperAction( gridobj=self.backend, actionClass=actionClass, legal_action=self.game_rules.legal_action) # action that affect the grid made by the environment. self.helper_action_env = HelperAction( gridobj=self.backend, actionClass=Action, legal_action=self.game_rules.legal_action) self.helper_observation = ObservationHelper( gridobj=self.backend, observationClass=observationClass, rewardClass=rewardClass, env=self) # handles input data if not isinstance(chronics_handler, ChronicsHandler): raise Grid2OpException( "Parameter \"chronics_handler\" used to build the Environment should derived form the " "grid2op.ChronicsHandler class, type provided is \"{}\"". format(type(chronics_handler))) self.chronics_handler = chronics_handler self.chronics_handler.initialize( self.name_load, self.name_gen, self.name_line, self.name_sub, names_chronics_to_backend=names_chronics_to_backend) self.names_chronics_to_backend = names_chronics_to_backend # test to make sure the backend is consistent with the chronics generator self.chronics_handler.check_validity(self.backend) # reward function self.reward_helper = RewardHelper(rewardClass=rewardClass) self.reward_helper.initialize(self) # controler for voltage if not issubclass(self.voltagecontrolerClass, ControlVoltageFromFile): raise Grid2OpException( "Parameter \"voltagecontrolClass\" should derive from \"ControlVoltageFromFile\"." ) self.voltage_controler = self.voltagecontrolerClass( gridobj=self.backend, controler_backend=self.backend) # performs one step to load the environment properly (first action need to be taken at first time step after # first injections given) self._reset_maintenance() do_nothing = self.helper_action_env({}) *_, fail_to_start, _ = self.step(do_nothing) if fail_to_start: raise Grid2OpException( "Impossible to initialize the powergrid, the powerflow diverge at iteration 0." ) # test the backend returns object of the proper size self.backend.assert_grid_correct_after_powerflow() # for gym compatibility self.action_space = self.helper_action_player # this should be an action !!! self.observation_space = self.helper_observation # this return an observation. self.reward_range = self.reward_helper.range() self.viewer = None self.metadata = {'render.modes': ["human", "rgb_array"]} self.spec = None self.current_reward = self.reward_range[0] self.done = False self._reset_vectors_and_timings()
def __init__(self, init_grid_path: str, chronics_handler, backend, parameters, names_chronics_to_backend=None, actionClass=TopologyAction, observationClass=CompleteObservation, rewardClass=FlatReward, legalActClass=AllwaysLegal): """ Initialize the environment. See the descirption of :class:`grid2op.Environment.Environment` for more information. Parameters ---------- init_grid_path: ``str`` Used to initailize :attr:`Environment.init_grid_path` chronics_handler backend parameters names_chronics_to_backend actionClass observationClass rewardClass legalActClass """ # TODO documentation!! # some timers self._time_apply_act = 0 self._time_powerflow = 0 self._time_extract_obs = 0 # define logger self.logger = None # and calendar data self.time_stamp = None self.nb_time_step = 0 # specific to power system if not isinstance(parameters, Parameters): raise Grid2OpException( "Parameter \"parameters\" used to build the Environment should derived form the " "grid2op.Parameters class, type provided is \"{}\"".format( type(parameters))) self.parameters = parameters if not isinstance(rewardClass, type): raise Grid2OpException( "Parameter \"rewardClass\" used to build the Environment should be a type (a class) " "and not an object (an instance of a class). " "It is currently \"{}\"".format(type(rewardClass))) if not issubclass(rewardClass, Reward): raise Grid2OpException( "Parameter \"rewardClass\" used to build the Environment should derived form the grid2op.Reward class, " "type provided is \"{}\"".format(type(rewardClass))) self.rewardClass = rewardClass # backend self.init_grid_path = os.path.abspath(init_grid_path) if not isinstance(backend, Backend): raise Grid2OpException( "Parameter \"backend\" used to build the Environment should derived form the grid2op.Backend class, " "type provided is \"{}\"".format(type(backend))) self.backend = backend self.backend.load_grid( self.init_grid_path) # the real powergrid of the environment self.backend.assert_grid_correct() *_, tmp = self.backend.generators_info() # rules of the game if not isinstance(legalActClass, type): raise Grid2OpException( "Parameter \"legalActClass\" used to build the Environment should be a type " "(a class) and not an object (an instance of a class). " "It is currently \"{}\"".format(type(legalActClass))) if not issubclass(legalActClass, LegalAction): raise Grid2OpException( "Parameter \"legalActClass\" used to build the Environment should derived form the " "grid2op.LegalAction class, type provided is \"{}\"".format( type(legalActClass))) self.game_rules = GameRules(legalActClass=legalActClass) # action helper if not isinstance(actionClass, type): raise Grid2OpException( "Parameter \"actionClass\" used to build the Environment should be a type (a class) " "and not an object (an instance of a class). " "It is currently \"{}\"".format(type(legalActClass))) if not issubclass(actionClass, Action): raise Grid2OpException( "Parameter \"actionClass\" used to build the Environment should derived form the " "grid2op.Action class, type provided is \"{}\"".format( type(actionClass))) if not isinstance(observationClass, type): raise Grid2OpException( "Parameter \"actionClass\" used to build the Environment should be a type (a class) " "and not an object (an instance of a class). " "It is currently \"{}\"".format(type(legalActClass))) if not issubclass(observationClass, Observation): raise Grid2OpException( "Parameter \"observationClass\" used to build the Environment should derived form the " "grid2op.Observation class, type provided is \"{}\"".format( type(observationClass))) # action affecting the _grid that will be made by the agent self.helper_action_player = HelperAction(gridobj=self.backend, actionClass=actionClass, game_rules=self.game_rules) # action that affect the _grid made by the environment. self.helper_action_env = HelperAction(gridobj=self.backend, actionClass=Action, game_rules=self.game_rules) self.helper_observation = ObservationHelper( gridobj=self.backend, observationClass=observationClass, rewardClass=rewardClass, env=self) # observation self.current_obs = None # type of power flow to play # if True, then it will not disconnect lines above their thermal limits self.no_overflow_disconnection = self.parameters.NO_OVERFLOW_DISCONNECTION self.timestep_overflow = np.zeros(shape=(self.backend.n_line, ), dtype=np.int) self.nb_timestep_overflow_allowed = np.full( shape=(self.backend.n_line, ), fill_value=self.parameters.NB_TIMESTEP_POWERFLOW_ALLOWED) # store actions "cooldown" self.times_before_line_status_actionable = np.zeros( shape=(self.backend.n_line, ), dtype=np.int) self.max_timestep_line_status_deactivated = self.parameters.NB_TIMESTEP_LINE_STATUS_REMODIF self.times_before_topology_actionable = np.zeros( shape=(self.backend.n_sub, ), dtype=np.int) self.max_timestep_topology_deactivated = self.parameters.NB_TIMESTEP_TOPOLOGY_REMODIF # for maintenance operation self.time_next_maintenance = np.zeros(shape=(self.backend.n_line, ), dtype=np.int) - 1 self.duration_next_maintenance = np.zeros( shape=(self.backend.n_line, ), dtype=np.int) # hazard (not used outside of this class, information is given in `time_remaining_before_line_reconnection` self._hazard_duration = np.zeros(shape=(self.backend.n_line, ), dtype=np.int) # hard overflow part self.hard_overflow_threshold = self.parameters.HARD_OVERFLOW_THRESHOLD self.time_remaining_before_line_reconnection = np.full( shape=(self.backend.n_line, ), fill_value=0, dtype=np.int) self.env_dc = self.parameters.ENV_DC # handles input data if not isinstance(chronics_handler, ChronicsHandler): raise Grid2OpException( "Parameter \"chronics_handler\" used to build the Environment should derived form the " "grid2op.ChronicsHandler class, type provided is \"{}\"". format(type(chronics_handler))) self.chronics_handler = chronics_handler self.chronics_handler.initialize( self.backend.name_load, self.backend.name_gen, self.backend.name_line, self.backend.name_sub, names_chronics_to_backend=names_chronics_to_backend) self.names_chronics_to_backend = names_chronics_to_backend # test to make sure the backend is consistent with the chronics generator self.chronics_handler.check_validity(self.backend) # store environment modifications self._injection = None self._maintenance = None self._hazards = None self.env_modification = None # reward self.reward_helper = RewardHelper(rewardClass=rewardClass) self.reward_helper.initialize(self) # performs one step to load the environment properly (first action need to be taken at first time step after # first injections given) self._reset_maintenance() do_nothing = self.helper_action_env({}) *_, fail_to_start, _ = self.step(do_nothing) if fail_to_start: raise Grid2OpException( "Impossible to initialize the powergrid, the powerflow diverge at iteration 0." ) # test the backend returns object of the proper size self.backend.assert_grid_correct_after_powerflow() # for gym compatibility self.action_space = self.helper_action_player # this should be an action !!! self.observation_space = self.helper_observation # this return an observation. self.reward_range = self.reward_helper.range() self.viewer = None self.metadata = {'render.modes': []} self.spec = None self._reset_vectors_and_timings()
def setUp(self): """ The case file is a representation of the case14 as found in the ieee14 powergrid. :return: """ self.tolvect = 1e-2 self.tol_one = 1e-5 self.game_rules = GameRules() self.gridobj = GridObjects() self.gridobj.init_grid_vect( name_prod=["gen_{}".format(i) for i in range(5)], name_load=["load_{}".format(i) for i in range(11)], name_line=["line_{}".format(i) for i in range(20)], name_sub=["sub_{}".format(i) for i in range(14)], sub_info=np.array([3, 6, 4, 6, 5, 6, 3, 2, 5, 3, 3, 3, 4, 3], dtype=np.int), load_to_subid=np.array([1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13]), gen_to_subid=np.array([0, 1, 2, 5, 7]), line_or_to_subid=np.array( [0, 0, 1, 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 8, 8, 9, 11, 12]), line_ex_to_subid=np.array([ 1, 4, 2, 3, 4, 3, 4, 6, 8, 5, 10, 11, 12, 7, 8, 9, 13, 10, 12, 13 ]), ##### load_to_sub_pos=np.array([4, 2, 5, 4, 4, 4, 1, 1, 1, 2, 1]), gen_to_sub_pos=np.array([2, 5, 3, 5, 1]), line_or_to_sub_pos=np.array( [0, 1, 1, 2, 3, 1, 2, 3, 4, 3, 1, 2, 3, 1, 2, 2, 3, 0, 0, 1]), line_ex_to_sub_pos=np.array( [0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 2, 2, 3, 0, 1, 2, 2, 0, 0, 0]), ##### load_pos_topo_vect=np.array( [7, 11, 18, 23, 28, 39, 41, 44, 47, 51, 54]), gen_pos_topo_vect=np.array([2, 8, 12, 29, 34]), line_or_pos_topo_vect=np.array([ 0, 1, 4, 5, 6, 10, 15, 16, 17, 22, 25, 26, 27, 31, 32, 37, 38, 40, 46, 50 ]), line_ex_pos_topo_vect=np.array([ 3, 19, 9, 13, 20, 14, 21, 30, 35, 24, 45, 48, 52, 33, 36, 42, 55, 43, 49, 53 ])) # pdb.set_trace() self.helper_action = HelperAction( self.gridobj, legal_action=self.game_rules.legal_action) self.helper_action_env = HelperAction( self.gridobj, legal_action=self.game_rules.legal_action, actionClass=Action) self.res = { 'name_gen': ['gen_0', 'gen_1', 'gen_2', 'gen_3', 'gen_4'], 'name_load': [ 'load_0', 'load_1', 'load_2', 'load_3', 'load_4', 'load_5', 'load_6', 'load_7', 'load_8', 'load_9', 'load_10' ], 'name_line': [ 'line_0', 'line_1', 'line_2', 'line_3', 'line_4', 'line_5', 'line_6', 'line_7', 'line_8', 'line_9', 'line_10', 'line_11', 'line_12', 'line_13', 'line_14', 'line_15', 'line_16', 'line_17', 'line_18', 'line_19' ], 'name_sub': [ 'sub_0', 'sub_1', 'sub_2', 'sub_3', 'sub_4', 'sub_5', 'sub_6', 'sub_7', 'sub_8', 'sub_9', 'sub_10', 'sub_11', 'sub_12', 'sub_13' ], 'sub_info': [3, 6, 4, 6, 5, 6, 3, 2, 5, 3, 3, 3, 4, 3], 'load_to_subid': [1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13], 'gen_to_subid': [0, 1, 2, 5, 7], 'line_or_to_subid': [0, 0, 1, 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 8, 8, 9, 11, 12], 'line_ex_to_subid': [ 1, 4, 2, 3, 4, 3, 4, 6, 8, 5, 10, 11, 12, 7, 8, 9, 13, 10, 12, 13 ], 'load_to_sub_pos': [4, 2, 5, 4, 4, 4, 1, 1, 1, 2, 1], 'gen_to_sub_pos': [2, 5, 3, 5, 1], 'line_or_to_sub_pos': [0, 1, 1, 2, 3, 1, 2, 3, 4, 3, 1, 2, 3, 1, 2, 2, 3, 0, 0, 1], 'line_ex_to_sub_pos': [0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 2, 2, 3, 0, 1, 2, 2, 0, 0, 0], 'load_pos_topo_vect': [7, 11, 18, 23, 28, 39, 41, 44, 47, 51, 54], 'gen_pos_topo_vect': [2, 8, 12, 29, 34], 'line_or_pos_topo_vect': [ 0, 1, 4, 5, 6, 10, 15, 16, 17, 22, 25, 26, 27, 31, 32, 37, 38, 40, 46, 50 ], 'line_ex_pos_topo_vect': [ 3, 19, 9, 13, 20, 14, 21, 30, 35, 24, 45, 48, 52, 33, 36, 42, 55, 43, 49, 53 ], 'gen_type': None, 'gen_pmin': None, 'gen_pmax': None, 'gen_redispatchable': None, 'gen_max_ramp_up': None, 'gen_max_ramp_down': None, 'gen_min_uptime': None, 'gen_min_downtime': None, 'gen_cost_per_MW': None, 'gen_startup_cost': None, 'gen_shutdown_cost': None, 'subtype': 'Action.Action' } self.size_act = 229
class TestLoadingBackendFunc(unittest.TestCase): def setUp(self): """ The case file is a representation of the case14 as found in the ieee14 powergrid. :return: """ self.tolvect = 1e-2 self.tol_one = 1e-5 self.game_rules = GameRules() self.gridobj = GridObjects() self.gridobj.init_grid_vect( name_prod=["gen_{}".format(i) for i in range(5)], name_load=["load_{}".format(i) for i in range(11)], name_line=["line_{}".format(i) for i in range(20)], name_sub=["sub_{}".format(i) for i in range(14)], sub_info=np.array([3, 6, 4, 6, 5, 6, 3, 2, 5, 3, 3, 3, 4, 3], dtype=np.int), load_to_subid=np.array([1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13]), gen_to_subid=np.array([0, 1, 2, 5, 7]), line_or_to_subid=np.array( [0, 0, 1, 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 8, 8, 9, 11, 12]), line_ex_to_subid=np.array([ 1, 4, 2, 3, 4, 3, 4, 6, 8, 5, 10, 11, 12, 7, 8, 9, 13, 10, 12, 13 ]), ##### load_to_sub_pos=np.array([4, 2, 5, 4, 4, 4, 1, 1, 1, 2, 1]), gen_to_sub_pos=np.array([2, 5, 3, 5, 1]), line_or_to_sub_pos=np.array( [0, 1, 1, 2, 3, 1, 2, 3, 4, 3, 1, 2, 3, 1, 2, 2, 3, 0, 0, 1]), line_ex_to_sub_pos=np.array( [0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 2, 2, 3, 0, 1, 2, 2, 0, 0, 0]), ##### load_pos_topo_vect=np.array( [7, 11, 18, 23, 28, 39, 41, 44, 47, 51, 54]), gen_pos_topo_vect=np.array([2, 8, 12, 29, 34]), line_or_pos_topo_vect=np.array([ 0, 1, 4, 5, 6, 10, 15, 16, 17, 22, 25, 26, 27, 31, 32, 37, 38, 40, 46, 50 ]), line_ex_pos_topo_vect=np.array([ 3, 19, 9, 13, 20, 14, 21, 30, 35, 24, 45, 48, 52, 33, 36, 42, 55, 43, 49, 53 ])) # pdb.set_trace() self.helper_action = HelperAction( self.gridobj, legal_action=self.game_rules.legal_action) self.helper_action_env = HelperAction( self.gridobj, legal_action=self.game_rules.legal_action, actionClass=Action) self.res = { 'name_gen': ['gen_0', 'gen_1', 'gen_2', 'gen_3', 'gen_4'], 'name_load': [ 'load_0', 'load_1', 'load_2', 'load_3', 'load_4', 'load_5', 'load_6', 'load_7', 'load_8', 'load_9', 'load_10' ], 'name_line': [ 'line_0', 'line_1', 'line_2', 'line_3', 'line_4', 'line_5', 'line_6', 'line_7', 'line_8', 'line_9', 'line_10', 'line_11', 'line_12', 'line_13', 'line_14', 'line_15', 'line_16', 'line_17', 'line_18', 'line_19' ], 'name_sub': [ 'sub_0', 'sub_1', 'sub_2', 'sub_3', 'sub_4', 'sub_5', 'sub_6', 'sub_7', 'sub_8', 'sub_9', 'sub_10', 'sub_11', 'sub_12', 'sub_13' ], 'sub_info': [3, 6, 4, 6, 5, 6, 3, 2, 5, 3, 3, 3, 4, 3], 'load_to_subid': [1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13], 'gen_to_subid': [0, 1, 2, 5, 7], 'line_or_to_subid': [0, 0, 1, 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 8, 8, 9, 11, 12], 'line_ex_to_subid': [ 1, 4, 2, 3, 4, 3, 4, 6, 8, 5, 10, 11, 12, 7, 8, 9, 13, 10, 12, 13 ], 'load_to_sub_pos': [4, 2, 5, 4, 4, 4, 1, 1, 1, 2, 1], 'gen_to_sub_pos': [2, 5, 3, 5, 1], 'line_or_to_sub_pos': [0, 1, 1, 2, 3, 1, 2, 3, 4, 3, 1, 2, 3, 1, 2, 2, 3, 0, 0, 1], 'line_ex_to_sub_pos': [0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 2, 2, 3, 0, 1, 2, 2, 0, 0, 0], 'load_pos_topo_vect': [7, 11, 18, 23, 28, 39, 41, 44, 47, 51, 54], 'gen_pos_topo_vect': [2, 8, 12, 29, 34], 'line_or_pos_topo_vect': [ 0, 1, 4, 5, 6, 10, 15, 16, 17, 22, 25, 26, 27, 31, 32, 37, 38, 40, 46, 50 ], 'line_ex_pos_topo_vect': [ 3, 19, 9, 13, 20, 14, 21, 30, 35, 24, 45, 48, 52, 33, 36, 42, 55, 43, 49, 53 ], 'gen_type': None, 'gen_pmin': None, 'gen_pmax': None, 'gen_redispatchable': None, 'gen_max_ramp_up': None, 'gen_max_ramp_down': None, 'gen_min_uptime': None, 'gen_min_downtime': None, 'gen_cost_per_MW': None, 'gen_startup_cost': None, 'gen_shutdown_cost': None, 'subtype': 'Action.Action' } self.size_act = 229 def tearDown(self): pass # self.backend._grid.delete() def compare_vect(self, pred, true): return np.max(np.abs(pred - true)) <= self.tolvect def test_instanciate_action(self): """ test i can instanciate an action without crashing :return: """ action = self.helper_action() def test_size(self): action = self.helper_action() action.size() def test_proper_size(self): action = self.helper_action() assert action.size() == self.size_act def test_size_action_space(self): assert self.helper_action.size() == self.size_act def test_print_notcrash(self): """ test the conversion to str does not crash :return: """ action = self.helper_action({}) a = "{}".format(action) def test_change_p(self): """ :return: """ new_vect = np.random.randn(self.helper_action.n_load) action = self.helper_action({"injection": {"load_p": new_vect}}) self.compare_vect(action._dict_inj["load_p"], new_vect) for i in range(self.helper_action.n_load): assert action.effect_on(load_id=i)["new_p"] == new_vect[i] def test_change_v(self): """ :return: """ new_vect = np.random.randn(self.helper_action.n_gen) action = self.helper_action({"injection": {"prod_v": new_vect}}) self.compare_vect(action._dict_inj["prod_v"], new_vect) for i in range(self.helper_action.n_gen): assert action.effect_on(gen_id=i)["new_v"] == new_vect[i] def test_change_p_q(self): """ :return: """ new_vect = np.random.randn(self.helper_action.n_load) new_vect2 = np.random.randn(self.helper_action.n_load) action = self.helper_action( {"injection": { "load_p": new_vect, "load_q": new_vect2 }}) assert self.compare_vect(action._dict_inj["load_p"], new_vect) assert self.compare_vect(action._dict_inj["load_q"], new_vect2) for i in range(self.helper_action.n_load): assert action.effect_on(load_id=i)["new_p"] == new_vect[i] assert action.effect_on(load_id=i)["new_q"] == new_vect2[i] def test_update_disconnection_1(self): """ Test if the disconnection is working properly :return: """ for i in range(self.helper_action.n_line): disco = np.full(shape=self.helper_action.n_line, fill_value=0, dtype=np.int) disco[i] = 1 action = self.helper_action({"set_line_status": disco}) for j in range(self.helper_action.n_line): assert action.effect_on(line_id=j)["set_line_status"] == disco[ j], "problem with line {} if line {} is disconnected".format( j, i) assert action.effect_on( line_id=j)["change_line_status"] == False def test_update_disconnection_m1(self): """ Test if the disconnection is working properly :return: """ for i in range(self.helper_action.n_line): disco = np.full(shape=self.helper_action.n_line, fill_value=0, dtype=np.int) disco[i] = -1 action = self.helper_action({"set_line_status": disco}) for j in range(self.helper_action.n_line): assert action.effect_on(line_id=j)["set_line_status"] == disco[ j], "problem with line {} if line {} is disconnected".format( j, i) assert action.effect_on( line_id=j)["change_line_status"] == False def test_update_hazard(self): """ Same test as above, but with hazard :return: """ for i in range(self.helper_action.n_line): disco = np.full(shape=self.helper_action.n_line, fill_value=False, dtype=np.bool) disco[i] = True action = self.helper_action({"hazards": disco}) for j in range(self.helper_action.n_line): expected_res = -1 if j == i else 0 assert action.effect_on( line_id=j )["set_line_status"] == expected_res, "problem with line {} if line {} is disconnected".format( j, i) assert action.effect_on( line_id=j)["change_line_status"] == False def test_update_status(self): for i in range(self.helper_action.n_line): disco = np.full(shape=self.helper_action.n_line, fill_value=False, dtype=np.bool) disco[i] = True action = self.helper_action({"change_line_status": disco}) for j in range(self.helper_action.n_line): expected_res = j == i assert action.effect_on(line_id=j)["set_line_status"] == 0 assert action.effect_on( line_id=j)["change_line_status"] == expected_res def test_update_set_topo_by_dict_obj(self): action = self.helper_action({"set_bus": {"loads_id": [(1, 3)]}}) assert action.effect_on(load_id=1)["set_bus"] == 3 assert action.effect_on(load_id=1)["change_bus"] == False assert action.effect_on(load_id=0)["set_bus"] == 0 assert action.effect_on(load_id=0)["change_bus"] == False def test_update_set_topo_by_dict_sub(self): arr = np.array([1, 1, 1, 2, 2, 2], dtype=np.int) action = self.helper_action( {"set_bus": { "substations_id": [(1, arr)] }}) assert action.effect_on(line_id=2)["set_bus_or"] == 1 assert action.effect_on(line_id=3)["set_bus_or"] == 1 assert action.effect_on(line_id=4)["set_bus_or"] == 2 assert action.effect_on(line_id=0)["set_bus_ex"] == 1 assert action.effect_on(load_id=0)["set_bus"] == 2 assert action.effect_on(gen_id=1)["set_bus"] == 2 assert action.effect_on(load_id=1)["set_bus"] == 0 assert action.effect_on(gen_id=0)["set_bus"] == 0 def test_update_set_topo_by_dict_sub2(self): arr = np.array([1, 1, 1, 2, 2, 2], dtype=np.int) arr3 = np.array([1, 2, 1, 2, 1, 2], dtype=np.int) action = self.helper_action( {"set_bus": { "substations_id": [(3, arr3), (1, arr)] }}) assert action.effect_on(line_id=2)["set_bus_or"] == 1 assert action.effect_on(line_id=3)["set_bus_or"] == 1 assert action.effect_on(line_id=4)["set_bus_or"] == 2 assert action.effect_on(line_id=0)["set_bus_ex"] == 1 assert action.effect_on(load_id=0)["set_bus"] == 2 assert action.effect_on(gen_id=1)["set_bus"] == 2 assert action.effect_on(load_id=1)["set_bus"] == 0 assert action.effect_on(gen_id=0)["set_bus"] == 0 def test_update_change_bus_by_dict_obj(self): action = self.helper_action({"change_bus": {"loads_id": [1]}}) assert action.effect_on(load_id=1)["set_bus"] == 0 assert action.effect_on(load_id=1)["change_bus"] == True assert action.effect_on(load_id=0)["set_bus"] == 0 assert action.effect_on(load_id=0)["change_bus"] == False def test_update_change_bus_by_dict_sub(self): arr = np.array([True, True, True, False, False, False], dtype=np.bool) action = self.helper_action( {"change_bus": { "substations_id": [(1, arr)] }}) assert action.effect_on(line_id=2)["change_bus_or"] == True assert action.effect_on(line_id=3)["change_bus_or"] == True assert action.effect_on(line_id=4)["change_bus_or"] == False assert action.effect_on(line_id=0)["change_bus_ex"] == True assert action.effect_on(load_id=0)["change_bus"] == False assert action.effect_on(gen_id=1)["change_bus"] == False assert action.effect_on(load_id=1)["change_bus"] == False assert action.effect_on(gen_id=0)["change_bus"] == False def test_update_change_bus_by_dict_sub2(self): arr = np.array([True, True, True, False, False, False], dtype=np.bool) arr3 = np.array([True, False, True, False, True, False], dtype=np.bool) action = self.helper_action( {"change_bus": { "substations_id": [(3, arr3), (1, arr)] }}) assert action.effect_on(line_id=2)["change_bus_or"] == True assert action.effect_on(line_id=3)["change_bus_or"] == True assert action.effect_on(line_id=4)["change_bus_or"] == False assert action.effect_on(line_id=0)["change_bus_ex"] == True assert action.effect_on(load_id=0)["change_bus"] == False assert action.effect_on(gen_id=1)["change_bus"] == False assert action.effect_on(load_id=1)["change_bus"] == False assert action.effect_on(gen_id=0)["change_bus"] == False def test_ambiguity_topo(self): action = self.helper_action({"change_bus": { "lines_or_id": [1] }}) # i switch the bus of the origin of powerline 1 action.update({"set_bus": { "lines_or_id": [(1, 1)] }}) # i set the origin of powerline 1 to bus 1 try: action() raise RuntimeError( "This should hav thrown an InvalidBusStatus error") except InvalidBusStatus as e: pass def test_ambiguity_line_status_when_set_and_change(self): arr = np.zeros(self.helper_action.n_line) arr[1] = -1 action = self.helper_action( {"set_line_status": arr}) # i switch set the status of powerline 1 to "disconnected" action.update({"change_line_status": [1]}) # i asked to change this status try: action() raise RuntimeError( "This should hav thrown an InvalidBusStatus error") except InvalidLineStatus as e: pass def test_ambiguity_line_reconnected_without_bus(self): arr = np.zeros(self.helper_action.n_line) arr[1] = 1 action = self.helper_action( {"set_line_status": arr}) # i switch set the status of powerline 1 to "connected" # and i don't say on which bus to connect it try: action() raise RuntimeError( "This should have thrown an InvalidBusStatus error") except InvalidLineStatus as e: pass def test_set_status_and_setbus_isambiguous(self): """ :return: """ arr = np.array([1, 1, 1, 2, 2, 2], dtype=np.int) id_ = 2 action = self.helper_action( {"set_bus": { "substations_id": [(1, arr)] }}) arr2 = np.zeros(self.helper_action.n_line) arr2[id_] = -1 action.update({"set_line_status": arr2}) try: action() raise RuntimeError( "This should have thrown an InvalidBusStatus error") except InvalidLineStatus as e: pass def test_hazard_overides_setbus(self): """ :return: """ arr = np.array([1, 1, 1, 2, 2, 2], dtype=np.int) id_ = 2 action = self.helper_action( {"set_bus": { "substations_id": [(1, arr)] }}) assert action.effect_on(line_id=id_)["set_bus_or"] == 1 action.update({"hazards": [id_]}) assert action.effect_on(line_id=id_)["set_bus_or"] == 0 assert action.effect_on(line_id=id_)["set_line_status"] == -1 assert action.effect_on(line_id=id_)["set_bus_ex"] == 0 def test_action_str(self): arr1 = np.array([False, False, False, True, True, True], dtype=np.bool) arr2 = np.array([1, 1, 2, 2], dtype=np.int) id_1 = 1 id_2 = 12 action = self.helper_action({ "change_bus": { "substations_id": [(id_1, arr1)] }, "set_bus": { "substations_id": [(id_2, arr2)] } }) res = action.__str__() act_str = 'This action will:\n\t - NOT change anything to the injections\n\t - NOT perform any redispatching ' \ 'action\n\t - NOT force any line status\n\t - NOT switch any line status\n\t - Change the bus of the ' \ 'following element:\n\t \t - switch bus of line (origin) 4 [on substation 1]\n\t \t - switch bus of ' \ 'load 0 [on substation 1]\n\t \t - switch bus of generator 1 [on substation 1]\n\t - Set the bus of ' \ 'the following element:\n\t \t - assign bus 1 to line (extremity) 18 [on substation 12]\n\t \t - ' \ 'assign bus 1 to line (origin) 19 [on substation 12]\n\t \t - assign bus 2 to load 9 ' \ '[on substation 12]\n\t \t - assign bus 2 to line (extremity) 12 [on substation 12]' assert res == act_str def test_to_vect(self): arr1 = np.array([False, False, False, True, True, True], dtype=np.bool) arr2 = np.array([1, 1, 2, 2], dtype=np.int) id_1 = 1 id_2 = 12 action = self.helper_action({ "change_bus": { "substations_id": [(id_1, arr1)] }, "set_bus": { "substations_id": [(id_2, arr2)] } }) res = action.to_vect() tmp = np.array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 2., 2., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0. ]) assert np.all(res[np.isfinite(tmp)] == tmp[np.isfinite(tmp)]) assert np.all(np.isfinite(res) == np.isfinite(tmp)) def test__eq__(self): arr1 = np.array([False, False, False, True, True, True], dtype=np.bool) arr2 = np.array([1, 1, 2, 2], dtype=np.int) id_1 = 1 id_2 = 12 action1 = self.helper_action({ "change_bus": { "substations_id": [(id_1, arr1)] }, "set_bus": { "substations_id": [(id_2, arr2)] } }) action2 = self.helper_action({ "change_bus": { "substations_id": [(id_1, arr1)] }, "set_bus": { "substations_id": [(id_2, arr2)] } }) action3 = self.helper_action() assert action1 == action2 assert action1 != action3 def test_from_vect(self): arr1 = np.array([False, False, False, True, True, True], dtype=np.bool) arr2 = np.array([1, 1, 2, 2], dtype=np.int) id_1 = 1 id_2 = 12 action1 = self.helper_action({ "change_bus": { "substations_id": [(id_1, arr1)] }, "set_bus": { "substations_id": [(id_2, arr2)] } }) action2 = self.helper_action({}) vect_act1 = action1.to_vect() action2.from_vect(vect_act1) # pdb.set_trace() # if i load an action with from_vect it's equal to the original one assert action1 == action2 vect_act2 = action2.to_vect() # if i convert it back to a vector, it's equal to the original converted vector assert np.all(vect_act1[np.isfinite(vect_act2)] == vect_act2[ np.isfinite(vect_act2)]) assert np.all(np.isfinite(vect_act1) == np.isfinite(vect_act2)) def test_call_change_set(self): arr1 = np.array([False, False, False, True, True, True], dtype=np.bool) arr2 = np.array([1, 1, 2, 2], dtype=np.int) id_1 = 1 id_2 = 12 new_vect = np.random.randn(self.helper_action.n_load) new_vect2 = np.random.randn(self.helper_action.n_load) change_status_orig = np.random.randint( 0, 2, self.helper_action.n_line).astype(np.bool) set_status_orig = np.random.randint(-1, 2, self.helper_action.n_line) set_status_orig[change_status_orig] = 0 change_topo_vect_orig = np.random.randint( 0, 2, self.helper_action.dim_topo).astype(np.bool) # powerline that are set to be reconnected, can't be moved to another bus change_topo_vect_orig[self.helper_action.line_or_pos_topo_vect[ set_status_orig == 1]] = False change_topo_vect_orig[self.helper_action.line_ex_pos_topo_vect[ set_status_orig == 1]] = False # powerline that are disconnected, can't be moved to the other bus change_topo_vect_orig[self.helper_action.line_or_pos_topo_vect[ set_status_orig == -1]] = False change_topo_vect_orig[self.helper_action.line_ex_pos_topo_vect[ set_status_orig == -1]] = False set_topo_vect_orig = np.random.randint(0, 3, self.helper_action.dim_topo) set_topo_vect_orig[ change_topo_vect_orig] = 0 # don't both change and set # I need to make sure powerlines that are reconnected are indeed reconnected to a bus set_topo_vect_orig[self.helper_action.line_or_pos_topo_vect[ set_status_orig == 1]] = 1 set_topo_vect_orig[self.helper_action.line_ex_pos_topo_vect[ set_status_orig == 1]] = 1 # I need to make sure powerlines that are disconnected are not assigned to a bus set_topo_vect_orig[self.helper_action.line_or_pos_topo_vect[ set_status_orig == -1]] = 0 set_topo_vect_orig[self.helper_action.line_ex_pos_topo_vect[ set_status_orig == -1]] = 0 action = self.helper_action({ "change_bus": change_topo_vect_orig, "set_bus": set_topo_vect_orig, "injection": { "load_p": new_vect, "load_q": new_vect2 }, "change_line_status": change_status_orig, "set_line_status": set_status_orig }) dict_injection, set_status, change_status, set_topo_vect, switcth_topo_vect, redispatching = action( ) assert "load_p" in dict_injection assert np.all(dict_injection["load_p"] == new_vect) assert "load_q" in dict_injection assert np.all(dict_injection["load_q"] == new_vect2) assert np.all(set_status == set_status_orig) assert np.all(change_status == change_status_orig) assert np.all(set_topo_vect == set_topo_vect_orig) assert np.all(switcth_topo_vect == change_topo_vect_orig) def test_get_topological_impact(self): id_1 = 1 id_2 = 12 id_line = 17 id_line2 = 15 arr1 = np.array([False, False, False, True, True, True], dtype=np.bool) arr2 = np.array([1, 1, 2, 2], dtype=np.int) arr_line1 = np.full(self.helper_action.n_line, fill_value=False, dtype=np.bool) arr_line1[id_line] = True arr_line2 = np.full(self.helper_action.n_line, fill_value=0, dtype=np.int) arr_line2[id_line2] = 2 do_nothing = self.helper_action({}) aff_lines, aff_subs = do_nothing.get_topological_impact() assert np.sum(aff_lines) == 0 assert np.sum(aff_subs) == 0 act_sub1 = self.helper_action( {"change_bus": { "substations_id": [(id_1, arr1)] }}) aff_lines, aff_subs = act_sub1.get_topological_impact() assert np.sum(aff_lines) == 0 assert np.sum(aff_subs) == 1 assert aff_subs[id_1] act_sub1_sub12 = self.helper_action({ "change_bus": { "substations_id": [(id_1, arr1)] }, "set_bus": { "substations_id": [(id_2, arr2)] } }) aff_lines, aff_subs = act_sub1_sub12.get_topological_impact() assert np.sum(aff_lines) == 0 assert np.sum(aff_subs) == 2 assert aff_subs[id_1] assert aff_subs[id_2] act_sub1_sub12_line1 = self.helper_action({ "change_bus": { "substations_id": [(id_1, arr1)] }, "set_bus": { "substations_id": [(id_2, arr2)] }, "change_line_status": arr_line1 }) aff_lines, aff_subs = act_sub1_sub12_line1.get_topological_impact() assert np.sum(aff_lines) == 1 assert aff_lines[id_line] == 1 assert np.sum(aff_subs) == 2 assert aff_subs[id_1] assert aff_subs[id_2] act_sub1_sub12_line1_line2 = self.helper_action({ "change_bus": { "substations_id": [(id_1, arr1)] }, "set_bus": { "substations_id": [(id_2, arr2)] }, "change_line_status": arr_line1, "set_line_status": arr_line2 }) aff_lines, aff_subs = act_sub1_sub12_line1_line2.get_topological_impact( ) assert np.sum(aff_lines) == 2 assert aff_lines[id_line] == 1 assert aff_lines[id_line2] == 1 assert np.sum(aff_subs) == 2 assert aff_subs[id_1] assert aff_subs[id_2] def test_to_dict(self): dict_ = self.helper_action.to_dict() assert dict_ == self.res def test_from_dict(self): res = HelperAction.from_dict(self.res) assert np.all(res.name_gen == self.helper_action.name_gen) assert np.all(res.name_load == self.helper_action.name_load) assert np.all(res.name_line == self.helper_action.name_line) assert np.all(res.sub_info == self.helper_action.sub_info) assert np.all(res.load_to_subid == self.helper_action.load_to_subid) assert np.all(res.gen_to_subid == self.helper_action.gen_to_subid) assert np.all( res.line_or_to_subid == self.helper_action.line_or_to_subid) assert np.all( res.line_ex_to_subid == self.helper_action.line_ex_to_subid) assert np.all( res.load_to_sub_pos == self.helper_action.load_to_sub_pos) assert np.all(res.gen_to_sub_pos == self.helper_action.gen_to_sub_pos) assert np.all( res.line_or_to_sub_pos == self.helper_action.line_or_to_sub_pos) assert np.all( res.line_ex_to_sub_pos == self.helper_action.line_ex_to_sub_pos) assert np.all( res.load_pos_topo_vect == self.helper_action.load_pos_topo_vect) assert np.all( res.gen_pos_topo_vect == self.helper_action.gen_pos_topo_vect) assert np.all(res.line_or_pos_topo_vect == self.helper_action.line_or_pos_topo_vect) assert np.all(res.line_ex_pos_topo_vect == self.helper_action.line_ex_pos_topo_vect) # pdb.set_trace() assert np.all(res.actionClass == self.helper_action.actionClass) def test_json_serializable(self): dict_ = self.helper_action.to_dict() res = json.dumps(obj=dict_, indent=4, sort_keys=True) def test_json_loadable(self): dict_ = self.helper_action.to_dict() tmp = json.dumps(obj=dict_, indent=4, sort_keys=True) res = HelperAction.from_dict(json.loads(tmp)) assert np.all(res.name_gen == self.helper_action.name_gen) assert np.all(res.name_load == self.helper_action.name_load) assert np.all(res.name_line == self.helper_action.name_line) assert np.all(res.sub_info == self.helper_action.sub_info) assert np.all(res.load_to_subid == self.helper_action.load_to_subid) assert np.all(res.gen_to_subid == self.helper_action.gen_to_subid) assert np.all( res.line_or_to_subid == self.helper_action.line_or_to_subid) assert np.all( res.line_ex_to_subid == self.helper_action.line_ex_to_subid) assert np.all( res.load_to_sub_pos == self.helper_action.load_to_sub_pos) assert np.all(res.gen_to_sub_pos == self.helper_action.gen_to_sub_pos) assert np.all( res.line_or_to_sub_pos == self.helper_action.line_or_to_sub_pos) assert np.all( res.line_ex_to_sub_pos == self.helper_action.line_ex_to_sub_pos) assert np.all( res.load_pos_topo_vect == self.helper_action.load_pos_topo_vect) assert np.all( res.gen_pos_topo_vect == self.helper_action.gen_pos_topo_vect) assert np.all(res.line_or_pos_topo_vect == self.helper_action.line_or_pos_topo_vect) assert np.all(res.line_ex_pos_topo_vect == self.helper_action.line_ex_pos_topo_vect) assert np.all(res.actionClass == self.helper_action.actionClass) def test_as_dict(self): act = self.helper_action_env({}) dict_ = act.as_dict() assert dict_ == {} def test_to_from_vect_action(self): act = self.helper_action_env({}) vect_ = act.to_vect() act2 = self.helper_action_env.from_vect(vect_) assert act == act2 def test_sum_shape_equal_size(self): act = self.helper_action_env({}) assert act.size() == np.sum(act.shape()) def test_shape_correct(self): act = self.helper_action_env({}) assert act.shape().shape == act.dtype().shape def test_redispatching(self): act = self.helper_action_env({"redispatch": [1, 10]}) act = self.helper_action_env({"redispatch": [(1, 10), (2, 100)]}) act = self.helper_action_env( {"redispatch": np.array([10, 20, 30, 40, 50])})
def __init__(self, action_space): HelperAction.__init__(self, action_space, action_space.legal_action, action_space.subtype) self.space_prng = action_space.space_prng self.seed_used = action_space.seed_used
def setUp(self): """ The case file is a representation of the case14 as found in the ieee14 powergrid. :return: """ # from ADNBackend import ADNBackend # self.backend = ADNBackend() # self.path_matpower = "/home/donnotben/Documents/RL4Grid/RL4Grid/data" # self.case_file = "ieee14_ADN.xml" # self.backend.load_grid(self.path_matpower, self.case_file) self.tolvect = 1e-2 self.tol_one = 1e-5 self.game_rules = GameRules() self.gridobj = GridObjects() self.gridobj.init_grid_vect(name_prod=["gen_{}".format(i) for i in range(5)], name_load=["load_{}".format(i) for i in range(11)], name_line=["line_{}".format(i) for i in range(20)], name_sub=["sub_{}".format(i) for i in range(14)], sub_info=np.array([3, 6, 4, 6, 5, 6, 3, 2, 5, 3, 3, 3, 4, 3], dtype=np.int), load_to_subid=np.array([1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13]), gen_to_subid=np.array([0, 1, 2, 5, 7]), line_or_to_subid=np.array([ 0, 0, 1, 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 8, 8, 9, 11, 12]), line_ex_to_subid=np.array([ 1, 4, 2, 3, 4, 3, 4, 6, 8, 5, 10, 11, 12, 7, 8, 9, 13, 10, 12, 13]), ##### load_to_sub_pos=np.array([4, 2, 5, 4, 4, 4, 1, 1, 1, 2, 1]), gen_to_sub_pos=np.array([2, 5, 3, 5, 1]), line_or_to_sub_pos=np.array([0, 1, 1, 2, 3, 1, 2, 3, 4, 3, 1, 2, 3, 1, 2, 2, 3, 0, 0, 1]), line_ex_to_sub_pos=np.array([0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 2, 2, 3, 0, 1, 2, 2, 0, 0, 0]), ##### load_pos_topo_vect=np.array([ 7, 11, 18, 23, 28, 39, 41, 44, 47, 51, 54]), gen_pos_topo_vect=np.array([ 2, 8, 12, 29, 34]), line_or_pos_topo_vect=np.array([ 0, 1, 4, 5, 6, 10, 15, 16, 17, 22, 25, 26, 27, 31, 32, 37, 38, 40, 46, 50]), line_ex_pos_topo_vect=np.array([ 3, 19, 9, 13, 20, 14, 21, 30, 35, 24, 45, 48, 52, 33, 36, 42, 55, 43, 49, 53])) # pdb.set_trace() self.helper_action = HelperAction(self.gridobj, game_rules=self.game_rules) self.helper_action_env = HelperAction(self.gridobj, game_rules=self.game_rules, actionClass=Action) self.res = {'name_gen': ['gen_0', 'gen_1', 'gen_2', 'gen_3', 'gen_4'], 'name_load': ['load_0', 'load_1', 'load_2', 'load_3', 'load_4', 'load_5', 'load_6', 'load_7', 'load_8', 'load_9', 'load_10'], 'name_line': ['line_0', 'line_1', 'line_2', 'line_3', 'line_4', 'line_5', 'line_6', 'line_7', 'line_8', 'line_9', 'line_10', 'line_11', 'line_12', 'line_13', 'line_14', 'line_15', 'line_16', 'line_17', 'line_18', 'line_19'], 'name_sub': ["sub_0", "sub_1", "sub_2", "sub_3", "sub_4", "sub_5", "sub_6", "sub_7", "sub_8", "sub_9", "sub_10", "sub_11", "sub_12", "sub_13"], 'sub_info': [3, 6, 4, 6, 5, 6, 3, 2, 5, 3, 3, 3, 4, 3], 'load_to_subid': [1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13], 'gen_to_subid': [0, 1, 2, 5, 7], 'line_or_to_subid': [0, 0, 1, 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 8, 8, 9, 11, 12], 'line_ex_to_subid': [1, 4, 2, 3, 4, 3, 4, 6, 8, 5, 10, 11, 12, 7, 8, 9, 13, 10, 12, 13], 'load_to_sub_pos': [4, 2, 5, 4, 4, 4, 1, 1, 1, 2, 1], 'gen_to_sub_pos': [2, 5, 3, 5, 1], 'line_or_to_sub_pos': [0, 1, 1, 2, 3, 1, 2, 3, 4, 3, 1, 2, 3, 1, 2, 2, 3, 0, 0, 1], 'line_ex_to_sub_pos': [0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 2, 2, 3, 0, 1, 2, 2, 0, 0, 0], 'load_pos_topo_vect': [7, 11, 18, 23, 28, 39, 41, 44, 47, 51, 54], 'gen_pos_topo_vect': [2, 8, 12, 29, 34], 'line_or_pos_topo_vect': [0, 1, 4, 5, 6, 10, 15, 16, 17, 22, 25, 26, 27, 31, 32, 37, 38, 40, 46, 50], 'line_ex_pos_topo_vect': [3, 19, 9, 13, 20, 14, 21, 30, 35, 24, 45, 48, 52, 33, 36, 42, 55, 43, 49, 53], 'subtype': 'Action.Action'}
def _action_env_setup(self): return HelperAction(self.gridobj, legal_action=self.game_rules.legal_action, actionClass=PowerLineSet)
def _action_env_setup(self): return HelperAction(self.gridobj, legal_action=self.game_rules.legal_action, actionClass=TopoAndRedispAction)