Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] Annotation of /trunk/gadget/seq_optimize_template.h
[mareframe] / trunk / gadget / seq_optimize_template.h Repository:
ViewVC logotype

Annotation of /trunk/gadget/seq_optimize_template.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 16 - (view) (download)

1 : ulcessvp 12 #ifndef SEQ_OPTIMIZE_TEMPLATE_H
2 :     #define SEQ_OPTIMIZE_TEMPLATE_H
3 :    
4 :     #include <iostream>
5 :     #include <cstdlib>
6 :     #include <sys/time.h>
7 :    
8 : ulcessvp 16 #ifdef _OPENMP
9 : ulcessvp 12 #include <omp.h>
10 :     #endif
11 :    
12 :     class Siman {
13 :     int _nvars;
14 :     unsigned _seed;
15 :     unsigned _seedM;
16 :     unsigned _seedP;
17 :     int _l;
18 :     int _nt;
19 :     int _NT;
20 :     int _ns;
21 :     int _NS;
22 :     IntVector _param;
23 :     DoubleVector* _x;
24 :     DoubleVector* _lowerb;
25 :     DoubleVector* _upperb;
26 :     DoubleVector _vm;
27 :     double _t;
28 :     double _rt;
29 :     IntVector _nacp;
30 :     int _nacc; //The number of accepted function evaluations
31 :     int _nrej; //The number of rejected function evaluations
32 :     int _naccmet; //The number of metropolis accepted function evaluations
33 :     double _nsdiv;
34 :     int _tempcheck;
35 :     double _simanneps;
36 :     DoubleVector _fstar;
37 :     double _lratio;
38 :     double _uratio;
39 :     double _cs;
40 :     DoubleVector* _bestx;
41 :     int _scale;
42 :     int* _converge; //NN
43 :     double* _score; //NN
44 :    
45 :     public:
46 :    
47 :     //Required Default constructor : Params()
48 :     Siman(){
49 :     _nvars = 0;
50 :     _seed = 1234;
51 :     _seedM = 1234;
52 :     _seedP = 1234;
53 :     _l = 0;
54 :     _nt = 0;
55 :     _NT = 0;
56 :     _ns = 0;
57 :     _NS = 0;
58 :     _param = IntVector();
59 :     _x = NULL;
60 :     _lowerb = NULL;
61 :     _upperb = NULL;
62 :     _vm = DoubleVector();
63 :     _t = 0.;
64 :     _rt = 0.;
65 :     _nacp = IntVector();
66 :     _nacc = 0; //The number of accepted function evaluations
67 :     _nrej = 0; //The number of rejected function evaluations
68 :     _naccmet = 0; //The number of metropolis accepted function evaluations
69 :     _nsdiv = 0;
70 :     _tempcheck = 0;
71 :     _simanneps = 0;
72 :     _fstar = DoubleVector();
73 :     _lratio = 0.;
74 :     _uratio = 0.;
75 :     _cs = 0.;
76 :     _bestx = NULL;
77 :     _scale = 0;
78 :     _converge =NULL;
79 :     _score = 0;
80 :     }
81 :    
82 :     Siman(unsigned &seed, unsigned &seedM, unsigned &seedP, int &nvars, int &nt, int &ns, IntVector param, DoubleVector* x,
83 :     DoubleVector* lowerb, DoubleVector* upperb, DoubleVector vm, double t, double &rt,
84 :     double nsdiv, int &tempcheck, double &simanneps, DoubleVector fstar,
85 :     double &lratio, double &uratio, double &cs, DoubleVector* bestx, int &scale, int* converge,
86 :     double* score):
87 :     _seed(seed), _seedM(seedM), _seedP(seedP), _l(0), _nvars(nvars), _nt(nt), _NT(0), _ns(ns), _NS(0), _t(t),
88 :     _rt(rt), _nsdiv(nsdiv), _nacc(1), _tempcheck(tempcheck), _simanneps(simanneps), _lratio(lratio),
89 :     _uratio(uratio), _cs(cs), _scale(scale){
90 :     _param = IntVector(param);
91 :     _x = x;
92 :     _lowerb = lowerb;
93 :     _upperb = upperb;
94 :     _vm = DoubleVector(vm);
95 :     _nacp = IntVector(_nvars, 0);
96 :     _nrej = 0;
97 :     _naccmet = 0;
98 :     randomizeParams();
99 :     _fstar = DoubleVector(fstar);
100 :     _bestx = bestx;
101 :     _converge = converge;
102 :     _score = score;
103 :     }
104 :    
105 :     //Required Copy constructor : Params(const Params& other)
106 :     Siman(const Siman& other): _nvars(other._nvars), _seed(other._seed), _seedM(other._seedM), _seedP(other._seedP),
107 :     _l(other._l), _nt(other._nt), _NT(other._NT), _ns(other._ns), _NS(other._NS), _param(IntVector(other._param)),
108 :     _x(other._x), _lowerb(other._lowerb),_upperb(other._upperb), _vm(DoubleVector(other._vm)), _t(other._t),
109 :     _rt(other._rt), _nacp(IntVector(other._nacp)), _nacc(other._nacc), _nrej(other._nrej),
110 :     _naccmet(other._naccmet), _nsdiv(other._nsdiv), _tempcheck(other._tempcheck), _simanneps(other._simanneps),
111 :     _fstar(DoubleVector(other._fstar)), _lratio(other._lratio), _uratio(other._uratio), _cs(other._cs),
112 :     _bestx(other._bestx), _scale(other._scale), _converge(other._converge), _score(other._score){}
113 :    
114 :     //Required operator= : Params& operator= (const Params& other)
115 :     Siman& operator= (const Siman& other){
116 :     _nvars = other._nvars;
117 :     _seed = other._seed;
118 :     _seedM = other._seedM;
119 :     _seedP = other._seedP;
120 :     _l = other._l;
121 :     _nt = other._nt;
122 :     _NT = other._NT;
123 :     _ns = other._ns;
124 :     _NS = other._NS;
125 :     _param = IntVector(other._param);
126 :     _x = other._x;
127 :     _lowerb = other._lowerb;
128 :     _upperb = other._upperb;
129 :     _vm = DoubleVector(other._vm);
130 :     _t = other._t;
131 :     _rt = other._rt;
132 :     _nacp = IntVector(other._nacp);
133 :     _nacc = other._nacc; //The number of accepted function evaluations
134 :     _nrej = other._nrej; //The number of rejected function evaluations
135 :     _naccmet = other._naccmet; //The number of metropolis accepted function evaluations
136 :     _nsdiv = other._nsdiv;
137 :     _tempcheck = other._tempcheck;
138 :     _simanneps = other._simanneps;
139 :     _fstar = DoubleVector(other._fstar);
140 :     _lratio = other._lratio;
141 :     _uratio = other._uratio;
142 :     _cs = other._cs;
143 :     _bestx = other._bestx;
144 :     _scale = other._scale;
145 :     _converge =other._converge;
146 :     _score = other._score;
147 :    
148 :     return *this;
149 :     }
150 :     //Destructor : ~Params()
151 :     ~Siman(){
152 :    
153 :     }
154 :    
155 :     unsigned* getSeed(){return &_seed;}
156 :    
157 :     unsigned* getSeedM(){return &_seedM;}
158 :    
159 :     unsigned getSeedM_(){return _seedM;}
160 :    
161 :     void setSeedM(unsigned val){_seedM = val;}
162 :    
163 :     int getNvars(){return _nvars;}
164 :    
165 :     int getL(){return _l;}
166 :    
167 :     void incrementL() { _l = (_l +1) % _nvars;}
168 :    
169 :     IntVector& getParam(){return _param;}
170 :    
171 :     DoubleVector& getX(){return *_x;}
172 :    
173 :     void setX(DoubleVector* point) {_x=point;}
174 :    
175 :     DoubleVector& getLowerb(){return *_lowerb;}
176 :    
177 :     DoubleVector& getUpperb(){return *_upperb;}
178 :    
179 :     DoubleVector& getVm(){return _vm;}
180 :     void setVm(DoubleVector vm){_vm = vm;}
181 :    
182 :     DoubleVector& getFstar(){return _fstar;}
183 :    
184 :     double getT(){return _t;}
185 :    
186 :     void setT(double val){_t = val;}
187 :    
188 :     void incrementNacc() {_nacc++;}
189 :    
190 :     void incrementNrej(){_nrej++;}
191 :    
192 :     void incrementNaccmet(){_naccmet++;}
193 :    
194 :     void incrementNacp(int i){_nacp[i]++;}
195 :    
196 :     void randomizeParams(){
197 :     int rnumber, rcheck, i, rchange = 0;
198 :     while (rchange < _nvars) {
199 :     rnumber = rand_r(&_seedP) % _nvars;
200 :     rcheck = 1;
201 :     for (i = 0; i < rchange; i++)
202 :     if ((_param)[i] == rnumber)
203 :     rcheck = 0;
204 :     if (rcheck) {
205 :     (_param)[rchange] = rnumber;
206 :     rchange++;
207 :     }
208 :     }
209 :     }
210 :    
211 :     int getNs() const {return _ns;}
212 :    
213 :     int getNS() const {return _NS;}
214 :    
215 :     void setNS(int ns = 0) {_NS = ns;}
216 :    
217 :     void incrementNS() {_NS++;}
218 :    
219 :     int getNt() const {return _nt;}
220 :    
221 :     int getNT() const {return _NT;}
222 :    
223 :     void setNT(int nt) {_NT = nt;}
224 :    
225 :     void incrementNT() {_NT++;};
226 :    
227 :     double getSimanneps() const {return _simanneps;}
228 :    
229 :     int getTempcheck() const {return _tempcheck;}
230 :    
231 :     const int getNacp(int i) const {return _nacp[i];}
232 :    
233 :     void setNacp(int i, int val) {_nacp[i] = val;}
234 :    
235 :     double getCs() const {return _cs;}
236 :    
237 :     double getLratio() const {return _lratio;}
238 :    
239 :     double getUratio() const {return _uratio;}
240 :    
241 :     void setConverge(int val) {(*_converge) = val;}
242 :    
243 :     DoubleVector* getBestx() {return _bestx;}
244 :    
245 :     int getScale() const {return _scale;}
246 :    
247 :     double getRt() const {return _rt;}
248 :    
249 :     double getNsdiv() const {return _nsdiv;}
250 :    
251 :     int* getConverge() const {return _converge;}
252 :    
253 :     int getNacc() const {return _nacc;}
254 :    
255 :     int getNaccmet() const {return _naccmet;}
256 :    
257 :     int getNrej() const {return _nrej;}
258 :    
259 :     void setNrej(int nrej) {_nrej = nrej;};
260 :    
261 :     double* getScore() const {return _score;}
262 :     };
263 :    
264 :     /* IMPROVEMENTS:
265 :    
266 :     - OpenMP parallel implementation added
267 : ulcessvp 16 - Can be compiled with OpenMP using macro _OPENMP
268 : ulcessvp 12 - Memory for items is dynamically reused in the parallel searches
269 :     - Items whose evaluation has not begun when a new optimum is found are not evaluated (only possible in TBB search)
270 :     */
271 :    
272 :     /// Performs a reproducible optimization process, sequentially in seq_opt or in parallel in paral_opt
273 :     template<class SEED_T, class PARAMS_T, class CONTROL_T, double Evaluator(
274 :     const PARAMS_T&), void BuildNewParams(SEED_T&, PARAMS_T&)>
275 :     class ReproducibleSearch {
276 :    
277 :     const SEED_T initialSeed_;
278 :     const PARAMS_T initialParams_;
279 :     const int maxIters_;
280 :    
281 :     SEED_T seed_;
282 :     PARAMS_T p_;
283 :     int iters_;
284 :     double optimumValue_;
285 :     double funcval_;
286 :     double searchTime_;
287 :    
288 :     /// Stores an individual item to evaluate and the data to restore the search state
289 :     /// if it is chosen as new optimum item
290 :     struct ParalEvalItem_t {
291 :     int order_;
292 :     PARAMS_T p_;
293 :     SEED_T seed_; ///< seed AFTER generation of p_
294 :     double value_;
295 :    
296 :     ParalEvalItem_t(int order, const PARAMS_T& p, const SEED_T& seed,
297 :     double value = 0.) :
298 :     order_(order), p_(p), seed_(seed), value_(value) {
299 :     }
300 :    
301 :     ParalEvalItem_t() :
302 :     value_(0.) {
303 :     }
304 :    
305 :     void evaluateItem() {
306 :     value_ = Evaluator(p_);
307 :     }
308 :    
309 :     void print() const {
310 :     std::cout << order_ << ' ' << p_ << " v= " << value_ << std::endl;
311 :     }
312 :    
313 :     };
314 :    
315 :     /// Internal storage for search items with a padding field to avoid false sharing
316 :     struct ParalEvalItemStorage_t {
317 :     ParalEvalItem_t pei_;
318 :     char padding_[128];
319 :     };
320 :    
321 :     public:
322 :    
323 :     ReproducibleSearch(const SEED_T& seed, const PARAMS_T& params, int max_iters = 200) :
324 :     initialSeed_(seed), initialParams_(params), maxIters_(max_iters) {}
325 :    
326 :     // Set the problem in its initial state
327 :     void reset() {
328 :     seed_ = initialSeed_;
329 :     p_ = initialParams_;
330 :     std::cout << ": " << p_ << " v= " << Evaluator(p_) << std::endl;
331 :     }
332 :    
333 :     //########################################################
334 :    
335 :    
336 :     void seq_opt(double funcval) {
337 :     struct timeval t0, t1, t;
338 :     reset();
339 :    
340 :     CONTROL_T control_object;
341 :     gettimeofday(&t0, NULL);
342 :    
343 :     seed_.setX(&p_);
344 :    
345 :     funcval_ = funcval;
346 :     optimumValue_ = funcval_;
347 :     bool quit = false;
348 :     for (iters_ = 1; iters_ <= maxIters_; ++iters_) {
349 :     PARAMS_T p = p_;
350 :    
351 :     BuildNewParams(seed_, p);
352 :    
353 :     const double prev_val = funcval_;
354 :     const double val = Evaluator(p);
355 :    
356 :     if (control_object.mustAccept(prev_val, val, seed_, iters_)) {
357 :     int i;
358 :     for (i = 0; i < seed_.getNvars(); i++){
359 :     p_[i] = p[i];
360 :     }
361 :     funcval_ = val;
362 :     } else if ((*seed_.getConverge()) == -1) return;
363 :    
364 :     control_object.optimum(val, optimumValue_, iters_, p_, initialParams_, seed_);
365 :    
366 :     if ((quit=control_object.mustTerminate(optimumValue_, funcval_, seed_, iters_))) {
367 :     break;
368 :     }
369 :     }
370 :    
371 :     control_object.printResult(quit, seed_, iters_);
372 :     gettimeofday(&t1, NULL);
373 :     timersub(&t1, &t0, &t);
374 :     searchTime_ = t.tv_sec + t.tv_usec / 1000000.0;
375 :     }
376 :    
377 :     void paral_opt_omp(double funcval, int nthreads, int paral_tokens = -1) {
378 :     struct timeval t0, t1, t;
379 :    
380 :     reset();
381 :    
382 :     CONTROL_T control_object;
383 :    
384 :     gettimeofday(&t0, NULL);
385 :    
386 :     #if defined(_OPENMP)
387 :     omp_set_num_threads(nthreads);
388 :     #endif
389 :    
390 :     if (paral_tokens < 0) {
391 :     paral_tokens = nthreads;
392 :     }
393 :    
394 :     seed_.setX(&p_);
395 :     funcval_ = funcval;
396 :     optimumValue_ = funcval_;
397 :     bool quit = false;
398 :    
399 :     ParalEvalItemStorage_t * const storage =
400 :     new ParalEvalItemStorage_t[paral_tokens]; //Guarda los datos de los diferentes threads
401 :    
402 :     int old_paral_tokens = paral_tokens;
403 :     for (iters_ = 1; iters_ <= maxIters_;) {
404 :    
405 :     if ((seed_.getL() + paral_tokens -1) >= seed_.getNvars())
406 :     paral_tokens = seed_.getNvars() - seed_.getL();
407 :    
408 :     if ((iters_ + paral_tokens - 1) > maxIters_) {
409 :     paral_tokens = maxIters_ - (iters_ - 1);
410 :     }
411 :    
412 :     for (int i = 0; i < paral_tokens; i++) {
413 :     storage[i].pei_.p_ = p_;
414 :     BuildNewParams(seed_, storage[i].pei_.p_);
415 :     storage[i].pei_.seed_ = seed_;
416 :     seed_.incrementL();
417 :     }
418 :     const double prev_val = funcval_;
419 :    
420 :     #pragma omp parallel for
421 :     for (int i = 0; i < paral_tokens; i++) {
422 :     storage[i].pei_.evaluateItem();
423 :     }
424 :    
425 :     int j, i=0; //Notice that we need it after the loop
426 :     if (control_object.mustAccept(prev_val, storage[i].pei_.value_, storage[i].pei_.seed_, iters_+i)) {
427 :     for (j = 0; j < seed_.getNvars(); j++)
428 :     p_[j] = storage[i].pei_.p_[j];
429 :     funcval_ = storage[i].pei_.value_;
430 :     } else{
431 :     if ((*storage[i].pei_.seed_.getConverge()) == -1) return;
432 :    
433 :     for (i = 1; i < paral_tokens; i++) {
434 :     storage[i].pei_.seed_.setSeedM(storage[i-1].pei_.seed_.getSeedM_());
435 :     if (control_object.mustAccept(prev_val, storage[i].pei_.value_, storage[i].pei_.seed_, iters_+i)) {
436 :     for (j = 0; j < seed_.getNvars(); j++)
437 :     p_[j] = storage[i].pei_.p_[j];
438 :     funcval_ = storage[i].pei_.value_;
439 :     break;
440 :     } else if ((*storage[i].pei_.seed_.getConverge()) == -1) return;
441 :     }
442 :     }
443 :     i = (i == paral_tokens) ? (i - 1) : i;
444 :    
445 :     seed_ = storage[i].pei_.seed_;
446 :     seed_.setNrej(seed_.getNrej()+i);
447 :     control_object.optimum(storage[i].pei_.value_, optimumValue_, iters_,
448 :     storage[i].pei_.p_, initialParams_, seed_);
449 :    
450 :     iters_ += i+1;
451 :    
452 :     if ((quit=control_object.mustTerminate(optimumValue_, funcval_, seed_, iters_))) {
453 :     break;
454 :     }
455 :     paral_tokens = old_paral_tokens;
456 :     }
457 :     control_object.printResult(quit, seed_ ,iters_);
458 :    
459 :     gettimeofday(&t1, NULL);
460 :     timersub(&t1, &t0, &t);
461 :     searchTime_ = t.tv_sec + t.tv_usec / 1000000.0;
462 :    
463 :     delete[] storage;
464 :     }
465 :    
466 :     /// Only used by sequential search when verbose
467 :     void print(int iter, double val) const {
468 :     ParalEvalItem_t v(iter, p_, seed_, val);
469 :     v.print();
470 :     }
471 :    
472 :     const PARAMS_T& getResult() const {
473 :     return p_;
474 :     }
475 :    
476 :     double getOptimumValue() const {
477 :     return funcval_;
478 :     }
479 :    
480 :     double getSearchTime() const {
481 :     return searchTime_;
482 :     }
483 :    
484 :     int iterations() const {
485 :     return iters_;
486 :     }
487 :    
488 :     };
489 :    
490 :     #endif

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge