Pyomo无法使用Gurobi求解器

时间:2019-01-19 15:35:03

标签: python-3.x gurobi pyomo

我首先要说我是python&pyomo的新手。

我已经完成了一个在使用“ glpk”求解器时可以正常运行的程序,并且能够获得预期的结果,因此,我对该程序的运行充满信心。以下是代码。

import pandas as pd
from pyomo.environ import *
import numpy as np


rawfile = "C:/Users/User/Downloads/chickenwings2.csv"

df_raw = pd.read_csv(rawfile, index_col='Name')
print (df_raw)

Set = df_raw.index.tolist()
count = dict(zip(df_raw.index,df_raw['count']))
price = dict(zip(df_raw.index,df_raw['price']))

#print (count,price)

model = ConcreteModel()
model.x = Var(Set, within=NonNegativeIntegers)

model.obj = Objective(expr= sum(price[i]*model.x[i] for i in Set), sense=minimize)

model.count_con = Constraint(expr=sum(count[i]*model.x[i] for i in Set) == 200)

opt = SolverFactory("glpk") 
opt_success = opt.solve(model)

total_count = sum(count[i]*value(model.x[i]) for i in Set)
print('Total Count:', total_count)
print('Total Price:', value(model.obj))

print('%5s %5s %12s' % ('Set','Count', 'Order Count'))
print('=========================')

for i in Set:
    if value(model.x[i]>0):
        print ('%5s %5s %5s' % (i,count[i], value(model.x[i])))

print('=========================')

但是,当我尝试通过如下所示更改代码来使用gurobi求解器时,我无法获得任何结果。

opt = SolverFactory("gurobi", solver_io="python") 

opt = SolverFactory("gurobi")

错误:

Traceback (most recent call last):
  File "D:/Python learning/ProjektX/chicken wings.py", line 26, in <module>
    opt_success = opt.solve(model)
  File "D:\EngineeringSoftware\Anaconda\lib\site-packages\pyomo\solvers\plugins\solvers\direct_solver.py", line 68, in solve
    self.available(exception_flag=True)
  File "D:\EngineeringSoftware\Anaconda\lib\site-packages\pyomo\solvers\plugins\solvers\direct_or_persistent_solver.py", line 301, in available
    "plugin").format(type(self)))
pyutilib.common._exceptions.ApplicationError: No Python bindings available for <class 'pyomo.solvers.plugins.solvers.gurobi_direct.GurobiDirect'> solver plugin

WARNING: Could not locate the 'gurobi' executable, which is required for
    solver gurobi

对于gurobi的安装,我已经使用anaconda遵循了gurobi的安装过程

conda config --add channels http://conda.anaconda.org/gurobi
conda install gurobi

我已经在GRB_LICENSE_FILE名称下创建了一个环境变量,并将变量值定向到.lic文件的位置。

我已经用尽了所有可以从互联网上找到的所有选项,因此我在这里请求pyomo社区的帮助来启发我。

3 个答案:

答案 0 :(得分:2)

听起来您需要将包含gurobi可执行文件的文件夹添加到Windows和Anaconda路径。

有关此操作的逐步指南,请参见此stackoverflow帖子:How to add a folder to `Path` environment variable in Windows 10 (with screenshots)

有关在您的conda路径中添加文件夹的信息,请参见此帖子:How to add folder to search path for a given Anaconda environment?

答案 1 :(得分:1)

关于您尝试在此行中将Python接口用于Gurobi:

  

opt = SolverFactory("gurobi", solver_io="python")

您可能会获得

Traceback (most recent call last):
  File "D:/Python learning/ProjektX/chicken wings.py", line 26, in <module>
    opt_success = opt.solve(model)
  File "D:\EngineeringSoftware\Anaconda\lib\site-packages\pyomo\solvers\plugins\solvers\direct_solver.py", line 68, in solve
    self.available(exception_flag=True)
  File "D:\EngineeringSoftware\Anaconda\lib\site-packages\pyomo\solvers\plugins\solvers\direct_or_persistent_solver.py", line 301, in available
    "plugin").format(type(self)))
pyutilib.common._exceptions.ApplicationError: No Python bindings available for <class 'pyomo.solvers.plugins.solvers.gurobi_direct.GurobiDirect'> solver plugin

错误。导致这种错误的原因之一是必须安装Gurobi的Python绑定。此绑定是Gurobi安装随附的,但默认情况下未安装。前提是您已经拥有有效的Gurobi许可证,建议您访问

http://www.gurobi.com/documentation/8.1/quickstart_mac/the_gurobi_python_interfac.html

要总结支持页面,

  1. 以管理员权限打开命令提示符;
  2. 导航到您的Gurobi安装所在的文件夹;
  3. 键入python setup.py install以运行Python绑定的安装。

您可以通过在Python控制台中输入import gurobipy行来检查它是否已安装。

答案 2 :(得分:0)

感谢Bethany.N和V.Brunelle的反馈。赞赏。

我尝试将文件夹添加到Windows和anaconda路径(错误代码仍然存在),并尝试安装gurobipy(Gurobi包中没有setup.py文件)。

但是,我使用Pycharm来运行代码,但是基于用户指南: “ http://www.gurobi.com/documentation/8.1/quickstart_mac/installing_the_anaconda_py.html#section:Anaconda

它明确提到要使用Spyder IDE或Jupyter,因此我将代码导入了Spyder中,我可以使代码正常工作。甚至可以使用[“ gurobi”]或[“ gurobi”,solver_io =“ python”]作为求解器。

Pycharm + gurobi似乎有一些问题。