chiark / gitweb /
fix support for maxtime in Luksan algorithms and ORIG_DIRECT; thanks to Jurgen Werner...
authorstevenj <stevenj@alum.mit.edu>
Thu, 28 Oct 2010 23:00:02 +0000 (19:00 -0400)
committerstevenj <stevenj@alum.mit.edu>
Thu, 28 Oct 2010 23:00:02 +0000 (19:00 -0400)
darcs-hash:20101028230002-c8de0-c51903a73ffc18f3ddd4e5d68edc3580d1676f3d.gz

13 files changed:
api/optimize.c
cobyla/cobyla.c
direct/DIRect.c
direct/DIRsubrout.c
direct/direct-internal.h
direct/direct.h
direct/direct_wrap.c
luksan/plip.c
luksan/plis.c
luksan/pnet.c
newuoa/newuoa.c
util/nlopt-util.h
util/stop.c

index cfaa3c5d92452d6cc76993f4710815ccde813ae8..25e08527783dd4ed29998d5b04c801e78e036bc7 100644 (file)
@@ -227,7 +227,9 @@ static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
                                                                    opt->fc));
              if (!opt->work) return NLOPT_OUT_OF_MEMORY;
              dret = direct_optimize(f_direct, opt, ni, lb, ub, x, minf,
-                                    stop.maxeval, -1, 0.0, 0.0,
+                                    stop.maxeval, -1,
+                                    stop.start, stop.maxtime,
+                                    0.0, 0.0,
                                     pow(stop.xtol_rel, (double) n), -1.0,
                                     stop.force_stop,
                                     stop.minf_max, 0.0,
@@ -248,6 +250,8 @@ static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
                  case DIRECT_MAXFEVAL_EXCEEDED:
                  case DIRECT_MAXITER_EXCEEDED:
                       return NLOPT_MAXEVAL_REACHED;
+                 case DIRECT_MAXTIME_EXCEEDED:
+                      return NLOPT_MAXTIME_REACHED;
                  case DIRECT_GLOBAL_FOUND:
                       return NLOPT_MINF_MAX_REACHED;
                  case DIRECT_VOLTOL:
@@ -271,7 +275,7 @@ static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
                   return NLOPT_FAILURE;
              break;
 #else
-             return NLOPT_FAILURE;
+             return NLOPT_INVALID_ARGS;
 #endif
 
 #if 0
index 64c605ff1fc1111d11c5f74526670503171bcc98..f8b08e99f0b4ce2997d72db97dd7ea2c9ea0dc74 100644 (file)
@@ -562,8 +562,10 @@ static nlopt_result cobylb(int *n, int *m, int *mpp,
 
 L40:
   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;
+  else if (stop->nevals > 0) {
+       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;
 
   stop->nevals++;
index 1bd4bbfa204acf254dcb6229b82a874a7a605898..f0a7c1a95cd7f6bd311d3f26a1a80f0e21d729d3 100644 (file)
@@ -44,7 +44,7 @@
 /* |   Lipschitz continues. However, DIRECT has proven to be effective on  | */
 /* |   more complex problems than these.                                   | */
 /* +-----------------------------------------------------------------------+ */
-/* Subroutine */ void direct_direct_(fp fcn, doublereal *x, integer *n, doublereal *eps, doublereal epsabs, integer *maxf, integer *maxt, int *force_stop, doublereal *minf, doublereal *l, 
+/* Subroutine */ void direct_direct_(fp fcn, doublereal *x, integer *n, doublereal *eps, doublereal epsabs, integer *maxf, integer *maxt, double starttime, double maxtime, int *force_stop, doublereal *minf, doublereal *l, 
        doublereal *u, integer *algmethod, integer *ierror, FILE *logfile, 
        doublereal *fglobal, doublereal *fglper, doublereal *volper, 
        doublereal *sigmaper, void *fcn_data)
            logfile, arrayi, &maxi, list2, w, &x[1], &l[1], &u[1], 
            minf, &minpos, thirds, levels, &MAXFUNC, &MAXDEEP, n, n, &
            fmax, &ifeasiblef, &iinfesiblef, ierror, fcn_data, jones,
-            force_stop);
+                   starttime, maxtime, force_stop);
 /* +-----------------------------------------------------------------------+ */
 /* | Added error checking.                                                 | */
 /* +-----------------------------------------------------------------------+ */
        }
        if (*ierror == -102) goto L100;
     }
+    else if (*ierror == DIRECT_MAXTIME_EXCEEDED) goto L100;
     numfunc = maxi + 1 + maxi;
     actmaxdeep = 1;
     oldpos = 0;
                     *ierror = -102;
                     goto L100;
                }
+               if (nlopt_stop_time_(starttime, maxtime)) {
+                    *ierror = DIRECT_MAXTIME_EXCEEDED;
+                    goto L100;
+               }
                if (oops > 0) {
                    if (logfile)
                         fprintf(logfile, "WARNING: Error occured in routine DIRsamplef.\n");
index b5889a59e8cd96069e612ff74f22865249d2a2ce..7414961fb2d15db91d5b56c5a9d6eb2bce62becd 100644 (file)
@@ -1144,7 +1144,7 @@ L50:
        doublereal *thirds, doublereal *levels, integer *maxfunc, const integer *
        maxdeep, integer *n, integer *maxor, doublereal *fmax, integer *
        ifeasiblef, integer *iinfeasible, integer *ierror, void *fcndata,
-                                     integer jones, int *force_stop)
+       integer jones, double starttime, double maxtime, int *force_stop)
 {
     /* System generated locals */
     integer c_dim1, c_offset, length_dim1, length_offset, list2_dim1, 
@@ -1262,6 +1262,10 @@ L50:
     point[1] = 0;
     *free = 2;
     delta = thirds[1];
+    if (nlopt_stop_time_(starttime, maxtime)) {
+        *ierror = DIRECT_MAXTIME_EXCEEDED;
+        return;
+    }
     direct_dirget_i__(&length[length_offset], &c__1, &arrayi[1], maxi, n, maxfunc);
     new__ = *free;
     direct_dirsamplepoints_(&c__[c_offset], &arrayi[1], &delta, &c__1, &new__, &
@@ -1288,6 +1292,10 @@ L50:
         *ierror = -102;
         return;
     }
+    if (nlopt_stop_time_(starttime, maxtime)) {
+        *ierror = DIRECT_MAXTIME_EXCEEDED;
+        return;
+    }
 /* +-----------------------------------------------------------------------+ */
 /* | JG 01/23/01 Added error checking.                                     | */
 /* +-----------------------------------------------------------------------+ */
index ed3e4dfbfc99f7044d2939698234cf82ae42361e..fb4b274ac93d5a530d9abc00e4aa106cca4f5442 100644 (file)
@@ -40,7 +40,7 @@ extern void direct_dirinit_(
      doublereal *thirds, doublereal *levels, integer *maxfunc, const integer *
      maxdeep, integer *n, integer *maxor, doublereal *fmax, integer *
      ifeasiblef, integer *iinfeasible, integer *ierror, void *fcndata,
-     integer jones, int *force_stop);
+     integer jones, double starttime, double maxtime, int *force_stop);
 extern void direct_dirinitlist_(
      integer *anchor, integer *free, integer *
      point, doublereal *f, integer *maxfunc, const integer *maxdeep);
@@ -108,7 +108,9 @@ extern void direct_dirsamplef_(
 /* DIRect.c */
 extern void direct_direct_(
      fp fcn, doublereal *x, integer *n, doublereal *eps, doublereal epsabs,
-     integer *maxf, integer *maxt, int *force_stop, doublereal *minf, doublereal *l, 
+     integer *maxf, integer *maxt, 
+     double starttime, double maxtime, 
+     int *force_stop, doublereal *minf, doublereal *l, 
      doublereal *u, integer *algmethod, integer *ierror, FILE *logfile, 
      doublereal *fglobal, doublereal *fglper, doublereal *volper, 
      doublereal *sigmaper, void *fcn_data);
index cd6a1247f84979f45f0941ce9b194652b088bb5a..2b100b2384aca3e29091af0faa1263a03a7393a4 100644 (file)
@@ -28,6 +28,7 @@ typedef enum {
      DIRECT_GLOBAL_FOUND = 3,
      DIRECT_VOLTOL = 4,
      DIRECT_SIGMATOL = 5,
+     DIRECT_MAXTIME_EXCEEDED = 6,
 
      DIRECT_OUT_OF_MEMORY = -100,
      DIRECT_INVALID_ARGS = -101,
@@ -44,7 +45,8 @@ extern direct_return_code direct_optimize(
 
      double *x, double *minf, 
 
-     int max_feval, int max_iter,
+     int max_feval, int max_iter, 
+     double start, double maxtime,
      double magic_eps, double magic_eps_abs,
      double volume_reltol, double sigma_reltol,
      int *force_stop,
index 330743d566b633e11ad1f6cdb9d3a7cc5f1cfe6e..98d7c2809734e119c2198f15ac4812349e1d8160 100644 (file)
@@ -48,6 +48,7 @@ direct_return_code direct_optimize(
      double *x, double *minf, 
 
      int max_feval, int max_iter,
+     double start, double maxtime,
      double magic_eps, double magic_eps_abs,
      double volume_reltol, double sigma_reltol,
      int *force_stop,
@@ -86,7 +87,8 @@ direct_return_code direct_optimize(
      }
      
      direct_direct_(f, x, &dimension, &magic_eps, magic_eps_abs,
-                   &max_feval, &max_iter, force_stop,
+                   &max_feval, &max_iter, 
+                   start, maxtime, force_stop,
                    minf,
                    l, u,
                    &algmethod,
index c7aef146184ac43eb0c6509ae4909ac0037b0d11..7d4e59990ec518aafa482393e7eac3a8582a8f3e 100644 (file)
@@ -277,10 +277,10 @@ static void plip_(int *nf, int *nb, double *x, int *
     if (*iterm != 0) {
        goto L11190;
     }
-    if (nlopt_stop_time(stop)) { *iterm = 100; goto L11190; }
     *f = objgrad(*nf, &x[1], &gf[1], objgrad_data);
     ++stop->nevals;
     ++stat_1->nfg;
+    if (nlopt_stop_time(stop)) { *iterm = 100; goto L11190; }
 L11120:
     luksan_pytrcg__(nf, nf, &ix[1], &gf[1], &umax, gmax, &kbf, &iold);
     luksan_pyfut1__(nf, f, &fo, &umax, gmax, xstop, stop, tolg, 
@@ -516,6 +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 100: return NLOPT_MAXTIME_REACHED;
         case -999: return NLOPT_FORCED_STOP;
         default: return NLOPT_FAILURE;
      }
index 925895399d23d4f33e95237aeb684af8df9afea0..03552f0413e7d96b59e807aa05686dede7d81b32 100644 (file)
@@ -259,10 +259,10 @@ static void plis_(int *nf, int *nb, double *x, int *
     if (*iterm != 0) {
        goto L11190;
     }
-    if (nlopt_stop_time(stop)) { *iterm = 100; goto L11190; }
     *f = objgrad(*nf, &x[1], &gf[1], objgrad_data);
     ++stop->nevals;
     ++stat_1->nfg;
+    if (nlopt_stop_time(stop)) { *iterm = 100; goto L11190; }
 L11120:
     luksan_pytrcg__(nf, nf, &ix[1], &gf[1], &umax, gmax, &kbf, &iold);
     luksan_pyfut1__(nf, f, &fo, &umax, gmax, xstop, stop, tolg, 
@@ -509,6 +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 100: return NLOPT_MAXTIME_REACHED;
         case -999: return NLOPT_FORCED_STOP;
         default: return NLOPT_FAILURE;
      }
index f37195c6335f880abf11126045e1267a871a2c94..ac009c74847828c68712a4d36b5f974776e60d29 100644 (file)
@@ -301,6 +301,7 @@ static void pnet_(int *nf, int *nb, double *x, int *
     *f = objgrad(*nf, &x[1], &gf[1], objgrad_data);
     ++stop->nevals;
     ++stat_1->nfg;
+    if (nlopt_stop_time(stop)) { *iterm = 100; goto L11080; }
     ld = kd;
 L11020:
     luksan_pytrcg__(nf, nf, &ix[1], &gf[1], &umax, gmax, &kbf, &iold);
@@ -659,6 +660,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 100: return NLOPT_MAXTIME_REACHED;
         case -999: return NLOPT_FORCED_STOP;
         default: return NLOPT_FAILURE;
      }
index cc352151a64a53172f05f8b278f5740dc33ffdc8..7f5568f74d9b34fc0c98d50b7d797719c9e47f24 100644 (file)
@@ -2100,8 +2100,10 @@ L290:
     ++nf;
 L310:
     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;
+    else if (stop->nevals > 0) {
+        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;
 
     stop->nevals++;
index 222ce6f69d2f34cd4fd186a084632a0d0b9e9cce..29934d47307548c7eb7244a4f69a25a519fd983c 100644 (file)
@@ -92,6 +92,7 @@ extern int nlopt_stop_xs(const nlopt_stopping *stop,
                         const double *xs, const double *oldxs,
                         const double *scale_min, const double *scale_max);
 extern int nlopt_stop_evals(const nlopt_stopping *stop);
+extern int nlopt_stop_time_(double start, double maxtime);
 extern int nlopt_stop_time(const nlopt_stopping *stop);
 extern int nlopt_stop_evalstime(const nlopt_stopping *stop);
 extern int nlopt_stop_forced(const nlopt_stopping *stop);
index 09e8686986e861254c4017ee10ac33632b117d0a..7484ecbbf84b7246295ba7ea00726c9fadeac232 100644 (file)
@@ -86,9 +86,14 @@ int nlopt_stop_evals(const nlopt_stopping *s)
      return (s->maxeval > 0 && s->nevals >= s->maxeval);
 }
 
+int nlopt_stop_time_(double start, double maxtime)
+{
+     return (maxtime > 0 && nlopt_seconds() - start >= maxtime);
+}
+
 int nlopt_stop_time(const nlopt_stopping *s)
 {
-     return (s->maxtime > 0 && nlopt_seconds() - s->start >= s->maxtime);
+     return nlopt_stop_time_(s->start, s->maxtime);
 }
 
 int nlopt_stop_evalstime(const nlopt_stopping *stop)