在 Pyomo 中构建模型并尝试在 google colab 中使用 gurobi 求解器

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

我在 google colab 中使用 pyomo 构建了一个模型,这是我的兴趣所在。模型太大。 当我使用下面的代码时,它返回一个错误,说我需要一个完整的许可证才能使用 gurobi 求解器:

...model constructed with pyomo.....

#solve the pyomo model:
opt = SolverFactory("gurobi", solver_io="python")
out = opt.solve(model)

我不想在 Gurobi 环境中创建模型。我已经用 pyomo 创建了它,我只想使用求解器。所以我在 gurobi.com 申请了 WLS 许可证。 现在有了凭据,我按照gurobi网站中的说明尝试了以下代码:

...model constructed with pyomo.....

#SOLVING THE PYOMO MODEL:
############# IF USING GUROBI SOLVER (CONVEX APPLICATIONS - LINEAR, CONIC OR QUADRATIC)############
!pip install gurobipy pyomo
import gurobipy as gp
params = {  # Create an environment with your WLS license
"WLSACCESSID": 'STRING INPUT',
"WLSSECRET": 'STRING INPUT',
"LICENSEID": INT INPUT,
}
env = gp.Env(params=params)

# Create the model within the Gurobi environment
#model = gp.Model(env=env)
model = gp.Model(env=env)
#solver: gurobi
opt = SolverFactory("gurobi", solver_io="python")
out = opt.solve(model)

但正如我之前所说,我不想使用 Gurobi Environment 创建,所以这段代码似乎覆盖了我原来的 pyomo 模型,并且仍然存在以下错误:

`
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: gurobipy in /usr/local/lib/python3.10/dist-packages (10.0.1)
Requirement already satisfied: pyomo in /usr/local/lib/python3.10/dist-packages (6.5.0)
Requirement already satisfied: ply in /usr/local/lib/python3.10/dist-packages (from pyomo) (3.11)
Set parameter WLSAccessID
Set parameter WLSSecret
Set parameter LicenseID to value "my license"
Academic license - for non-commercial use only - registered to "my email"
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-230-bd70bd49a037> in <cell line: 28>()
     26 #solver: gurobi
     27 opt = SolverFactory("gurobi", solver_io="python")
---> 28 out = opt.solve(model)
     29 
     30 

3 frames
/usr/local/lib/python3.10/dist-packages/pyomo/solvers/plugins/solvers/direct_or_persistent_solver.py in _set_instance(self, model, kwds)
    183                 "'_presolve' method must be a Model or a Block".format(type(self))
    184             )
--> 185             raise ValueError(msg)
    186         self._pyomo_model = model
    187         self._symbolic_solver_labels = kwds.pop(

ValueError: The problem instance supplied to the <class 'pyomo.solvers.plugins.solvers.gurobi_direct.GurobiDirect'> plugin '_presolve' method must be a Model or a Block
`

拜托,解决我构建的 pyomo 模型的代码应该是什么? 谢谢!

python google-colaboratory mathematical-optimization pyomo gurobi
© www.soinside.com 2019 - 2024. All rights reserved.