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

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 
 

参考链接:

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

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Matlab使用Ipopt可以通过AMPL来调用。AMPL是一种建模语言工具,它允许用户以类似于数学方式编写问题的语法来编写优化问题。在AMPL制定了问题后,可以使用已编译的Ipopt AMPL求解器可执行文件ipopt来解决问题。通过直接链接代码来连接您的问题需要花费一些时间来编写,但对于大型问题可能更有效。的示例代码。该代码展示了如何使用AMPL语法在Matlab调用Ipopt求解器来解决一个非线性优化问题。代码定义了变量和约束,并指定了目标函数。然后通过使用solve命令来解决问题,并使用display命令来显示解的结果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [MATLAB使用IPOPT去解NLP问题接口AMPL 工具](https://blog.csdn.net/caokaifa/article/details/125535963)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [利用APOPT、IPOPT等大规模非线性规划求解器,解决数据调和、移动视界估计、实时优化、动态仿真、非线性MPC...](https://blog.csdn.net/weixin_46039719/article/details/131197213)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值