chiark / gitweb /
Use trusty
[nlopt.git] / crs / crs.c
index 9bdd293c0a576f2995e0e45e195c3127970381aa..16f4ad5e63556fe1bbd09dde79531fe629c4bdbe 100644 (file)
--- a/crs/crs.c
+++ b/crs/crs.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2008 Massachusetts Institute of Technology
+/* Copyright (c) 2007-2014 Massachusetts Institute of Technology
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files (the
@@ -132,6 +132,7 @@ static nlopt_result crs_trial(crs_data *d)
      do {
          d->p[0] = d->f(n, d->p + 1, NULL, d->f_data);
          d->stop->nevals++;
+         if (nlopt_stop_forced(d->stop)) return NLOPT_FORCED_STOP;
          if (d->p[0] < worst->k[0]) break;
          if (nlopt_stop_evals(d->stop)) return NLOPT_MAXEVAL_REACHED;
          if (nlopt_stop_time(d->stop)) return NLOPT_MAXTIME_REACHED;
@@ -164,14 +165,23 @@ 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 */
+          nlopt_stop_msg(stop, "population %d should be >= dimension + 1 = %d",
+                         d->N, n+1);
+          return NLOPT_INVALID_ARGS;
+     }
 
      d->n = n;
      d->stop = stop;
@@ -223,13 +233,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);