Gymnasium: [Question] Reset function leads to an Assertion error because of the seed

Question

Hi all,

I have the following reset function

    def reset (self, **kwargs):
        seed = 1
        super().reset(seed=seed)
        self.state_difference_costs_to_conventional_solution = 0
        self.state_difference_peak_to_conventional_solution = 0
        self.state_thermal_discomfort = 0
        info = {}
        self.observation_space = np.array([0,0, 0])

        return  self.observation_space, info

This leads to the Assertion error of the gynasium environment checker AssertionError: Mostly likely the environment reset function does not call super().reset(seed=seed)as the random number generators are not different when different seeds are passed toenv.reset.

I tried multiple things but they all were not successfull.

It says that super().reset(seed=seed) is not called but it is in fact called as you can see. I also tried super().reset(seed=None) but this leads to the same error. I also tried:

    def reset (self):
        self.state_difference_costs_to_conventional_solution = 0
        self.state_difference_peak_to_conventional_solution = 0
        self.state_thermal_discomfort = 0
        info = {}
        self.observation_space = np.array([0,0, 0])

        return  self.observation_space, info

which leads to the error

    raise gym.error.Error(
gymnasium.error.Error: The `reset` method does not provide a `seed` or `**kwargs` keyword argument.

Actually I don’t need a seed argument at all as I don’t use it. Can anyone tell me why this problem occurs and how to solve it?

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 29 (12 by maintainers)

Most upvoted comments

@PBerit In your version you pass an instance of your DSM_Env to gym.register and not the DSM_Env class.

Try: gym.register("dsm-env-v0", lambda: DSM_Env(<params>))

I can confirm that @pseudo-rnd-thoughts code doesn’t give that warning about render spec with your render function from your original source code:

def render (self):
        pass

with:

gymnasium==0.29.1

@mvhensbergen : Thanks for your answer and effort. It now runs without any warnings. Also thanks to @pseudo-rnd-thoughts . I really appreciate your help.

@PBerit

I have cut down your code sample to the essentials below:

import gymnasium as gym
from gymnasium import Env
from gymnasium.spaces import Discrete, Box, Tuple, MultiDiscrete

import pickle
import numpy as np

class DSM_Env(Env):
    def __init__(self):
        self.action_space = Discrete(3)
        self.observation_space = Box(low=np.array([0, 0, 0]), high=np.array([1, 1, 1]),  dtype=np.float64)

    def reset (self, **kwargs):
        super().reset(**kwargs)
        obs = np.array([0, 0, 0])
        info = {}
        return  obs, info

    def step(self, action ):
        self.observation_space = np.array([1, 1,1])
        reward = 0
        done = False
        info = {}
        return self.observation_space, reward, done, False, info

env = DSM_Env()
env.reset()
print(type(env.observation_space))
action = env.action_space.sample()
obj, rewards, done, _, info = env.step(action)
print(type(env.observation_space))

One remark, your original step function did not return the amount of variables that was required by the new library (got a Warning from the library when running) so you might also not run the latest version of gymnasium?

The output of this script is: <class 'gymnasium.spaces.box.Box'> <class 'numpy.ndarray'>

The above script shows that after your step function, you have overwritten the ‘observation_space’ variable in Env and to a ndarray instead of a Box. This corrupts your Env.

If you remove the ‘self.’ in the step function (thus making a local variable) you will return an observation without corrupting the observation_space variable.

I would advise to carefully look into the example code and tutorial to get a bit more feeling about the classes and try to rewrite your code.

In addition, I don’t think this GitHub issue tracker is meant to discuss these things as it appears your issue is not a problem with the library itself ( and also has been closed by the maintainer). If you like you can contact me further outside this platform or perhaps find help elsewhere?

I will need to look into this a bit more (as I am responding while at work). I can try your code tomorrow - that will probably make me better see what the problem is.

@pseudo-rnd-thoughts : Thanks for your answer. What I don’t understand is why I can’t return self.observation_space = np.array([0,0, 0])

In the official documentation (https://gymnasium.farama.org/api/env/) it says: “observation (ObsType) – Observation of the initial state. This will be an element of observation_space (typically a numpy array) and is analogous to the observation returned by step().” Actually In my step' function, I return self.observation_space`

As the documentation states what you should return is an observation which is -an element of- the observation space. What you do is overwrite the definition of the observation_space itself and return it.

So self.observation_space is the collection of all possible observations, step should return only one observation out of that space.

I hope this helps.