1 /*--------------------------------------------------*/
2 /* NOMAD_PLOT -- single and bi-objective versions */
3 /*--------------------------------------------------*/
4 #include "nomad.hpp"
5 using namespace std;
6 using namespace NOMAD;
7
8 class Interface_Graphique
9 {
10 public:
11
Interface_Graphique()12 Interface_Graphique(){};
~Interface_Graphique()13 ~Interface_Graphique(){};
14
create_multi()15 void create_multi()
16 {
17 ofstream deb("ini.txt", ios::out | ios::trunc);
18 {
19 deb << "0" <<"\n" ;
20 deb.close();
21 }
22 rename("ini.txt", "init.txt");
23 }
24
create_single()25 void create_single()
26 {
27 ofstream deb("ini.txt", ios::out | ios::trunc);
28 {
29 deb << "1" <<"\n";
30 deb.close();
31 }
32 rename("ini.txt", "init.txt");
33 }
34
new_single(int bbe,double f)35 void new_single(int bbe, double f)
36 {
37 FILE* fp = NULL;
38 fp = fopen( "out.txt", "rb" );
39 while(!fp)
40 {
41 fp = fopen( "out.txt", "rb" );
42 }
43 fclose(fp);
44
45 ofstream deb("out.txt", ios::out | ios::trunc);
46 {
47 deb << bbe << "\n"<< f;
48 deb.close();
49 }
50
51 rename("out.txt", "in.txt");
52 }
53
new_multi(int nb_cache_pts,double * cache_f1,double * cache_f2,int nb_pareto_pts,double * pareto_f1,double * pareto_f2) const54 void new_multi(int nb_cache_pts, double* cache_f1, double* cache_f2,
55 int nb_pareto_pts, double* pareto_f1, double* pareto_f2) const
56 {
57 FILE* fp = NULL;
58 fp = fopen( "out.txt", "rb" );
59 while(!fp)
60 {
61 fp = fopen( "out.txt", "rb" );
62 }
63 fclose(fp);
64
65 ofstream deb("out.txt", ios::out | ios::trunc);
66 {
67 deb << nb_cache_pts << "\n";
68 for (int i = 0; i < nb_cache_pts; ++i)
69 {
70 deb << cache_f1[i] << "\n"<< cache_f2[i] << "\n";
71 }
72 deb << nb_pareto_pts << "\n";
73 for (int i = 0; i < nb_pareto_pts; ++i)
74 {
75 deb << pareto_f1[i] << "\n"<< pareto_f2[i] << "\n";
76 }
77 deb.close();
78 }
79
80 rename("out.txt", "in.txt");
81 }
82 };
83
84
85 /*----------------------------------------------------*/
86 /* custom evaluator used to define update_success() */
87 /* (bi-objective) */
88 /*----------------------------------------------------*/
89 class My_Multi_Obj_Evaluator : public Multi_Obj_Evaluator {
90
91 private:
92
93 Double _h_min;
94 set<const Eval_Point *> _cache_pts;
95
96
97 public:
98
99 Interface_Graphique inter;
100 // constructor:
101 My_Multi_Obj_Evaluator ( const Parameters & p );
102
103 // destructor:
~My_Multi_Obj_Evaluator(void)104 virtual ~My_Multi_Obj_Evaluator ( void ) {}
105
106 // plot the points:
107 void plot_points ( int nb_cache_pts ,
108 double * cache_f1 ,
109 double * cache_f2 ,
110 int nb_pareto_pts ,
111 double * pareto_f1 ,
112 double * pareto_f2 ) const;
113
114 // function that is invoked after each MADS run:
115 virtual void update_mads_run ( const Stats & stats ,
116 const Evaluator_Control & ev_control ,
117 const Barrier & true_barrier ,
118 const Barrier & sgte_barrier ,
119 const Pareto_Front & pareto_front );
120 };
121
122 /*-----------------------*/
123 /* constructor (bi-obj) */
124 /*-----------------------*/
My_Multi_Obj_Evaluator(const Parameters & p)125 My_Multi_Obj_Evaluator::My_Multi_Obj_Evaluator ( const Parameters & p ) :
126 Multi_Obj_Evaluator ( p ) ,
127 _h_min ( p.get_h_min() ) {
128 inter.create_multi();
129 }
130
131 /*------------------------------------------------*/
132 /* function that is invoked after each MADS run */
133 /* (bi-obj) */
134 /*------------------------------------------------*/
update_mads_run(const Stats & stats,const Evaluator_Control & ev_control,const Barrier & true_barrier,const Barrier & sgte_barrier,const Pareto_Front & pareto_front)135 void My_Multi_Obj_Evaluator::update_mads_run ( const Stats & stats ,
136 const Evaluator_Control & ev_control ,
137 const Barrier & true_barrier ,
138 const Barrier & sgte_barrier ,
139 const Pareto_Front & pareto_front ) {
140
141 // objective indexes:
142 int i1 = Multi_Obj_Evaluator::get_i1();
143 int i2 = Multi_Obj_Evaluator::get_i2();
144
145 // browse all the cache points:
146 const Cache & cache = ev_control.get_cache();
147 const Eval_Point * cur = cache.begin();
148 size_t size_before;
149 vector<const Eval_Point *> cache_pts;
150 vector<const Eval_Point *> pareto_pts;
151
152 while (cur) {
153 if ( cur->is_feasible(_h_min) && cur->is_eval_ok() ) {
154 size_before = _cache_pts.size();
155 _cache_pts.insert ( cur );
156 if ( _cache_pts.size() > size_before )
157 cache_pts.push_back ( cur );
158 }
159 cur = cache.next();
160 }
161
162 int nb_cache_pts = cache_pts.size();
163 double * cache_f1 = new double [nb_cache_pts];
164 double * cache_f2 = new double [nb_cache_pts];
165
166 int k;
167
168 for ( k = 0 ; k < nb_cache_pts ; ++k ) {
169 cache_f1[k] = cache_pts[k]->get_bb_outputs()[i1].value();
170 cache_f2[k] = cache_pts[k]->get_bb_outputs()[i2].value();
171 }
172
173 // browse the pareto points:
174 int nb_pareto_pts = pareto_front.size();
175 double * pareto_f1 = new double [nb_pareto_pts];
176 double * pareto_f2 = new double [nb_pareto_pts];
177
178 k = 0;
179 cur = pareto_front.begin();
180 while ( cur ) {
181
182 pareto_f1[k] = cur->get_bb_outputs()[i1].value();
183 pareto_f2[k] = cur->get_bb_outputs()[i2].value();
184
185 cur = pareto_front.next();
186 ++k;
187 }
188
189 // call the plot function:
190 plot_points ( nb_cache_pts , cache_f1 , cache_f2 ,
191 nb_pareto_pts , pareto_f1 , pareto_f2 );
192
193 delete [] cache_f1;
194 delete [] cache_f2;
195 delete [] pareto_f1;
196 delete [] pareto_f2;
197 }
198
199 /*----------------------------*/
200 /* plot points (multi-obj) */
201 /*----------------------------*/
plot_points(int nb_cache_pts,double * cache_f1,double * cache_f2,int nb_pareto_pts,double * pareto_f1,double * pareto_f2) const202 void My_Multi_Obj_Evaluator::plot_points ( int nb_cache_pts ,
203 double * cache_f1 ,
204 double * cache_f2 ,
205 int nb_pareto_pts ,
206 double * pareto_f1 ,
207 double * pareto_f2 ) const{
208 inter.new_multi(nb_cache_pts, cache_f1, cache_f2, nb_pareto_pts, pareto_f1, pareto_f2);
209 }
210
211 /*----------------------------------------------------*/
212 /* custom evaluator used to define update_success() */
213 /* (single-objective) */
214 /*----------------------------------------------------*/
215 class My_Evaluator : public Evaluator {
216
217 private:
218 Interface_Graphique inter;
219
220 public:
221
222 // constructor:
223 My_Evaluator ( const Parameters & p );
224
225 // destructor:
~My_Evaluator(void)226 virtual ~My_Evaluator ( void ) {}
227
228 // plot a new success:
229 void plot_success ( int bbe , const Double & f );
230
231 // function that is invoked at each success:
232 virtual void update_success ( const Stats & stats , const Eval_Point & x );
233
234 };
235
236 /*---------------------------*/
237 /* constructor (single-obj) */
238 /*---------------------------*/
My_Evaluator(const Parameters & p)239 My_Evaluator::My_Evaluator ( const Parameters & p ) : Evaluator ( p ) {
240 inter.create_single();
241 }
242
243 /*--------------------------------------------*/
244 /* function that is invoked at each success */
245 /* (single-obj) */
246 /*--------------------------------------------*/
update_success(const Stats & stats,const Eval_Point & x)247 void My_Evaluator::update_success ( const Stats & stats , const Eval_Point & x ) {
248 if ( x.is_feasible ( _p.get_h_min() ) )
249 plot_success ( stats.get_bb_eval()+1 , x.get_f() );
250 }
251
252 /*-----------------------------------*/
253 /* plot a new success (single-obj) */
254 /*-----------------------------------*/
plot_success(int bbe,const Double & f)255 void My_Evaluator::plot_success ( int bbe , const Double & f ) {
256 inter.new_single ( bbe, f.value() );
257 }
258
259 /*-------------------------*/
260 /* tells the GUI to wait */
261 /*-------------------------*/
GUI_wait(void)262 void GUI_wait ( void ) {
263 ofstream fin("stop.txt", ios::out | ios::trunc);
264 {
265 fin.close();
266 }
267 }
268
269 /*------------------------------------------*/
270 /* NOMAD main function */
271 /*------------------------------------------*/
main(int argc,char ** argv)272 int main ( int argc , char ** argv ) {
273
274 // display:
275 Display out ( std::cout );
276 out.precision ( DISPLAY_PRECISION_STD );
277
278 // NOMAD initializations:
279 begin ( argc , argv );
280
281 try {
282
283 // usage:
284 if ( argc < 2 ) {
285 display_usage ( cerr );
286 return EXIT_SUCCESS;
287 }
288
289 // parameters file:
290 string param_file_name = argv[1];
291 string opt = param_file_name;
292 NOMAD::toupper(opt);
293
294 // display version if option '-v' has been specified:
295 if ( opt == "-V" ) {
296 display_version ( out );
297 return EXIT_SUCCESS;
298 }
299
300 // display info if option '-i' has been specified:
301 if ( opt == "-I" || opt == "-INFO" ) {
302 display_info ( out );
303 display_usage ( out );
304 return EXIT_SUCCESS;
305 }
306
307 // parameters creation:
308 Parameters p ( out );
309
310 // display help on parameters if option '-h' has been specified:
311 if ( opt == "-H" || opt == "-HELP" ) {
312 p.help( (argc>2) ? argv[2] : "all" );
313 return EXIT_SUCCESS;
314 }
315
316 // read parameters file:
317 p.read ( param_file_name );
318
319 // parameters check:
320 p.check();
321
322 // display NOMAD info:
323 if ( p.get_display_degree() > 1 )
324 display_info ( out );
325
326 // parameters display:
327 if ( p.get_display_degree() > 2 )
328 out << endl << "parameters:" << endl << p << endl;
329
330 // single-objective:
331 if ( p.get_nb_obj() == 1 ) {
332
333 // custom evaluator:
334 My_Evaluator ev ( p );
335
336 // algorithm creation and execution:
337 Mads mads ( p , &ev );
338 mads.run();
339
340 // plot the last point:
341 const Eval_Point * bf = mads.get_best_feasible();
342 if ( bf )
343 ev.plot_success ( mads.get_stats().get_bb_eval() , bf->get_f() );
344 }
345
346 // bi-objective:
347 else {
348
349 // custom evaluator:
350 My_Multi_Obj_Evaluator ev ( p );
351
352 // algorithm creation and execution:
353 Mads mads ( p , &ev );
354 mads.multi_run();
355 }
356
357
358 }
359 catch ( exception & e ) {
360 cerr << "\nNOMAD has been interrupted (" << e.what() << ")\n\n";
361 }
362
363 GUI_wait();
364
365
366 Slave::stop_slaves ( out );
367 end();
368
369 return EXIT_SUCCESS;
370 }
371
372 /*------------------------------------------*/
373 /* display NOMAD version */
374 /*------------------------------------------*/
display_version(const NOMAD::Display & out)375 void NOMAD::display_version ( const NOMAD::Display & out )
376 {
377 #ifdef USE_MPI
378 if ( !NOMAD::Slave::is_master() )
379 return;
380 #endif
381 out << std::endl << "NOMAD - version " << NOMAD::VERSION << " - www.gerad.ca/nomad"
382 << std::endl << std::endl;
383 }
384
385 /*------------------------------------------*/
386 /* display NOMAD information */
387 /*------------------------------------------*/
display_info(const NOMAD::Display & out)388 void NOMAD::display_info ( const NOMAD::Display & out )
389 {
390 #ifdef USE_MPI
391 if ( !NOMAD::Slave::is_master() )
392 return;
393 #endif
394 NOMAD::display_version ( out );
395 out << NOMAD::open_block ( "Copyright (C) 2001-2010" )
396 << "Mark A. Abramson - The Boeing Company" << std::endl
397 << "Charles Audet - Ecole Polytechnique de Montreal" << std::endl
398 << "Gilles Couture - Ecole Polytechnique de Montreal" << std::endl
399 << "John E. Dennis, Jr. - Rice University" << std::endl
400 << "Sebastien Le Digabel - Ecole Polytechnique de Montreal" << std::endl
401 << NOMAD::close_block()
402 << std::endl
403 << "Funded in part by AFOSR and Exxon Mobil." << std::endl
404 << std::endl
405 << "License : \'" << NOMAD::LGPL_FILE << "\'" << std::endl
406 << "User guide: \'" << NOMAD::USER_GUIDE_FILE << "\'" << std::endl
407 << "Examples : \'" << NOMAD::EXAMPLES_DIR << "\'" << std::endl
408 << "Tools : \'" << NOMAD::TOOLS_DIR << "\'" << std::endl
409 << std::endl
410 << "Please report bugs to nomad@gerad.ca"
411 << std::endl;
412 }
413
414 /*------------------------------------------*/
415 /* display NOMAD usage */
416 /*------------------------------------------*/
display_usage(const NOMAD::Display & out)417 void NOMAD::display_usage ( const NOMAD::Display & out )
418 {
419 #ifdef USE_MPI
420 if ( !NOMAD::Slave::is_master() )
421 return;
422 out << std::endl
423 << "Run NOMAD.MPI: mpirun -np p nomad.MPI parameters_file" << std::endl
424 << "Info : nomad -i" << std::endl
425 << "Help : nomad -h keyword (or 'all')" << std::endl
426 << "Version : nomad -v" << std::endl
427 << std::endl;
428 #else
429 out << std::endl
430 << "Run NOMAD: nomad parameters_file" << std::endl
431 << "Info : nomad -i" << std::endl
432 << "Help : nomad -h keyword(s) (or 'all')" << std::endl
433 << "Version : nomad -v" << std::endl
434 << std::endl;
435 #endif
436 }
437