From: stevenj Date: Sat, 25 Aug 2007 16:51:55 +0000 (-0400) Subject: use nlopt_stopping in StoGO (currently only for maxevals and maxtime) X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=25a8e57e2a03605b4e868d9debce3b1975016975;p=nlopt.git use nlopt_stopping in StoGO (currently only for maxevals and maxtime) darcs-hash:20070825165155-c8de0-f3a61c73a2e539e9751e4cacfa7fd03d1fe5aeb9.gz --- diff --git a/api/nlopt.c b/api/nlopt.c index 7b0e097..2a32580 100644 --- a/api/nlopt.c +++ b/api/nlopt.c @@ -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); diff --git a/stogo/global.cc b/stogo/global.cc index 2f61703..76fcd14 100644 --- a/stogo/global.cc +++ b/stogo/global.cc @@ -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() //#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; }; diff --git a/stogo/local.cc b/stogo/local.cc index 655a337..4868f2d 100644 --- a/stogo/local.cc +++ b/stogo/local.cc @@ -11,8 +11,20 @@ #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); diff --git a/stogo/local.h b/stogo/local.h index 21136dd..d9d4c41 100644 --- a/stogo/local.h +++ b/stogo/local.h @@ -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 diff --git a/stogo/stogo.cc b/stogo/stogo.cc index 1390837..10bafa4 100644 --- a/stogo/stogo.cc +++ b/stogo/stogo.cc @@ -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) { diff --git a/stogo/stogo.h b/stogo/stogo.h index c5cd6d2..9577b55 100644 --- a/stogo/stogo.h +++ b/stogo/stogo.h @@ -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 */ diff --git a/util/nlopt-util.h b/util/nlopt-util.h index ea9d7cf..59bbcb2 100644 --- a/util/nlopt-util.h +++ b/util/nlopt-util.h @@ -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" */ diff --git a/util/stop.c b/util/stop.c index a48a83b..52a9c78 100644 --- a/util/stop.c +++ b/util/stop.c @@ -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); +}