jhmenke / grid2op_pp_baseline

Baseline agent for usage of pandapower in Grid2Op
BSD 3-Clause "New" or "Revised" License
5 stars 2 forks source link

Compartible with l2rpn_neurips_2020_track1? (AssertionError) #5

Closed Alwinator closed 2 years ago

Alwinator commented 4 years ago

Is the PandaPowerOPFAgent compatible with the new l2rpn_neurips_2020_track1 environment?

I created a small submission for testing purposes:

__init__.py

from .my_baseline import make_agent

my_baseline.py

from l2rpn_baselines.PandapowerOPFAgent import PandapowerOPFAgent

def make_agent(env, submission_dir):
    agent = PandapowerOPFAgent(env.action_space, env.init_grid_path)
    return agent

Afterwards I run check_submission.py. I got the following error:

Traceback (most recent call last):
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 306, in <module>
    main()
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 282, in main
    agent_seeds=agent_seeds,
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 1076, in run
    env_seeds=env_seeds, max_iter=max_iter, agent_seeds=agent_seeds)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 819, in _run_sequential
    max_iter=max_iter)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 592, in run_one_episode
    pbar=pbar, env_seed=env_seed, max_iter=max_iter, agent_seed=agent_seed)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 694, in _run_one_episode
    act = agent.act(obs, reward, done)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/PandapowerOPFAgent.py", line 122, in act
    self.parse_observation_to_grid(observation)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/PandapowerOPFAgent.py", line 68, in parse_observation_to_grid
    assert len(self.grid.ext_grid) == 1 and len(obs.gen_type) == len(self.grid.gen) + len(self.grid.ext_grid)
AssertionError

Thank you in advance!

jhmenke commented 4 years ago

I haven't tested it due to lack of time unfortunately. Can you post the individual values?

len(self.grid.ext_grid), len(obs.gen_type), len(self.grid.gen), len(self.grid.ext_grid)

Probably only the parse function needs to be modified slightly.

Alwinator commented 4 years ago

len(self.grid.ext_grid) = 0 len(obs.gen_type) = 22 len(self.grid.gen) = 22

jhmenke commented 4 years ago

If you change it to two separate asserts, does it work?

assert len(self.grid.ext_grid) == 1 or self.grid.gen.slack.any() assert len(obs.gen_type) == len(self.grid.gen) + len(self.grid.ext_grid)

Basically the external grid as a reference is missing, but this could just be a generator. If any generator has the attribute "slack" set to True, then the rest of the code should be fine. If ext_grid is referenced later in the code, it should be replaced by a loc to the res_gen.loc[res_gen.slack]. I can do it later this week.

Alwinator commented 4 years ago

Now the assertion works, but I get a new error:

Traceback (most recent call last):
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 306, in <module>
    main()
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 282, in main
    agent_seeds=agent_seeds,
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 1076, in run
    env_seeds=env_seeds, max_iter=max_iter, agent_seeds=agent_seeds)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 819, in _run_sequential
    max_iter=max_iter)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 592, in run_one_episode
    pbar=pbar, env_seed=env_seed, max_iter=max_iter, agent_seed=agent_seed)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 694, in _run_one_episode
    act = agent.act(obs, reward, done)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/PandapowerOPFAgent.py", line 125, in act
    self.parse_observation_to_grid(observation)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/PandapowerOPFAgent.py", line 75, in parse_observation_to_grid
    self.grid.gen.type = obs.gen_type[:-1]
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/pandas/core/generic.py", line 5303, in __setattr__
    self[name] = value
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/pandas/core/frame.py", line 2938, in __setitem__
    self._set_item(key, value)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/pandas/core/frame.py", line 3000, in _set_item
    value = self._sanitize_column(key, value)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/pandas/core/frame.py", line 3636, in _sanitize_column
    value = sanitize_index(value, self.index, copy=False)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/pandas/core/internals/construction.py", line 611, in sanitize_index
    raise ValueError("Length of values does not match length of index")
ValueError: Length of values does not match length of index
jhmenke commented 4 years ago

Yes the whole code is written under the assumption that there is an ext_grid available.

self.grid.gen.type = obs.gen_type[:-1]

This should now be

self.grid.gen.type = obs.gen_type

etc. There are some other things to change, but i can modify it tomorrow at the earliest.

Alwinator commented 4 years ago

There are some other things to change, but i can modify it tomorrow at the earliest.

Thank you! I'll test it tomorrow again!

jhmenke commented 4 years ago

Can you try with the current commit?

Alwinator commented 4 years ago
Traceback (most recent call last):
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 306, in <module>
    main()
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 282, in main
    agent_seeds=agent_seeds,
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 1076, in run
    env_seeds=env_seeds, max_iter=max_iter, agent_seeds=agent_seeds)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 819, in _run_sequential
    max_iter=max_iter)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 592, in run_one_episode
    pbar=pbar, env_seed=env_seed, max_iter=max_iter, agent_seed=agent_seed)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 694, in _run_one_episode
    act = agent.act(obs, reward, done)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/PandapowerOPFAgent.py", line 169, in act
    p_dispatched_current = np.array(self.grid.gen.p_mw.tolist() + self.grid.ext_grid.p_mw.tolist())
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/pandas/core/generic.py", line 5274, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'p_mw'
jhmenke commented 4 years ago

now?

Alwinator commented 4 years ago

Unfortunately, yes.

jhmenke commented 4 years ago

i mean with my latest commit, does it still appear?

Alwinator commented 4 years ago

Yes

jhmenke commented 4 years ago

i don't have the new competition setup unfortunately, so i cannot easily try myself. but try again now please.

Alwinator commented 4 years ago
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed 
OPF failed
Traceback (most recent call last):
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 306, in <module>
    main()
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 282, in main
    agent_seeds=agent_seeds,
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 1076, in run
    env_seeds=env_seeds, max_iter=max_iter, agent_seeds=agent_seeds)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 819, in _run_sequential
    max_iter=max_iter)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 592, in run_one_episode
    pbar=pbar, env_seed=env_seed, max_iter=max_iter, agent_seed=agent_seed)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 694, in _run_one_episode
    act = agent.act(obs, reward, done)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/PandapowerOPFAgent.py", line 150, in act
    if line_state["indisponibility"] > 0:
KeyError: 'indisponibility'
jhmenke commented 4 years ago

OPF failed is not an error, it just means that the OPF cannot solve this problem or some inputs are not parsed correctly.

About the later error, according to the docs of state_of, the key should exist so it seems to be an issue with Grid2Op. https://grid2op.readthedocs.io/en/latest/observation.html#grid2op.Observation.BaseObservation.state_of

Alwinator commented 4 years ago

Should I send you the check_your_submission.py script? Then you can better check the code.

jhmenke commented 4 years ago

You can ask in the Grid2Op Discord why indisponibility does not exist and if that is intended, then i can fix it accordingly.

I cannot really do anything about the OPF failed, that's part of the challenge i guess to make it run.

Alwinator commented 4 years ago

I also reported the issue on the Discord server.

Tezirg commented 4 years ago

indisponibility does not exists anymore, the docstring is still present tough. The correct key to use is cooldown_time

jhmenke commented 4 years ago

okay, replaced it with cooldown_time

jhmenke commented 4 years ago

If you export the grid with pp.to_json(...) right before the OPF is called (when it fails), i can take a look tomorrow if there's anything done to easily solve it, but i'm afraid that's all the time i can spend on it.

Alwinator commented 4 years ago

I mean the exception is gone, but still OPF failed all the time.

Alwinator commented 4 years ago

If you export the grid with pp.to_json(...) right before the OPF is called (when it fails), i can take a look tomorrow if there's anything done to easily solve it, but i'm afraid that's all the time i can spend on it.

grid.json

I also get the following messages:

OPF failed
...
Observation: Line 18 out of service!
...
Observation: Line 13 out of service!
...
Observation: Line 14 out of service!
...
Observation: Line 9 out of service!
...
Observation: Line 40 out of service!
...
Observation: Line 41 out of service!
...
Observation: Line 45 out of service!
...
Observation: Line 23 out of service!
...
Observation: Line 27 out of service!
...
Observation: Line 39 out of service!
...
Observation: trafo 1 out of service!
jhmenke commented 4 years ago

That's likely a cascading failure because the first issue was not handled correctly.

You can try with

import pandapower.toolbox as ppt ppt.opf_task(self.grid)

before the OPF, maybe it can give you some information about what's wrong.

Alwinator commented 4 years ago

Now I also tried it with PowerModels and it works for approximately 1 minute without OPF failed, but then it caches:

Traceback (most recent call last):
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 306, in <module>
    main()
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 282, in main
    agent_seeds=agent_seeds,
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 1076, in run
    env_seeds=env_seeds, max_iter=max_iter, agent_seeds=agent_seeds)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 819, in _run_sequential
    max_iter=max_iter)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 592, in run_one_episode
    pbar=pbar, env_seed=env_seed, max_iter=max_iter, agent_seed=agent_seed)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 694, in _run_one_episode
    act = agent.act(obs, reward, done)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/PandapowerOPFAgent.py", line 202, in act
    action_space["redispatch"] = [(idx, p) for idx, p in zip(np.arange(len(self.grid.gen) + 1)[gen_p_redispatched],
IndexError: boolean index did not match indexed array along dimension 0; dimension is 23 but corresponding boolean dimension is 22
jhmenke commented 4 years ago

okay that should be fixed now

Alwinator commented 4 years ago

The error above is fixed, but I get get the following new error:

Traceback (most recent call last):
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 306, in <module>
    main()
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 282, in main
    agent_seeds=agent_seeds,
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 1076, in run
    env_seeds=env_seeds, max_iter=max_iter, agent_seeds=agent_seeds)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 819, in _run_sequential
    max_iter=max_iter)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 592, in run_one_episode
    pbar=pbar, env_seed=env_seed, max_iter=max_iter, agent_seed=agent_seed)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 694, in _run_one_episode
    act = agent.act(obs, reward, done)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/PandapowerOPFAgent.py", line 175, in act
    self.opf_type.lower(), logger=self.logger)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/pp_functions.py", line 55, in run_opf
    pp.runpm_ots(grid, pm_nl_solver="ipopt", pm_model="ACPPowerModel")  # PowerModels.jl OPF
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/pandapower/runpm.py", line 320, in runpm_ots
    _runpm(net)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/pandapower/opf/run_powermodels.py", line 38, in _runpm
    read_pm_results_to_net(net, ppc, ppci, result_pm)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/pandapower/converter/powermodels/from_pm.py", line 18, in read_pm_results_to_net
    result, multinetwork = pm_results_to_ppc_results(net, ppc, ppci, result_pm)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/pandapower/converter/powermodels/from_pm.py", line 51, in pm_results_to_ppc_results
    for i, bus in sol["bus"].items():
KeyError: 'bus'
jhmenke commented 4 years ago

That's an issue i have no idea about, you can post in the pandapower Github, but will probably not receive a response in the next days, so you would have to debug yourself.

Alwinator commented 4 years ago

Hello @jhmenke, I am back. The bug in pandapower should now be fixed with the latest commit in the develop branch. Now the PandaPowerOPFAgent need some changes, because pp.runpm_ots function can throw an OPFNotConverged Exception. I tried to improve your code as following:

(pp_functions.py)

try:
    pp.runpm_ots(grid, pm_nl_solver="ipopt", pm_model="ACPPowerModel")  # PowerModels.jl OPF
    grid.line.loc[:, "in_service"] = grid.res_line.loc[:, "in_service"].values.astype(bool)
    grid.trafo.loc[:, "in_service"] = grid.res_trafo.loc[:, "in_service"].values.astype(bool)
except OPFNotConverged:
    pass

But still, after around 30 minutes I get an exception:

------------------------------------
        Detailed error Logs         
------------------------------------
Traceback (most recent call last):
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 306, in <module>
    main()
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 282, in main
    agent_seeds=agent_seeds,
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 1076, in run
    env_seeds=env_seeds, max_iter=max_iter, agent_seeds=agent_seeds)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 819, in _run_sequential
    max_iter=max_iter)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 592, in run_one_episode
    pbar=pbar, env_seed=env_seed, max_iter=max_iter, agent_seed=agent_seed)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 694, in _run_one_episode
    act = agent.act(obs, reward, done)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/PandapowerOPFAgent.py", line 236, in act
    assert not denied, f"{reason} {action}"
AssertionError: Grid2OpException AmbiguousAction InvalidRedispatching "Some redispatching amount are bellow the maximum ramp down" This action will:
     - NOT change anything to the injections
     - Redispatch gen_41_19 of -10.400001525878906
     - Redispatch gen_45_20 of 1.309594750404358
     - Redispatch gen_54_26 of 2.7095935344696045
     - Redispatch gen_55_29 of 2.7095935344696045
     - Redispatch gen_64_35 of -2.79998779296875
     - Redispatch gen_68_37 of 6.471207618713379
     - force reconnection of 1 powerlines ([34])
     - force disconnection of 1 powerlines ([57])
     - NOT switch any line status
     - NOT switch anything in the topology
     - Set the bus of the following element:
         - assign bus 1 to line (origin) 34 [on substation 23]
         - assign bus 1 to line (extremity) 34 [on substation 25]
------------------------------------
      End Detailed error Logs       
------------------------------------

Could you please take a look what is wrong.

Thank you in advance!

jhmenke commented 4 years ago

Can you check what the ramp_down limits are for gen with name gen_41_19? It's probably -10. but somehow PowerModels chose -10.4 despite it being illegal.

edit: If that is the case, you need to np.clip the redispatch vector into the range of the ramp up / down and then assign the residual power to a generator that is not near it's ramp limits. This should happen in https://github.com/jhmenke/grid2op_pp_baseline/blob/2656137da11c4163fa7ef3757aae350d27f8eb65/PandapowerOPFAgent.py#L194 ff but it seems there could be a bug.

Actually, this selects the first ramped down generator, which may not be the best choice always. You can change it so that the generator with the least ramp down is chosen instead:

https://github.com/jhmenke/grid2op_pp_baseline/blob/2656137da11c4163fa7ef3757aae350d27f8eb65/PandapowerOPFAgent.py#L199

power_down = np.argmax(p_redispatched[np.where(p_redispatched < 0)])

Alwinator commented 4 years ago

Thanks, but then I get the following:

ERROR: ingestion program failed with error: 
object of type 'numpy.int64' has no len()

------------------------------------
        Detailed error Logs         
------------------------------------
Traceback (most recent call last):
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 306, in <module>
    main()
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 282, in main
    agent_seeds=agent_seeds,
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 1076, in run
    env_seeds=env_seeds, max_iter=max_iter, agent_seeds=agent_seeds)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 819, in _run_sequential
    max_iter=max_iter)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 592, in run_one_episode
    pbar=pbar, env_seed=env_seed, max_iter=max_iter, agent_seed=agent_seed)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 694, in _run_one_episode
    act = agent.act(obs, reward, done)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/PandapowerOPFAgent.py", line 200, in act
    p_redispatched[power_down] -= redispatch_sum / len(power_down)
TypeError: object of type 'numpy.int64' has no len()
------------------------------------
      End Detailed error Logs       
------------------------------------
jhmenke commented 4 years ago

So you can confirm that is the problem in the first place?

this should work then (if you confirm then i will push a fix)

if redispatch_sum > 0.:
    power_up = np.argmin(p_redispatched[np.where(p_redispatched > 0)])
    p_redispatched[power_up] -= redispatch_sum
elif redispatch_sum < 0.:
    power_down = np.argmax(p_redispatched[np.where(p_redispatched < 0)])
    p_redispatched[power_down] -= redispatch_sum
Alwinator commented 4 years ago

So you can confirm that is the problem in the first place?

Yes, the p_redispatched includes a -10.3999939.

this should work then (if you confirm then i will push a fix)

Thank you, I will try it, but it takes time. Last time it took 50 minutes until the error appeared.

Alwinator commented 4 years ago

Unfortunately it crashed in the next line:

assert abs(p_redispatched[gen_p_redispatched].sum()) < 1e-14

abs(p_redispatched[gen_p_redispatched].sum()) = -1.3294594621022853

Traceback (most recent call last):
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 306, in <module>
    main()
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 282, in main
    agent_seeds=agent_seeds,
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 1076, in run
    env_seeds=env_seeds, max_iter=max_iter, agent_seeds=agent_seeds)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 819, in _run_sequential
    max_iter=max_iter)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 592, in run_one_episode
    pbar=pbar, env_seed=env_seed, max_iter=max_iter, agent_seed=agent_seed)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 694, in _run_one_episode
    act = agent.act(obs, reward, done)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/PandapowerOPFAgent.py", line 204, in act
    assert abs(p_redispatched[gen_p_redispatched].sum()) < 1e-14
AssertionError
jhmenke commented 4 years ago

That's weird since the overall sum should still be the same. It's probably best to debug this right in the code at the point of failure. The sum of p_redispatched[gen_p_redispatched] should be zero for a valid redispatch action.

Alwinator commented 4 years ago

Ohh sorry I forgot the abs(). The result is +1.3294594621022853, but still there is the AssertionError

jhmenke commented 4 years ago

Doesn't matter,

if redispatch_sum > 0.: power_up = np.argmin(p_redispatched[np.where(p_redispatched > 0)]) p_redispatched[power_up] -= redispatch_sum elif redispatch_sum < 0.: power_down = np.argmax(p_redispatched[np.where(p_redispatched < 0)]) p_redispatched[power_down] -= redispatch_sum

This should make the sum 0., maybe -= has to be switched to += or something like that.. as i said, you can probably solve it easily in the debugger

Alwinator commented 4 years ago

Unfortunately I did not find the problem. Could you please fix the problems. If you want I can send you all scripts you need. Thanks you in advance!

jhmenke commented 4 years ago

Can you export the p_redispatched, redispatch_sum and gen_p_redispatched variables via numpy.save(...) right after line 194 (when the sum is computed)? Then i could take a look.

Alwinator commented 4 years ago

Thank you!

This is the archive including the three numpy files: github.tar.gz

jhmenke commented 4 years ago

please see the latest commit

Alwinator commented 4 years ago

After 50 minutes:

Traceback (most recent call last):
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 306, in <module>
    main()
  File "/home/alwin/PycharmProjects/l2rpn-challenge/submission/utils/ingestion_program_local//ingestion.py", line 282, in main
    agent_seeds=agent_seeds,
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 1076, in run
    env_seeds=env_seeds, max_iter=max_iter, agent_seeds=agent_seeds)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 819, in _run_sequential
    max_iter=max_iter)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 592, in run_one_episode
    pbar=pbar, env_seed=env_seed, max_iter=max_iter, agent_seed=agent_seed)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/grid2op/Runner/Runner.py", line 694, in _run_one_episode
    act = agent.act(obs, reward, done)
  File "/home/alwin/miniconda3/envs/l2rpn_challenge/lib/python3.7/site-packages/l2rpn_baselines/PandapowerOPFAgent/PandapowerOPFAgent.py", line 238, in act
    assert not denied, f"{reason} {action}"
AssertionError: Grid2OpException AmbiguousAction InvalidRedispatching "Some redispatching amount are bellow the maximum ramp down" This action will:
     - NOT change anything to the injections
     - Redispatch gen_41_19 of -9.769369125366211
     - Redispatch gen_45_20 of 1.3999968767166138
     - Redispatch gen_54_26 of 2.799999237060547
     - Redispatch gen_55_29 of 2.799999237060547
     - Redispatch gen_60_32 of -4.30000114440918
     - Redispatch gen_64_35 of -2.79998779296875
     - Redispatch gen_68_37 of 9.869362831115723
     - force reconnection of 2 powerlines ([41 57])
     - NOT switch any line status
     - NOT switch anything in the topology
     - Set the bus of the following element:
         - assign bus 1 to line (origin) 41 [on substation 26]
         - assign bus 1 to line (extremity) 41 [on substation 28]
         - assign bus 1 to line (extremity) 57 [on substation 28]
         - assign bus 1 to line (origin) 57 [on substation 31]
jhmenke commented 4 years ago

Not sure what it could be now, you confirmed that -10 is the allowed ramp down, right?

Alwinator commented 4 years ago

No, you said it might be -10. ramp_down: array([ -5., -10., 0., 0., -10.], dtype=float32) I am not sure, but for me it looks like it depends on the generator.

jhmenke commented 4 years ago

Sure, then it's weird that the OPF wants to set -10 when the limit is -5.

It parsed here: https://github.com/jhmenke/grid2op_pp_baseline/blob/2656137da11c4163fa7ef3757aae350d27f8eb65/PandapowerOPFAgent.py#L121

Alwinator commented 4 years ago

Are you sure that you don't want to try it with the official check_your_submission.py script? You can download it here. No account required.

jhmenke commented 4 years ago

yes i don't have Julia set up and can unfortunately not spend a large amount of time on debugging - sorry. it should be easily debugged though