1 /*-------------------------------------------------------------------------------------*/
2 /*  NOMAD - Nonlinear Optimization by Mesh Adaptive Direct search - version 3.7.2      */
3 /*                                                                                     */
4 /*  Copyright (C) 2001-2015  Mark Abramson        - the Boeing Company, Seattle        */
5 /*                           Charles Audet        - Ecole Polytechnique, Montreal      */
6 /*                           Gilles Couture       - Ecole Polytechnique, Montreal      */
7 /*                           John Dennis          - Rice University, Houston           */
8 /*                           Sebastien Le Digabel - Ecole Polytechnique, Montreal      */
9 /*                           Christophe Tribes    - Ecole Polytechnique, Montreal      */
10 /*                                                                                     */
11 /*  funded in part by AFOSR and Exxon Mobil                                            */
12 /*                                                                                     */
13 /*  Author: Sebastien Le Digabel                                                       */
14 /*                                                                                     */
15 /*  Contact information:                                                               */
16 /*    Ecole Polytechnique de Montreal - GERAD                                          */
17 /*    C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada                  */
18 /*    e-mail: nomad@gerad.ca                                                           */
19 /*    phone : 1-514-340-6053 #6928                                                     */
20 /*    fax   : 1-514-340-5665                                                           */
21 /*                                                                                     */
22 /*  This program is free software: you can redistribute it and/or modify it under the  */
23 /*  terms of the GNU Lesser General Public License as published by the Free Software   */
24 /*  Foundation, either version 3 of the License, or (at your option) any later         */
25 /*  version.                                                                           */
26 /*                                                                                     */
27 /*  This program is distributed in the hope that it will be useful, but WITHOUT ANY    */
28 /*  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A    */
29 /*  PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.   */
30 /*                                                                                     */
31 /*  You should have received a copy of the GNU Lesser General Public License along     */
32 /*  with this program. If not, see <http://www.gnu.org/licenses/>.                     */
33 /*                                                                                     */
34 /*  You can find information on the NOMAD software at www.gerad.ca/nomad               */
35 /*-------------------------------------------------------------------------------------*/
36 /**
37   \file   Stats.hpp
38   \brief  Algorithm stats (headers)
39   \author Sebastien Le Digabel
40   \date   2010-04-22
41   \see    Stats.cpp
42 */
43 #ifndef __STATS__
44 #define __STATS__
45 
46 #include "Clock.hpp"
47 #include "Double.hpp"
48 #include "Model_Stats.hpp"
49 
50 namespace NOMAD {
51 
52   /// Algorithm stats.
53   class Stats {
54 
55   private:
56 
57     /// Number of evaluations.
58     /**
59        Blackbox evaluations + cache hits.
60     */
61     int _eval;
62 
63     /// Number of simulated blackbox evaluations.
64     /**
65        Blackbox evaluations + initial cache hits.
66     */
67     int _sim_bb_eval;
68 
69     int           _sgte_eval;        ///< Number of surrogate evaluations.
70     int           _sgte_cost;        ///< Surrogate cost.
71     int           _bb_eval;          ///< Number of blackbox evaluations.
72 		int           _block_eval;    ///< Number of block of evaluations.
73     int           _failed_eval;      ///< Number of failed evaluations.
74     int           _cache_hits;       ///< Number of cache hits.
75     int           _interrupted_eval; ///< Number of interrupted sequence of evaluations.
76     int           _iterations;       ///< Number of iterations.
77     NOMAD::Double _stat_sum;         ///< Sum stat (output of type NOMAD::STAT_SUM).
78     NOMAD::Double _stat_avg;         ///< Average stat (output of type NOMAD::STAT_AVG).
79     int           _cnt_avg;          ///< Number of values for the \c avg stat.
80     int           _p1_iterations;    ///< Iterations in MADS phase one.
81     int           _p1_bbe;           ///< Blackbox evaluations in phase one.
82     NOMAD::Clock  _clock;            ///< Wall-clock time.
83     int           _mads_runs;        ///< Number of MADS runs (for multi-objective runs).
84 
85     // Polls:
86     int           _nb_poll_searches; ///< Number of poll searches.
87     int           _poll_pts;         ///< Number of poll points.
88     int           _poll_success;     ///< Number of poll successes.
89 
90     // Extended polls:
91     int           _nb_ext_polls;      ///< Number of extended polls.
92     int           _ext_poll_pts;      ///< Number of extended poll points.
93     int           _ext_poll_succ;     ///< Number of extended poll successes.
94     int           _ext_poll_bb_eval;  ///< Number of extended poll blackbox evaluations.
95     int           _ext_poll_descents; ///< Number of extended poll descents.
96 
97     // Speculative searches:
98     int           _nb_spec_searches;  ///< Number of speculative searches.
99     int           _spec_pts;          ///< Number of speculative search points.
100     int           _spec_success;      ///< Number of speculative search successes.
101 
102 #ifdef USE_MPI
103     int           _asynchronous_success; ///< Number of asynchronous successes.
104     int           _MPI_data_size;        ///< Size of MPI messages.
105 #endif
106 
107     // LH searches:
108     int           _nb_LH_searches;    ///< Number of Latin-Hypercube (LH) searches.
109     int           _LH_pts;            ///< Number of Latin-Hypercube (LH) search points.
110     int           _LH_success;        ///< Number of LH search successes.
111 
112     // Cache searches:
113     int           _nb_cache_searches; ///< Number of cache searches (CS).
114     int           _CS_pts;            ///< Number of CS search points.
115     int           _CS_success;        ///< Number of CS search successes.
116 
117     /// Model stats.
118     NOMAD::Model_Stats _model_stats;
119 
120     // VNS searches:
121     int           _nb_VNS_searches;   ///< Number of VNS searches.
122     int           _VNS_pts;           ///< Number of VNS search points.
123     int           _VNS_success;       ///< Number of VNS search successes.
124     int           _VNS_bb_eval;       ///< Number of VNS blackbox evaluations.
125     int           _VNS_sgte_eval;     ///< Number of VNS surrogate evaluations.
126 
127     // User searches:
128     int           _nb_usr_searches;   ///< Number of user searches.
129     int           _usr_srch_pts;      ///< Number of user search points.
130     int           _usr_srch_success;  ///< Number of user search successes.
131 
132 	  // Dynamic management of poll directions
133 	int				_nb_success_dyn_dir;  ///< Number of successfull polling in the direction added dynamically
134 
135 
136 public:
137 
138     /// Constructor.
139     /**
140        \param sgte_cost Surrogate cost -- \b IN -- \b optional (default = \c -1).
141     */
Stats(int sgte_cost=-1)142     explicit Stats ( int sgte_cost = -1 ) : _sgte_cost(sgte_cost) { reset(); }
143 
144     /// Copy constructor.
145     /**
146        \param s The copied object -- \b IN.
147     */
Stats(const Stats & s)148     explicit Stats ( const Stats & s )
149        : _eval                 ( s._eval                 ) ,
150 	 _sim_bb_eval          ( s._sim_bb_eval          ) ,
151 	 _sgte_eval            ( s._sgte_eval            ) ,
152 	 _sgte_cost            ( s._sgte_cost            ) ,
153 	 _bb_eval              ( s._bb_eval              ) ,
154 		_block_eval        ( s._block_eval        ) ,
155 	 _failed_eval          ( s._failed_eval          ) ,
156 	 _cache_hits           ( s._cache_hits           ) ,
157 	 _interrupted_eval     ( s._interrupted_eval     ) ,
158 	 _iterations           ( s._iterations           ) ,
159 	 _stat_sum             ( s._stat_sum             ) ,
160 	 _stat_avg             ( s._stat_avg             ) ,
161 	 _cnt_avg              ( s._cnt_avg              ) ,
162 	 _p1_iterations        ( s._p1_iterations        ) ,
163 	 _p1_bbe               ( s._p1_bbe               ) ,
164 	 _clock                ( s._clock                ) ,
165 	 _mads_runs            ( s._mads_runs            ) ,
166 	 _nb_poll_searches     ( s._nb_poll_searches     ) ,
167 	 _poll_pts             ( s._poll_pts             ) ,
168 	 _poll_success         ( s._poll_success         ) ,
169 	 _nb_ext_polls         ( s._nb_ext_polls         ) ,
170 	 _ext_poll_pts         ( s._ext_poll_pts         ) ,
171 	 _ext_poll_succ        ( s._ext_poll_succ        ) ,
172 	 _ext_poll_bb_eval     ( s._ext_poll_bb_eval     ) ,
173 	 _ext_poll_descents    ( s._ext_poll_descents    ) ,
174 	 _nb_spec_searches     ( s._nb_spec_searches     ) ,
175 	 _spec_pts             ( s._spec_pts             ) ,
176 	 _spec_success         ( s._spec_success         ) ,
177 #ifdef USE_MPI
178 	 _asynchronous_success ( s._asynchronous_success ) ,
179 	 _MPI_data_size        ( s._MPI_data_size        ) ,
180 #endif
181 	 _nb_LH_searches       ( s._nb_LH_searches       ) ,
182 	 _LH_pts               ( s._LH_pts               ) ,
183 	 _LH_success           ( s._LH_success           ) ,
184 	 _nb_cache_searches    ( s._nb_cache_searches    ) ,
185 	 _CS_pts               ( s._CS_pts               ) ,
186 	 _CS_success           ( s._CS_success           ) ,
187 	 _nb_VNS_searches      ( s._nb_VNS_searches      ) ,
188 	 _VNS_pts              ( s._VNS_pts              ) ,
189 	 _VNS_success          ( s._VNS_success          ) ,
190 	 _VNS_bb_eval          ( s._VNS_bb_eval          ) ,
191 	 _VNS_sgte_eval        ( s._VNS_sgte_eval        ) ,
192 	 _nb_usr_searches      ( s._nb_usr_searches      ) ,
193 	 _usr_srch_pts         ( s._usr_srch_pts         ) ,
194 	 _usr_srch_success     ( s._usr_srch_success     ) ,
195 	 _nb_success_dyn_dir   ( s._nb_success_dyn_dir   ) {}
196 
197     /// Affectation operator.
198     /**
199        \param s The right-hand side object -- \b IN.
200     */
201     Stats & operator = ( const Stats & s );
202 
203     /// Destructor.
~Stats(void)204     virtual ~Stats ( void ) {}
205 
206     /// Reset the stats.
207     void reset ( void );
208 
209     /// Update stats from another NOMAD::Stats object.
210     /**
211        \param s The other NOMAD::Stats object -- \b IN.
212        \param for_search A flag equal to \c true if the method
213                          has been invoked within a search step
214 			 -- \b IN.
215     */
216     void update ( const Stats & s , bool for_search );
217 
218     /// Update the \c sum stat.
219     /**
220        \param d New \c sum element -- \b IN.
221     */
222     void update_stat_sum ( const NOMAD::Double & d );
223 
224     /// Update the \c avg stat.
225     /**
226        \param d New \c avg element -- \b IN.
227     */
228     void update_stat_avg ( const NOMAD::Double & d );
229 
230     /// Add \c 1 to stat \c _mads_run.
add_mads_run(void)231     void add_mads_run ( void ) { ++_mads_runs; }
232 
233     /// Set the number of MADS runs.
set_mads_runs(int mads_runs)234     void set_mads_runs ( int mads_runs ) { _mads_runs = mads_runs; }
235 
236     /// Add \c 1 to stat \c _eval.
add_eval(void)237     void add_eval ( void ) { ++_eval; }
238 
239     /// Add \c 1 to stat \c _sim_bb_eval.
add_sim_bb_eval(void)240     void add_sim_bb_eval ( void ) { ++_sim_bb_eval; }
241 
242     /// Add \c 1 to stat \c _sgte_eval.
add_sgte_eval(void)243     void add_sgte_eval ( void ) { ++_sgte_eval; }
244 
245 	  /// Add \c count_eval to stat \c _sgte_eval.
add_sgte_eval(int count_eval)246 	  void add_sgte_eval ( int count_eval ) { _sgte_eval+=count_eval; }
247 
248     /// Add \c 1 to stat \c _bb_eval.
add_bb_eval(void)249     void add_bb_eval ( void ) { ++_bb_eval; }
250 
251 		/// Add \c 1 to stat \c _block_eval.
add_one_block_eval(void)252 		void add_one_block_eval ( void ) { ++_block_eval; }
253 
254 	  /// Add \c count_eval to stat \c _bb_eval.
add_bb_eval(int count_eval)255 	  void add_bb_eval ( int count_eval ) { _bb_eval+=count_eval; }
256 
257     /// Add \c 1 to stat \c _failed_eval.
add_failed_eval(void)258     void add_failed_eval ( void ) { ++_failed_eval; }
259 
260     /// Add \c 1 to stat \c _cache_hits.
add_cache_hit(void)261     void add_cache_hit ( void ) { ++_cache_hits; }
262 
263     /// Add \c 1 to stat \c _interrupted_eval.
add_interrupted_eval(void)264     void add_interrupted_eval ( void ) { ++_interrupted_eval; }
265 
266     /// Add \c 1 to stat \c _iterations.
add_iteration(void)267     void add_iteration ( void ) { ++_iterations; }
268 
269     /// Add \c 1 to stat \c _nb_poll_searches.
add_nb_poll_searches(void)270     void add_nb_poll_searches ( void ) { ++_nb_poll_searches; }
271 
272     /// Add \c 1 to stat \c _poll_success.
add_poll_success(void)273     void add_poll_success ( void ) { ++_poll_success; }
274 
275     /// Add \c 1 to stat \c _nb_ext_polls.
add_nb_ext_polls(void)276     void add_nb_ext_polls ( void ) { ++_nb_ext_polls; }
277 
278     /// Add \c 1 to stat \c _ext_poll_succ.
add_ext_poll_succ(void)279     void add_ext_poll_succ ( void ) { ++_ext_poll_succ; }
280 
281     /// Add \c 1 to stat \c _ext_poll_descents.
add_ext_poll_descent(void)282     void add_ext_poll_descent ( void ) { ++_ext_poll_descents; }
283 
284     /// Add \c 1 to stat \c _nb_spec_searches.
add_nb_spec_searches(void)285     void add_nb_spec_searches ( void ) { ++_nb_spec_searches; }
286 
287     /// Add \c 1 to stat \c _spec_success.
add_spec_success(void)288     void add_spec_success ( void ) { ++_spec_success; }
289 
290     /// Add \c 1 to stat \c _LH_success.
add_LH_success(void)291     void add_LH_success ( void ) { ++_LH_success; }
292 
293     /// Add \c 1 to stat \c _nb_cache_searches.
add_nb_cache_searches(void)294     void add_nb_cache_searches ( void ) { ++_nb_cache_searches; }
295 
296     /// Add \c 1 to stat \c _nb_LH_searches.
add_nb_LH_searches(void)297     void add_nb_LH_searches ( void ) { ++_nb_LH_searches; }
298 
299     /// Add \c 1 to stat \c _CS_success.
add_CS_success(void)300     void add_CS_success ( void ) { ++_CS_success; }
301 
302     /// Add \c 1 to stat \c _nb_VNS_searches.
add_nb_VNS_searches(void)303     void add_nb_VNS_searches ( void ) { ++_nb_VNS_searches; }
304 
305     /// Add \c 1 to stat \c _VNS_success.
add_VNS_success(void)306     void add_VNS_success ( void ) { ++_VNS_success; }
307 
308     /// Add \c 1 to stat \c _nb_usr_searches.
add_nb_usr_searches(void)309     void add_nb_usr_searches ( void ) { ++_nb_usr_searches; }
310 
311     /// Add \c 1 to stat \c _usr_srch_success.
add_usr_srch_success(void)312     void add_usr_srch_success ( void ) { ++_usr_srch_success; }
313 
314 
315 	/// Add \c 1 to stat \c _nb_success_dyn_dir.
add_nb_success_dyn_dir(void)316 	  void add_nb_success_dyn_dir(void) {++_nb_success_dyn_dir;}
317 
318     /// Add an integer to stat \c _p1_iterations.
319     /**
320        \param i The integer -- \b IN.
321     */
add_p1_iterations(int i)322     void add_p1_iterations ( int i ) { _p1_iterations += i; }
323 
324     /// Add an integer to stat \c _p1_bbe.
325     /**
326        \param i The integer -- \b IN.
327     */
add_p1_bbe(int i)328     void add_p1_bbe ( int i ) { _p1_bbe += i; }
329 
330     /// Add an integer to stat \c _poll_pts.
331     /**
332        \param i The integer -- \b IN.
333     */
add_poll_pts(int i)334     void add_poll_pts ( int i ) { _poll_pts += i; }
335 
336     /// Add an integer to stat \c _ext_poll_pts.
337     /**
338        \param i The integer -- \b IN.
339     */
add_ext_poll_pts(int i)340     void add_ext_poll_pts ( int i ) { _ext_poll_pts += i; }
341 
342     /// Add an integer to stat \c _ext_poll_bb_eval.
343     /**
344        \param i The integer -- \b IN.
345     */
add_ext_poll_bb_eval(int i)346     void add_ext_poll_bb_eval ( int i ) { _ext_poll_bb_eval += i;}
347 
348     /// Add an integer to stat \c _spec_pts.
349     /**
350        \param i The integer -- \b IN.
351     */
add_spec_pts(int i)352     void add_spec_pts ( int i ) { _spec_pts += i; }
353 
354     /// Add an integer to stat \c _LH_pts.
355     /**
356        \param i The integer -- \b IN.
357     */
add_LH_pts(int i)358     void add_LH_pts ( int i ) { _LH_pts += i; }
359 
360     /// Add an integer to stat \c _CS_pts.
361     /**
362        \param i The integer -- \b IN.
363     */
add_CS_pts(int i)364     void add_CS_pts ( int i ) { _CS_pts += i; }
365 
366     /// Update model stats.
update_model_stats(const NOMAD::Model_Stats & ms)367     void update_model_stats ( const NOMAD::Model_Stats & ms )
368     {
369       _model_stats.update ( ms );
370     }
371 
372     /// Add an integer to stat \c _VNS_pts.
373     /**
374        \param i The integer -- \b IN.
375     */
add_VNS_pts(int i)376     void add_VNS_pts ( int i ) { _VNS_pts += i; }
377 
378     /// Add an integer to stat \c _VNS_bb_eval.
379     /**
380        \param i The integer -- \b IN.
381     */
add_VNS_bb_eval(int i)382     void add_VNS_bb_eval ( int i ) { _VNS_bb_eval += i; }
383 
384     /// Add an integer to stat \c _VNS_sgte_eval.
385     /**
386        \param i The integer -- \b IN.
387     */
add_VNS_sgte_eval(int i)388     void add_VNS_sgte_eval ( int i ) { _VNS_sgte_eval += i; }
389 
390     /// Add an integer to stat \c _usr_srch_pts.
391     /**
392        \param i The integer -- \b IN.
393     */
add_usr_srch_pts(int i)394     void add_usr_srch_pts ( int i ) { _usr_srch_pts += i; }
395 
396 #ifdef USE_MPI
397 
398     /// Add \c 1 to stat \c _asynchronous_success.
add_asynchronous_success(void)399     void add_asynchronous_success ( void ) { ++_asynchronous_success; }
400 
401     /// Add an integer to stat \c _MPI_data_size.
402     /**
403        \param i The integer -- \b IN.
404     */
set_MPI_data_size(int i)405     void set_MPI_data_size ( int i ) { _MPI_data_size = i; }
406 #endif
407 
408     /// Access to the stat \c _eval.
409     /**
410        \return The stat \c _eval.
411     */
get_eval(void) const412     int get_eval ( void ) const { return _eval; }
413 
414     /// Access to the stat \c _sim_bb_eval.
415     /**
416        \return The stat \c _sim_bb_eval.
417     */
get_sim_bb_eval(void) const418     int get_sim_bb_eval ( void ) const { return _sim_bb_eval; }
419 
420     /// Access to the stat \c _sgte_eval.
421     /**
422        \return The stat \c _sgte_eval.
423     */
get_sgte_eval(void) const424     int get_sgte_eval ( void ) const { return _sgte_eval; }
425 
426     /// Access to the real time stat.
427     /**
428        \return The real time stat.
429     */
get_real_time(void) const430     int get_real_time ( void ) const { return _clock.get_real_time(); }
431 
432     /// Access to the stat \c _iterations.
433     /**
434        \return The stat \c _iterations.
435     */
get_iterations(void) const436     int get_iterations ( void ) const { return _iterations; }
437 
438     /// Access to the stat \c _failed_eval.
439     /**
440        \return The stat \c _failed_eval.
441     */
get_failed_eval(void) const442     int get_failed_eval ( void ) const { return _failed_eval; }
443 
444     /// Access to the stat \c _mads_runs.
445     /**
446        \return The stat \c _mads_runs.
447     */
get_mads_runs(void) const448     int get_mads_runs ( void ) const { return _mads_runs; }
449 
450     /// Access to the stat \c _LH_pts.
451     /**
452        \return The stat \c _LH_pts.
453     */
get_LH_pts(void) const454     int get_LH_pts ( void ) const { return _LH_pts; }
455 
456     /// Access to the stat \c _CS_pts.
457     /**
458        \return The stat \c _CS_pts.
459     */
get_CS_pts(void) const460     int get_CS_pts ( void ) const { return _CS_pts; }
461 
462     /// Access to the stat \c _VNS_bb_eval.
463     /**
464        \return The stat \c _VNS_bb_eval.
465     */
get_VNS_bb_eval(void) const466     int get_VNS_bb_eval ( void ) const { return _VNS_bb_eval; }
467 
468     /// Access to the stat \c _VNS_sgte_eval.
469     /**
470        \return The stat \c _VNS_sgte_eval.
471     */
get_VNS_sgte_eval(void) const472     int get_VNS_sgte_eval ( void ) const { return _VNS_sgte_eval; }
473 
474     /// Access to the number of cache hits.
475     /**
476        \return The number of cache hits.
477     */
get_cache_hits(void) const478     int get_cache_hits ( void ) const { return _cache_hits; }
479 
480     /// Access to the number of blackbox evaluations (includes surrogate cost).
481     /**
482        \return The number of blackbox evaluations.
483     */
get_bb_eval(void) const484     int get_bb_eval ( void ) const
485     {
486       return ( _sgte_cost > 0 ) ? _bb_eval + _sgte_eval / _sgte_cost : _bb_eval;
487     }
488 
489 
490 		/// Access to the number of block of evaluations (includes bb and surrogates).
491 		/**
492 		 \return The number of blackbox evaluations.
493 		 */
get_block_eval(void) const494 		int get_block_eval ( void ) const
495 		{
496 			return _block_eval;
497 		}
498 
499     /// Access to the \c sum stat.
500     /**
501        \return The \c sum stat.
502     */
get_stat_sum(void) const503     NOMAD::Double get_stat_sum ( void ) const { return _stat_sum; }
504 
505     /// Access to the \c avg stat.
506     /**
507        \return The \c avg stat.
508     */
get_stat_avg(void) const509     NOMAD::Double get_stat_avg ( void ) const
510     {
511       return ( _cnt_avg > 0 ) ? _stat_avg/_cnt_avg : NOMAD::Double();
512     }
513 
514     /// Access to the model stats.
515     /**
516        \return The model stats.
517     */
get_model_stats(void) const518     const NOMAD::Model_Stats & get_model_stats ( void ) const { return _model_stats; }
519 
520     /// Display.
521     /**
522        \param out The NOMAD::Display object -- \b IN.
523     */
524     void display ( const NOMAD::Display & out ) const;
525   };
526 
527   /// Display a NOMAD::Stats object.
528   /**
529      \param out The NOMAD::Display object               -- \b IN.
530      \param s   The NOMAD::Stats object to be displayed -- \b IN.
531      \return    The NOMAD::Display object.
532   */
operator <<(const NOMAD::Display & out,const NOMAD::Stats & s)533   inline const NOMAD::Display & operator << ( const NOMAD::Display & out ,
534 					      const NOMAD::Stats   & s     ) {
535     s.display ( out );
536     return out;
537   }
538 }
539 
540 #endif
541