Ipopt 被设计为灵活适用于各种应用程序,并且有多种方式与 Ipopt 接口,允许特定的数据结构和线性求解器技术。尽管如此,已经包含了一个标准表示,应该满足大多数用户。
这个教程将讨论MALTAB中AMPL调用Ipopt去解NLP问题。
AMPL 是一种建模语言工具,它允许用户以类似于数学方式编写问题的语法编写优化问题。一旦在 AMPL 中制定了问题,就可以使用(已编译的)Ipopt AMPL 求解器可执行文件 ipopt 轻松解决问题。通过直接链接代码来连接您的问题需要花费点时间来编写,但对于大型问题可能更有效。
接下来使用例子来测试:
开始点:
最优解:
matlab 中通过AMPL使用Ipopt
# tell ampl to use the ipopt executable as a solver
# make sure ipopt is in the path!
option solver ipopt;
# declare the variables and their bounds,
# set notation could be used, but this is straightforward
var x1 >= 1, <= 5;
var x2 >= 1, <= 5;
var x3 >= 1, <= 5;
var x4 >= 1, <= 5;
# specify the objective function
minimize obj:
x1 * x4 * (x1 + x2 + x3) + x3;
# specify the constraints
s.t.
inequality:
x1 * x2 * x3 * x4 >= 25;
equality:
x1^2 + x2^2 + x3^2 +x4^2 = 40;
# specify the starting point
let x1 := 1;
let x2 := 5;
let x3 := 5;
let x4 := 1;
# solve the problem
solve;
# print the solution
display x1;
display x2;
display x3;
display x4;
具体代码例子:
MALTAB中AMPL调用Ipopt去解NLP问题-机器学习文档类资源-CSDN下载
运行代码,解压,定位到工作目录中,使用!ampl hs071_ampl.mod运行即可:
运行后结果:
******************************************************************************
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
******************************************************************************
This is Ipopt version 3.12.1, running with linear solver ma27.
Number of nonzeros in equality constraint Jacobian...: 4
Number of nonzeros in inequality constraint Jacobian.: 4
Number of nonzeros in Lagrangian Hessian.............: 10
Total number of variables............................: 4
variables with only lower bounds: 0
variables with lower and upper bounds: 4
variables with only upper bounds: 0
Total number of equality constraints.................: 1
Total number of inequality constraints...............: 1
inequality constraints with only lower bounds: 1
inequality constraints with lower and upper bounds: 0
inequality constraints with only upper bounds: 0
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
0 1.6109693e+001 1.12e+001 5.28e-001 0.0 0.00e+000 - 0.00e+000 0.00e+000 0
1 1.7410406e+001 7.49e-001 2.25e+001 -0.3 7.97e-001 - 3.19e-001 1.00e+000f 1
2 1.8001613e+001 7.52e-003 4.96e+000 -0.3 5.60e-002 2.0 9.97e-001 1.00e+000h 1
3 1.7199482e+001 4.00e-002 4.24e-001 -1.0 9.91e-001 - 9.98e-001 1.00e+000f 1
4 1.6940955e+001 1.59e-001 4.58e-002 -1.4 2.88e-001 - 9.66e-001 1.00e+000h 1
5 1.7003411e+001 2.16e-002 8.42e-003 -2.9 7.03e-002 - 9.68e-001 1.00e+000h 1
6 1.7013974e+001 2.03e-004 8.65e-005 -4.5 6.22e-003 - 1.00e+000 1.00e+000h 1
7 1.7014017e+001 2.76e-007 2.18e-007 -10.3 1.43e-004 - 9.99e-001 1.00e+000h 1
8 1.7014017e+001 2.84e-014 2.29e-014 -11.0 1.04e-007 - 1.00e+000 1.00e+000h 1
Number of Iterations....: 8
(scaled) (unscaled)
Objective...............: 1.7014017140224134e+001 1.7014017140224134e+001
Dual infeasibility......: 2.2928101314633036e-014 2.2928101314633036e-014
Constraint violation....: 2.8421709430404007e-014 2.8421709430404007e-014
Complementarity.........: 1.0023967333275279e-011 1.0023967333275279e-011
Overall NLP error.......: 1.0023967333275279e-011 1.0023967333275279e-011
Number of objective function evaluations = 9
Number of objective gradient evaluations = 9
Number of equality constraint evaluations = 9
Number of inequality constraint evaluations = 9
Number of equality constraint Jacobian evaluations = 9
Number of inequality constraint Jacobian evaluations = 9
Number of Lagrangian Hessian evaluations = 8
Total CPU secs in IPOPT (w/o function evaluations) = 0.002
Total CPU secs in NLP function evaluations = 0.000
EXIT: Optimal Sol
Ipopt 3.12.1: Optimal Solution Found
suffix ipopt_zU_out OUT;
suffix ipopt_zL_out OUT;
x1 = 1
x2 = 4.743
x3 = 3.82115
x4 = 1.37941
参考链接: