Python error, no results on the internet openai-gym

Question:

import gym
env = gym.make("FrozenLake-v1")
env.reset()
env.render()
env.step(1)
env.render()
Something went wrong with pygame. This should never happen.
  File "C:UsersardgnOneDriveBelgelerGitHubKutuphaneler-cheatsheetsGYMLibraryOPENAI", line 7, in <module>
    env.render()

This is the error I get when I use the above code

Asked By: ardgndg

||

Answers:

Code works for me if I add render_mode="human"

import gym

env = gym.make("FrozenLake-v1", render_mode="human")
env.reset()

env.render()
env.step(1)
env.render()

without render_mode="human" (or render_mode="rgb_array") it doesn’t initiate Surface in PyGame in line here, and this makes problem.

And this displays text "Something went wrong with pygame. This should never happen."

        if self.window_surface is None:
            pygame.init()

            if mode == "human":
                pygame.display.init()
                pygame.display.set_caption("Frozen Lake")
                self.window_surface = pygame.display.set_mode(self.window_size)
            elif mode == "rgb_array":
                self.window_surface = pygame.Surface(self.window_size)

        assert (
            self.window_surface is not None
        ), "Something went wrong with pygame. This should never happen."

BTW:

I see that mode "human" also runs render() automatically in every step() and reset()

def step(self, a):
    transitions = self.P[self.s][a]
    i = categorical_sample([t[0] for t in transitions], self.np_random)
    p, s, r, t = transitions[i]
    self.s = s
    self.lastaction = a

    if self.render_mode == "human":
        self.render()
    return (int(s), r, t, False, {"prob": p})

def reset(
    self,
    *,
    seed: Optional[int] = None,
    options: Optional[dict] = None,
):
    super().reset(seed=seed)
    self.s = categorical_sample(self.initial_state_distrib, self.np_random)
    self.lastaction = None

    if self.render_mode == "human":
        self.render()
    return int(self.s), {"prob": 1}

EDIT:

Code from some tutorials – it uses render_mode="human".

import gym

env = gym.make("FrozenLake-v1", render_mode="human")

observation, info = env.reset()

for _ in range(1000):
    observation, reward, terminated, truncated, info = env.step(env.action_space.sample())

    if terminated or truncated:
        observation, info = env.reset()

env.close()    
Answered By: furas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.