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);
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;
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;
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) {
#endif
}
- if (!InTime())
+ if (!InTime() || info == LS_MaxEvalTime)
break;
}
*noutside=nout;
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() {
#ifndef GLOBAL_H
#define GLOBAL_H
+#include "nlopt-util.h"
+
#include <queue>
//#include "function.h"
#include "tools.h"
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;
};
#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 ;
f=glob.ObjectiveGradient(x_av,g_av,OBJECTIVE_AND_GRADIENT);
g(0)=g_av(axis);
}
+ IF_NLOPT_CHECK_EVALS;
FC++;GC++;
if (axis == -1) {
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) {
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);
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
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
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;
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) {
#ifndef STOGO_H
#define STOGO_H
+#include "nlopt-util.h"
+
#ifdef __cplusplus
extern "C" {
#endif
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
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 */
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" */
{
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);
+}