非線性規(guī)劃(NLP)求解

根據(jù)官網(wǎng)例程實(shí)現(xiàn)非線性規(guī)劃問(wèn)題求解褂傀。

2018-10-17_135910.jpg

文件demo.cpp代碼。


#include <vector>
#include <math.h>
#include <cppad/cppad.hpp>
#include <cppad/ipopt/solve.hpp>
#include "Eigen-3.3/Eigen/Core"
#include "Eigen-3.3/Eigen/QR"

using CppAD::AD;

class FG_eval{
 public:
  typedef CPPAD_TESTVECTOR( AD<double> ) ADvector;
  void operator()(ADvector& fg,const ADvector& x){
    assert(fg.size()==3);
    assert(x.size()==4);
    //var
    AD<double> x1=x[0];
    AD<double> x2=x[1];
    AD<double> x3=x[2];
    AD<double> 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<nx;i++){
   xl[i]=1.0;
   xu[i]=5.0;
 }
 //lower and upper limits for constraints
 Dvector gl(ng), gu(ng); 
 gl[0]=25.0; gu[0]=1.0e19;
 gl[1]=40.0; gu[1]=40.0;

 //object that computes objective and constraints

 FG_eval fg_eval;

 //options
 std::string options;
 //turn off any printing
 options +="Integer print_level  0\n";
 options += "String  sb           yes\n";
 // maximum number of iterations
 options += "Integer max_iter     10\n";
 // approximate accuracy in first order necessary conditions;
 // see Mathematical Programming, Volume 106, Number 1,
 // Pages 25-57, Equation (6) 
 options += "Numeric tol          1e-6\n";
 // derivative testing
 options += "String  derivative_test            second-order\n";
 // maximum amount of random pertubation; e.g.,
 // when evaluation finite diff
 options += "Numeric point_perturbation_radius  0.\n";
/*
 std::string options;
 options += "Integer print_level  0\n";
 options += "Sparse  true        forward\n";
 options += "Sparse  true        reverse\n";
*/
 // place to return solution
 CppAD::ipopt::solve_result<Dvector> solution;

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

 // Check some of the solution values

 ok &=solution.status == CppAD::ipopt::solve_result<Dvector>::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<double> 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;
}

說(shuō)明:變量初始值/變量下限/變量上限/約束下限/約束上限/目標(biāo)函數(shù)+等式不等式約束/
類FG_eval 組織方程满哪;主程序中組織變量/上限/下限/求解參數(shù)等

class FG_eval{
 public:
  typedef CPPAD_TESTVECTOR( AD<double> ) ADvector;
  void operator()(ADvector& fg,const ADvector& x){
    assert(fg.size()==3);
    assert(x.size()==4);
    //var
    AD<double> x1=x[0];
    AD<double> x2=x[1];
    AD<double> x3=x[2];
    AD<double> 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;
  }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市再层,隨后出現(xiàn)的幾起案子缩赛,更是在濱河造成了極大的恐慌,老刑警劉巖聪廉,帶你破解...
    沈念sama閱讀 222,378評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瞬痘,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡板熊,警方通過(guò)查閱死者的電腦和手機(jī)框全,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)干签,“玉大人津辩,你說(shuō)我怎么就攤上這事∪堇停” “怎么了喘沿?”我有些...
    開封第一講書人閱讀 168,983評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)鸭蛙。 經(jīng)常有香客問(wèn)我摹恨,道長(zhǎng),這世上最難降的妖魔是什么娶视? 我笑而不...
    開封第一講書人閱讀 59,938評(píng)論 1 299
  • 正文 為了忘掉前任晒哄,我火速辦了婚禮,結(jié)果婚禮上肪获,老公的妹妹穿的比我還像新娘寝凌。我一直安慰自己,他們只是感情好孝赫,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,955評(píng)論 6 398
  • 文/花漫 我一把揭開白布较木。 她就那樣靜靜地躺著,像睡著了一般青柄。 火紅的嫁衣襯著肌膚如雪伐债。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,549評(píng)論 1 312
  • 那天致开,我揣著相機(jī)與錄音峰锁,去河邊找鬼。 笑死双戳,一個(gè)胖子當(dāng)著我的面吹牛虹蒋,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 41,063評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼魄衅,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼峭竣!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起晃虫,我...
    開封第一講書人閱讀 39,991評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤皆撩,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后哲银,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體毅访,經(jīng)...
    沈念sama閱讀 46,522評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,604評(píng)論 3 342
  • 正文 我和宋清朗相戀三年盘榨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了喻粹。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,742評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡草巡,死狀恐怖守呜,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情山憨,我是刑警寧澤查乒,帶...
    沈念sama閱讀 36,413評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站郁竟,受9級(jí)特大地震影響玛迄,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜棚亩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,094評(píng)論 3 335
  • 文/蒙蒙 一蓖议、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧讥蟆,春花似錦勒虾、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至质况,卻和暖如春愕宋,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背结榄。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工中贝, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人潭陪。 一個(gè)月前我還...
    沈念sama閱讀 49,159評(píng)論 3 378
  • 正文 我出身青樓雄妥,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親依溯。 傳聞我的和親對(duì)象是個(gè)殘疾皇子老厌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,747評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容