• MATLAB中使用IPOPT去解NLP问题的接口:AMPL 工具


    Ipopt 被设计为灵活适用于各种应用程序,并且有多种方式与 Ipopt 接口,允许特定的数据结构和线性求解器技术。尽管如此,已经包含了一个标准表示,应该满足大多数用户。

    这个教程将讨论MALTAB中AMPL调用Ipopt去解NLP问题。

    AMPL 是一种建模语言工具,它允许用户以类似于数学方式编写问题的语法编写优化问题。一旦在 AMPL 中制定了问题,就可以使用(已编译的)Ipopt AMPL 求解器可执行文件 ipopt 轻松解决问题。通过直接链接代码来连接您的问题需要花费点时间来编写,但对于大型问题可能更有效。

    接下来使用例子来测试:

    开始点:

     

    最优解:

     

     matlab 中通过AMPL使用Ipopt 

    1. # tell ampl to use the ipopt executable as a solver
    2. # make sure ipopt is in the path!
    3. option solver ipopt;
    4. # declare the variables and their bounds,
    5. # set notation could be used, but this is straightforward
    6. var x1 >= 1, <= 5;
    7. var x2 >= 1, <= 5;
    8. var x3 >= 1, <= 5;
    9. var x4 >= 1, <= 5;
    10. # specify the objective function
    11. minimize obj:
    12. x1 * x4 * (x1 + x2 + x3) + x3;
    13. # specify the constraints
    14. s.t.
    15. inequality:
    16. x1 * x2 * x3 * x4 >= 25;
    17. equality:
    18. x1^2 + x2^2 + x3^2 +x4^2 = 40;
    19. # specify the starting point
    20. let x1 := 1;
    21. let x2 := 5;
    22. let x3 := 5;
    23. let x4 := 1;
    24. # solve the problem
    25. solve;
    26. # print the solution
    27. display x1;
    28. display x2;
    29. display x3;
    30. 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 
     

    参考链接:

    NLP 3:Interfacing your NLP to Ipopt - 走看看

  • 相关阅读:
    有什么自定义表单工具功能较好?
    数据库系统原理与应用教程(073)—— MySQL 练习题:操作题 131-140(十七):综合练习
    Matlab:有关字符串数组的常见问题解答
    Android | 再探 RecyclerView 之名词解析
    JavaScript 伪数组和数组
    uniapp pc端和移动端列表上拉刷新加载分页
    【译】ASP.NET Core 6 中的性能改进
    解决flex:1失效的问题
    【附源码】计算机毕业设计JAVA房产客户信息管理系统
    【docker安装Mysql并配置主从复制】
  • 原文地址:https://blog.csdn.net/caokaifa/article/details/125535963