IPOPT工具解決非線性規划最優化問題使用案例
簡單介紹
ipopt是一個解決非線性規划最優化問題的工具集,當然,它也能夠用於解決線性規划問題的求解。它提供了c/c++接口,很易於使用。
問題
解決類似以下的非線性問題:

Ipopt工具採用內點法求解非線性優化問題。
求解前的准備
須要計算
1. 梯度
計算目標函數的梯度,和約束條件Jacobian矩陣
2. Hessian矩陣

delta and lambda are parameters for object function and constraints functions (lambda is multiplier of Lagrangian)
演示樣例
求解以下的最優化問題:

第一步:
求解目標函數的梯度:

第二步:
求解約束條件的Jacobian矩陣:

第三步:
求解目標函數和約束條件的Hessian矩陣。即求解

得到

至此,准備工作已經就緒,接下來調用Ipopt 的API接口進行計算。
1.get_nlp_info設置以下的參數
a) n=4;//變量x個數
b) m=2;//約束條件個數
c) nnz_jac_g=8;//Jacobian非零個數
d) Nnz_h_lag=10;//Hessian非零個數
2.get_bounds_info 設置以下的參數
a) x_l[i]設置xi的下界值
b) x_u[i]設置xi的上界值
c) g_l[i]設置約束i的下界值
d) g_u[i]設置約束i的上界值
3.get_start_point設置以下參數
a) x[i]設置第i個變量的初始迭代值
4.eval_f設置以下參數
a) object_value設置目標函數計算方式(本例:object_value=x0*x3*(x0+x1+x2) + x2)
5.eval_grad_f設置目標函數的梯度
a) grad_f[i]設置目標函數對第i個變量的偏導。本比例如以下:

6.eval_g設置約束條件
a) G[i]約束條件i,本比例如以下:

7.eval_jac_g設置Jacobian矩陣
a) iRow和jCol設置非零行列的坐標
b) Values設置矩陣迭代值,假設values==NULL。即尚未初始化時。須要設置Jacobian矩陣哪些下標位置非零。例如以下圖:

Value==NULL(左);value != NULL(右)
8.eval_h設置Hessian矩陣
a) iRow和jCol設置非零行列的坐標
b) obj_factor為目標函數系數
c) lambda[i]為第i個約束的拉格朗日乘子
d) values設置矩陣的迭代求值,本例僅僅有目標函數和兩個約束條件,因此如所看到的。
i. 目標函數

ii. 約束1

iii. 約束2

9.finalize_solution求解
a) status為返回的求解狀態
b) obj_value:最優值
c) x:最優解變量取值
d) z_l 拉格朗日乘子下界
e) z_u 拉格朗日乘子上屆
f) lambda 最優解拉格朗日乘子取值
C++ API
Svn地址:https://projects.coin-or.org/svn/Ipopt/stable/3.11
演示樣例程序位於源碼:Ipopt/test路徑下。
自己定義類繼承於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);
//@}
