我希望在有关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建模。
答案 0 :(得分:0)
昨天我和老师开会了。实际上,我忽略了模型中的错误,这使其不可行。 Bonmin可以正确解决问题,但输出仍然看起来很奇怪(例如,模型没有约束)。他确实理解求解器的输出有些奇怪,但是,他说我不应该再担心它了。
对于如此愚蠢的错误,我深表歉意。希望没人花很多时间思考这个问题(当然除了我以外)。