简体   繁体   English

在 Pyomo 中实现 MINLP 求解器“apopt”

[英]Implementation of MINLP solver "apopt" in Pyomo

I have a mixed integer non linear problem in Pyomo with an objective function and several constraints consisting of non-linear terms and binary variables.我在 Pyomo 中有一个混合的 integer 非线性问题,目标是 function 和几个由非线性项和二进制变量组成的约束。

The popular solver "ipopt" finds a solution, but it treats the binary variables as continuous variables.流行的求解器“ipopt”找到了一个解决方案,但它将二进制变量视为连续变量。

opt=SolverFactory("ipopt")
results=opt.solve(instance)
results.write()
instance.load(results)

Now I have already tried desperately to try two solvers that can solve mixed-integer non-linear problems.现在我已经拼命尝试了两个可以解决混合整数非线性问题的求解器。

  1. First I tried the MindPy solver ( https://pyomo.readthedocs.io/en/stable/contributed_packages/mindtpy.html ).首先,我尝试了 MindPy 求解器( https://pyomo.readthedocs.io/en/stable/contributed_packages/mindtpy.html )。 Unfortunately without success:不幸的是没有成功:

I always get the error message: " type NoneType doesn't define round method ".我总是收到错误消息:“类型 NoneType 没有定义圆形方法”。 This surprises me, because the ipopt-solver finds a solution without problems and the mindtpy-solver is a mixture of a linear solver and a non-linear solver and should actually get this solved.这让我感到惊讶,因为 ipopt-solver 找到了没有问题的解决方案,而 mindtpy-solver 是线性求解器和非线性求解器的混合体,实际上应该可以解决这个问题。

opt=SolverFactory('mindtpy').solve(instance, mip_solver="glpk", nlp_solver="ipopt", tee=True)
results=opt.solve(instance)
results.write()
instance.load(results)

2)Then I tried the apopt solver. 2)然后我尝试了apopt求解器。 You have to download it separately from "https://github.com/APMonitor/apopt" and put all files into the working directory.您必须从“https://github.com/APMonitor/apopt”单独下载它并将所有文件放入工作目录。

Then I tried to execute the following code, unfortunately without success:然后我尝试执行以下代码,不幸的是没有成功:

opt=SolverFactory("apopt.py")
results=opt.solve(instance)
results.write()
instance.load(results)

I always get the following error message: "Error message: [WinError 193] %1 is not a valid Win32 application ".我总是收到以下错误消息:“错误消息: [WinError 193] %1 不是有效的 Win32 应用程序”。 This is probably related to the fact that my Python interpreter requires an apopt.exe since I have a Windows machine.这可能与我的 Python 解释器需要 apopt.exe 相关,因为我有一台 Windows 机器。 Attempts such as converting the.py to an.exe file have failed.尝试将 .py 转换为 .exe 文件等尝试失败。 Also, specifying Solverfactory(..., "executable=C\Users\Python...\\apopt.py" separately did not work.此外,单独指定 Solverfactory(..., "executable=C\Users\Python...\\apopt.py" 不起作用。

Does anyone have an idea how to get the solver "apopt" and/or the solver "Mindtpy" to work and can do something with the error messages?有谁知道如何让求解器“接受”和/或求解器“Mindtpy”工作并且可以对错误消息做些什么? Thank you very much in advance!非常感谢您!

Edit:编辑:

Here is an exemplary and simple concrete model.这是一个示例性和简单的具体 model。 I have tried to translate it into easier code.我试图将它翻译成更简单的代码。 As I've already said, the ipopt solver finds a solution:正如我已经说过的,ipopt 求解器找到了一个解决方案:

model = pyo.ConcreteModel()

model.x = pyo.Var([1,2,3,4], domain=pyo.NonNegativeReals)

model.x = pyo.Var([5], domain=pyo.Binary)

model.OBJ = pyo.Objective(expr = 2*model.x[1] + 3*model.x[2] + 3*model.x[3] + 4*model.x[4])

model.Constraint1 = pyo.Constraint(expr = 3*model.x[1] + 4*model.x[2] >= 1)

model.Constraint2 = pyo.Constraint(expr = 3*model.x[3] + 4*model.x[4] >= 1)

model.Constraint3 =pyo.Constraint(expr = 1000*cos(model.x[3]) < 1000)

model. Constraint4=pyo.Constraint(expr = 1000*sin(model.x[4]) < 1000)

model.Constraint5=pyo.Constraint(expr = model.x[2] <= 10000*(1-model.x[5])

model.Constraint6= pyo.Constraint (expr=model.x[2] <= 10000*(model.x[5]))

"type NoneType doesn't define round method" “类型 NoneType 没有定义圆形方法”

You should (almost) never use a round() function in your MINLP model.您应该(几乎)永远不要在您的 MINLP model 中使用 round() function。 It is not needed either.它也不需要。 Instead, use an integer variable, like in:相反,使用 integer 变量,如:

x-0.5 <= y <= x+0.5 
x continuous variable
y integer variable

The reason why round() is really, really bad, is because it is non-differentiable and not continuous. round() 之所以非常非常糟糕,是因为它不可微且不连续。 Almost all NLP and MINLP solvers assume smooth functions (sometimes it is useful to read the documentation).几乎所有 NLP 和 MINLP 求解器都假定函数平滑(有时阅读文档很有用)。


After fixing your model (quite a few problems with it), I could not reproduce the error message about round().在修复您的 model(有很多问题)之后,我无法重现有关 round() 的错误消息。

D:\tmp>type pyom1.py
import pyomo.environ as pyo

model = pyo.ConcreteModel()

model.x = pyo.Var([1,2,3,4], domain=pyo.NonNegativeReals)
model.y = pyo.Var(domain=pyo.Binary)

model.OBJ = pyo.Objective(expr = 2*model.x[1] + 3*model.x[2] + 3*model.x[3] + 4*model.x[4])

model.Constraint1 = pyo.Constraint(expr = 3*model.x[1] + 4*model.x[2] >= 1)
model.Constraint2 = pyo.Constraint(expr = 3*model.x[3] + 4*model.x[4] >= 1)
model.Constraint3 = pyo.Constraint(expr = 1000*pyo.cos(model.x[3]) <= 1000)
model.Constraint4 = pyo.Constraint(expr = 1000*pyo.sin(model.x[4]) <= 1000)
model.Constraint5 = pyo.Constraint(expr = model.x[2] <= 10000*(1-model.y))
model.Constraint6 = pyo.Constraint (expr=model.x[2] <= 10000*(model.y))

pyo.SolverFactory('mindtpy').solve(model, mip_solver='cbc', nlp_solver='ipopt', tee=True)


D:\tmp>python.exe pyom1.py
INFO: ---Starting MindtPy---
INFO: Original model has 6 constraints (2 nonlinear) and 0 disjunctions, with
    5 variables, of which 1 are binary, 0 are integer, and 4 are continuous.
INFO: rNLP is the initial strategy being used.
INFO: NLP 1: Solve relaxed integrality
INFO: NLP 1: OBJ: 1.666666661289117  LB: -inf  UB: inf
INFO: ---MindtPy Master Iteration 0---
INFO: MIP 1: Solve master problem.
INFO: MIP 1: OBJ: 1.6666666499999998  LB: 1.6666666499999998  UB: inf
INFO: NLP 2: Solve subproblem for fixed binaries.
INFO: NLP 2: OBJ: 1.6666666716089886  LB: 1.6666666499999998  UB:
    1.6666666716089886
INFO: MindtPy exiting on bound convergence. LB: 1.6666666499999998 + (tol
    0.0001) >= UB: 1.6666666716089886

D:\tmp>

Try adding the path to apopt.py to the PATH variable.尝试将 apopt.py 的路径添加到PATH变量中。 The apopt.py program acts like an executable with the model.nl as an argument to the solver and it produces a sol solution file that is then processed to retrieve the solution. apopt.py 程序就像一个可执行文件,将model.nl作为求解器的参数,它生成一个sol解决方案文件,然后对其进行处理以检索解决方案。 Unlike other solvers in AIMS or Pyomo, APOPT computes remotely on a public server.与 AIMS 或 Pyomo 中的其他求解器不同,APOPT 在公共服务器上进行远程计算。 Here are additional instructions on running APOPT.以下是有关运行 APPT 的附加说明

APOPT Solver APPT 求解器

APOPT (for Advanced Process OPTimizer) is a software package for solving large-scale optimization problems of any of these forms: APOPT(用于 Advanced Process OPTimizer)是一款软件 package,用于解决以下 forms 中的任何一个的大规模优化问题:

  • Linear programming (LP)线性规划 (LP)
  • Quadratic programming (QP)二次规划 (QP)
  • Quadratically constrained quadratic program (QCQP)二次约束二次规划 (QCQP)
  • Nonlinear programming (NLP)非线性规划 (NLP)
  • Mixed integer programming (MIP)混合 integer 编程(MIP)
  • Mixed integer linear programming (MILP)混合 integer 线性规划(MILP)
  • Mixed integer nonlinear programming (MINLP)混合 integer 非线性规划(MINLP)

Applications of the APOPT include chemical reactors, friction stir welding, prevention of hydrate formation in deep-sea pipelines, computational biology, solid oxide fuel cells, and flight controls for Unmanned Aerial Vehicles (UAVs). APPT 的应用包括化学反应器、搅拌摩擦焊接、防止深海管道中的水合物形成、计算生物学、固体氧化物燃料电池和无人驾驶飞行器 (UAV) 的飞行控制。 APOPT is supported in AMPL, APMonitor, Gekko, and Pyomo. AMPL、APMonitor、Gekko 和 Pyomo 支持 APOPT。

APOPT Online Solver for Mixed Integer Nonlinear Programming Reads output from AMPL, Pyomo, or other NL File Writers.用于混合 Integer 非线性编程的 APOPT 在线求解器从 AMPL、Pyomo 或其他 NL 文件编写器读取 output。 Similar to other solvers, this script reads the model (NL) file and produces a solution (sol) file.与其他求解器类似,此脚本读取 model (NL) 文件并生成解 (sol) 文件。 It sends the NL file to a remote server, computes a solution (remotely), and retrieves a solution (sol) file through an internet connection.它将 NL 文件发送到远程服务器,(远程)计算解决方案,并通过 Internet 连接检索解决方案 (sol) 文件。 It communicates with the server http://byu.apopt.com that is hosting the APOPT solver.它与托管 APPT 求解器的服务器http://byu.apopt.com通信。 Contact support@apmonitor.com for support, especially if there is a feature request or a concern about a problem solution.联系 support@apmonitor.com 以获得支持,尤其是在有功能请求或对问题解决方案有疑虑时。

Instructions for usage:使用说明:

  • Place apopt.py in an appropriate folder in the system path (eg Linux, /usr/bin/)将 apopt.py 放在系统路径中的适当文件夹中(例如 Linux、/usr/bin/)
  • Set appropriate permissions to make the script executable (eg chmod 775 apopt.py)设置适当的权限以使脚本可执行(例如 chmod 775 apopt.py)
  • In AMPL, Pyomo, or other NL file write, set solver option to apopt.py在 AMPL、Pyomo 或其他 NL 文件写入中,将求解器选项设置为 apopt.py
  • Test installation by running apopt.py -test通过运行 apopt.py -test 测试安装
  • Visit apopt.com for additional information and solver option help访问 apopt.com 了解更多信息和求解器选项帮助

Information on the APOPT solver with references can be found at the Wikipedia article for APOPT.可以在Wikipedia 文章中找到有关 APPT 求解器的信息以及参考资料。 APOPT has integration with Gekko and can run locally with m=GEKKO(remote=False) . APPT 与Gekko集成,可以使用m=GEKKO(remote=False)在本地运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM