chiark / gitweb /
use nlopt_stopping in StoGO (currently only for maxevals and maxtime)
authorstevenj <stevenj@alum.mit.edu>
Sat, 25 Aug 2007 16:51:55 +0000 (12:51 -0400)
committerstevenj <stevenj@alum.mit.edu>
Sat, 25 Aug 2007 16:51:55 +0000 (12:51 -0400)
darcs-hash:20070825165155-c8de0-f3a61c73a2e539e9751e4cacfa7fd03d1fe5aeb9.gz

api/nlopt.c
stogo/global.cc
stogo/global.h
stogo/local.cc
stogo/local.h
stogo/stogo.cc
stogo/stogo.h
util/nlopt-util.h
util/stop.c

index 7b0e09772447c84388ed016fae6f8179bed7faba..2a3258065901b88a745dc2234c8703095dc0d86b 100644 (file)
@@ -265,8 +265,7 @@ static nlopt_result nlopt_minimize_(
              d.xtmp = (double *) malloc(sizeof(double) * n*2);
              if (!d.xtmp) return NLOPT_OUT_OF_MEMORY;
              memcpy(d.xtmp + n, x, sizeof(double) * n); d.x0 = d.xtmp + n;
-             iret = stogo_minimize(n, f_recenter, &d, x, fmin, lb, ub,
-                                   maxeval, maxtime,
+             iret = stogo_minimize(n, f_recenter, &d, x, fmin, lb, ub, &stop,
                                    algorithm == NLOPT_GLOBAL_STOGO
                                    ? 0 : 2*n);
              recenter_x(n, x, lb, ub, d.x0, x);
index 2f61703349ba90cce35de5c931a0cc9c2e188058..76fcd14bc65f6979ab8919c9ee9500cc5dec76d2 100644 (file)
@@ -35,8 +35,12 @@ Global::Global(RTBox D, Pobj o, Pgrad g, GlobalParams P): Domain(D) {
   Gradient=g;
 
   // Initialize parameters
+#ifdef NLOPT_UTIL_H
+  stop = P.stop;
+#else
   maxtime=P.maxtime;
   maxeval = P.maxeval;
+#endif
   numeval = 0;
   eps_cl=P.eps_cl; mu=P.mu; rshift=P.rshift;
   det_pnts=P.det_pnts; rnd_pnts=P.rnd_pnts;
@@ -47,8 +51,12 @@ Global::Global(RTBox D, Pobj o, Pgrad g, GlobalParams P): Domain(D) {
 Global& Global::operator=(const Global &G) {
   // Copy the problem info and parameter settings
   Domain=G.Domain; Objective=G.Objective;  Gradient=G.Gradient;
+#ifdef NLOPT_UTIL_H
+  stop = G.stop;
+#else
   maxtime=G.maxtime;
   maxeval = G.maxeval;
+#endif
   numeval = G.numeval;
   eps_cl=G.eps_cl; mu=G.mu; rshift=G.rshift;
   det_pnts=G.det_pnts; rnd_pnts=G.rnd_pnts;
@@ -115,11 +123,15 @@ double Global::NewtonTest(RTBox box, int axis, RCRVector x_av, int *noutside) {
   while ( !SampleBox.EmptyBox() ) {
     SampleBox.RemoveTrial(tmpTrial) ;
     info = local(tmpTrial, box, Domain, eps_cl, &maxgrad, *this,
-                axis, x_av) ;
+                axis, x_av
+#ifdef NLOPT_UTIL_H
+                , stop
+#endif
+                ) ;
     // What should we do when info=LS_Unstable?
     if (info == LS_Out)
       nout++;
-    if (info == LS_New ) {
+    else if (info == LS_New ) {
       box.AddTrial(tmpTrial) ;
 
       if (tmpTrial.objval<=fbound+mu && tmpTrial.objval<=box.fmin+mu) {
@@ -135,7 +147,7 @@ double Global::NewtonTest(RTBox box, int axis, RCRVector x_av, int *noutside) {
 #endif
     }
 
-    if (!InTime())
+    if (!InTime() || info == LS_MaxEvalTime)
       break;
   }
   *noutside=nout;
@@ -280,7 +292,11 @@ double Global::GetTime()
 
 bool Global::InTime()
 {
+#ifdef NLOPT_UTIL_H
+  return !nlopt_stop_evalstime(stop);
+#else
  return (maxtime <= 0.0 || GetTime()<maxtime) && (!maxeval || numeval<maxeval);
+#endif
 }
 
 double Global::GetMinValue() {
index e9d8814c035e79fd901363b1bde03b5b16cd5555..36c2b0e15c1713d64e0f661c7638f099d9b45394 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef GLOBAL_H
 #define GLOBAL_H
 
+#include "nlopt-util.h"
+
 #include <queue>
 //#include "function.h"
 #include "tools.h"
@@ -24,8 +26,12 @@ typedef objgrad* Pobjgrad ;
 
 class GlobalParams {
 public:
+#ifdef NLOPT_UTIL_H
+  nlopt_stopping *stop;
+#else
   double maxtime;
   long int maxeval;
+#endif
   double eps_cl, mu, rshift;
   int det_pnts, rnd_pnts;
 };
index 655a337221aa98ff41b99a79c23d609671600c65..4868f2dd3e5954b2d64a28e0c813b28446cc37cc 100644 (file)
 #include "local.h"
 #include "tools.h"
 
+#ifdef NLOPT_UTIL_H
+#  define IF_NLOPT_CHECK_EVALS stop->nevals++; \
+                               if (nlopt_stop_evalstime(stop)) \
+                                  return LS_MaxEvalTime
+#else
+#  define IF_NLOPT_CHECK_EVALS 
+#endif
+
 int local(Trial &T, TBox &box, TBox &domain, double eps_cl, double *mgr,
-          Global &glob, int axis, RCRVector x_av) {
+          Global &glob, int axis, RCRVector x_av
+#ifdef NLOPT_UTIL_H
+         , nlopt_stopping *stop
+#endif
+         ) {
 
   int k_max, info, outside ;
   int k, i, good_enough, iTmp ;
@@ -63,6 +75,7 @@ int local(Trial &T, TBox &box, TBox &domain, double eps_cl, double *mgr,
     f=glob.ObjectiveGradient(x_av,g_av,OBJECTIVE_AND_GRADIENT);
     g(0)=g_av(axis);
   }
+  IF_NLOPT_CHECK_EVALS;
   FC++;GC++;
 
   if (axis == -1) {
@@ -217,7 +230,8 @@ int local(Trial &T, TBox &box, TBox &domain, double eps_cl, double *mgr,
       x_av(axis)=x_new(0);
       f_new=glob.ObjectiveGradient(x_av,g_av,OBJECTIVE_AND_GRADIENT);
     }
-    FC++;
+    IF_NLOPT_CHECK_EVALS;
+    FC++; GC++;
     gemv('N',0.5,B,h_dl,0.0,z);
     ro = (f_new-f) / (dot(g,h_dl) + dot(h_dl,z)); // Quadratic model
     if (ro > 0.75) {
@@ -236,11 +250,12 @@ int local(Trial &T, TBox &box, TBox &domain, double eps_cl, double *mgr,
        glob.ObjectiveGradient(x_av,g_av,GRADIENT_ONLY);
        g_new(0)=g_av(axis);
       }
+      GC++;
+      IF_NLOPT_CHECK_EVALS;
 #else
       if (axis != -1)
        g_new(0)=g_av(axis);
 #endif
-      GC++;
 
       // y=g_new-g
       copy(g_new,y);
index 21136dda8018d351ad4772be374bbe32c7b987f3..d9d4c41694af09084c35314570b49f447529e49c 100644 (file)
@@ -11,7 +11,7 @@
 extern int FC, GC ;
 
 // Results of local search
-enum {LS_Unstable, LS_MaxIter, LS_Old, LS_New,LS_Out} ;
+enum {LS_Unstable, LS_MaxIter, LS_Old, LS_New,LS_Out, LS_MaxEvalTime} ;
 
 const double delta_coef = 1.0/2.0; // Initialization of trust region
 const double epsilon = 1.0E-4 ;    // Stopping criterion, var 1e-4
@@ -20,6 +20,10 @@ const int max_iter=50 ;            // Max iterations = max_iter*dim. of problem
 
 extern double MacEpsilon ;   //  min {x >= 0 : 1 + x > 1}
 
-int local(Trial &, TBox &, TBox &, double, double*, Global&, int, RCRVector) ;
+int local(Trial &, TBox &, TBox &, double, double*, Global&, int, RCRVector
+#ifdef NLOPT_UTIL_H
+         , nlopt_stopping *stop
+#endif
+     );
 
 #endif
index 13908376ad5f9a84e14d0774186a08c4ebdfb1a5..10bafa4e48e4797a89ff88be989490fd7b55514a 100644 (file)
@@ -30,7 +30,11 @@ int stogo_minimize(int n,
                   objective_func fgrad, void *data,
                   double *x, double *fmin,
                   const double *l, const double *u,
+#ifdef NLOPT_UTIL_H
+                  nlopt_stopping *stop,
+#else
                   long int maxeval, double maxtime,
+#endif
                   int nrandom)
 {
   GlobalParams params;
@@ -40,9 +44,7 @@ int stogo_minimize(int n,
   params.det_pnts=2*n+1 - nrandom; 
   params.eps_cl=0.1; params.rshift=0.3;
   params.mu=1.0E-4;
-
-  params.maxtime = maxtime;
-  params.maxeval = maxeval;
+  params.stop = stop;
 
   TBox D(n);
   for (int i = 0; i < n; ++i) {
index c5cd6d2a6a5540032c87953db7a229c7db12c42f..9577b5562f9813b9b5e6a366767c0a310998d579 100644 (file)
@@ -4,6 +4,8 @@
 #ifndef STOGO_H
 #define STOGO_H
 
+#include "nlopt-util.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -34,8 +36,8 @@ typedef double (*objective_func)(int n, const double *x, double *grad,
              search space
 
        maxeval: if nonzero, a maximum number of fgrad evaluations
-
        maxtime: if nonzero, a maximum time (in seconds)
+        -- REPLACED in NLopt by nlopt_stopping *stop
 
        nrandom: number of randomized search points to use per box,
                 in addition to 2*n+1 deterministic search points
@@ -55,7 +57,11 @@ int stogo_minimize(int n,
                    objective_func fgrad, void *data,
                    double *x, double *fmin,
                    const double *l, const double *u,
-                   long int maxeval, double maxtime,
+#ifdef NLOPT_UTIL_H
+                  nlopt_stopping *stop,
+#else
+                  long int maxeval, double maxtime,
+#endif
                   int nrandom);
 
 extern int stogo_verbose; /* set to nonzero for verbose output */
index ea9d7cf309f43ec5489f9242a0948f12e9416e0f..59bbcb29d2944bd7d5842a578627ecdfcaab80f6 100644 (file)
@@ -34,6 +34,7 @@ extern int nlopt_stop_xs(const nlopt_stopping *stop,
                         const double *scale_min, const double *scale_max);
 extern int nlopt_stop_evals(const nlopt_stopping *stop);
 extern int nlopt_stop_time(const nlopt_stopping *stop);
+extern int nlopt_stop_evalstime(const nlopt_stopping *stop);
 
 #ifdef __cplusplus
 }  /* extern "C" */
index a48a83b5a0a99f9d13b5106b5184fc1b4d470d75..52a9c7876755787da3fc95bbf772bffba3d17154 100644 (file)
@@ -57,3 +57,8 @@ int nlopt_stop_time(const nlopt_stopping *s)
 {
      return (s->maxtime > 0 && nlopt_seconds() - s->start >= s->maxtime);
 }
+
+int nlopt_stop_evalstime(const nlopt_stopping *stop)
+{
+     return nlopt_stop_evals(stop) || nlopt_stop_time(stop);
+}