Grid2op/grid2op

Bug: Cannot serialize action if it modifies all elements (e.g. all lines)

Opened this issue · 0 comments

Environment

  • Grid2op version: 1.11.0.dev0
  • System: arch
  • Python: 1.12.4

Bug description

If you try to store and then load a serialized version of a Grid2Op action, this will usually work without a problem. However, there is an edge case where it fails (with a misleading/confusing error message) if the action modified all line statuses. This only happens if you modify all lines, even modify all-but-one will not throw the error. It also does not happen if you directly reload the action from the serialized form (i.e. the error only happens if you write and read the serialized form from file)

How to reproduce

Minimal code to reproduce the error:

import json
import tempfile
import grid2op
from pathlib import Path
env = grid2op.make("l2rpn_case14_sandbox")
init_obs = env.reset(options={"time serie id":0})
all_but_one_lines_on = env.action_space({"set_line_status":[(name, 1) for name in env.name_line[0:-1]]})
all_lines_on = env.action_space({"set_line_status":[(name, 1) for name in env.name_line[:]]})

with tempfile.TemporaryDirectory() as tmpdirname:
    for act in [all_but_one_lines_on, all_lines_on]:
        with open(Path(tmpdirname) / "act.json", "w") as f:
            json.dump(act.as_serializable_dict(), f)
        with open(Path(tmpdirname) / "act.json", "r") as f:
            dict_ = json.load(f)
        act = env.action_space(dict_) # Will throw error on 'all_lines_on' (2nd iteration)
        print(act)

This results in the following error message:

IllegalAction: Grid2OpException IllegalAction "Impossible to modify the line status with your input. Please consult the documentation. The error was:
"Grid2OpException IllegalAction "new_bus should be between -1 and 1, found a value  > 1"""

Note from additional tests, this seems to also be the case for other action, e.g. with redispatch:

import json
import tempfile
import grid2op
from pathlib import Path
env = grid2op.make("l2rpn_case14_sandbox")
init_obs = env.reset(options={"time serie id":0})
all_gens_but_one = env.action_space({"redispatch":[(name, 0.01) for name in env.name_gen[0:-1]]})
all_gens = env.action_space({"redispatch":[(name, 0.01) for name in env.name_gen[:]]})

with tempfile.TemporaryDirectory() as tmpdirname:
    for act in [all_gens_but_one, all_gens]:
        with open(Path(tmpdirname) / "act.json", "w") as f:
            json.dump(act.as_serializable_dict(), f)
        with open(Path(tmpdirname) / "act.json", "r") as f:
            dict_ = json.load(f)
        act = env.action_space(dict_) # Will throw error on 'all_gens' (2nd iteration)
        print(act)

Will give the error message:

IllegalAction: Grid2OpException IllegalAction "Impossible to modify the redispatching with your input. Please consult the documentation. The error was:
"too many indices for array: array is 1-dimensional, but 2 were indexed""

Expected output

Expect no error message to be thrown.