From c9f67c6b28d94d0b10db38bc3e405f7ecb8fb83e Mon Sep 17 00:00:00 2001 From: stevenj Date: Wed, 18 Nov 2009 19:47:24 -0500 Subject: [PATCH] added get/set stochastic_population functions for greater control over stochastic algorithms darcs-hash:20091119004724-c8de0-3ae4d1219ac30c2add587bcfb1bb7d029f0bc4a2.gz --- api/f77funcs.h | 9 +++++++++ api/nlopt.c | 19 +++++++++++++++++-- api/nlopt.h | 3 +++ crs/crs.c | 19 +++++++++++++------ crs/crs.h | 1 + mlsl/mlsl.c | 10 ++++++++-- mlsl/mlsl.h | 1 + 7 files changed, 52 insertions(+), 10 deletions(-) diff --git a/api/f77funcs.h b/api/f77funcs.h index 87ae76e..045f5b9 100644 --- a/api/f77funcs.h +++ b/api/f77funcs.h @@ -103,3 +103,12 @@ void F77(nlosls,NLOSLS)(int *ideriv, int *inonderiv, int *maxeval) nlopt_algorithm nonderiv = (nlopt_algorithm) *inonderiv; nlopt_set_local_search_algorithm(deriv, nonderiv, *maxeval); } + +void F77(nlogsp,NLOGSP)(int *pop) +{ + *pop = nlopt_get_stochastic_population(); +} +void F77(nlossp,NLOSSP)(const int *pop) +{ + nlopt_set_stochastic_population(*pop); +} diff --git a/api/nlopt.c b/api/nlopt.c index 46170f6..7696ca4 100644 --- a/api/nlopt.c +++ b/api/nlopt.c @@ -266,6 +266,19 @@ void nlopt_set_local_search_algorithm(nlopt_algorithm deriv, /*************************************************************************/ +/* For stochastic algorithms, there is often an initial "population" + size to seed the search. Like above, we let the user + call nlopt_{set/get}_stochastic_population in order to get/set the + defaults. The special stochastic population size of "0" means + that the optimization algorithm should pick its default population. */ + +static int stochastic_population = 0; + +int nlopt_get_stochastic_population(void) { return stochastic_population; } +void nlopt_set_stochastic_population(int pop) { stochastic_population = pop; } + +/*************************************************************************/ + /* same as nlopt_minimize_econstrained, but xtol_abs is required to be non-NULL */ static nlopt_result nlopt_minimize_( @@ -477,7 +490,8 @@ static nlopt_result nlopt_minimize_( 1 + (algorithm - NLOPT_LD_TNEWTON) / 2); case NLOPT_GN_CRS2_LM: - return crs_minimize(n, f, f_data, lb, ub, x, minf, &stop, 0); + return crs_minimize(n, f, f_data, lb, ub, x, minf, &stop, + stochastic_population, 0); case NLOPT_GN_MLSL: case NLOPT_GD_MLSL: @@ -497,6 +511,7 @@ static nlopt_result nlopt_minimize_( ? local_search_alg_nonderiv : local_search_alg_deriv, local_search_maxeval, + stochastic_population, algorithm >= NLOPT_GN_MLSL_LDS); case NLOPT_LD_MMA: @@ -561,7 +576,7 @@ static nlopt_result nlopt_minimize_( m, fc, fc_data, fc_datum_size, p, h, h_data, h_datum_size, lb, ub, x, minf, &stop, - 0); + stochastic_population); default: return NLOPT_INVALID_ARGS; diff --git a/api/nlopt.h b/api/nlopt.h index 2e2d38d..19fbe47 100644 --- a/api/nlopt.h +++ b/api/nlopt.h @@ -176,6 +176,9 @@ NLOPT_EXTERN void nlopt_set_local_search_algorithm(nlopt_algorithm deriv, nlopt_algorithm nonderiv, int maxeval); +NLOPT_EXTERN int nlopt_get_stochastic_population(void); +NLOPT_EXTERN void nlopt_set_stochastic_population(int pop); + #ifdef __cplusplus } /* extern "C" */ #endif /* __cplusplus */ diff --git a/crs/crs.c b/crs/crs.c index 9bdd293..ce60101 100644 --- a/crs/crs.c +++ b/crs/crs.c @@ -164,14 +164,20 @@ static void crs_destroy(crs_data *d) static nlopt_result crs_init(crs_data *d, int n, const double *x, const double *lb, const double *ub, nlopt_stopping *stop, nlopt_func f, void *f_data, - int lds) + int population, int lds) { int i; - /* TODO: how should we set the initial population size? - the Kaelo and Ali paper suggests 10*(n+1), but should - we add more random points if maxeval is large, or... ? */ - d->N = 10 * (n + 1); /* heuristic initial population size */ + if (!population) { + /* TODO: how should we set the default population size? + the Kaelo and Ali paper suggests 10*(n+1), but should + we add more random points if maxeval is large, or... ? */ + d->N = 10 * (n + 1); /* heuristic initial population size */ + } + else + d->N = population; + if (d->N < n + 1) /* population must be big enough for a simplex */ + return NLOPT_INVALID_ARGS; d->n = n; d->stop = stop; @@ -223,13 +229,14 @@ nlopt_result crs_minimize(int n, nlopt_func f, void *f_data, double *x, /* in: initial guess, out: minimizer */ double *minf, nlopt_stopping *stop, + int population, /* initial population (0=default) */ int lds) /* random or low-discrepancy seq. (lds) */ { nlopt_result ret; crs_data d; rb_node *best; - ret = crs_init(&d, n, x, lb, ub, stop, f, f_data, lds); + ret = crs_init(&d, n, x, lb, ub, stop, f, f_data, population, lds); if (ret < 0) return ret; best = rb_tree_min(&d.t); diff --git a/crs/crs.h b/crs/crs.h index 4a19e36..deca434 100644 --- a/crs/crs.h +++ b/crs/crs.h @@ -36,6 +36,7 @@ nlopt_result crs_minimize(int n, nlopt_func f, void *f_data, double *x, /* in: initial guess, out: minimizer */ double *minf, nlopt_stopping *stop, + int population, /* initial population (0=default) */ int random); /* random or low-discrepancy seq. */ #ifdef __cplusplus diff --git a/mlsl/mlsl.c b/mlsl/mlsl.c index 144335b..cf35730 100644 --- a/mlsl/mlsl.c +++ b/mlsl/mlsl.c @@ -283,6 +283,7 @@ nlopt_result mlsl_minimize(int n, nlopt_func f, void *f_data, nlopt_stopping *stop, nlopt_algorithm local_alg, int local_maxeval, + int Nsamples, /* #samples/iteration (0=default) */ int lds) /* random or low-discrepancy seq. (lds) */ { nlopt_result ret = NLOPT_SUCCESS; @@ -290,6 +291,12 @@ nlopt_result mlsl_minimize(int n, nlopt_func f, void *f_data, int i; pt *p; + if (!Nsamples) + d.N = 4; /* FIXME: what is good number of samples per iteration? */ + else + d.N = Nsamples; + if (d.N < 1) return NLOPT_INVALID_ARGS; + d.n = n; d.lb = lb; d.ub = ub; d.stop = stop; @@ -300,7 +307,6 @@ nlopt_result mlsl_minimize(int n, nlopt_func f, void *f_data, d.local_alg = local_alg; d.local_maxeval = local_maxeval; d.gamma = MLSL_GAMMA; - d.N = 4; /* FIXME: what is good number of samples per iteration? */ d.R_prefactor = sqrt(2./K2PI) * pow(gam(n) * MLSL_SIGMA, 1.0/n); for (i = 0; i < n; ++i) @@ -319,7 +325,7 @@ nlopt_result mlsl_minimize(int n, nlopt_func f, void *f_data, if (!p) { ret = NLOPT_OUT_OF_MEMORY; goto done; } /* FIXME: how many sobol points to skip, if any? */ - nlopt_sobol_skip(d.s, (unsigned) (10*n+1), p->x); + nlopt_sobol_skip(d.s, (unsigned) (10*n+d.N), p->x); memcpy(p->x, x, n * sizeof(double)); p->f = f(n, x, NULL, f_data); diff --git a/mlsl/mlsl.h b/mlsl/mlsl.h index d0ce4b8..e8dcf19 100644 --- a/mlsl/mlsl.h +++ b/mlsl/mlsl.h @@ -38,6 +38,7 @@ nlopt_result mlsl_minimize(int n, nlopt_func f, void *f_data, nlopt_stopping *stop, nlopt_algorithm local_alg, int local_maxeval, + int Nsamples, /* #samples/iteration (0=default) */ int lds); #ifdef __cplusplus -- 2.30.2