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);
//@}