chiark / gitweb /
added option to StoGO (currently disabled) to use NLopt LBFGS for local minimization...
[nlopt.git] / stogo / global.cc
1 /*
2    Multi Dimensional Global Search.
3
4    Author: Steinn Gudmundsson
5    Email: steinng@hotmail.com
6
7    This program is supplied without any warranty whatsoever.
8
9    NB The RNGs seed should be initialized using some timer
10 */
11
12 #include <iostream>
13
14 #include <iterator>
15 #include <algorithm>
16 #include <stack>
17
18 #include "stogo_config.h"
19 #include "global.h"
20 #include "local.h"
21 #include "nlopt-util.h"
22
23 // Timer stuff
24 double   StartTime;
25
26 double MacEpsilon ;
27 int FC=0, GC=0 ;
28
29 int stogo_verbose = 0; /* set to nonzero for verbose output */
30
31 Global::Global(RTBox D, Pobj o, Pgrad g, GlobalParams P): Domain(D) {
32
33   dim=Domain.GetDim();
34   Objective=o;
35   Gradient=g;
36
37   // Initialize parameters
38 #ifdef NLOPT_UTIL_H
39   stop = P.stop;
40 #else
41   maxtime=P.maxtime;
42   maxeval = P.maxeval;
43 #endif
44   numeval = 0;
45   eps_cl=P.eps_cl; mu=P.mu; rshift=P.rshift;
46   det_pnts=P.det_pnts; rnd_pnts=P.rnd_pnts;
47   fbound=DBL_MAX;
48 }
49
50 #if 0 // not necessary; default copy is sufficient 
51 Global& Global::operator=(const Global &G) {
52   // Copy the problem info and parameter settings
53   Domain=G.Domain; Objective=G.Objective;  Gradient=G.Gradient;
54 #ifdef NLOPT_UTIL_H
55   stop = G.stop;
56 #else
57   maxtime=G.maxtime;
58   maxeval = G.maxeval;
59 #endif
60   numeval = G.numeval;
61   eps_cl=G.eps_cl; mu=G.mu; rshift=G.rshift;
62   det_pnts=G.det_pnts; rnd_pnts=G.rnd_pnts;
63   return *this;
64 }
65 #endif
66
67 void Global::FillRegular(RTBox SampleBox, RTBox box) {
68   // Generation of regular sampling points
69   double w;
70   int i, flag, dir;
71   Trial tmpTrial(dim);
72   RVector m(dim), x(dim);
73
74   if (det_pnts>0) {
75     box.Midpoint(m) ;
76     tmpTrial.objval=DBL_MAX ;
77     // Add the rest
78     i=1 ; flag=1 ; dir=0 ;
79     x=m ; 
80     while (i<det_pnts) {
81       w=box.Width(dir) ;
82       x(dir)=m(dir)+flag*rshift*w ;
83       tmpTrial.xvals=x ; 
84       SampleBox.AddTrial(tmpTrial) ;
85       flag=-flag;
86       if (flag==1 && dir<dim) {
87         x(dir)=m(dir) ;
88         dir++ ;
89       }
90       i++ ;
91     }
92     // Add midpoint
93     tmpTrial.xvals=m ; 
94     SampleBox.AddTrial(tmpTrial) ;
95   }
96 }
97
98 void Global::FillRandom(RTBox SampleBox, RTBox box) {
99   // Generation of stochastic sampling points
100   Trial tmpTrial(dim);
101
102   tmpTrial.objval=DBL_MAX;
103   for (int i=1 ; i<=rnd_pnts ; i++) {
104     for (int dir=0 ; dir<dim ; dir++)
105       tmpTrial.xvals(dir) = nlopt_urand(box.lb(dir), box.ub(dir));
106     SampleBox.AddTrial(tmpTrial) ;
107   }
108 }
109
110 double Global::NewtonTest(RTBox box, int axis, RCRVector x_av, int *noutside) {
111   // Perform the Newton test
112
113   int info,nout=0;
114   Trial tmpTrial(dim);
115   TBox SampleBox(dim) ;
116   double maxgrad=0 ;
117
118   // Create sampling points
119   FillRandom(SampleBox, box);
120   FillRegular(SampleBox, box);
121
122   // Perform the actual sampling
123   while ( !SampleBox.EmptyBox() ) {
124     SampleBox.RemoveTrial(tmpTrial) ;
125     info = local(tmpTrial, box, Domain, eps_cl, &maxgrad, *this,
126                  axis, x_av
127 #ifdef NLOPT_UTIL_H
128                  , stop
129 #endif
130                  ) ;
131     // What should we do when info=LS_Unstable?
132     if (info == LS_Out)
133       nout++;
134     else if (info == LS_New ) {
135       box.AddTrial(tmpTrial) ;
136
137       if (tmpTrial.objval<=fbound+mu && tmpTrial.objval<=box.fmin+mu) {
138         if (stogo_verbose) {
139           cout << "Found a candidate, x=" << tmpTrial.xvals;
140           cout << " F=" <<tmpTrial.objval << " FC=" << FC << endl;
141         }
142         SolSet.push_back(tmpTrial);
143       }
144 #ifdef GS_DEBUG
145       cout << "Found a stationary point, X= " << tmpTrial.xvals;
146       cout <<" objval=" << tmpTrial.objval << endl;
147 #endif
148     }
149
150     if (!InTime() || info == LS_MaxEvalTime)
151       break;
152   }
153   *noutside=nout;
154   return maxgrad;
155 }
156
157 void Global::ReduceOrSubdivide(RTBox box, int axis, RCRVector x_av) {
158   TBox B1(dim), B2(dim);
159   Trial tmpTrial(dim);
160   double maxgrad;
161   int ns,nout;
162
163   // Monotonicity test has not been implemented yet
164   maxgrad=NewtonTest(box, axis, x_av, &nout);
165   ns=box.NStationary() ;
166   if (ns==0) {
167     // All iterates outside
168     // NB result=Intersection(B,boundary(Domain))
169     Garbage.push(box) ;
170   }
171   else
172     if (ns==1 && nout==0) {
173       // All iterates converge to same point
174       Garbage.push(box) ;
175     }
176     else
177       if ( (ns>1) && (box.LowerBound(maxgrad)>fbound) ) {
178         // Several stationary points found and lower bound > fbound
179         Garbage.push(box) ;
180       }
181       else {
182         // Subdivision
183         B1.ClearBox() ; B2.ClearBox() ;
184         box.split(B1,B2) ;
185         CandSet.push(B1) ; CandSet.push(B2) ;
186       }
187
188   // Update fbound
189   if (box.fmin < fbound) {
190     fbound=box.fmin ;
191 #ifdef GS_DEBUG
192     cout <<"*** Improving fbound, fbound=" << fbound << endl;
193 #endif
194   }
195 }
196
197 void Global::Search(int axis, RCRVector x_av){
198   Trial tmpTrial(dim) ;
199   TBox box(dim), B1(dim), B2(dim);
200   RVector m(dim), x(dim);
201   int inner_iter, outer_iter;
202
203   MacEpsilon=eps(); // Get machine precision
204   if (det_pnts>2*dim+1) {
205     det_pnts=2*dim+1;
206     if (stogo_verbose)
207       cout << "Warning: Reducing det_pnts to " << det_pnts << endl;
208   }
209
210   // Initialize timer
211   StartTime = nlopt_seconds();
212
213   // Clear priority_queues
214   while (!Garbage.empty())
215     Garbage.pop();
216   while (!CandSet.empty())
217     CandSet.pop();
218
219   box=Domain;
220   CandSet.push(box);
221   int done=0 ; outer_iter=0 ;
222
223   while (!done) {
224     outer_iter++ ;
225
226     // Inner loop
227     inner_iter=0 ;
228     while (!CandSet.empty()) {
229       inner_iter++ ;
230       // Get best box from Candidate set
231       box=CandSet.top() ; CandSet.pop() ;
232
233 #ifdef GS_DEBUG
234       cout << "Iteration..." << inner_iter << " #CS=" << CandSet.size()+1 ;
235       cout << " Processing " << box.NStationary() << " trials in the box " <<box;
236 #endif
237       ReduceOrSubdivide(box, axis, x_av);
238
239       if (!InTime()) {
240         done=TRUE;
241         if (stogo_verbose)
242           cout << "The program has run out of time or function evaluations\n";
243         break;
244       }
245
246     } // inner while-loop
247     if (stogo_verbose)
248       cout << endl << "*** Inner loop completed ***" << endl ;
249     
250     // Reduce SolSet if necessary
251     SolSet.erase(remove_if(SolSet.begin(), SolSet.end(),
252                            TrialGT(fbound+mu)),SolSet.end());
253     if (InTime()) {
254       if (stogo_verbose) {
255         cout << "Current set of minimizers (" << SolSet.size() << ")" << endl ;
256         DispMinimizers() ;
257       }
258
259       while (!Garbage.empty()) {
260         box=Garbage.top() ;
261         Garbage.pop() ;
262         // Split box
263         B1.ClearBox() ; B2.ClearBox() ;
264         box.split(B1,B2) ;
265         // Add boxes to Candidate set
266         CandSet.push(B1) ; CandSet.push(B2) ;
267       }
268     }
269   } // Outer while-loop
270
271   if (stogo_verbose) {
272     cout << "Number of outer iterations : " << outer_iter << endl;
273     cout << "Number of unexplored boxes : " << CandSet.size() << endl;
274     cout << "Number of boxes in garbage : " << Garbage.size() << endl;
275     cout << "Number of elements in SolSet : " << SolSet.size() << endl;
276     cout << "Number of function evaluations : " << FC << endl;
277     cout << "Number of gradient evaluations : " << GC << endl;
278   }
279
280   if (axis != -1) {
281     // Return minimizer when doing the AV method
282     tmpTrial=SolSet.back();
283     x_av(axis)=tmpTrial.xvals(0);
284   }
285 }
286
287 /************* Various utility functions ****************/
288 double Global::GetTime()
289 {
290   return nlopt_seconds() - StartTime;
291 }
292
293 bool Global::InTime()
294 {
295 #ifdef NLOPT_UTIL_H
296   return !nlopt_stop_evalstime(stop);
297 #else
298  return (maxtime <= 0.0 || GetTime()<maxtime) && (!maxeval || numeval<maxeval);
299 #endif
300 }
301
302 double Global::GetMinValue() {
303   return fbound;
304 }
305
306 void Global::SetMinValue(double new_fb) {
307   fbound=new_fb;
308 }
309
310 void Global::SetDomain(RTBox box) {
311   Domain=box;
312 }
313
314 void Global::GetDomain(RTBox box) {
315   box=Domain;
316 }
317
318 void Global::DispMinimizers() {
319   copy(SolSet.begin(), SolSet.end(), ostream_iterator<Trial>(cout));
320 }
321
322 double Global::OneMinimizer(RCRVector x) {
323   if (NoMinimizers()) return 0.0;
324   for (int i=0;i<x.GetLength();i++) x(i) = SolSet.front().xvals(i);
325   return SolSet.front().objval;
326 }
327
328 bool Global::NoMinimizers() {
329   return SolSet.empty();
330 }
331
332 void Global::ClearSolSet() {
333   SolSet.erase(SolSet.begin(), SolSet.end()) ;
334 }
335
336 void Global::AddPoint(RCRVector x, double f) {
337   Trial T(dim);
338   T.xvals=x; T.objval=f;
339   Domain.AddTrial(T);
340   SolSet.push_back(T);
341 }