gpt4 book ai didi

python - 尝试使用 pyomo 和 bonmin 求解模型。模型未正确传递给求解器

转载 作者:行者123 更新时间:2023-12-01 07:50:04 24 4
gpt4 key购买 nike

我希望在有关 Kronqvist 的“扩展支持超平面算法”的运筹学研讨会中使用 Pyomo 和 Bonmin。我想用bonmin来比较算法的性能。首先我想测试一下 Bonmin 是否安装正确以及它是否与 pyomo 一起工作,所以我从上面提到的论文中建模了一个例子。这是一个凸问题,所以bonmin应该能够解决它。

我的操作系统是 Windows 7,因此我使用 Cygwin 安装了 Bonmin 并将其添加到我的 PATH 中。我在安装过程中没有注意到任何错误消息。对于编程,我使用 Anaconda 和 Jupyter Notebook。

我目前正在阅读 pyomos 文档,但到目前为止还没有任何运气。

from pyomo.environ import *
import numpy
import scipy

# Konkretes Optimierungsproblem

model = ConcreteModel(name = "Example 1")

model.x1 = Var(bounds=(1,20), within=Reals)
model.x2 = Var(bounds=(1,20), within=Integers)

model.obj = Objective(expr=(-1)*model.x1-model.x2)

model.g1 = Constraint(expr=0.15*((model.x1 - 8)**2)+0.1*((model.x2 - 6)**2)+0.025*exp(model.x1)*((model.x2)**(-2))-5<=0)
model.g2 = Constraint(expr=(model.x1)**(-1) + (model.x2)**(-1) - (model.x1)**(-0.5) * (model.x2) ** (-0.5)+4<=0)
model.l1 = Constraint(expr=2 * (model.x1) - 3 * (model.x2) -2<=0)

#Just some output to analze the generated model

print(model)
dir(model)
print(model.g2.expr)
model.x1 = 5
print(value(model.x1))

opt = SolverFactory('bonmin')
#opt.options['bonmin.algorithm'] = 'Bonmin'
print('using Bonmin')
# Set Options for solver.
opt.options['bonmin.solution_limit'] = '1'
opt.options['bonmin.time_limit'] = 1800
results = opt.solve(model)
results.write()

结果如下:

using Bonmin
WARNING: Loading a SolverResults object with a warning status into
model=Example 1;
message from solver=bonmin\x3a Infeasible problem
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Lower bound: -inf
Upper bound: inf
Number of objectives: 1
Number of constraints: 0
Number of variables: 0
Sense: unknown
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: warning
Message: bonmin\x3a Infeasible problem
Termination condition: infeasible
Id: 220
Error rc: 0
Time: 0.16000056266784668
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0

正如您所看到的,模型没有正确传输到求解器,因为根据求解器,问题既没有约束也没有变量。

问题可能是解算器的警告表明它安装不正确,还是由于问题不可行?

编辑:根据 Bethany Nicholson 在评论中的建议,我在代码中添加了选项 tee=tree ,这导致了以下输出

using Bonmin
Bonmin 1.8.7 using Cbc 2.10.0 and Ipopt 3.12.12
bonmin: bonmin.solution_limit=1
bonmin.time_limit=1800
bonmin.solution_limit=1
bonmin.time_limit=1800


******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
For more information visit http://projects.coin-or.org/Ipopt
******************************************************************************

NLP0012I
Num Status Obj It time Location
NLP0014I 1 INFEAS 4.0986776 27 0.031
NLP0014I 2 INFEAS 4.0986776 27 0.015
Cbc0006I The LP relaxation is infeasible or too expensive

"Finished"
WARNING: Loading a SolverResults object with a warning status into
model=Example 1;
message from solver=bonmin\x3a Infeasible problem
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Lower bound: -inf
Upper bound: inf
Number of objectives: 1
Number of constraints: 0
Number of variables: 0
Sense: unknown
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: warning
Message: bonmin\x3a Infeasible problem
Termination condition: infeasible
Id: 220
Error rc: 0
Time: 0.23000025749206543
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0

编辑2:我尝试过其他简单的问题。虽然 Bonmin 正确解决了问题(x1=4,x2=2,minvalue=-10),但其输出表明传递给求解器的问题没有约束,而它显然有五个(即使四个可能被解释为界限)。此外,输出对我来说仍然有点奇怪。为什么它说“解数 = 0”? (我还没有完成 pyomo 的完整文档,也许我只需要设置额外的参数)这是线性问题:

from pyomo.environ import *
import numpy
import scipy

model = ConcreteModel(name = "Linear problem")

model.x1 = Var(domain = Reals)
model.x2 = Var(domain = Reals)

model.obj = Objective(expr=-1*(2*model.x1+model.x2))

model.l1 = Constraint(expr=model.x1>=0)
model.l2 = Constraint(expr=model.x2>=0)
model.l3 = Constraint(expr=model.x1<=4)
model.l4 = Constraint(expr=model.x2<=4)
model.l5 = Constraint(expr=model.x1+model.x2<=6)
model.pprint()
2 Var Declarations
x1 : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : None : None : None : False : True : Reals
x2 : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : None : None : None : False : True : Reals

1 Objective Declarations
obj : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : minimize : - (2*x1 + x2)

5 Constraint Declarations
l1 : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : 0.0 : x1 : +Inf : True
l2 : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : 0.0 : x2 : +Inf : True
l3 : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : -Inf : x1 : 4.0 : True
l4 : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : -Inf : x2 : 4.0 : True
l5 : Size=1, Index=None, Active=True
Key : Lower : Body : Upper : Active
None : -Inf : x1 + x2 : 6.0 : True

8 Declarations: x1 x2 obj l1 l2 l3 l4 l5

opt = SolverFactory('bonmin')
opt.options['bonmin.solution_limit'] = '1'
opt.options['bonmin.time_limit'] = 1800
results = opt.solve(model, tee = True)
results.write()

Bonmin 1.8.7 using Cbc 2.10.0 and Ipopt 3.12.12
bonmin: bonmin.solution_limit=1
bonmin.time_limit=1800
bonmin.solution_limit=1
bonmin.time_limit=1800

Cbc3007W No integer variables - nothing to do

******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
For more information visit http://projects.coin-or.org/Ipopt
******************************************************************************

NLP0012I
Num Status Obj It time Location
NLP0014I 1 OPT -10 4 0
Cbc3007W No integer variables - nothing to do

"Finished"
# ==========================================================
# = Solver Results =
# ==========================================================
# ----------------------------------------------------------
# Problem Information
# ----------------------------------------------------------
Problem:
- Lower bound: -inf
Upper bound: inf
Number of objectives: 1
Number of constraints: 0
Number of variables: 2
Sense: unknown
# ----------------------------------------------------------
# Solver Information
# ----------------------------------------------------------
Solver:
- Status: ok
Message: bonmin\x3a Optimal
Termination condition: optimal
Id: 3
Error rc: 0
Time: 0.20000028610229492
# ----------------------------------------------------------
# Solution Information
# ----------------------------------------------------------
Solution:
- number of solutions: 0
number of solutions displayed: 0

如果我似乎在用代码向我的帖子发送垃圾邮件,我很抱歉。只是认为这会有所帮助,因为我认为这表明问题在于求解器而不是 pyomo 的建模。

最佳答案

昨天我和导师开了个会。我实际上忽略了模型中的一个错误,这使得它不可行。 Bonmin 正确地解决了这个问题,但输出看起来仍然很奇怪(例如模型没有约束)。他也确实理解求解器有些奇怪的输出,但是,他说我不应该再关心它。

对于犯下如此愚蠢的错误,我深表歉意。希望没有人花太多时间思考这个问题(当然除了我)。

关于python - 尝试使用 pyomo 和 bonmin 求解模型。模型未正确传递给求解器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56279728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com