非线性规划(NLP)求解

根据官网例程实现非线性规划问题求解。

2018-10-17_135910.jpg

文件demo.cpp代码。


#include 
#include 
#include 
#include 
#include "Eigen-3.3/Eigen/Core"
#include "Eigen-3.3/Eigen/QR"

using CppAD::AD;

class FG_eval{
 public:
  typedef CPPAD_TESTVECTOR( AD ) ADvector;
  void operator()(ADvector& fg,const ADvector& x){
    assert(fg.size()==3);
    assert(x.size()==4);
    //var
    AD x1=x[0];
    AD x2=x[1];
    AD x3=x[2];
    AD x4=x[3];
    //cost function f(x)
    fg[0]=x1*x4*(x1+x2+x3)+x3;
    //constraints function
    fg[1]=x1*x2*x3*x4;
    fg[2]=x1*x1+x2*x2+x3*x3+x4*x4;

    return;
  }
};



int main() {
 bool ok=true;
 size_t i;
 typedef CPPAD_TESTVECTOR(double) Dvector;
 
 // number of independent vars
 size_t nx=4;
 // number of constraints
 size_t ng=2;
 // initial value of the independent vars
 Dvector xi(nx);
 xi[0]=1.0;
 xi[1]=5.0;
 xi[2]=5.0;
 xi[3]=1.0;
 //lower and upper limits for var
 Dvector xl(nx), xu(nx);
 for(i=0;i solution;

 // solve the problem
 CppAD::ipopt::solve(
      options,xi,xl,xu,gl,gu,fg_eval,solution
 );

 // Check some of the solution values

 ok &=solution.status == CppAD::ipopt::solve_result::success;

 double check_x[]  = { 1.000000, 4.743000, 3.82115, 1.379408 };
 double check_zl[] = { 1.087871, 0.,       0.,      0.       };
 double check_zu[] = { 0.,       0.,       0.,      0.       };
 double rel_tol    = 1e-6;  // relative tolerance
 double abs_tol    = 1e-6;  // absolute tolerance
 std::vector array; 
 for(i = 0; i < nx; i++)
   {    ok &= CppAD::NearEqual(
               check_x[i],  solution.x[i],   rel_tol, abs_tol
       // array[i]=solution.x[i];
          );

          ok &= CppAD::NearEqual(
               check_zl[i], solution.zl[i], rel_tol, abs_tol
          );
          ok &= CppAD::NearEqual(
               check_zu[i], solution.zu[i], rel_tol, abs_tol
          );

   }

 std::cout << ok << std::endl;

 std::cout << solution.x[0] << std::endl;
 std::cout << solution.x[1] << std::endl;
 std::cout << solution.x[2] << std::endl;
 std::cout << solution.x[3] << std::endl;
 std::cout << solution.zl[0] << std::endl;
}

说明:变量初始值/变量下限/变量上限/约束下限/约束上限/目标函数+等式不等式约束/
类FG_eval 组织方程;主程序中组织变量/上限/下限/求解参数等

class FG_eval{
 public:
  typedef CPPAD_TESTVECTOR( AD ) ADvector;
  void operator()(ADvector& fg,const ADvector& x){
    assert(fg.size()==3);
    assert(x.size()==4);
    //var
    AD x1=x[0];
    AD x2=x[1];
    AD x3=x[2];
    AD x4=x[3];
    //cost function f(x)
    fg[0]=x1*x4*(x1+x2+x3)+x3;
    //constraints function
    fg[1]=x1*x2*x3*x4;
    fg[2]=x1*x1+x2*x2+x3*x3+x4*x4;

    return;
  }
};

你可能感兴趣的:(非线性规划(NLP)求解)