def test_reconnect_disconnected(self): with warnings.catch_warnings(): warnings.filterwarnings("ignore") params = Parameters() params.MAX_SUB_CHANGED = 0 params.NO_OVERFLOW_DISCONNECTION = True env_case2 = make("rte_case5_example", test=True, param=params) obs = env_case2.reset() # reset is good line_id = 5 # Disconnect the line disco_act = env_case2.action_space.disconnect_powerline( line_id=line_id) obs, reward, done, info = env_case2.step(disco_act) # Line has been disconnected assert info["is_illegal"] == False assert done == False assert np.sum(obs.line_status) == (env_case2.n_line - 1) # Reconnect the line reco_act = env_case2.action_space.reconnect_powerline(line_id=line_id, bus_or=1, bus_ex=2) obs, reward, done, info = env_case2.step(reco_act) # Check reconnecting is legal assert info["is_illegal"] == False assert done == False # Check line has been reconnected assert np.sum(obs.line_status) == (env_case2.n_line)
def test_change_parameters_forecast_fromissue_128(self): """ this is basic test of the env.change_forecast_parameters() function It only checks the right parameters are used for the environment (or obs_env) but it do not currently check the observation (with the cooldown for example) This is the example taken from https://github.com/rte-france/Grid2Op/issues/128 (first remak) """ # modify the parmeters for simulate param = Parameters() param.MAX_SUB_CHANGED = 9999 param.MAX_LINE_STATUS_CHANGED = 9999 self.env1.change_forecast_parameters(param) self.env1.reset() obs, *_ = self.env1.step(self.env1.action_space()) # and you can simulate the impact of action modifying as many substation as you want, for example act = self.env1.action_space( {"set_bus": { "lines_or_id": [(0, 2), (10, 2)] }}) # this action modfies both substation 0 and substation 8 sim_o, sim_r, sim_d, sim_info = obs.simulate(act) assert sim_info["is_illegal"] is False
def test_creation_with_params(self): p = Parameters() p.MAX_SUB_CHANGED = 666 mme = MultiMixEnvironment(PATH_DATA_MULTIMIX, param=p) assert mme.current_obs is not None assert mme.current_env is not None assert mme.parameters.MAX_SUB_CHANGED == 666
def setUp(self): with warnings.catch_warnings(): warnings.filterwarnings("ignore") params = Parameters() params.MAX_SUB_CHANGED = 2 self.env = make("rte_case14_test", test=True, chronics_class=ChangeNothing, param=params, backend=self.make_backend())
def test_change_parameters_forecast(self): """ this is basic test of the env.change_forecast_parameters() function It only checks the right parameters are used for the environment (or obs_env) but it do not currently check the observation (with the cooldown for example) """ param = Parameters() param.NO_OVERFLOW_DISCONNECTION = True param.MAX_SUB_CHANGED = 5 param.MAX_LINE_STATUS_CHANGED = 5 param.NB_TIMESTEP_COOLDOWN_LINE = 5 param.NB_TIMESTEP_COOLDOWN_SUB = 5 param.ENV_DC = True real_orig_param = copy.deepcopy(self.env1.parameters) self.env1.change_forecast_parameters(param) # in these first checks, parameters are not modified self._check_env_param(self.env1, real_orig_param) self._check_env_param(self.env1._observation_space.obs_env, real_orig_param) obs, *_ = self.env1.step(self.env1.action_space()) _ = obs.simulate(self.env1.action_space()) self._check_env_param(self.env1, real_orig_param) self._check_env_param(self.env1._observation_space.obs_env, real_orig_param) # reset triggers the modification obs = self.env1.reset() _ = obs.simulate(self.env1.action_space()) self._check_env_param(self.env1, real_orig_param) self._check_env_param(self.env1._observation_space.obs_env, param)
def setUp(self): with warnings.catch_warnings(): warnings.filterwarnings("ignore") params = Parameters() params.MAX_SUB_CHANGED = 2 self.env = make("case14_test", chronics_class=ChangeNothing, param=params)
def get_parameters(): # generate the environments parameters param = Parameters() param.NO_OVERFLOW_DISCONNECTION = True param.NB_TIMESTEP_COOLDOWN_LINE = 0 param.NB_TIMESTEP_COOLDOWN_SUB = 0 param.MAX_SUB_CHANGED = 99999 param.MAX_LINE_STATUS_CHANGED = 99999 param.NB_TIMESTEP_COOLDOWN_SUB = 0 return param
def setUp(self): with warnings.catch_warnings(): warnings.filterwarnings("ignore") params = Parameters() params.MAX_SUB_CHANGED = 0 params.NB_TIMESTEP_POWERFLOW_ALLOWED = 2 rules = DefaultRules self.env = make("rte_case14_test", test=True, chronics_class=ChangeNothing, param=params, gamerules_class=rules)
def test_sub_dont_change(self): """test that i cannot reconect a powerline by acting on the substation""" with warnings.catch_warnings(): warnings.filterwarnings("ignore") params = Parameters() params.MAX_SUB_CHANGED = 1 params.MAX_LINE_STATUS_CHANGED = 1 params.NB_TIMESTEP_COOLDOWN_LINE = 3 params.NB_TIMESTEP_COOLDOWN_SUB = 3 params.NO_OVERFLOW_DISCONNECTION = True env = make("rte_case5_example", test=True, param=params) l_id = 2 # prepare the actions disco_act = env.action_space.disconnect_powerline(line_id=l_id) reco_act = env.action_space.reconnect_powerline(line_id=l_id, bus_or=1, bus_ex=1) set_or_1_act = env.action_space({"set_bus": {"lines_or_id": [(l_id, 1)]}}) set_ex_1_act = env.action_space({"set_bus": {"lines_ex_id": [(l_id, 1)]}}) obs, reward, done, info = env.step(disco_act) assert obs.rho[l_id] == 0. assert obs.time_before_cooldown_line[l_id] == 3 assert env.backend._grid.line.iloc[l_id]["in_service"] == False # i have a cooldown to 2 so i cannot reconnect it assert obs.time_before_cooldown_line[l_id] == 3 obs, reward, done, info = env.step(reco_act) assert obs.rho[l_id] == 0. assert info["is_illegal"] assert obs.time_before_cooldown_line[l_id] == 2 assert env.backend._grid.line.iloc[l_id]["in_service"] == False # this is not supposed to reconnect it either (cooldown) assert obs.time_before_cooldown_line[l_id] == 2 obs, reward, done, info = env.step(set_or_1_act) # pdb.set_trace() # assert info["is_illegal"] assert env.backend._grid.line.iloc[l_id]["in_service"] == False assert obs.rho[l_id] == 0. assert obs.time_before_cooldown_line[l_id] == 1 assert env.backend._grid.line.iloc[l_id]["in_service"] == False # and neither is that (cooldown) assert obs.time_before_cooldown_line[l_id] == 1 obs, reward, done, info = env.step(set_ex_1_act) assert obs.rho[l_id] == 0. assert obs.time_before_cooldown_line[l_id] == 0 assert env.backend._grid.line.iloc[l_id]["in_service"] == False # and now i can reconnect obs, reward, done, info = env.step(reco_act) assert obs.rho[l_id] != 0. assert obs.time_before_cooldown_line[l_id] == 3 assert env.backend._grid.line.iloc[l_id]["in_service"] == True
def setUp(self) -> None: # init the environment param = Parameters() param.NO_OVERFLOW_DISCONNECTION = True # i can act on all powerline / substation at once param.MAX_LINE_STATUS_CHANGED = 999999 param.MAX_SUB_CHANGED = 999999 # i can act every step on every line / substation (no cooldown) param.NB_TIMESTEP_COOLDOWN_LINE = 0 param.NB_TIMESTEP_COOLDOWN_SUB = 0 self.env = grid2op.make("l2rpn_case14_sandbox", param=param) self.obs = self.env.reset()
def test_change_parameters_basic(self): """ this is basic test of the env.change_parameters() function It only checks the right parameters are used for the environment but it do not currently check the observation (with the cooldown for example) """ param = Parameters() param.NO_OVERFLOW_DISCONNECTION = True param.MAX_SUB_CHANGED = 5 param.MAX_LINE_STATUS_CHANGED = 5 param.NB_TIMESTEP_COOLDOWN_LINE = 5 param.NB_TIMESTEP_COOLDOWN_SUB = 5 real_orig_param = copy.deepcopy(self.env1.parameters) self.env1.change_parameters(param) # parameters have not changed yet _ = self.env1.step(self.env1.action_space()) self._check_env_param(self.env1, real_orig_param) # reset triggers the change of parameters self.env1.reset() self._check_env_param(self.env1, param)
#use lightsim2grid to go a lot faster if available try: from lightsim2grid.LightSimBackend import LightSimBackend backend = LightSimBackend() except: from grid2op.Backend import PandaPowerBackend backend = PandaPowerBackend() print( "You might need to install the LightSimBackend (provisory name) to gain massive speed up" ) print('MultiTopology') params = Parameters() params.init_from_dict({"NO_OVERFLOW_DISCONNECTION": True}) params.MAX_SUB_CHANGED = 999 #to have unlimited sub actions at a timestep params.MAX_LINE_STATUS_CHANGED = 999 #to have unlimited line actions at a timestep path_save = "grid2viz/data/agents/multiTopology-baseline" os.makedirs(path_save, exist_ok=True) with make(dataset, param=params, backend=backend) as env: agent = MultipleTopologyAgent(env.action_space, env.observation_space) runner = Runner(**env.get_params_for_runner(), agentClass=None, agentInstance=agent) #need to be seeded for reproducibility as this takes random redispatching actions runner.run(nb_episode=1, path_save="grid2viz/data/agents/multiTopology-baseline", nb_process=1, max_iter=30, env_seeds=[0],
def generate_dataset(name_env, nb_rows, dir_out="training_dataset", agent_type="do_nothing", expe_type="powerline", verbose=True, **kwargsagent): # TODO seed for reproductible expriments # TODO remove thermal limits param = Parameters() if isinstance(name_env, str): env_tmp = grid2op.make(dataset=name_env, gamerules_class=AlwaysLegal) param = env_tmp.parameters param.NO_OVERFLOW_DISCONNECTION = True # i can act on all powerline / substation at once param.MAX_LINE_STATUS_CHANGED = 999999 param.MAX_SUB_CHANGED = 999999 # i can act every step on every line / substation (no cooldown) param.NB_TIMESTEP_COOLDOWN_LINE = 0 param.NB_TIMESTEP_COOLDOWN_SUB = 0 env = grid2op.make(dataset=name_env, gamerules_class=AlwaysLegal, param=param) else: raise NotImplementedError( "Unknwown environment! Please provide an environment name.") if isinstance(agent_type, str): agent = get_agent(env, agent_type, **kwargsagent) else: if isinstance(agent_type, BaseAgent): # the agent is already provided agent = agent_type else: raise NotImplementedError( "agent_type should be a string or a grid2op agent !") dir_out_abs = os.path.abspath(dir_out) if not os.path.exists(dir_out_abs): os.mkdir(dir_out_abs) # input X prod_p = np.full((nb_rows, env.n_gen), fill_value=np.NaN, dtype=dt_float) prod_v = np.full((nb_rows, env.n_gen), fill_value=np.NaN, dtype=dt_float) load_p = np.full((nb_rows, env.n_load), fill_value=np.NaN, dtype=dt_float) load_q = np.full((nb_rows, env.n_load), fill_value=np.NaN, dtype=dt_float) # input tau if expe_type == "powerline": tau = np.full((nb_rows, env.n_line), fill_value=np.NaN, dtype=dt_int) elif expe_type == "topo": raise RuntimeError("This is not coded at the moment.") tau = np.full((nb_rows, env.dim_topo), fill_value=np.NaN, dtype=dt_int) else: raise NotImplementedError() # output flow_a = np.full((nb_rows, env.n_line), fill_value=np.NaN, dtype=dt_float) line_v = np.full((nb_rows, env.n_line), fill_value=np.NaN, dtype=dt_float) flow_p = np.full((nb_rows, env.n_line), fill_value=np.NaN, dtype=dt_float) flow_q = np.full((nb_rows, env.n_line), fill_value=np.NaN, dtype=dt_float) obs = env.reset() reward = env.reward_range[0] done = False t = 0 if verbose: pbar = tqdm(total=nb_rows) while t < nb_rows: act = agent.act(obs, reward, done) obs, reward, done, info = env.step(act) if done: # action was not valid, i restart # TODO random chronics # TODO random begining of start obs = env.reset() reward = env.reward_range[0] done = False continue # so action is valid, i store the results prod_p[t, :] = obs.prod_p prod_v[t, :] = obs.prod_v load_p[t, :] = obs.load_p load_q[t, :] = obs.load_q if expe_type == "powerline": tau[t, :] = 1 - obs.line_status # 0 = connected, 1 = disconnected elif expe_type == "topo": tau[t, :] = obs.topo_vect - 1 # 0 = on bus 1, 1 = on bus 2 flow_a[t, :] = obs.a_or line_v[t, :] = obs.v_or flow_p[t, :] = obs.p_or flow_q[t, :] = obs.q_or t += 1 if verbose: pbar.update(1) if verbose: pbar.close() for arr_, arr_n in zip( [prod_p, prod_v, load_p, load_q, tau, flow_a, flow_p, flow_q, line_v], [ "prod_p", "prod_v", "load_p", "load_q", "tau", "flow_a", "flow_p", "flow_q", "line_v" ]): tmp_nm = os.path.join(dir_out_abs, "{}.npy".format(arr_n)) np.save(file=tmp_nm, arr=arr_)
save_path, num_pre_training_steps, logs_path) if __name__ == "__main__": from grid2op.MakeEnv import make from grid2op.Reward import * from grid2op.Action import * from grid2op.Parameters import Parameters import sys args = cli() # Use custom params params = Parameters() params.MAX_SUB_CHANGED = 2 # Create grid2op game environement env = make(args.data_dir, param=params, action_class=TopologyChangeAndDispatchAction, reward_class=CombinedScaledReward) # Only load 128 steps in ram env.chronics_handler.set_chunk_size(128) # Register custom reward for training cr = env.reward_helper.template_reward cr.addReward("overflow", CloseToOverflowReward(), 50.0) cr.addReward("game", GameplayReward(), 200.0) cr.addReward("recolines", LinesReconnectedReward(), 50.0)