Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetching the 'True' Chronics with obs.simulate(...) #657

Open
DEUCE1957 opened this issue Nov 6, 2024 · 2 comments
Open

Fetching the 'True' Chronics with obs.simulate(...) #657

DEUCE1957 opened this issue Nov 6, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@DEUCE1957
Copy link
Contributor

DEUCE1957 commented Nov 6, 2024

Environment

  • Grid2op version: 1.11.0.dev0
  • System: ArchLinux
  • Python: 3.12.7

Bug description

A normal, fully initialized, Environment has a chronics_handler - from which you can get the name / ID of the current episode:

import grid2op
env = grid2op.make("l2rpn_case14_sandbox")
obs = env.reset()
print(f"Current Episode '{env.chronics_handler.get_name()}'")

The call to the Reward Function has the following signature (from BaseReward):

def __call__(self, action:BaseAction, env:BaseEnv, has_error:bool, is_done:bool, is_illegal:bool, is_ambigious:bool):

Hence it should be possible to use env.chronics_handler.get_name() inside a custom reward function, since BaseEnv includes the chronics handler. This is useful for, for instance, fetching the load profiles.

However, this will throw an error if we use obs.simulate():

  File ".../L2RPN/Rewards/EnergyNotSuppliedReward.py", line 22, in __call__
    ep_id = env.chronics_handler.get_name()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: '_ObsCH' object has no attribute 'get_name'

Since the _ObsEnv class is used in place of the normal BaseEnv, the normal chronics handler does not exist.

My current workaround is to use:

expected_load = env.get_obs(_update_state=False).load_p

Minimal Example

from logging import Logger
import grid2op
from grid2op.Action import BaseAction
from grid2op.Reward import BaseReward
from grid2op.Environment import BaseEnv
from grid2op.Environment._obsEnv import _ObsEnv

internal_env = None
class WeirdReward(BaseReward):

    def __init__(self, logger: Logger = None):
        super().__init__(logger)

    def __call__(self, action: BaseAction, env:BaseEnv, has_error: bool, is_done: bool, is_illegal: bool, is_ambiguous: bool) -> float:
        global internal_env
        if isinstance(env, _ObsEnv):
            print(env)
            internal_env = env
        print(env.chronics_handler.get_name())

        return super().__call__(action, env, has_error, is_done, is_illegal, is_ambiguous)

env = grid2op.make("l2rpn_case14_sandbox", reward_class=WeirdReward)
obs = env.reset()

act = env.action_space({})
sim_obs, sim_reward, sim_done, sim_info = obs.simulate(act)
@DEUCE1957 DEUCE1957 added the bug Something isn't working label Nov 6, 2024
@BDonnot
Copy link
Collaborator

BDonnot commented Nov 6, 2024

Hello,

I will fix it ASAP.

In the mean time (the grid2op release with this fix will wait for some other modifications) I suggest you use:

class WeirdReward(BaseReward):

    def __init__(self, logger: Logger = None):
        super().__init__(logger)

    def __call__(self, action: BaseAction, env:BaseEnv, has_error: bool, is_done: bool, is_illegal: bool, is_ambiguous: bool) -> float:
        if self.is_simulated_env(env):
            return 0. # the data for the simulation does not mean anything, better not use it
        # now you can use env.chronics_handler.get_name()
        return super().__call__(action, env, has_error, is_done, is_illegal, is_ambiguous)

@BDonnot
Copy link
Collaborator

BDonnot commented Nov 6, 2024

Oh and by the way, the "True" chronics will never be fed to "obs.simulate" this is a design choice to ensure that a user cannot access the "future" data, in the same way that accessing a real simulator will not provide you any data about the future.

BDonnot added a commit to BDonnot/Grid2Op that referenced this issue Nov 6, 2024
Signed-off-by: DONNOT Benjamin <[email protected]>
DEUCE1957 pushed a commit to DEUCE1957/Grid2Op that referenced this issue Nov 8, 2024
Signed-off-by: DONNOT Benjamin <[email protected]>
Signed-off-by: Xavier Weiss <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants