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

Error creating new variables using as_networkx() #327

Closed
EloyAnguiano opened this issue Jun 1, 2022 · 1 comment
Closed

Error creating new variables using as_networkx() #327

EloyAnguiano opened this issue Jun 1, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@EloyAnguiano
Copy link

EloyAnguiano commented Jun 1, 2022

Environment

  • Grid2op version: 1.7.0
  • Stable Baselines 3 version: 1.4.0
  • System: Ubuntu 18.04 LTS
  • Python 3.9.12

Bug description

I'm trying to add some features to the space using the as_networkx method but sometimes there are some problems with that conversion.

Command line

# python script.py

Code snippet

import torch as th
from stable_baselines3 import SAC
from stable_baselines3.common.torch_layers import BaseFeaturesExtractor
import gym
from grid2op import make
from grid2op.gym_compat import BoxGymActSpace, GymEnv
from copy import deepcopy
import networkx as nx
from gym.spaces import Box
import numpy as np
import torch.nn as nn

def build_matrix(obs, key):
    if isinstance(obs, nx.Graph):
        graph = obs
    else:
        graph = obs.as_networkx()

    matrix = nx.attr_matrix(graph, edge_attr=key)[0]
    return matrix

class FeatureExtractor(BaseFeaturesExtractor):
    def __init__(self, observation_space:  gym.spaces.Dict, features_dim: int, device):
        super(FeatureExtractor, self).__init__(
            observation_space, features_dim)

        self.device = device

        space = observation_space["rho_matrix"]
        n = space.shape[0] * space.shape[1]

        self.last_dense = nn.Sequential(
            nn.Linear(n, self.features_dim),
            nn.ReLU())


    def forward(self, observations: gym.spaces.Dict) -> th.Tensor:
        return self.last_dense.forward(observations["rho_matrix"].flatten(1))

POLICY = {  "features_extractor_class": FeatureExtractor,
            "features_extractor_kwargs": {
                "features_dim": 256,
                "device": "cpu",
            },
            "activation_fn": th.nn.ReLU}
if __name__ == "__main__":

    grid2op_env = make("rte_case5_example", test=True)

    env = GymEnv(grid2op_env)  # create the gym environment

    # CHANGING ACTION SPACE
    env.action_space = BoxGymActSpace(env.init_env.action_space)

    # CHANGING OBSERVATION SPACE
    # Adding vars
    grid2op_obs = grid2op_env.reset()

    # Adding vars
    graph = deepcopy(grid2op_obs).as_networkx()

    # graph_vars = {}
    graph_vars = {"rho":(-1000, 1000)}

    for k in graph_vars.keys():
        shape_ = build_matrix(graph, k).shape
        env.observation_space.add_key(f"{k}_matrix",
                                        lambda obs: build_matrix(obs, k),
                                        Box(shape=shape_, 
                                            low=np.full(shape_, graph_vars[k][0]),
                                            high=np.full(shape_, graph_vars[k][1])))

    agent = SAC("MultiInputPolicy",env, policy_kwargs=POLICY)
    agent.learn(total_timesteps=1e5)

Current output

ValueError: zero-size array to reduction operation maximum which has no identity
@EloyAnguiano EloyAnguiano added the bug Something isn't working label Jun 1, 2022
@BDonnot
Copy link
Collaborator

BDonnot commented Jun 3, 2022

Hello,

Thanks for notifying us of this bug.

It's rather easy to reproduce with your code snippet. Thanks. Hopefully i'll be able to fix it for the next release.

BDonnot referenced this issue in BDonnot/Grid2Op Jun 3, 2022
BDonnot added a commit that referenced this issue Jun 3, 2022
fix issue #327 and implement l2rpn 2022 score
@BDonnot BDonnot closed this as completed Jun 3, 2022
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