属性错误:模块“_Box2D”没有属性“RAND_LIMIT_swigconstant”

问题描述 投票:0回答:5

我正在尝试运行一个强化月球着陆器 正在学习,但是运行的时候却出现错误。 另外我的电脑是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 但它仍然不起作用,有人可以帮助我吗?

python machine-learning box2d reinforcement-learning
5个回答
49
投票

尝试这个“pip3 install box2d box2d-kengz”


5
投票

“pip install box2d box2d-kengz --user”对我有用:)

以防其他人可能会发现它提供了信息。


4
投票

如果您已经有 box2d-py,请卸载并重新安装

pip uninstall box2d-py

那么,

pip install box2d-py

这对我有用。


3
投票

pip install gym[all]
为我工作


0
投票

对我来说,这是两件事的结合。

首先我想安装

pip install box2d box2d-kengz
,但是收到了关于无法构建轮子的错误。

我尝试了很多建议的方法来解决轮子的问题,最后这个命令解决了它:

pip install --upgrade setuptools wheel

之后我就能够安装python包并解决问题了。

祝你好运:)

© www.soinside.com 2019 - 2024. All rights reserved.