2 Multi Dimensional Global Search.
4 Author: Steinn Gudmundsson
5 Email: steinng@hotmail.com
7 This program is supplied without any warranty whatsoever.
9 NB The RNGs seed should be initialized using some timer
18 #include "stogo_config.h"
21 #include "nlopt-util.h"
29 int stogo_verbose = 0; /* set to nonzero for verbose output */
31 Global::Global(RTBox D, Pobj o, Pgrad g, GlobalParams P): Domain(D) {
37 // Initialize parameters
45 eps_cl=P.eps_cl; mu=P.mu; rshift=P.rshift;
46 det_pnts=P.det_pnts; rnd_pnts=P.rnd_pnts;
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;
61 eps_cl=G.eps_cl; mu=G.mu; rshift=G.rshift;
62 det_pnts=G.det_pnts; rnd_pnts=G.rnd_pnts;
67 void Global::FillRegular(RTBox SampleBox, RTBox box) {
68 // Generation of regular sampling points
72 RVector m(dim), x(dim);
76 tmpTrial.objval=DBL_MAX ;
78 i=1 ; flag=1 ; dir=0 ;
82 x(dir)=m(dir)+flag*rshift*w ;
84 SampleBox.AddTrial(tmpTrial) ;
86 if (flag==1 && dir<dim) {
94 SampleBox.AddTrial(tmpTrial) ;
98 void Global::FillRandom(RTBox SampleBox, RTBox box) {
99 // Generation of stochastic sampling points
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) ;
110 double Global::NewtonTest(RTBox box, int axis, RCRVector x_av, int *noutside) {
111 // Perform the Newton test
115 TBox SampleBox(dim) ;
118 // Create sampling points
119 FillRandom(SampleBox, box);
120 FillRegular(SampleBox, box);
122 // Perform the actual sampling
123 while ( !SampleBox.EmptyBox() ) {
124 SampleBox.RemoveTrial(tmpTrial) ;
125 info = local(tmpTrial, box, Domain, eps_cl, &maxgrad, *this,
131 // What should we do when info=LS_Unstable?
134 else if (info == LS_New ) {
135 box.AddTrial(tmpTrial) ;
137 if (tmpTrial.objval<=fbound+mu && tmpTrial.objval<=box.fmin+mu) {
139 cout << "Found a candidate, x=" << tmpTrial.xvals;
140 cout << " F=" <<tmpTrial.objval << " FC=" << FC << endl;
142 SolSet.push_back(tmpTrial);
145 cout << "Found a stationary point, X= " << tmpTrial.xvals;
146 cout <<" objval=" << tmpTrial.objval << endl;
150 if (!InTime() || info == LS_MaxEvalTime)
157 void Global::ReduceOrSubdivide(RTBox box, int axis, RCRVector x_av) {
158 TBox B1(dim), B2(dim);
163 // Monotonicity test has not been implemented yet
164 maxgrad=NewtonTest(box, axis, x_av, &nout);
165 ns=box.NStationary() ;
167 // All iterates outside
168 // NB result=Intersection(B,boundary(Domain))
172 if (ns==1 && nout==0) {
173 // All iterates converge to same point
177 if ( (ns>1) && (box.LowerBound(maxgrad)>fbound) ) {
178 // Several stationary points found and lower bound > fbound
183 B1.ClearBox() ; B2.ClearBox() ;
185 CandSet.push(B1) ; CandSet.push(B2) ;
189 if (box.fmin < fbound) {
192 cout <<"*** Improving fbound, fbound=" << fbound << endl;
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;
203 MacEpsilon=eps(); // Get machine precision
204 if (det_pnts>2*dim+1) {
207 cout << "Warning: Reducing det_pnts to " << det_pnts << endl;
211 StartTime = nlopt_seconds();
213 // Clear priority_queues
214 while (!Garbage.empty())
216 while (!CandSet.empty())
221 int done=0 ; outer_iter=0 ;
228 while (!CandSet.empty()) {
230 // Get best box from Candidate set
231 box=CandSet.top() ; CandSet.pop() ;
234 cout << "Iteration..." << inner_iter << " #CS=" << CandSet.size()+1 ;
235 cout << " Processing " << box.NStationary() << " trials in the box " <<box;
237 ReduceOrSubdivide(box, axis, x_av);
242 cout << "The program has run out of time or function evaluations\n";
246 } // inner while-loop
248 cout << endl << "*** Inner loop completed ***" << endl ;
250 // Reduce SolSet if necessary
251 SolSet.erase(remove_if(SolSet.begin(), SolSet.end(),
252 TrialGT(fbound+mu)),SolSet.end());
255 cout << "Current set of minimizers (" << SolSet.size() << ")" << endl ;
259 while (!Garbage.empty()) {
263 B1.ClearBox() ; B2.ClearBox() ;
265 // Add boxes to Candidate set
266 CandSet.push(B1) ; CandSet.push(B2) ;
269 } // Outer while-loop
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;
281 // Return minimizer when doing the AV method
282 tmpTrial=SolSet.back();
283 x_av(axis)=tmpTrial.xvals(0);
287 /************* Various utility functions ****************/
288 double Global::GetTime()
290 return nlopt_seconds() - StartTime;
293 bool Global::InTime()
296 return !nlopt_stop_evalstime(stop);
298 return (maxtime <= 0.0 || GetTime()<maxtime) && (!maxeval || numeval<maxeval);
302 double Global::GetMinValue() {
306 void Global::SetMinValue(double new_fb) {
310 void Global::SetDomain(RTBox box) {
314 void Global::GetDomain(RTBox box) {
318 void Global::DispMinimizers() {
319 copy(SolSet.begin(), SolSet.end(), ostream_iterator<Trial>(cout));
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;
328 bool Global::NoMinimizers() {
329 return SolSet.empty();
332 void Global::ClearSolSet() {
333 SolSet.erase(SolSet.begin(), SolSet.end()) ;
336 void Global::AddPoint(RCRVector x, double f) {
338 T.xvals=x; T.objval=f;