AttributeError: 模块 ‘_Box2D’ 没有属性 ‘RAND_LIMIT_swigconstant’

我在尝试运行一个基于强化学习的月球着陆器程序,但运行时出现了错误。我的电脑是OSX系统。

这是月球着陆器的代码:

import numpy as npimport gymimport csvfrom keras.models import Sequentialfrom keras.layers import Dense, Activation, Flattenfrom keras.optimizers import Adamfrom rl.agents.dqn import DQNAgentfrom rl.policy import BoltzmannQPolicy, EpsGreedyQPolicyfrom rl.memory import SequentialMemoryimport ioimport sysimport 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 resultsold_stdout = sys.stdoutsys.stdout = mystdout = io.StringIO()# Evaluate our algorithm for a few episodes.dqn.test(env, nb_episodes=200, visualize=False)# Reset stdoutsys.stdout = old_stdoutresults_text = mystdout.getvalue()# Print results textprint("results")print(results_text)# Extact a rewards list from the resultstotal_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 averageprint("total rewards", total_rewards)print("average total reward", np.mean(total_rewards))# Write total rewards to filef = 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,但仍然不起作用,有人能帮我吗?


回答:

试试这个 ‘pip3 install box2d box2d-kengz’

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注