Exemplo n.º 1
0
    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()
Exemplo n.º 2
0
    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
        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
        obs, reward, done, info = env.step(set_or_1_act)
        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
        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
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
#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],
               agent_seeds=[0],
Exemplo n.º 6
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_)