本文整理汇总了C++中nlopt_opt::f方法的典型用法代码示例。如果您正苦于以下问题:C++ nlopt_opt::f方法的具体用法?C++ nlopt_opt::f怎么用?C++ nlopt_opt::f使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nlopt_opt
的用法示例。
在下文中一共展示了nlopt_opt::f方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: nlopt_optimize_
/* unlike nlopt_optimize() below, only handles minimization case */
static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
{
const double *lb, *ub;
nlopt_algorithm algorithm;
nlopt_func f; void *f_data;
unsigned n, i;
int ni;
nlopt_stopping stop;
if (!opt || !x || !minf || !opt->f
|| opt->maximize) return NLOPT_INVALID_ARGS;
/* reset stopping flag */
nlopt_set_force_stop(opt, 0);
opt->force_stop_child = NULL;
/* copy a few params to local vars for convenience */
n = opt->n;
ni = (int) n; /* most of the subroutines take "int" arg */
lb = opt->lb; ub = opt->ub;
algorithm = opt->algorithm;
f = opt->f; f_data = opt->f_data;
if (n == 0) { /* trivial case: no degrees of freedom */
*minf = opt->f(n, x, NULL, opt->f_data);
return NLOPT_SUCCESS;
}
*minf = HUGE_VAL;
/* make sure rand generator is inited */
nlopt_srand_time_default(); /* default is non-deterministic */
/* check bound constraints */
for (i = 0; i < n; ++i)
if (lb[i] > ub[i] || x[i] < lb[i] || x[i] > ub[i])
return NLOPT_INVALID_ARGS;
stop.n = n;
stop.minf_max = opt->stopval;
stop.ftol_rel = opt->ftol_rel;
stop.ftol_abs = opt->ftol_abs;
stop.xtol_rel = opt->xtol_rel;
stop.xtol_abs = opt->xtol_abs;
stop.nevals = 0;
stop.maxeval = opt->maxeval;
stop.maxtime = opt->maxtime;
stop.start = nlopt_seconds();
stop.force_stop = &(opt->force_stop);
switch (algorithm) {
case NLOPT_GN_DIRECT:
case NLOPT_GN_DIRECT_L:
case NLOPT_GN_DIRECT_L_RAND:
if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
return cdirect(ni, f, f_data,
lb, ub, x, minf, &stop, 0.0,
(algorithm != NLOPT_GN_DIRECT)
+ 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND
? 2 : (algorithm != NLOPT_GN_DIRECT))
+ 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND
? 1 : (algorithm != NLOPT_GN_DIRECT)));
case NLOPT_GN_DIRECT_NOSCAL:
case NLOPT_GN_DIRECT_L_NOSCAL:
case NLOPT_GN_DIRECT_L_RAND_NOSCAL:
if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
return cdirect_unscaled(ni, f, f_data, lb, ub, x, minf,
&stop, 0.0,
(algorithm != NLOPT_GN_DIRECT)
+ 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 2 : (algorithm != NLOPT_GN_DIRECT))
+ 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 1 : (algorithm != NLOPT_GN_DIRECT)));
case NLOPT_GN_ORIG_DIRECT:
case NLOPT_GN_ORIG_DIRECT_L: {
direct_return_code dret;
if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
opt->work = malloc(sizeof(double) *
nlopt_max_constraint_dim(opt->m,
opt->fc));
if (!opt->work) return NLOPT_OUT_OF_MEMORY;
dret = direct_optimize(f_direct, opt, ni, lb, ub, x, minf,
stop.maxeval, -1,
stop.start, stop.maxtime,
0.0, 0.0,
pow(stop.xtol_rel, (double) n), -1.0,
stop.force_stop,
stop.minf_max, 0.0,
NULL,
algorithm == NLOPT_GN_ORIG_DIRECT
? DIRECT_ORIGINAL
: DIRECT_GABLONSKY);
free(opt->work); opt->work = NULL;
switch (dret) {
case DIRECT_INVALID_BOUNDS:
case DIRECT_MAXFEVAL_TOOBIG:
case DIRECT_INVALID_ARGS:
return NLOPT_INVALID_ARGS;
case DIRECT_INIT_FAILED:
//.........这里部分代码省略.........
示例2: nlopt_optimize_
/* unlike nlopt_optimize() below, only handles minimization case */
static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
{
const double *lb, *ub;
nlopt_algorithm algorithm;
nlopt_func f; void *f_data;
unsigned n, i;
int ni;
nlopt_stopping stop;
if (!opt || !x || !minf || !opt->f
|| opt->maximize) RETURN_ERR(NLOPT_INVALID_ARGS, opt,
"NULL args to nlopt_optimize_");
/* reset stopping flag */
nlopt_set_force_stop(opt, 0);
opt->force_stop_child = NULL;
/* copy a few params to local vars for convenience */
n = opt->n;
ni = (int) n; /* most of the subroutines take "int" arg */
lb = opt->lb; ub = opt->ub;
algorithm = opt->algorithm;
f = opt->f; f_data = opt->f_data;
if (n == 0) { /* trivial case: no degrees of freedom */
*minf = opt->f(n, x, NULL, opt->f_data);
return NLOPT_SUCCESS;
}
*minf = HUGE_VAL;
/* check bound constraints */
for (i = 0; i < n; ++i)
if (lb[i] > ub[i] || x[i] < lb[i] || x[i] > ub[i]) {
nlopt_set_errmsg(opt, "bounds %d fail %g <= %g <= %g",
i, lb[i], x[i], ub[i]);
return NLOPT_INVALID_ARGS;
}
stop.n = n;
stop.minf_max = opt->stopval;
stop.ftol_rel = opt->ftol_rel;
stop.ftol_abs = opt->ftol_abs;
stop.xtol_rel = opt->xtol_rel;
stop.xtol_abs = opt->xtol_abs;
stop.nevals = 0;
stop.maxeval = opt->maxeval;
stop.maxtime = opt->maxtime;
stop.force_stop = &(opt->force_stop);
stop.stop_msg = &(opt->errmsg);
switch (algorithm) {
case NLOPT_LD_SLSQP:
return nlopt_slsqp(n, f, f_data,
opt->m, opt->fc,
opt->p, opt->h,
lb, ub, x, minf, &stop);
default:
return NLOPT_INVALID_ARGS;
}
return NLOPT_SUCCESS; /* never reached */
}