首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:模块'cplex‘没有属性'Cplex’

AttributeError:模块'cplex‘没有属性'Cplex’
EN

Stack Overflow用户
提问于 2019-10-08 07:05:41
回答 2查看 1.8K关注 0票数 1

我试图使用Python中的CPLEX优化一个线性规划问题。我已经安装了,以及Python中的docplex。

当我运行程序时,我有以下错误:

代码语言:javascript
复制
AttributeError: module 'cplex' has no attribute 'Cplex'

有用代码:

代码语言:javascript
复制
import docplex.mp.model as cpx

import random

import pandas as pd

n = 10
m = 5
set_I = range(1, n+1)
set_J = range(1, m+1)
c = {(i,j): random.normalvariate(0,1) for i in set_I for j in set_J}
a = {(i,j): random.normalvariate(0,5) for i in set_I for j in set_J}
l = {(i,j): random.randint(0,10) for i in set_I for j in set_J}
u = {(i,j): random.randint(10,20) for i in set_I for j in set_J}
b = {j: random.randint(0,30) for j in set_J}


opt_model = cpx.Model(name="MIP Model")

# if x is Binary
x_vars  = {(i,j): opt_model.binary_var(name="x_{0}_{1}".format(i,j)) for i in set_I for j in set_J}


# <= constraints
constraints = {j : opt_model.add_constraint(ct=opt_model.sum(a[i,j] * x_vars[i,j] for i in set_I) <= b[j], ctname="constraint_{0}".format(j)) for j in set_J}

objective = opt_model.sum(x_vars[i,j] * c[i,j] for i in set_I  for j in set_J)

opt_model.minimize(objective)

opt_model.solve()


opt_df = pd.DataFrame.from_dict(x_vars, orient="index", columns = ["variable_object"])

opt_df.index = pd.MultiIndex.from_tuples(opt_df.index, names=["column_i", "column_j"])
opt_df.reset_index(inplace=True)

opt_df["solution_value"] = opt_df["variable_object"].apply(lambda item: item.solution_value)

print(opt_df)

我从:https://medium.com/@m.moarefdoost/optimization-modeling-in-python-pulp-gurobi-and-cplex-7f25acb03d7d提取了这段代码

我是CPLEX和Python的初学者,所以我试着运行这段代码来验证我已经安装了所有的东西。

有人遇到过同样的问题吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-09 20:57:28

我发现了我的代码错误。实际上,在运行代码之前,我还没有正确安装setup.py。

做完之后,一切都很好!

票数 1
EN

Stack Overflow用户

发布于 2020-12-14 17:11:02

我也遇到了同样的问题。在设置CPLEX的Python时,我犯了几个错误。

  1. 我没有使用位于yourCplexhome/python/VERSION/PLATFORM中的脚本setup.py,而是使用位于CPLEX_Studio1210/python中的脚本setup.py。
  2. 我没有将PYTHONPATH环境变量PYTHONPATH设置为yourCplexhome/python/VERSION/PLATFORM的值

来源

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58281600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档