1

Try to run the cartpole environment on my macbook pro and the render function cannot work as there shows no animation about the cart. However, the code seems to work and my DQN can train under the environment. Still no animation after trying some tips provided by stack overflow. Codes as below

import gym
env = gym.make('CartPole-v0')
env.reset()
for _ in range(1000):
    env.render()
    env.step(env.action_space.sample()) # take a random action
env.close()

1 Answers1

0

I don't know the particular reason why this happens, but for latest chipsets (m1,m2,m1 pro,m1 max) are not rendering the environment of the gym while the algorithm works just as fine as normal. Fortunately I have found one fix for this problem. For that, we need to use miniconda.

  1. install miniconda in your MacBook. For that, open terminal, type

    $ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh

    $ sh ./Miniconda3-latest-MacOSX-x86_64.sh

press enter and yes for all the terms and conditions (if you are okay with them)

then, activate the conda environment by

conda miniconda3/bin/activate

once it is active, you will get (base) prefix indicating its active. Once it is done, continue with gym installation by,

Note: this did not work for me when I created a virtual env in the conda. I use just the base version to render and it works.

conda install -c conda-forge gym

this will install all requirements and libraries.

You can test the rendering by executing the following code:

import gym

env = gym.make('MountainCarContinuous-v0')

for i_episode in range(20):
    observation = env.reset()
    for t in range(100):
        env.render()
        print(observation)
        action = env.action_space.sample()
        obs, reward, done, info = env.step(action)
        print(obs, reward, done) 
        if done:
            print("Episode finished after {} timesteps".format(t+1))
            break
env.close()

Hope this helps.

Jithin Palepu
  • 596
  • 1
  • 7
  • 18