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

[Bug] check_env() output error on the reset() method, observation space not matching #932

Closed
3 of 4 tasks
francescomaldonato opened this issue Jun 13, 2022 · 6 comments
Closed
3 of 4 tasks
Labels
custom gym env Issue related to Custom Gym Env duplicate This issue or pull request already exists more information needed Please fill the issue template completely

Comments

@francescomaldonato
Copy link

Describe the bug
The check_env() function gives an error when checking the reset() method on the environment. I use for my environment a Dictionary of variables, which was recognized and passed the check_env() test last time I ran it on the 25th of May 2022.

Code example
Init_space = {
'ev_power': Init_space_ev_power,
'total_load': Init_space_total_load,
'total_cost': Init_space_total_cost,
'Available_energy_sources': Available_energy_State_space
}

def init:
spaces = {
'ev_power': gym.spaces.Box(low=0, high=18.5, shape=(simulation_len,)),
'total_load': gym.spaces.Box(low=-30, high=30, shape=(simulation_len,)),
'total_cost': gym.spaces.Box(low=-0.30, high=0.60, shape=(simulation_len,)),
'Available_energy_sources': gym.spaces.Box(low=0.0, high=100, shape=(simulation_len,EV_simulation_n))
}

  dict_space = gym.spaces.Dict(spaces)
  self.observation_space = dict_space

....
...
def reset(self):
self.state = Init_space
#reset vpp session time
self.vpp_length = len(time_serie)

return self.state

Error message


AssertionError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/stable_baselines3/common/env_checker.py in _check_returned_values(env, observation_space, action_space)
147 try:
--> 148 _check_obs(obs[key], observation_space.spaces[key], "reset")
149 except AssertionError as e:

3 frames
AssertionError: The observation returned by the reset() method does not match the given observation space

During handling of the above exception, another exception occurred:

AssertionError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/stable_baselines3/common/env_checker.py in _check_returned_values(env, observation_space, action_space)
148 _check_obs(obs[key], observation_space.spaces[key], "reset")
149 except AssertionError as e:
--> 150 raise AssertionError(f"Error while checking key={key}: " + str(e))
151 else:
152 _check_obs(obs, observation_space, "reset")

AssertionError: Error while checking key=ev_power: The observation returned by the reset() method does not match the given observation space

System Info
Describe the characteristic of your environment:

  • Running my code on a Google CoLab notebook running on hosted runtime
  • Stable Baselines was installed with !pip
  • Gym was installed with !pip
  • Using Ubuntu 22.04
  • Python version (I don't know what Google Colab host machines run)

Additional context
My environment was working 2 weeks ago.
I ran it today from my machine on a Colab notebook and it doesn't work. Desperately, I tried running it from another machine, still from a Colab notebook but with no luck.
I attached a screenshot of the error.

Checklist

  • I have checked that there is no similar issue in the repo (required)
    Screenshot from 2022-06-13 16-12-24

Expected behavior

The check_env() function should just successfully check the environment

You can use sb3.get_system_info() to print relevant packages info:

import stable_baselines3 as sb3
sb3.get_system_info()

--> output of sb3.get_system_info()
OS: Linux-5.4.188+-x86_64-with-Ubuntu-18.04-bionic #1 SMP Sun Apr 24 10:03:06 PDT 2022
Python: 3.7.13
Stable-Baselines3: 1.5.0
PyTorch: 1.11.0+cu113
GPU Enabled: False
Numpy: 1.21.6
Gym: 0.17.3

({'GPU Enabled': 'False',
'Gym': '0.17.3',
'Numpy': '1.21.6',
'OS': 'Linux-5.4.188+-x86_64-with-Ubuntu-18.04-bionic #1 SMP Sun Apr 24 10:03:06 PDT 2022',
'PyTorch': '1.11.0+cu113',
'Python': '3.7.13',
'Stable-Baselines3': '1.5.0'},
'OS: Linux-5.4.188+-x86_64-with-Ubuntu-18.04-bionic #1 SMP Sun Apr 24 10:03:06 PDT 2022\nPython: 3.7.13\nStable-Baselines3: 1.5.0\nPyTorch: 1.11.0+cu113\nGPU Enabled: False\nNumpy: 1.21.6\nGym: 0.17.3\n')

Checklist

  • I have checked that there is no similar issue in the repo (required)
  • I have read the documentation (required)
  • I have provided a minimal working example to reproduce the bug (required)
@francescomaldonato francescomaldonato added the bug Something isn't working label Jun 13, 2022
@qgallouedec
Copy link
Collaborator

  • Python version (I don't know what Google Colab host machines run)
>>> import sys
>>> sys.version
...

@qgallouedec
Copy link
Collaborator

I honestly can't understand your code. Can you provide minimal code that would allow anyone to reproduce the bug you describe?

@Miffyli Miffyli added the custom gym env Issue related to Custom Gym Env label Jun 13, 2022
@Miffyli
Copy link
Collaborator

Miffyli commented Jun 13, 2022

What @qgallouedec said. Also follow the instructions the error gives to fix your environment: if you feel there is something wrong with the checker, then share a minimal code to replicate the issue without your custom environment.

@araffin araffin added more information needed Please fill the issue template completely and removed bug Something isn't working labels Jun 13, 2022
@francescomaldonato
Copy link
Author

sys.version
3.7.13 (default, Apr 24 2022, 01:04:09)
[GCC 7.5.0]

Thank you for the fast reply. I believe there is really a bug on the check_env() function, from your last commit [Update doc and add check for unbounded action space https://github.com//pull/918[)]
precisely, in the env_checker.py.

Here there is a link to a minimal version of my code that reproduces the bug.
https://colab.research.google.com/drive/1Q10svLDWIKpgevetU4sv_cjjzBOEYgxu?usp=sharing

Thank you for your time!

@qgallouedec
Copy link
Collaborator

It is not minimal. Here is a minimal example reproducing the error.

from gym import Env
from gym.spaces import Discrete, Dict, Box
import numpy as np
from stable_baselines3.common.env_checker import check_env


class MyEnv(Env):
    def __init__(self):
        self.action_space = Discrete(3)

        spaces = {"key": Box(low=0, high=18.5, shape=(1,))}
        self.observation_space = Dict(spaces)

    def step(self, action):
        return {"key": np.zeros(1)}, 0, False, {}

    def reset(self):
        return {"key": np.zeros(1)}


env = MyEnv()

check_env(env)

The error comes from the observation space. By default, gym uses float32, but your env returns float64. To solve it, you just need to be consistent in the observation type. For example, by always using float32:

{"key": np.zeros(1, dtype=np.float32)}

@qgallouedec
Copy link
Collaborator

qgallouedec commented Jun 13, 2022

Duplicate #921, #746

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
custom gym env Issue related to Custom Gym Env duplicate This issue or pull request already exists more information needed Please fill the issue template completely
Projects
None yet
Development

No branches or pull requests

4 participants