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
19 #include "stogo_config.h"
29 Global::Global(RTBox D, Pobj o, Pgrad g, GlobalParams P): Domain(D) {
35 // Initialize parameters
39 eps_cl=P.eps_cl; mu=P.mu; rshift=P.rshift;
40 det_pnts=P.det_pnts; rnd_pnts=P.rnd_pnts;
44 #if 0 // not necessary; default copy is sufficient
45 Global& Global::operator=(const Global &G) {
46 // Copy the problem info and parameter settings
47 Domain=G.Domain; Objective=G.Objective; Gradient=G.Gradient;
51 eps_cl=G.eps_cl; mu=G.mu; rshift=G.rshift;
52 det_pnts=G.det_pnts; rnd_pnts=G.rnd_pnts;
57 void Global::FillRegular(RTBox SampleBox, RTBox box) {
58 // Generation of regular sampling points
62 RVector m(dim), x(dim);
67 tmpTrial.xvals=m ; tmpTrial.objval=DBL_MAX ;
68 SampleBox.AddTrial(tmpTrial) ;
70 i=1 ; flag=1 ; dir=0 ;
71 x=m ; w=box.Width(dir) ;
73 x(dir)=m(dir)+flag*rshift*w ;
75 SampleBox.AddTrial(tmpTrial) ;
77 if (flag==1 && dir<dim) {
87 void Global::FillRandom(RTBox SampleBox, RTBox box) {
88 // Generation of stochastic sampling points
91 tmpTrial.objval=DBL_MAX;
92 for (int i=1 ; i<=rnd_pnts ; i++) {
93 for (int dir=0 ; dir<dim ; dir++)
95 box.lb(dir)+(box.ub(dir)-box.lb(dir))*(double(rand())/RAND_MAX) ;
96 SampleBox.AddTrial(tmpTrial) ;
100 double Global::NewtonTest(RTBox box, int axis, RCRVector x_av, int *noutside) {
101 // Perform the Newton test
105 TBox SampleBox(dim) ;
108 // Create sampling points
109 FillRegular(SampleBox, box);
110 FillRandom(SampleBox, box);
112 // Perform the actual sampling
113 while ( !SampleBox.EmptyBox() ) {
114 SampleBox.RemoveTrial(tmpTrial) ;
115 info = local(tmpTrial, box, Domain, eps_cl, &maxgrad, *this,
117 // What should we do when info=LS_Unstable?
120 if (info == LS_New ) {
121 box.AddTrial(tmpTrial) ;
123 if (tmpTrial.objval<=fbound+mu && tmpTrial.objval<=box.fmin+mu) {
124 cout << "Found a candidate, x=" << tmpTrial.xvals;
125 cout << " F=" <<tmpTrial.objval << " FC=" << FC << endl;
126 SolSet.push_back(tmpTrial);
129 cout << "Found a stationary point, X= " << tmpTrial.xvals;
130 cout <<" objval=" << tmpTrial.objval << endl;
141 void Global::ReduceOrSubdivide(RTBox box, int axis, RCRVector x_av) {
142 TBox B1(dim), B2(dim);
147 // Monotonicity test has not been implemented yet
148 maxgrad=NewtonTest(box, axis, x_av, &nout);
149 ns=box.NStationary() ;
151 // All iterates outside
152 // NB result=Intersection(B,boundary(Domain))
156 if (ns==1 && nout==0) {
157 // All iterates converge to same point
161 if ( (ns>1) && (box.LowerBound(maxgrad)>fbound) ) {
162 // Several stationary points found and lower bound > fbound
167 B1.ClearBox() ; B2.ClearBox() ;
169 CandSet.push(B1) ; CandSet.push(B2) ;
173 if (box.fmin < fbound) {
176 cout <<"*** Improving fbound, fbound=" << fbound << endl;
181 void Global::Search(int axis, RCRVector x_av){
182 Trial tmpTrial(dim) ;
183 TBox box(dim), B1(dim), B2(dim);
184 RVector m(dim), x(dim);
185 int inner_iter, outer_iter;
187 MacEpsilon=eps(); // Get machine precision
188 if (det_pnts>2*dim+1) {
190 cout << "Warning: Reducing det_pnts to " << det_pnts << endl;
196 // Clear priority_queues
197 while (!Garbage.empty())
199 while (!CandSet.empty())
204 int done=0 ; outer_iter=0 ;
211 while (!CandSet.empty()) {
213 // Get best box from Candidate set
214 box=CandSet.top() ; CandSet.pop() ;
217 cout << "Iteration..." << inner_iter << " #CS=" << CandSet.size()+1 ;
218 cout << " Processing " << box.NStationary() << " trials in the box " <<box;
220 ReduceOrSubdivide(box, axis, x_av);
224 cout << "The program has run out of time or function evaluations\n";
228 } // inner while-loop
229 cout << endl << "*** Inner loop completed ***" << endl ;
231 // Reduce SolSet if necessary
232 SolSet.erase(remove_if(SolSet.begin(), SolSet.end(),
233 TrialGT(fbound+mu)),SolSet.end());
235 cout << "Current set of minimizers (" << SolSet.size() << ")" << endl ;
238 while (!Garbage.empty()) {
242 B1.ClearBox() ; B2.ClearBox() ;
244 // Add boxes to Candidate set
245 CandSet.push(B1) ; CandSet.push(B2) ;
248 } // Outer while-loop
250 cout << "Number of outer iterations : " << outer_iter << endl;
251 cout << "Number of unexplored boxes : " << CandSet.size() << endl;
252 cout << "Number of boxes in garbage : " << Garbage.size() << endl;
253 cout << "Number of elements in SolSet : " << SolSet.size() << endl;
254 cout << "Number of function evaluations : " << FC << endl;
255 cout << "Number of gradient evaluations : " << GC << endl;
258 // Return minimizer when doing the AV method
259 tmpTrial=SolSet.back();
260 x_av(axis)=tmpTrial.xvals(0);
264 /************* Various utility functions ****************/
265 long int Global::GetTime()
267 time_t ctime; time(&ctime);
268 return (long int)difftime(ctime,StartTime);
271 bool Global::InTime()
273 return (!maxtime || GetTime()<maxtime) && (!maxeval || numeval<maxeval);
276 double Global::GetMinValue() {
280 void Global::SetMinValue(double new_fb) {
284 void Global::SetDomain(RTBox box) {
288 void Global::GetDomain(RTBox box) {
292 void Global::DispMinimizers() {
293 copy(SolSet.begin(), SolSet.end(), ostream_iterator<Trial>(cout));
296 double Global::OneMinimizer(RCRVector x) {
297 if (NoMinimizers()) return 0.0;
298 for (int i=0;i<x.GetLength();i++) x(i) = SolSet.front().xvals(i);
299 return SolSet.front().objval;
302 bool Global::NoMinimizers() {
303 return SolSet.empty();
306 void Global::ClearSolSet() {
307 SolSet.erase(SolSet.begin(), SolSet.end()) ;
310 void Global::AddPoint(RCRVector x, double f) {
312 T.xvals=x; T.objval=f;