chiark / gitweb /
improve C++ header exceptions, rename NLOPT_FORCE_STOP to NLOPT_FORCED_STOP
authorstevenj <stevenj@alum.mit.edu>
Mon, 31 May 2010 16:59:10 +0000 (12:59 -0400)
committerstevenj <stevenj@alum.mit.edu>
Mon, 31 May 2010 16:59:10 +0000 (12:59 -0400)
darcs-hash:20100531165910-c8de0-347b8ba2ff62d825467d4baf6b21508f5364d1e0.gz

20 files changed:
api/nlopt-in.hpp
api/nlopt.3
api/nlopt.h
api/optimize.c
auglag/auglag.c
bobyqa/bobyqa.c
cdirect/cdirect.c
cobyla/cobyla.c
cquad/cquad.c
crs/crs.c
isres/isres.c
luksan/plip.c
luksan/plis.c
luksan/pnet.c
mlsl/mlsl.c
mma/mma.c
neldermead/nldrmd.c
neldermead/sbplx.c
newuoa/newuoa.c
praxis/praxis.c

index a18854443ac6c369c3887578e2761fa13581f59f..977d5aa6eaa94ab0d0af27662bce42f1d38e8ecf 100644 (file)
@@ -56,6 +56,19 @@ namespace nlopt {
   // (Note: it is inefficient to use std::vector<double> for the arguments,
   //  since that would require a copy to be made of NLopt's double* data.)
 
+  //////////////////////////////////////////////////////////////////////
+  
+  // NLopt-specific exceptions (corresponding to error codes):
+  class roundoff_limited : public std::runtime_error {
+  public:
+    roundoff_limited() : std::runtime_error("nlopt roundoff-limited") {}
+  };
+
+  class forced_stop : public std::runtime_error {
+  public:
+    forced_stop() : std::runtime_error("nlopt forced stop") {}
+  };
+
   //////////////////////////////////////////////////////////////////////
 
   class opt {
@@ -66,8 +79,9 @@ namespace nlopt {
       switch (ret) {
       case NLOPT_FAILURE: throw std::runtime_error("nlopt failure");
       case NLOPT_OUT_OF_MEMORY: throw std::bad_alloc();
-      case NLOPT_INVALID_ARGS: throw std::invalid_argument("nlopt");
-      case NLOPT_ROUNDOFF_LIMITED: throw std::runtime_error("nlopt roundoff");
+      case NLOPT_INVALID_ARGS: throw std::invalid_argument("nlopt invalid argument");
+      case NLOPT_ROUNDOFF_LIMITED: throw roundoff_limited();
+      case NLOPT_FORCED_STOP: throw forced_stop();
       default: break;
       }
     }
index e5e862ba334cefb0569652eca0c87cbbb608ea54..b2e435907663a14ee7eb75f0658236e051055de3 100644 (file)
@@ -635,7 +635,7 @@ Ran out of memory.
 .B NLOPT_ROUNDOFF_LIMITED
 Halted because roundoff errors limited progress.
 .TP
-.B NLOPT_FORCE_STOP
+.B NLOPT_FORCED_STOP
 Halted because the user called \fBnlopt_force_stop\fR(\fIopt\fR) on
 the optimization's \fBnlopt_opt\fR object \fIopt\fR from the user's
 objective function.
index 39cddfd40dc9de6d4c71f3ec0fec8c25243dfcd3..2f0975b19f0ce57dbe2b3dd8cb44251cc8522619 100644 (file)
@@ -135,7 +135,7 @@ typedef enum {
      NLOPT_INVALID_ARGS = -2,
      NLOPT_OUT_OF_MEMORY = -3,
      NLOPT_ROUNDOFF_LIMITED = -4,
-     NLOPT_FORCE_STOP = -5,
+     NLOPT_FORCED_STOP = -5,
      NLOPT_SUCCESS = 1, /* generic success code */
      NLOPT_STOPVAL_REACHED = 2,
      NLOPT_FTOL_REACHED = 3,
index 0877b01300f7cd8f7f203c80ab32c446b028fd26..0f35707fe2685668617d7ea52b7f997d80711546 100644 (file)
@@ -271,7 +271,7 @@ static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
              if (freedx) { free(opt->dx); opt->dx = NULL; }
              switch (iret) {
                  case -2: return NLOPT_INVALID_ARGS;
-                 case -20: return NLOPT_FORCE_STOP;
+                 case -20: return NLOPT_FORCED_STOP;
                  case -10: return NLOPT_MAXTIME_REACHED;
                  case -1: return NLOPT_MAXEVAL_REACHED;
                  case 0: return NLOPT_XTOL_REACHED;
index 4e5881e87d7ec31da4b5e5cf399f75cc9108b176..69d257ea27e35b546622dbd9a8ca3ce7ca189cf6 100644 (file)
@@ -216,7 +216,7 @@ nlopt_result auglag_minimize(int n, nlopt_func f, void *f_data,
               if (ret != NLOPT_SUCCESS) break;
          }
 
-         if (nlopt_stop_forced(stop)) {ret = NLOPT_FORCE_STOP; break;}
+         if (nlopt_stop_forced(stop)) {ret = NLOPT_FORCED_STOP; break;}
          if (nlopt_stop_evals(stop)) {ret = NLOPT_MAXEVAL_REACHED; break;}
           if (nlopt_stop_time(stop)) {ret = NLOPT_MAXTIME_REACHED; break;}
 
index 9ca6de7a4eecb853b007a36b64328e8fe09592a6..55f4f7c937fb68a95b12be787e54666ccc4ff4ca 100644 (file)
@@ -592,7 +592,7 @@ L260:
            goto L340;
        }
 
-       if (nlopt_stop_forced(stop)) return NLOPT_FORCE_STOP;
+       if (nlopt_stop_forced(stop)) return NLOPT_FORCED_STOP;
        else if (nlopt_stop_evals(stop)) return NLOPT_MAXEVAL_REACHED;
        else if (nlopt_stop_time(stop)) return NLOPT_MAXTIME_REACHED;
 
@@ -679,7 +679,7 @@ L260:
        if (f < fval[*kopt]) {
            *kopt = kpt;
        }
-       if (nlopt_stop_forced(stop)) return NLOPT_FORCE_STOP;
+       if (nlopt_stop_forced(stop)) return NLOPT_FORCED_STOP;
        else if (f < stop->minf_max) return NLOPT_MINF_MAX_REACHED;
        else if (nlopt_stop_evals(stop)) return NLOPT_MAXEVAL_REACHED;
        else if (nlopt_stop_time(stop)) return NLOPT_MAXTIME_REACHED;
@@ -1939,7 +1939,7 @@ L50:
        temp = xpt[nf + ipt * xpt_dim1] * xpt[nf + jpt * xpt_dim1];
        hq[ih] = (fbeg - fval[ipt + 1] - fval[jpt + 1] + f) / temp;
     }
-    if (nlopt_stop_forced(stop)) return NLOPT_FORCE_STOP;
+    if (nlopt_stop_forced(stop)) return NLOPT_FORCED_STOP;
     else if (f < stop->minf_max) return NLOPT_MINF_MAX_REACHED;
     else if (nlopt_stop_evals(stop)) return NLOPT_MAXEVAL_REACHED;
     else if (nlopt_stop_time(stop)) return NLOPT_MAXTIME_REACHED;
@@ -2572,7 +2572,7 @@ L360:
 /* L380: */
     }
 
-    if (nlopt_stop_forced(stop)) rc = NLOPT_FORCE_STOP;
+    if (nlopt_stop_forced(stop)) rc = NLOPT_FORCED_STOP;
     else if (nlopt_stop_evals(stop)) rc = NLOPT_MAXEVAL_REACHED;
     else if (nlopt_stop_time(stop)) rc = NLOPT_MAXTIME_REACHED;
     if (rc != NLOPT_SUCCESS) goto L720;
index ed315eeb9bd448716519b2cbe096b9244b5a0c06..d37e8913e73e37fa1bba20d731635420368bd0fe 100644 (file)
@@ -142,7 +142,7 @@ static double function_eval(const double *x, params *p) {
      p->stop->nevals++;
      return f;
 }
-#define FUNCTION_EVAL(fv,x,p,freeonerr) fv = function_eval(x, p); if (nlopt_stop_forced((p)->stop)) { free(freeonerr); return NLOPT_FORCE_STOP; } else if (p->minf < p->stop->minf_max) { free(freeonerr); return NLOPT_MINF_MAX_REACHED; } else if (nlopt_stop_evals((p)->stop)) { free(freeonerr); return NLOPT_MAXEVAL_REACHED; } else if (nlopt_stop_time((p)->stop)) { free(freeonerr); return NLOPT_MAXTIME_REACHED; }
+#define FUNCTION_EVAL(fv,x,p,freeonerr) fv = function_eval(x, p); if (nlopt_stop_forced((p)->stop)) { free(freeonerr); return NLOPT_FORCED_STOP; } else if (p->minf < p->stop->minf_max) { free(freeonerr); return NLOPT_MINF_MAX_REACHED; } else if (nlopt_stop_evals((p)->stop)) { free(freeonerr); return NLOPT_MAXEVAL_REACHED; } else if (nlopt_stop_time((p)->stop)) { free(freeonerr); return NLOPT_MAXTIME_REACHED; }
 
 #define THIRD (0.3333333333333333333333)
 
index 9be178b812242c3a1578ff9ba7e8ba9af25f46f3..a2caa9ffec61308e5bc8230ddde04e1c112999d9 100644 (file)
@@ -499,7 +499,7 @@ static nlopt_result cobylb(int *n, int *m, int *mpp,
      #*&!%*@ Fortran-66 spaghetti code */
 
 L40:
-  if (nlopt_stop_forced(stop)) rc = NLOPT_FORCE_STOP;
+  if (nlopt_stop_forced(stop)) rc = NLOPT_FORCED_STOP;
   else if (nlopt_stop_evals(stop)) rc = NLOPT_MAXEVAL_REACHED;
   else if (nlopt_stop_time(stop)) rc = NLOPT_MAXTIME_REACHED;
   if (rc != NLOPT_SUCCESS) goto L600;
index 8e3bb698626cdba8d6124bad97c683af456719ca..b2dc27cc5118fb0fb0c573d3a63f6075666b3cbb 100644 (file)
@@ -315,7 +315,7 @@ nlopt_result cquad_minimize(int n, nlopt_func f, void *f_data,
               memcpy(x, x0, sizeof(double) * n);
               feasible = 1;
          }
-         if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+         if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
          else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
          else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
          else if (*minf < stop->minf_max) ret = NLOPT_MINF_MAX_REACHED;
@@ -342,7 +342,7 @@ nlopt_result cquad_minimize(int n, nlopt_func f, void *f_data,
                         memcpy(x, xcur, sizeof(double) * n);
                         feasible = 1;
                    }
-                   if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+                   if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
                    else if (nlopt_stop_evals(stop)) 
                         ret = NLOPT_MAXEVAL_REACHED;
                    else if (nlopt_stop_time(stop)) 
index 36cbc3e2db03de6ceacf069e7e5acade431f016f..6421f3574f23e6fe109283b4c8913da666e6fde6 100644 (file)
--- a/crs/crs.c
+++ b/crs/crs.c
@@ -132,7 +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_FORCE_STOP;
+         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;
index 53da6157ab7ec114974ebe35a211572f1ef0ed56..47cc65c521c9b2ec565888f060e5f532453b15e4 100644 (file)
@@ -164,7 +164,7 @@ nlopt_result isres_minimize(int n, nlopt_func f, void *f_data,
                    if (ret != NLOPT_SUCCESS) goto done;
               }
 
-              if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+              if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
               else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
               else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
               if (ret != NLOPT_SUCCESS) goto done;
index 975a5b074eb7c5179e367b25f36295ca3b12c4b1..09b735d4986428e3239b45e45819548a3cb6e37b 100644 (file)
@@ -516,7 +516,7 @@ nlopt_result luksan_plip(int n, nlopt_func f, void *f_data,
         case 4: return NLOPT_SUCCESS; /* gradient tolerance reached */
         case 6: return NLOPT_SUCCESS;
         case 12: case 13: return NLOPT_MAXEVAL_REACHED;
-        case -999: return NLOPT_FORCE_STOP;
+        case -999: return NLOPT_FORCED_STOP;
         default: return NLOPT_FAILURE;
      }
 }
index d0e4adfc7cdf87e4920276295a996915bd73bc82..8e4c0e6886b1da4bc1e86d5dac2fd133ba1d5a6e 100644 (file)
@@ -509,7 +509,7 @@ nlopt_result luksan_plis(int n, nlopt_func f, void *f_data,
         case 4: return NLOPT_SUCCESS; /* gradient tolerance reached */
         case 6: return NLOPT_SUCCESS;
         case 12: case 13: return NLOPT_MAXEVAL_REACHED;
-        case -999: return NLOPT_FORCE_STOP;
+        case -999: return NLOPT_FORCED_STOP;
         default: return NLOPT_FAILURE;
      }
 }
index 4f506af4f44a832c6f6434a77968eb6c8f8ee78d..01e84cb43662394b71e7b5fa9db00d957cca6de3 100644 (file)
@@ -659,7 +659,7 @@ nlopt_result luksan_pnet(int n, nlopt_func f, void *f_data,
         case 4: return NLOPT_SUCCESS; /* gradient tolerance reached */
         case 6: return NLOPT_SUCCESS;
         case 12: case 13: return NLOPT_MAXEVAL_REACHED;
-        case -999: return NLOPT_FORCE_STOP;
+        case -999: return NLOPT_FORCED_STOP;
         default: return NLOPT_FAILURE;
      }
 }
index 70356a4bfec5cdd6618e23538d464e4d92a9f1c8..b6cca7f7b67a454c924ac439955796bddc76dfec 100644 (file)
@@ -333,7 +333,7 @@ nlopt_result mlsl_minimize(int n, nlopt_func f, void *f_data,
      if (!rb_tree_insert(&d.pts, (rb_key) p)) { 
          free(p); ret = NLOPT_OUT_OF_MEMORY; 
      }
-     if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+     if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
      else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
      else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
      else if (p->f < stop->minf_max) ret = NLOPT_MINF_MAX_REACHED;
@@ -358,7 +358,7 @@ nlopt_result mlsl_minimize(int n, nlopt_func f, void *f_data,
               if (!rb_tree_insert(&d.pts, (rb_key) p)) { 
                    free(p); ret = NLOPT_OUT_OF_MEMORY;
               }
-              if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+              if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
               else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
               else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
               else if (p->f < stop->minf_max) ret = NLOPT_MINF_MAX_REACHED;
@@ -385,7 +385,7 @@ nlopt_result mlsl_minimize(int n, nlopt_func f, void *f_data,
                    double t = nlopt_seconds();
 
                    if (nlopt_stop_forced(stop)) {
-                        ret = NLOPT_FORCE_STOP; break;
+                        ret = NLOPT_FORCED_STOP; break;
                    }
                    if (nlopt_stop_evals(stop)) {
                          ret = NLOPT_MAXEVAL_REACHED; break;
@@ -406,7 +406,7 @@ nlopt_result mlsl_minimize(int n, nlopt_func f, void *f_data,
                    if (!rb_tree_insert(&d.lms, lm)) { 
                         free(lm); ret = NLOPT_OUT_OF_MEMORY;
                    }
-                   else if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+                   else if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
                    else if (*lm < stop->minf_max) 
                         ret = NLOPT_MINF_MAX_REACHED;
                    else if (nlopt_stop_evals(stop))
index 638e07d2c48a1debfb3f4e8fd13845bb4726110d..9de104f90dcdf3ce233afe75e4480c99135d1d8c 100644 (file)
--- a/mma/mma.c
+++ b/mma/mma.c
@@ -235,7 +235,7 @@ nlopt_result mma_minimize(unsigned n, nlopt_func f, void *f_data,
 
      while (1) { /* outer iterations */
          double fprev = fcur;
-         if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+         if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
          else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
          else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
          else if (feasible && *minf < stop->minf_max) 
@@ -319,7 +319,7 @@ nlopt_result mma_minimize(unsigned n, nlopt_func f, void *f_data,
                    else if (new_infeasible_constraint) feasible = 0;
 
               }
-              if (nlopt_stop_forced(stop)) ret = NLOPT_FORCE_STOP;
+              if (nlopt_stop_forced(stop)) ret = NLOPT_FORCED_STOP;
               else if (nlopt_stop_evals(stop)) ret = NLOPT_MAXEVAL_REACHED;
               else if (nlopt_stop_time(stop)) ret = NLOPT_MAXTIME_REACHED;
               else if (feasible && *minf < stop->minf_max) 
index 4c30751068b7bfbefade604e52b400efba2ab0a1..ee86e92d1c8ddaebe7f4be2f3a214f376adbb3c8 100644 (file)
@@ -78,7 +78,7 @@ static int reflectpt(int n, double *xnew,
 
 #define CHECK_EVAL(xc,fc)                                                \
  stop->nevals++;                                                         \
- if (nlopt_stop_forced(stop)) { ret=NLOPT_FORCE_STOP; goto done; }        \
+ if (nlopt_stop_forced(stop)) { ret=NLOPT_FORCED_STOP; goto done; }        \
  if ((fc) <= *minf) {                                                    \
    *minf = (fc); memcpy(x, (xc), n * sizeof(double));                    \
    if (*minf < stop->minf_max) { ret=NLOPT_MINF_MAX_REACHED; goto done; } \
@@ -291,7 +291,7 @@ nlopt_result nldrmd_minimize(int n, nlopt_func f, void *f_data,
 
      *minf = f(n, x, NULL, f_data);
      stop->nevals++;
-     if (nlopt_stop_forced(stop)) return NLOPT_FORCE_STOP;
+     if (nlopt_stop_forced(stop)) return NLOPT_FORCED_STOP;
      if (*minf < 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;
index d5b7fe4e97493bdc79561c0fb251c4993097b8a5..0d98edc6004d49e81932f1d1f06cfa7a687ec206 100644 (file)
@@ -79,7 +79,7 @@ nlopt_result sbplx_minimize(int n, nlopt_func f, void *f_data,
 
      *minf = f(n, x, NULL, f_data);
      stop->nevals++;
-     if (nlopt_stop_forced(stop)) return NLOPT_FORCE_STOP;
+     if (nlopt_stop_forced(stop)) return NLOPT_FORCED_STOP;
      if (*minf < 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;
index d993813ad1a7eb4887ed24e5707c059f7cb07ae9..c497917cc2f6046bd2ca4659690c5d14807c94a1 100644 (file)
@@ -2099,7 +2099,7 @@ L290:
     }
     ++nf;
 L310:
-    if (nlopt_stop_forced(stop)) rc = NLOPT_FORCE_STOP;
+    if (nlopt_stop_forced(stop)) rc = NLOPT_FORCED_STOP;
     else if (nlopt_stop_evals(stop)) rc = NLOPT_MAXEVAL_REACHED;
     else if (nlopt_stop_time(stop)) rc = NLOPT_MAXTIME_REACHED;
     if (rc != NLOPT_SUCCESS) goto L530;
index c9f1356c5846408edbf89d8fe463455bb0a6406b..d6636a9fb69d75af9af887f19284b59af71c09aa 100644 (file)
@@ -1241,7 +1241,7 @@ L4:
         q_1->fbest = ret_val;
         memcpy(q_1->xbest, t, n * sizeof(double));
     }
-    if (nlopt_stop_forced(stop)) *ret = NLOPT_FORCE_STOP;
+    if (nlopt_stop_forced(stop)) *ret = NLOPT_FORCED_STOP;
     else if (nlopt_stop_evals(stop)) *ret = NLOPT_MAXEVAL_REACHED;
     else if (nlopt_stop_time(stop)) *ret = NLOPT_MAXTIME_REACHED;
     else if (ret_val <= stop->minf_max) *ret = NLOPT_MINF_MAX_REACHED;