Why does my CartPole-v1 game keep restarting when I try to play using Python's OpenAI gym library with correctly mapped keys?

48 views Asked by At

Cartpole-v1 (human Play)

import gym
from gym.utils.play import play
play(gym.make("CartPole-v1"),keys_to_action={"a":(0,),"d":(1,)},zoom=1)

I've mapped the keys to action correctly but every time I run it, the game keeps restarting without allowing me to play. As soon as it starts, it restarts.

1

There are 1 answers

1
Lexpj On

I assume you have used Gym v0.26.

A couple of things must be set right:

import gym
from gym.utils.play import play
play(gym.make("CartPole-v1", render_mode="rgb_array"),keys_to_action={"a":0,"d":1},zoom=1)

The render_mode must be set to 'rgb_array' or 'rgb_array_list'. Not specifying (i.e. None) will raise an AssertionError Furthermore, the keys_to_action parameter expects a dictionary with strings as keys and ints as values. Therefore the dictionary should look like {"a":0,"d":1}, or it will raise an AssertionError. With these fixes, the window stays open and inputs via the keys is possible.