• NLP 4:Interfacing your NLP to Ipopt 流程翻译


    博客转自:https://www.cnblogs.com/lcchuguo/p/5407709.html

    简单介绍

      ipopt是一个解决非线性规划最优化问题的工具集,当然,它也能够用于解决线性规划问题的求解。它提供了c/c++接口,很易于使用。

    问题

    解决类似以下的非线性问题:

    Ipopt工具採用内点法求解非线性优化问题。

    求解前的准备

    需要计算

    1. 梯度

    计算目标函数的梯度,和约束条件Jacobian矩阵

    2. Hessian矩阵

    delta and lambda are parameters for object function and constraints functions (lambda is multiplier of  Lagrangian)

    演示样例

    求解以下的最优化问题:

    1. 求解目标函数的梯度:
    2. 求解约束条件的Jacobian矩阵
    3. 求解目标函数和约束条件的Hessian矩阵。即求解

    至此,准备工作已经就绪,接下来调用Ipopt 的API接口进行计算。

    1.get_nlp_info设置以下的參数

    1. n=4;//变量x个数
    2.  m=2;//约束条件个数
    3.  nnz_jac_g=8;//Jacobian非零个数
    4. Nnz_h_lag=10;//Hessian非零个数   

    2.get_bounds_info 设置以下的參数

    1. x_l[i]设置xi的下界值
    2. x_u[i]设置xi的上界值
    3. g_l[i]设置约束i的下界值
    4.  g_u[i]设置约束i的上界值

    3.get_start_point设置以下參数

    1. x[i]设置第i个变量的初始迭代值

    4.eval_f设置以下參数

    1. bject_value设置目标函数计算方式(本例:object_value=x0*x3*(x0+x1+x2) + x2)

    5.eval_grad_f设置目标函数的梯度

    1. grad_f[i]设置目标函数对第i个变量的偏导。本比例如以下:

    6.eval_g设置约束条件

    1. G[i]约束条件i,本比例如以下:

    7.eval_jac_g设置Jacobian矩阵

    1. iRow和jCol设置非零行列的坐标
    2. Values设置矩阵迭代值,假设values==NULL。即尚未初始化时。须要设置Jacobian矩阵哪些下标位置非零。例如以下图:

    8.eval_h设置Hessian矩阵

    1. iRow和jCol设置非零行列的坐标
    2. obj_factor为目标函数系数
    3. lambda[i]为第i个约束的拉格朗日乘子
    4. values设置矩阵的迭代求值,本例仅仅有目标函数和两个约束条件,因此如所看到的。

    i.  目标函数

    ii.  约束1

    iii.  约束2

    9.finalize_solution求解

    1. status为返回的求解状态
    2. obj_value:最优值
    3.  x:最优解变量取值
    4.  z_l 拉格朗日乘子下界
    5. z_u 拉格朗日乘子上届
    6. lambda 最优解拉格朗日乘子取值 

    C++ API

    源代码位于: https://github.com/coin-or/Ipopt, 附带的例子在/path2Ipopt/Ipopt-3.12.11/Ipopt/examples.

    自己定义类继承于TNLP (public TNLP),使用命名空间:Ipopt (using namespace Ipopt),程序实现下面的虚函数即可

    /**@name Overloaded from TNLP */
    
      //@{
    
      /** Method to return some info about the nlp */
    
      virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
    
                                Index& nnz_h_lag, IndexStyleEnum& index_style);
    
     
    
      /** Method to return the bounds for my problem */
    
      virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
    
                                   Index m, Number* g_l, Number* g_u);
    
     
    
      /** Method to return the starting point for the algorithm */
    
      virtual bool get_starting_point(Index n, bool init_x, Number* x,
    
                                      bool init_z, Number* z_L, Number* z_U,
    
                                      Index m, bool init_lambda,
    
                                      Number* lambda);
    
     
    
      /** Method to return the objective value */
    
      virtual bool eval_f(Index n, const Number* x, bool new_x, Number& obj_value);
    
     
    
      /** Method to return the gradient of the objective */
    
      virtual bool eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f);
    
     
    
      /** Method to return the constraint residuals */
    
      virtual bool eval_g(Index n, const Number* x, bool new_x, Index m, Number* g);
    
     
    
      /** Method to return:
    
       *   1) The structure of the jacobian (if "values" is NULL)
    
       *   2) The values of the jacobian (if "values" is not NULL)
    
       */
    
      virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
    
                              Index m, Index nele_jac, Index* iRow, Index *jCol,
    
                              Number* values);
    
     
    
      /** Method to return:
    
       *   1) The structure of the hessian of the lagrangian (if "values" is NULL)
    
       *   2) The values of the hessian of the lagrangian (if "values" is not NULL)
    
       */
    
      virtual bool eval_h(Index n, const Number* x, bool new_x,
    
                          Number obj_factor, Index m, const Number* lambda,
    
                          bool new_lambda, Index nele_hess, Index* iRow,
    
                          Index* jCol, Number* values);
    
     
    
      //@}
    
     
    
     /** @name Solution Methods */
    
      //@{
    
      /** This method is called when the algorithm is complete so the TNLP can store/write the solution */
    
      virtual void finalize_solution(SolverReturn status,
    
                                     Index n, const Number* x, const Number* z_L, const Number* z_U,
    
                                     Index m, const Number* g, const Number* lambda,
    
                                     Number obj_value,
    
                                     const IpoptData* ip_data,
    
                                     IpoptCalculatedQuantities* ip_cq);
    
      //@}

  • 相关阅读:
    HTTP模拟工具【C#/Winform源码】、Json绑定TreeView控件、使用了MetroModernUI、RestSharp、Dapper.Net、Newtonsoft.Json、SmartThreadPool这几个主要开源框架
    Wince 创新布局
    数据解析
    Scrapy
    Requests模块
    爬虫简介
    周总结,基础的东西,全靠练习,多练就熟了.
    我的编程之路,从python重新开始。
    Python根据日期判断周几
    Celery定时任务
  • 原文地址:https://www.cnblogs.com/flyinggod/p/12953392.html
Copyright © 2020-2023  润新知