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
41 eps_cl=P.eps_cl; mu=P.mu; rshift=P.rshift;
42 det_pnts=P.det_pnts; rnd_pnts=P.rnd_pnts;
46 #if 0 // not necessary; default copy is sufficient
47 Global& Global::operator=(const Global &G) {
48 // Copy the problem info and parameter settings
49 Domain=G.Domain; Objective=G.Objective; Gradient=G.Gradient;
53 eps_cl=G.eps_cl; mu=G.mu; rshift=G.rshift;
54 det_pnts=G.det_pnts; rnd_pnts=G.rnd_pnts;
59 void Global::FillRegular(RTBox SampleBox, RTBox box) {
60 // Generation of regular sampling points
64 RVector m(dim), x(dim);
68 tmpTrial.objval=DBL_MAX ;
70 i=1 ; flag=1 ; dir=0 ;
74 x(dir)=m(dir)+flag*rshift*w ;
76 SampleBox.AddTrial(tmpTrial) ;
78 if (flag==1 && dir<dim) {
86 SampleBox.AddTrial(tmpTrial) ;
90 void Global::FillRandom(RTBox SampleBox, RTBox box) {
91 // Generation of stochastic sampling points
94 tmpTrial.objval=DBL_MAX;
95 for (int i=1 ; i<=rnd_pnts ; i++) {
96 for (int dir=0 ; dir<dim ; dir++)
97 tmpTrial.xvals(dir) = nlopt_urand(box.lb(dir), box.ub(dir));
98 SampleBox.AddTrial(tmpTrial) ;
102 double Global::NewtonTest(RTBox box, int axis, RCRVector x_av, int *noutside) {
103 // Perform the Newton test
107 TBox SampleBox(dim) ;
110 // Create sampling points
111 FillRandom(SampleBox, box);
112 FillRegular(SampleBox, box);
114 // Perform the actual sampling
115 while ( !SampleBox.EmptyBox() ) {
116 SampleBox.RemoveTrial(tmpTrial) ;
117 info = local(tmpTrial, box, Domain, eps_cl, &maxgrad, *this,
119 // What should we do when info=LS_Unstable?
122 if (info == LS_New ) {
123 box.AddTrial(tmpTrial) ;
125 if (tmpTrial.objval<=fbound+mu && tmpTrial.objval<=box.fmin+mu) {
127 cout << "Found a candidate, x=" << tmpTrial.xvals;
128 cout << " F=" <<tmpTrial.objval << " FC=" << FC << endl;
130 SolSet.push_back(tmpTrial);
133 cout << "Found a stationary point, X= " << tmpTrial.xvals;
134 cout <<" objval=" << tmpTrial.objval << endl;
145 void Global::ReduceOrSubdivide(RTBox box, int axis, RCRVector x_av) {
146 TBox B1(dim), B2(dim);
151 // Monotonicity test has not been implemented yet
152 maxgrad=NewtonTest(box, axis, x_av, &nout);
153 ns=box.NStationary() ;
155 // All iterates outside
156 // NB result=Intersection(B,boundary(Domain))
160 if (ns==1 && nout==0) {
161 // All iterates converge to same point
165 if ( (ns>1) && (box.LowerBound(maxgrad)>fbound) ) {
166 // Several stationary points found and lower bound > fbound
171 B1.ClearBox() ; B2.ClearBox() ;
173 CandSet.push(B1) ; CandSet.push(B2) ;
177 if (box.fmin < fbound) {
180 cout <<"*** Improving fbound, fbound=" << fbound << endl;
185 void Global::Search(int axis, RCRVector x_av){
186 Trial tmpTrial(dim) ;
187 TBox box(dim), B1(dim), B2(dim);
188 RVector m(dim), x(dim);
189 int inner_iter, outer_iter;
191 MacEpsilon=eps(); // Get machine precision
192 if (det_pnts>2*dim+1) {
195 cout << "Warning: Reducing det_pnts to " << det_pnts << endl;
199 StartTime = nlopt_seconds();
201 // Clear priority_queues
202 while (!Garbage.empty())
204 while (!CandSet.empty())
209 int done=0 ; outer_iter=0 ;
216 while (!CandSet.empty()) {
218 // Get best box from Candidate set
219 box=CandSet.top() ; CandSet.pop() ;
222 cout << "Iteration..." << inner_iter << " #CS=" << CandSet.size()+1 ;
223 cout << " Processing " << box.NStationary() << " trials in the box " <<box;
225 ReduceOrSubdivide(box, axis, x_av);
230 cout << "The program has run out of time or function evaluations\n";
234 } // inner while-loop
236 cout << endl << "*** Inner loop completed ***" << endl ;
238 // Reduce SolSet if necessary
239 SolSet.erase(remove_if(SolSet.begin(), SolSet.end(),
240 TrialGT(fbound+mu)),SolSet.end());
243 cout << "Current set of minimizers (" << SolSet.size() << ")" << endl ;
247 while (!Garbage.empty()) {
251 B1.ClearBox() ; B2.ClearBox() ;
253 // Add boxes to Candidate set
254 CandSet.push(B1) ; CandSet.push(B2) ;
257 } // Outer while-loop
260 cout << "Number of outer iterations : " << outer_iter << endl;
261 cout << "Number of unexplored boxes : " << CandSet.size() << endl;
262 cout << "Number of boxes in garbage : " << Garbage.size() << endl;
263 cout << "Number of elements in SolSet : " << SolSet.size() << endl;
264 cout << "Number of function evaluations : " << FC << endl;
265 cout << "Number of gradient evaluations : " << GC << endl;
269 // Return minimizer when doing the AV method
270 tmpTrial=SolSet.back();
271 x_av(axis)=tmpTrial.xvals(0);
275 /************* Various utility functions ****************/
276 double Global::GetTime()
278 return nlopt_seconds() - StartTime;
281 bool Global::InTime()
283 return (maxtime <= 0.0 || GetTime()<maxtime) && (!maxeval || numeval<maxeval);
286 double Global::GetMinValue() {
290 void Global::SetMinValue(double new_fb) {
294 void Global::SetDomain(RTBox box) {
298 void Global::GetDomain(RTBox box) {
302 void Global::DispMinimizers() {
303 copy(SolSet.begin(), SolSet.end(), ostream_iterator<Trial>(cout));
306 double Global::OneMinimizer(RCRVector x) {
307 if (NoMinimizers()) return 0.0;
308 for (int i=0;i<x.GetLength();i++) x(i) = SolSet.front().xvals(i);
309 return SolSet.front().objval;
312 bool Global::NoMinimizers() {
313 return SolSet.empty();
316 void Global::ClearSolSet() {
317 SolSet.erase(SolSet.begin(), SolSet.end()) ;
320 void Global::AddPoint(RCRVector x, double f) {
322 T.xvals=x; T.objval=f;