question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
qgallouedeccommented, Jun 13, 2022

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)}
1reaction
qgallouedeccommented, Jun 13, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Bug Report] Check_Env numpy format issues #683 - GitHub
Describe the bug Using check_env in a custom env that returns a numpy ... by the reset() method does not match the given...
Read more >
the `reset()` method does not match the given observation ...
Error: AssertionError: The observation returned by the `reset()` method does not match the given observation space.
Read more >
stable_baselines3.common.env_checker - Stable Baselines3
255): warnings.warn( f"It seems that your observation space {key} is an image but the " "upper and lower bounds are not in [0,...
Read more >
The observation returned by the `reset()` method is not ...
i'm strugling to understand how custom observation_space should be coded and how there isn't so much info about some topics i have to...
Read more >
Commit List - Confluence Mobile - Apache Software Foundation
revision committer date r1387565 kwall 2012‑09‑19 r1387583 kwall 2012‑09‑19 r1387785 aconway 2012‑09‑19
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found