chiark / gitweb /
added get/set stochastic_population functions for greater control over stochastic...
authorstevenj <stevenj@alum.mit.edu>
Thu, 19 Nov 2009 00:47:24 +0000 (19:47 -0500)
committerstevenj <stevenj@alum.mit.edu>
Thu, 19 Nov 2009 00:47:24 +0000 (19:47 -0500)
darcs-hash:20091119004724-c8de0-3ae4d1219ac30c2add587bcfb1bb7d029f0bc4a2.gz

api/f77funcs.h
api/nlopt.c
api/nlopt.h
crs/crs.c
crs/crs.h
mlsl/mlsl.c
mlsl/mlsl.h

index 87ae76e8849f2a1967b7eec766e2f975ece11b86..045f5b930e8076dcbf00819aad911b37d45b0c1d 100644 (file)
@@ -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);
+}
index 46170f6116511fa37c00e099d3106299c980f45d..7696ca4dd2ac312266d9eb74dc94f65c61766eae 100644 (file)
@@ -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;
index 2e2d38dc004b5f77f77e8908bb9417a030ec6532..19fbe47233be4663a118933567a9ec48cb8a010f 100644 (file)
@@ -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 */
index 9bdd293c0a576f2995e0e45e195c3127970381aa..ce60101400d8350f552fac6bf6710cf1aa1a30a4 100644 (file)
--- 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);
index 4a19e361befd43738dffe076aba32df45cc08371..deca434abc07bc2bc2c985aa1367536ce3876961 100644 (file)
--- 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
index 144335b5b08589aa9770e0f3d08f268593f4d286..cf3573093b7a2d73dc0aaded8e7090a45f4c9280 100644 (file)
@@ -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);
index d0ce4b81966ff973c01fba6c8539ff7067ea67f8..e8dcf1900d8410d7f5ea863c469cd66de1b41728 100644 (file)
@@ -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