NLopt 是一个轻量级开源非线性优化库, 为多种优化算法提供了统一的接口。
主页:https://nlopt.readthedocs.io/en/latest/
git:https://github.com/stevengj/nlopt
下载:
nlopt-2.4.2.tar.gz
安装:
./configure && make && sudo make install
nlopt_create
将根据给定的优化算法nlopt_algorithm
返回nlopt_opt
对象,n
为优化变量维数
nlopt_opt nlopt_create(nlopt_algorithm algorithm, unsigned n);
nlopt_algorithm
包含了
typedef enum {
/* Naming conventions:
NLOPT_{G/L}{D/N}_*
= global/local derivative/no-derivative optimization,
respectively
*_RAND algorithms involve some randomization.
*_NOSCAL algorithms are *not* scaled to a unit hypercube
(i.e. they are sensitive to the units of x)
*/
NLOPT_GN_DIRECT = 0,
NLOPT_GN_DIRECT_L,
NLOPT_GN_DIRECT_L_RAND,
NLOPT_GN_DIRECT_NOSCAL,
NLOPT_GN_DIRECT_L_NOSCAL,
NLOPT_GN_DIRECT_L_RAND_NOSCAL,
NLOPT_GN_ORIG_DIRECT,
NLOPT_GN_ORIG_DIRECT_L,
NLOPT_GD_STOGO,
NLOPT_GD_STOGO_RAND,
NLOPT_LD_LBFGS_NOCEDAL,
NLOPT_LD_LBFGS,
NLOPT_LN_PRAXIS,
NLOPT_LD_VAR1,
NLOPT_LD_VAR2,
NLOPT_LD_TNEWTON,
NLOPT_LD_TNEWTON_RESTART,
NLOPT_LD_TNEWTON_PRECOND,
NLOPT_LD_TNEWTON_PRECOND_RESTART,
NLOPT_GN_CRS2_LM,
NLOPT_GN_MLSL,
NLOPT_GD_MLSL,
NLOPT_GN_MLSL_LDS,
NLOPT_GD_MLSL_LDS,
NLOPT_LD_MMA,
NLOPT_LN_COBYLA,
NLOPT_LN_NEWUOA,
NLOPT_LN_NEWUOA_BOUND,
NLOPT_LN_NELDERMEAD,
NLOPT_LN_SBPLX,
NLOPT_LN_AUGLAG,
NLOPT_LD_AUGLAG,
NLOPT_LN_AUGLAG_EQ,
NLOPT_LD_AUGLAG_EQ,
NLOPT_LN_BOBYQA,
NLOPT_GN_ISRES,
/* new variants that require local_optimizer to be set,
not with older constants for backwards compatibility */
NLOPT_AUGLAG,
NLOPT_AUGLAG_EQ,
NLOPT_G_MLSL,
NLOPT_G_MLSL_LDS,
NLOPT_LD_SLSQP,
NLOPT_LD_CCSAQ,
NLOPT_GN_ESCH,
NLOPT_NUM_ALGORITHMS /* not an algorithm, just the number of them */
} nlopt_algorithm;
算法详情参考:https://nlopt.readthedocs.io/en/latest/NLopt_Algorithms/
nlopt_set_min_objective
与nlopt_set_max_objective
为nlopt_opt
设定目标函数nlopt_func
nlopt_result nlopt_set_min_objective(nlopt_opt opt, nlopt_func f, void* f_data);
nlopt_result nlopt_set_max_objective(nlopt_opt opt, nlopt_func f, void* f_data);
nlopt_func
的定义如下,n
为维数,x
为输入,grad
为梯度返回值,f_data
为函数所需传入的自定义参数
double f(unsigned n, const double* x, double* grad, void* f_data);
nlopt_set_lower_bounds
与nlopt_set_upper_bounds
设定优化变量上下边界
nlopt_result nlopt_set_lower_bounds(nlopt_opt opt, const double* lb);
nlopt_result nlopt_set_upper_bounds(nlopt_opt opt, const double* ub);
nlopt_add_inequality_constraint
与nlopt_add_equality_constraint
为nlopt_opt
添加不等式与等式约束
nlopt_result nlopt_add_inequality_constraint(nlopt_opt opt, nlopt_func fc, void* fc_data, double tol);
nlopt_result nlopt_add_equality_constraint(nlopt_opt opt, nlopt_func h, void* h_data, double tol);
nlopt_set_stopval
nlopt_set_ftol_rel
nlopt_set_ftol_abs
nlopt_set_xtol_rel
nlopt_set_xtol_abs
为 nlopt_opt
设定停止条件
nlopt_result nlopt_set_stopval(nlopt_opt opt, double stopval);
nlopt_result nlopt_set_ftol_rel(nlopt_opt opt, double tol);
nlopt_result nlopt_set_ftol_abs(nlopt_opt opt, double tol);
nlopt_result nlopt_set_xtol_rel(nlopt_opt opt, double tol);
nlopt_result nlopt_set_xtol_abs(nlopt_opt opt, const double* tol);
可以直接调用nlopt_minimize_econstrained
完成优化计算。
nlopt_result
NLOPT_STDCALL nlopt_minimize_econstrained(
nlopt_algorithm algorithm,
int n, nlopt_func_old f, void *f_data,
int m, nlopt_func_old fc, void *fc_data_, ptrdiff_t fc_datum_size,
int p, nlopt_func_old h, void *h_data_, ptrdiff_t h_datum_size,
const double *lb, const double *ub, /* bounds */
double *x, /* in: initial guess, out: minimizer */
double *minf, /* out: minimum */
double minf_max, double ftol_rel, double ftol_abs,
double xtol_rel, const double *xtol_abs,
double htol_rel, double htol_abs,
int maxeval, double maxtime)
/*Successful termination (positive return values)*/
NLOPT_SUCCESS` `=` `1
/*Generic success return value.*/
NLOPT_STOPVAL_REACHED` `=` `2
/*Optimization stopped because stopval (above) was reached.*/
NLOPT_FTOL_REACHED` `=` `3
/*Optimization stopped because ftol_rel or ftol_abs (above) was reached.*/
NLOPT_XTOL_REACHED` `=` `4
/*Optimization stopped because xtol_rel or xtol_abs (above) was reached.*/
NLOPT_MAXEVAL_REACHED` `=` `5
/*Optimization stopped because maxeval (above) was reached.*/
NLOPT_MAXTIME_REACHED` `=` `6
/*Optimization stopped because maxtime (above) was reached.*/
/*Error codes (negative return values)*/
NLOPT_FAILURE` `=` `-1
/*Generic failure code.*/
NLOPT_INVALID_ARGS` `=` `-2
/*Invalid arguments (e.g. lower bounds are bigger than upper bounds, an unknown algorithm was specified, etcetera).*/
NLOPT_OUT_OF_MEMORY` `=` `-3
/*Ran out of memory.*/
NLOPT_ROUNDOFF_LIMITED` `=` `-4
/*Halted because roundoff errors limited progress. (In this case, the optimization still typically returns a useful result.)*/
NLOPT_FORCED_STOP` `=` `-5
/*Halted because of a forced termination: the user called nlopt_force_stop(opt) on the optimization’s nlopt_opt object opt from the user’s objective function or constraints.*/