web-dev-qa-db-ja.com

AttributeError:モジュール '_Box2D'には属性 'Rand_LIMIT_swigconstant'がありません

強化学習でlunar_landerを実行しようとしていますが、実行するとエラーが発生します。さらに、私のコンピューターはosxシステムです。

月着陸船のコードは次のとおりです。

import numpy as np
import gym
import csv

from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from keras.optimizers import Adam

from rl.agents.dqn import DQNAgent
from rl.policy import BoltzmannQPolicy, EpsGreedyQPolicy
from rl.memory import SequentialMemory

import io
import sys
import csv

# Path environment changed to make things work properly
# export DYLD_FALLBACK_LIBRARY_PATH=$DYLD_FALLBACK_LIBRARY_PATH:/usr/lib


# Get the environment and extract the number of actions.
ENV_NAME = 'LunarLander-v2'
env = gym.make(ENV_NAME)
np.random.seed(123)
env.seed(123)
nb_actions = env.action_space.n

# Next, we build a very simple model.
model = Sequential()
model.add(Flatten(input_shape=(1,) + env.observation_space.shape))
model.add(Dense(16))
model.add(Activation('relu'))
model.add(Dense(16))
model.add(Activation('relu'))
model.add(Dense(16))
model.add(Activation('relu'))
model.add(Dense(nb_actions))
model.add(Activation('linear'))
#print(model.summary())

# Finally, we configure and compile our agent. You can use every built-in Keras optimizer and
# even the metrics!
memory = SequentialMemory(limit=300000, window_length=1)
policy = EpsGreedyQPolicy()
dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=10,
               target_model_update=1e-2, policy=policy)
dqn.compile(Adam(lr=1e-3), metrics=['mae'])

# After training is done, we save the final weights.
dqn.load_weights('dqn_{}_weights.h5f'.format(ENV_NAME))

# Redirect stdout to capture test results
old_stdout = sys.stdout
sys.stdout = mystdout = io.StringIO()

# Evaluate our algorithm for a few episodes.
dqn.test(env, nb_episodes=200, visualize=False)

# Reset stdout
sys.stdout = old_stdout

results_text = mystdout.getvalue()

# Print results text
print("results")
print(results_text)

# Extact a rewards list from the results
total_rewards = list()
for idx, line in enumerate(results_text.split('\n')):
    if idx > 0 and len(line) > 1:
        reward = float(line.split(':')[2].split(',')[0].strip())
        total_rewards.append(reward)

# Print rewards and average
print("total rewards", total_rewards)
print("average total reward", np.mean(total_rewards))

# Write total rewards to file
f = open("lunarlander_rl_rewards.csv",'w')
wr = csv.writer(f)
for r in total_rewards:
     wr.writerow([r,])
f.close()

エラーは次のとおりです。

Traceback (most recent call last):
  File "/s/user/Document/Semester2/Advanced Machine Learning/Lab/Lab6/lunar_lander_ml_states_player.py", line 23, in <module>
    env = gym.make(ENV_NAME)
  File "/s/user/anaconda/envs/untitled/lib/python3.6/site-packages/gym/envs/registration.py", line 167, in make
    return registry.make(id)
  File "/s/user/anaconda/envs/untitled/lib/python3.6/site-packages/gym/envs/registration.py", line 119, in make
    env = spec.make()
  File "/s/user/anaconda/envs/untitled/lib/python3.6/site-packages/gym/envs/registration.py", line 85, in make
    cls = load(self._entry_point)
  File "/s/user/anaconda/envs/untitled/lib/python3.6/site-packages/gym/envs/registration.py", line 14, in load
    result = entry_point.load(False)
  File "/s/user/anaconda/envs/untitled/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2405, in load
    return self.resolve()
  File "/s/user/anaconda/envs/untitled/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2411, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/s/user/anaconda/envs/untitled/lib/python3.6/site-packages/gym/envs/box2d/__init__.py", line 1, in <module>
    from gym.envs.box2d.lunar_lander import LunarLander
  File "/s/user/anaconda/envs/untitled/lib/python3.6/site-packages/gym/envs/box2d/lunar_lander.py", line 4, in <module>
    import Box2D
  File "/s/user/anaconda/envs/untitled/lib/python3.6/site-packages/Box2D/__init__.py", line 20, in <module>
    from .Box2D import *
  File "/s/user/anaconda/envs/untitled/lib/python3.6/site-packages/Box2D/Box2D.py", line 435, in <module>
    _Box2D.Rand_LIMIT_swigconstant(_Box2D)
AttributeError: module '_Box2D' has no attribute 'Rand_LIMIT_swigconstant'

https://github.com/pybox2d/pybox2d/blob/master/INSTALL.md のガイドに従ってBox2dを再インストールしようとしましたが、それでも機能しません。誰か助けてもらえますか?

4
HungryBird

これを試してください 'pip3 install box2dbox2d-kengz'

21
FlyingBurger

「pipinstallbox2d box2d-kengz--user」は私のために働いた:)

他の人がそれを有益だと思うかもしれない場合に備えて。

3
Dane Lee

それを試してみてください:

!pip install box2d-py

import gym

env = gym.make("BipedalWalker-v2")
env = gym.make('BipedalWalkerHardcore-v2')
env = gym.make('LunarLander-v2')
env = gym.make('CarRacing-v0')

私のために働いた。 colab.research.google.comを実行しています

0
Neurobionic