Farama-Foundation / Gymnasium

An API standard for single-agent reinforcement learning environments, with popular reference environments and related utilities (formerly Gym)
https://gymnasium.farama.org
MIT License
7k stars 781 forks source link

[Bug Report] ValueError: not enough values to unpack (expected 5, got 4) #77

Closed AchillesPlight closed 1 year ago

AchillesPlight commented 1 year ago

When setting up my envs, I get Value Error messages for my env.step(env.action_space.sample()) If you can help that will be greatly appreciated! (Mac OS, Jupyter Lab)

Here's the Code:

!pip3 install gym-super-mario-bros nes_py !pip3 install gym

from nes_py.wrappers import JoypadSpace import gym_super_mario_bros from gym_super_mario_bros.actions import SIMPLE_MOVEMENT env = gym_super_mario_bros.make('SuperMarioBros-v0') env = JoypadSpace(env, SIMPLE_MOVEMENT)

done = True for step in range(5000): if done: state = env.reset() state, reward, done, info = env.step(env.action_space.sample()) env.render()

env.close()

Error Message: ValueError Traceback (most recent call last) Cell In [45], line 5 3 if done: 4 state = env.reset() ----> 5 state, reward, done, info = env.step(env.action_space.sample()) 6 env.render() 8 env.close()

File /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/nes_py/wrappers/joypad_space.py:74, in JoypadSpace.step(self, action) 59 """ 60 Take a step using the given action. 61 (...) 71 72 """ 73 # take the step and record the output ---> 74 return self.env.step(self._action_map[action])

File /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/gym/wrappers/time_limit.py:50, in TimeLimit.step(self, action) 39 def step(self, action): 40 """Steps through the environment and if the number of steps elapsed exceeds max_episode_steps then truncate. 41 42 Args: (...) 48 49 """ ---> 50 observation, reward, terminated, truncated, info = self.env.step(action) 51 self._elapsed_steps += 1 53 if self._elapsed_steps >= self._max_episode_steps:

ValueError: not enough values to unpack (expected 5, got 4)

pseudo-rnd-thoughts commented 1 year ago

As the environment uses the old v21 API, you need to use apply_api_compatibility=True

from nes_py.wrappers import JoypadSpace
import gym_super_mario_bros
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT
import gym

env = gym.make('SuperMarioBros-v0', apply_api_compatibility=True, render_mode="human")
env = JoypadSpace(env, SIMPLE_MOVEMENT)

done = True
env.reset()
for step in range(5000):
    action = env.action_space.sample()
    obs, reward, terminated, truncated, info = env.step(action)
    done = terminated or truncated

    if done:
       env.reset()

env.close()
AchillesPlight commented 1 year ago

Thank you! This fixed the issue I was having. I'll be back soon with more errors. 🙏🏽

AchillesPlight commented 1 year ago

Hello again, the code you gave me worked a few times, but when I was working on it today, i got an error for this line of code:

env = gym_super_mario_bros.make('SuperMarioBros-v0', apply_api_compatibility=True, render_mode="human")

Error Message:

TypeError Traceback (most recent call last) Cell In [26], line 1 ----> 1 env = gym_super_mario_bros.make('SuperMarioBros-v0', apply_api_compatibility=True, render_mode="human") 2 env = JoypadSpace(env, SIMPLE_MOVEMENT)

File /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/gym/envs/registration.py:592, in make(id, max_episode_steps, autoreset, disable_env_checker, kwargs) 588 else: 589 # Assume it's a string 590 envcreator = load(spec.entry_point) --> 592 env = env_creator(kwargs) 594 # Copies the environment creation specification and kwargs to add to the environment specification details 595 spec = copy.deepcopy(spec_)

TypeError: init() got an unexpected keyword argument 'apply_api_compatibility'

pseudo-rnd-thoughts commented 1 year ago

You need to use gym or gymnasium v26+, gym.__version__, pip install --upgrade gym

lehoangan2906 commented 1 year ago

You need to use gym or gymnasium v26+, gym.__version__, pip install --upgrade gym

After upgrading gym and stable-baselines3, mine said:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behavior is the source of the following dependency conflicts.
stable-baselines3 1.6.2 requires gym==0.21, but you have gym 0.26.2 which is incompatible.

If I switch back to gym 0.21 then the error TypeError: init() got an unexpected keyword argument 'apply_api_compatibility'is still present.

I don't know how to fix this problem

lehoangan2906 commented 1 year ago

You need to use gym or gymnasium v26+, gym.__version__, pip install --upgrade gym

After upgrading gym and stable-baselines3, mine said:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behavior is the source of the following dependency conflicts.
stable-baselines3 1.6.2 requires gym==0.21, but you have gym 0.26.2 which is incompatible.

If I switch back to gym 0.21 then the error TypeError: init() got an unexpected keyword argument 'apply_api_compatibility'is still present.

I don't know how to fix this problem

Update: I solved the problem, thanks for all your very useful help!

excalibur100 commented 1 year ago

You need to use gym or gymnasium v26+, gym.__version__, pip install --upgrade gym

i have installed the latest version of gym and also used the new api compatibility i.e

Import the game

import gym_super_mario_bros

Import the Joypad wrapper

from nes_py.wrappers import JoypadSpace

Import the simplified controls

from gym_super_mario_bros.actions import SIMPLE_MOVEMENT

env = gym_super_mario_bros.make('SuperMarioBros-v0', apply_api_compatibility=True, render_mode="human") env = JoypadSpace(env, SIMPLE_MOVEMENT)

Create a flag - restart or not

done = True

Loop through each frame in the game

for step in range(5000):

Start the game to begin with

if done: 
    # Start the gamee
    env.reset()
# Do random actions
state, reward, done, info = env.step(env.action_space.sample())
# Show the game on the screen
env.render()

Close the game

env.close()

after this there is a value error but for not enough values:-

ValueError Traceback (most recent call last) Cell In[13], line 10 8 env.reset() 9 # Do random actions ---> 10 state, reward, done, info = env.step(env.action_space.sample()) 11 # Show the game on the screen 12 env.render()

ValueError: too many values to unpack (expected 4)