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 Report] The env checker does not detect when the environment is not deterministic after the first reset. #1084

Closed
1 task done
qgallouedec opened this issue Jun 13, 2024 · 2 comments · Fixed by #1086
Labels
bug Something isn't working

Comments

@qgallouedec
Copy link
Contributor

qgallouedec commented Jun 13, 2024

Describe the bug

From the doc

Therefore, reset() should (in the typical use case) be called with a seed right after initialization and then never again.

This implies that the reset with a seed, followed by a reset without a seed, must be deterministic.
However, when an environment does not respect this rule, the environment checker does not fail.

Code example

import gymnasium as gym
from gymnasium.utils.env_checker import check_env
import random


class MyEnv(gym.Env):
    def __init__(self):
        super().__init__()
        self.observation_space = gym.spaces.Discrete(5)
        self.action_space = gym.spaces.Discrete(5)

    def reset(self, seed=None, options=None):
        super().reset(seed=seed)
        if seed is not None:
            obs = self.np_random.integers(5)
        else:
            # generate a random observations, but not based on the inner rng -> bad => non deterministic
            obs = int(random.random() * 5)
        return obs, {}

    def step(self, action):
        obs = self.np_random.integers(5)
        return obs, 0, False, False, {}


register = gym.register(
    id="MyEnv-v0",
    entry_point=MyEnv,
    max_episode_steps=1000,
)

check_env(gym.make("MyEnv-v0"))  # Passes but:

env = gym.make("MyEnv-v0")

env.reset(seed=0)
obs1, _ = env.reset()

env.reset(seed=0)
obs2, _ = env.reset()

assert obs1 == obs2  # Fails

System info

gymnasium 0.29.1 (also tested with 1.0.0a2)
MacOS

Additional context

Context: realized panda-gym was in the case described above: qgallouedec/panda-gym#94

Checklist

  • I have checked that there is no similar issue in the repo
@qgallouedec qgallouedec added the bug Something isn't working label Jun 13, 2024
@qgallouedec qgallouedec changed the title [Bug Report] Bug title [Bug Report] The env checker does not detect when the environment is not deterministic after the first reset. Jun 13, 2024
@pseudo-rnd-thoughts
Copy link
Member

Good point, currently the env_checker is relatively minimal on its determinism testing.
If you want to make a PR that beefs up the step and reset determinism testing then I would happily add it

@qgallouedec
Copy link
Contributor Author

Sure! For the time being, I've just addressed the problem of this issue in the without dealing with step.

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

Successfully merging a pull request may close this issue.

2 participants