chiark / gitweb /
1a5b32f4149c71877267e0253b277b0209d9149e
[nlopt.git] / stogo / stogo.cc
1 // A C-callable front-end to the StoGO global-optimization library.
2 //  -- Steven G. Johnson
3
4 #include "stogo.h"
5 #include "global.h"
6
7 class MyGlobal : public Global {
8 protected:
9   objective_func my_func;
10   void *my_data;
11
12 public:
13
14   MyGlobal(RTBox D, GlobalParams P, objective_func func, void *data) : Global(D, 0, 0, P), my_func(func), my_data(data) {}
15   
16   virtual double ObjectiveGradient(RCRVector xy, RVector &grad, whichO which){
17     ++numeval;
18     switch (which) {
19     case GRADIENT_ONLY:
20     case OBJECTIVE_AND_GRADIENT:
21       return my_func(xy.GetLength(), xy.raw_data_const(), grad.raw_data(), my_data);
22     case OBJECTIVE_ONLY:
23       return my_func(xy.GetLength(), xy.raw_data_const(), NULL, my_data);
24     }
25   }
26 };
27
28 int stogo_minimize(int n,
29                    objective_func fgrad, void *data,
30                    double *x, double *fmin,
31                    const double *l, const double *u,
32                    long int maxeval, long int maxtime)
33 {
34   GlobalParams params;
35
36   // FIXME: WTF do these parameters mean?
37   params.det_pnts=2*n+1; params.rnd_pnts=0;
38   params.eps_cl=0.1; params.rshift=0.3;
39   params.mu=1.0E-4;
40
41   params.maxtime = maxtime;
42   params.maxeval = maxeval;
43
44   TBox D(n);
45   for (int i = 0; i < n; ++i) {
46     D.lb(i) = l[i];
47     D.ub(i) = u[i];
48   }
49
50   MyGlobal Problem(D, params, fgrad, data);
51   RVector dummyvec(n);
52   Problem.Search(-1, dummyvec);
53
54   if (Problem.NoMinimizers())
55     return 0;
56   
57   *fmin = Problem.OneMinimizer(dummyvec);
58   for (int i = 0; i < n; ++i) x[i] = dummyvec(i);
59   return 1;
60 }