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

2024-09-28 23:42:55 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我的操作系统是Windows7,所以我用Cygwin安装了Bonmin并将其添加到我的路径中。在安装过程中,我没有注意到任何错误消息。为了编程,我使用了Python与Jupyter笔记本。在

我目前正在阅读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()

结果如下:

^{pr2}$

如您所见,模型没有正确地传递到解算器,因为根据解算器,问题既没有约束也没有变量。在

问题可能是求解器的警告标志,意味着它没有正确安装,或者这是因为问题不可行?在

{bethas>在下面的代码中添加了由nicholas}添加的注释

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

编辑二:我试过其他简单的问题。虽然Bonmin正确地解决了问题(x1=4,x2=2,minvalue=-10),但是它的输出表明传递给解算器的问题没有约束,而它显然有5个约束(即使4个可能被解释为边界)。而且,输出对我来说还是有点奇怪。为什么它说“解决方案数=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的建模。在


Tags: ofnonetruemodelupperloweractivelimit
1条回答
网友
1楼 · 发布于 2024-09-28 23:42:55

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

我为犯了这么愚蠢的错误深表歉意。希望没有人花太多时间思考这个问题(当然除了我)。在

相关问题 更多 >