Why is the Pybullet_envs library giving an error?

231 views Asked by At

I am running some code from this youtube vidoe: https://www.youtube.com/watch?v=ioidsRlf79o. The library pybullet_envs in the main.py file of the code is throwing an error. Here is my code which is the exact same as his code:

import gym
import numpy as np
from gym import wrappers

if __name__ == '__main__':
    env = gym.make('InvertedPendulumBulletEnv-v0')
    agent = Agent(input_dims=env.observation_space.shape, env=env,
            n_actions=env.action_space.shape[0])
    n_games = 250
    # uncomment this line and do a mkdir tmp && mkdir video if you want to
    # record video of the agent playing the game.
    #env = wrappers.Monitor(env, 'tmp/video', video_callable=lambda episode_id: True, force=True)
    filename = 'inverted_pendulum.png'

    figure_file = 'plots/' + filename

    best_score = env.reward_range[0]
    score_history = []
    load_checkpoint = False

    if load_checkpoint:
        agent.load_models()
        env.render(mode='human')

    for i in range(n_games):
        observation = env.reset()
        done = False
        score = 0
        while not done:
            action = agent.choose_action(observation)
            observation_, reward, done, info = env.step(action)
            score += reward
            agent.remember(observation, action, reward, observation_, done)
            if not load_checkpoint:
                agent.learn()
            observation = observation_
        score_history.append(score)
        avg_score = np.mean(score_history[-100:])

        if avg_score > best_score:
            best_score = avg_score
            if not load_checkpoint:
                agent.save_models()

        print('episode ', i, 'score %.1f' % score, 'avg_score %.1f' % avg_score)

    if not load_checkpoint:
        x = [i+1 for i in range(n_games)]
        plot_learning_curve(x, score_history, figure_file)

However, I think the problem is coming only specifically from the library itself, pybullet_envs. Here is the error it is throwing:

Cell In[20], line 1
----> 1 import pybullet_envs
      2 import gym
      3 import numpy as np

File ~\anaconda3\lib\site-packages\pybullet_envs\__init__.py:14
      9     return gym.envs.registration.register(id, *args, **kvargs)
     12 # ------------bullet-------------
---> 14 register(
     15     id='HumanoidDeepMimicBackflipBulletEnv-v1',
     16     entry_point='pybullet_envs.deep_mimic.gym_env:HumanoidDeepMimicBackflipBulletEnv',
     17     max_episode_steps=2000,
     18     reward_threshold=2000.0,
     19 )
     21 register(
     22     id='HumanoidDeepMimicWalkBulletEnv-v1',
     23     entry_point='pybullet_envs.deep_mimic.gym_env:HumanoidDeepMimicWalkBulletEnv',
     24     max_episode_steps=2000,
     25     reward_threshold=2000.0,
     26 )
     28 register(
     29     id='CartPoleBulletEnv-v1',
     30     entry_point='pybullet_envs.bullet:CartPoleBulletEnv',
     31     max_episode_steps=200,
     32     reward_threshold=190.0,
     33 )

File ~\anaconda3\lib\site-packages\pybullet_envs\__init__.py:6, in register(id, *args, **kvargs)
      5 def register(id, *args, **kvargs):
----> 6   if id in registry.env_specs:
      7     return
      8   else:

AttributeError: 'dict' object has no attribute 'env_specs'
1

There are 1 answers

0
Dave On

Please try the following snippet and let us know.

import pybullet as p

import gym


from collections import UserDict

registry = UserDict(gym.envs.registration.registry)
registry.env_specs = gym.envs.registration.registry

gym.envs.registration.registry = registry

import pybullet_data
import pybullet_envs

The problem seems to be an update in gym, which now uses UserDict.