Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试使用pyomo和bonmin求解模型。模型未正确传递到解算器_Python_Jupyter Notebook_Solver_Pyomo - Fatal编程技术网

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

Python 尝试使用pyomo和bonmin求解模型。模型未正确传递到解算器,python,jupyter-notebook,solver,pyomo,Python,Jupyter Notebook,Solver,Pyomo,我希望在一个运筹学研讨会上使用Pyomo和Bonmin,该研讨会涉及Kronqvist的“扩展支持超平面算法”。我希望使用bonmin来比较算法的性能。首先,我想测试Bonmin是否正确安装,以及它是否与pyomo一起工作,因此我根据上面提到的文章建模了一个示例。这是一个凸问题,所以bonmin应该能够解决它 我的操作系统是Windows7,所以我使用Cygwin安装了Bonmin,并将其添加到我的路径中。在安装过程中,我没有注意到任何错误消息。对于编程,我使用Anaconda和Jupyter笔

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

我的操作系统是Windows7,所以我使用Cygwin安装了Bonmin,并将其添加到我的路径中。在安装过程中,我没有注意到任何错误消息。对于编程,我使用Anaconda和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()
如您所见,模型没有正确地传递到解算器,因为根据解算器,问题既没有约束也没有变量

问题可能是解算器的警告杆,这意味着它没有正确安装,还是因为问题不可行

编辑:正如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.environ导入*
进口numpy
进口西皮
模型=混凝土模型(名称=“线性问题”)
model.x1=Var(domain=Reals)
model.x2=Var(域=Reals)
model.obj=目标(expr=-1*(2*model.x1+model.x2))
model.l1=约束(expr=model.x1>=0)
model.l2=约束(expr=model.x2>=0)

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


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

看起来您的模型有问题,至少您有一个自由度问题,因为您声明了2个变量和3个约束。通过将
tee=True
选项添加到调用
solve
,您可以看到更详细的解算器输出。所有约束都是不等式。因此,自由度不应该是问题m、 对吗?“我为犯了这么愚蠢的错误深表歉意”你怎么知道?无论如何,每个人都会犯错误。下次再来!results对象有一些问题(请参阅)。
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