chiark / gitweb /
Use trusty
[nlopt.git] / crs / crs.c
index e5c0ba8f8dd3de7c7551bf37fe1ab2b772d9431b..16f4ad5e63556fe1bbd09dde79531fe629c4bdbe 100644 (file)
--- a/crs/crs.c
+++ b/crs/crs.c
@@ -1,3 +1,25 @@
+/* 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
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
+ */
+
 #include <stdlib.h>
 #include <string.h>
 
@@ -110,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;
@@ -142,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;
@@ -172,7 +204,7 @@ static nlopt_result crs_init(crs_data *d, int n, const double *x,
      memcpy(d->ps + 1, x, sizeof(double) * n);
      d->ps[0] = f(n, x, NULL, f_data);
      stop->nevals++;
-     rb_tree_insert(&d->t, d->ps);
+     if (!rb_tree_insert(&d->t, d->ps)) return NLOPT_OUT_OF_MEMORY;
      if (d->ps[0] < stop->minf_max) return NLOPT_MINF_MAX_REACHED;
      if (nlopt_stop_evals(stop)) return NLOPT_MAXEVAL_REACHED;
      if (nlopt_stop_time(stop)) return NLOPT_MAXTIME_REACHED;
@@ -187,7 +219,7 @@ static nlopt_result crs_init(crs_data *d, int n, const double *x,
          }
          k[0] = f(n, k + 1, NULL, f_data);
          stop->nevals++;
-         rb_tree_insert(&d->t, k);
+         if (!rb_tree_insert(&d->t, k)) return NLOPT_OUT_OF_MEMORY;
          if (k[0] < stop->minf_max) return NLOPT_MINF_MAX_REACHED;
          if (nlopt_stop_evals(stop)) return NLOPT_MAXEVAL_REACHED;
          if (nlopt_stop_time(stop)) return NLOPT_MAXTIME_REACHED;        
@@ -201,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);