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   Display.hpp
38   \brief  Custom class for display (headers)
39   \author Sebastien Le Digabel
40   \date   2010-03-30
41   \see    Display.cpp
42 */
43 #ifndef __DISPLAY__
44 #define __DISPLAY__
45 
46 #include "utils.hpp"
47 
48 namespace NOMAD {
49 
50   /// Custom display class.
51   /**
52      - This class is used instead of \c std::ostream ojects.
53      - Use \c std::endl as new line character; \c '\\n' will ignore indentation.
54      - Do not use \c << \c flush; : it would create a new indentation.
55        Use method \c Display::flush() instead.
56 
57      \b Two \b examples \b for \b creating \b indented \b blocks:
58 
59      \code
60      using namespace NOMAD;
61      Display out ( std::cout );
62      \endcode
63 
64      \b Example \b 1:
65 
66      \code
67      out << "line #1" << std::endl
68          << open_block()
69          << "line #2" << std::endl << "line #3" << std::endl
70          << open_block ( "begin of block 2" )
71          << "line #4" << std::endl << "line #5" << std::endl
72          << close_block ( "end of block 2" )
73          << close_block()
74          << std::endl;
75      \endcode
76 
77      \b Example \b 2:
78 
79      \code
80      out << "line #1" << std::endl;
81      out.open_block();
82      out << "line #2" << std::endl << "line #3" << std::endl;
83      out.open_block ("begin of block 2");
84      out << "line #4" << std::endl << "line #5" << std::endl;
85      out.close_block ("end of block 2");
86      out.close_block();
87      out << std::endl;
88      \endcode
89 
90      \b This \b displays \b twice:
91 
92      \verbatim
93      line #1
94      {
95          line #2
96 	 line #3
97          begin of block 2 {
98 	     line #4
99 	     line #5
100 	 } end of block 2
101      }
102      \endverbatim
103   */
104   class Display {
105 
106   private:
107 
108     std::ostream      & _out;           ///< Display.
109 
110     mutable std::string _indent_str;    ///< Indentation string (tabulations).
111     mutable bool        _newline;       ///< Indent or not.
112 
113     std::string         _open_brace;    ///< Braces of the indentation blocks.
114     std::string         _closed_brace;  ///< Defaults: "{" and "}".
115 
116     NOMAD::dd_type      _gen_dd;        ///< General display degree.
117     NOMAD::dd_type      _search_dd;     ///< Search display degree.
118     NOMAD::dd_type      _poll_dd;       ///< Poll display degree.
119     NOMAD::dd_type      _iter_dd;       ///< Iterative display degree.
120 
121     /// Private affectation operator.
122     /**
123        \param out The right-hand side object -- \b IN.
124     */
125     const Display & operator = ( const Display & out );
126 
127   public:
128 
129     /// Constructor.
130     /**
131        \param out A \c std::ostream that will be used for all displays
132                   (can be a \c std::ofstream)
133 		  -- \b IN -- \b optional (default = \c std::cout).
134      */
Display(std::ostream & out=std::cout)135     Display ( std::ostream & out = std::cout )
136       : _out          ( out                   ) , // can be a std::ofstream
137 	_newline      ( true                  ) ,
138 	_open_brace   ( "{"                   ) ,
139 	_closed_brace ( "}"                   ) ,
140 	_gen_dd       ( NOMAD::NORMAL_DISPLAY ) ,
141 	_search_dd    ( NOMAD::NORMAL_DISPLAY ) ,
142 	_poll_dd      ( NOMAD::NORMAL_DISPLAY ) ,
143 	_iter_dd      ( NOMAD::NORMAL_DISPLAY )   {}
144 
145     /// Copy constructor.
146     /**
147        \param out The copied object -- \b IN.
148     */
Display(const Display & out)149     Display ( const Display & out )
150       : _out          ( out._out          ) ,
151 	_indent_str   ( out._indent_str   ) ,
152 	_newline      ( out._newline      ) ,
153 	_open_brace   ( out._open_brace   ) ,
154 	_closed_brace ( out._closed_brace ) ,
155 	_gen_dd       ( out._gen_dd       ) ,
156 	_search_dd    ( out._search_dd    ) ,
157 	_poll_dd      ( out._poll_dd      ) ,
158 	_iter_dd      ( out._iter_dd      )   {}
159 
160     /// Destructor.
~Display(void)161     virtual ~Display ( void ) {}
162 
163     /// Flush.
164     /**
165        Must be used instead of \c out \c << \c std::flush.
166     */
flush(void) const167     void flush ( void ) const { _out << std::flush; }
168 
169     /*---------------*/
170     /*  GET methods  */
171     /*---------------*/
172 
173     /// Access to the indentation string.
174     /**
175       \return The indentation string.
176     */
get_indent_str(void) const177     const std::string get_indent_str ( void ) const { return _indent_str; }
178 
179     /// Access to the general display degree.
180     /**
181        \return _gen_dd.
182     */
get_gen_dd(void) const183     NOMAD::dd_type get_gen_dd ( void ) const { return _gen_dd; }
184 
185     /// Access to the search display degree.
186     /**
187        \return _search_dd.
188     */
get_search_dd(void) const189     NOMAD::dd_type get_search_dd ( void ) const { return _search_dd; }
190 
191     /// Access to the poll display degree.
192     /**
193        \return _poll_dd.
194     */
get_poll_dd(void) const195     NOMAD::dd_type get_poll_dd ( void ) const { return _poll_dd; }
196 
197     /// Access to the iterative display degree.
198     /**
199        \return _iter_dd.
200     */
get_iter_dd(void) const201     NOMAD::dd_type get_iter_dd ( void ) const { return _iter_dd; }
202 
203     /// Get the display degree for a specific search type.
204     /**
205        \param search The search type.
206        \return       The display degree.
207     */
208     NOMAD::dd_type get_display_degree ( NOMAD::search_type search ) const;
209 
210     /// Get the display degrees as a string of size 4.
211     /**
212        \param dd The string containing the display degrees -- \b OUT.
213     */
214     void get_display_degree ( std::string & dd ) const;
215 
216     /*---------------*/
217     /*  SET methods  */
218     /*---------------*/
219 
220     /// Set the indentation string.
221     /**
222       \param is The indentation string -- \b IN.
223     */
set_indent_str(const std::string & is)224     void set_indent_str ( const std::string & is ) { _indent_str = is; }
225 
226     /// Set the _open_brace string.
227     /**
228        \param ob The string -- \b IN.
229     */
set_open_brace(const std::string & ob)230     void set_open_brace   ( const std::string & ob ) { _open_brace = ob; }
231 
232     /// Set the _closed_brace string.
233     /**
234        \param cb The string -- \b IN.
235     */
set_closed_brace(const std::string & cb)236     void set_closed_brace ( const std::string & cb ) { _closed_brace = cb; }
237 
238     /// Set the display degrees.
239     /**
240        \param gen_dd    General display degree   -- \b IN.
241        \param search_dd Search display degree    -- \b IN.
242        \param poll_dd   Poll display degree      -- \b IN.
243        \param iter_dd   Iterative display degree -- \b IN.
244     */
245     void set_degrees ( NOMAD::dd_type gen_dd    ,
246 		       NOMAD::dd_type search_dd ,
247 		       NOMAD::dd_type poll_dd   ,
248 		       NOMAD::dd_type iter_dd     );
249 
250     /// Set all the display degrees to one given display degree.
251     /**
252        \param dd The 4 display degrees -- \b IN.
253     */
set_degrees(NOMAD::dd_type dd)254     void set_degrees ( NOMAD::dd_type dd ) { set_degrees ( dd , dd , dd , dd ); }
255 
256     /// Open an indentation block.
257     /**
258        \param msg Message displayed as the block title
259                   -- \b IN -- \b optional (default = empty string).
260     */
261     void open_block  ( const std::string & msg = "" ) const;
262 
263     /// Close an indentation block.
264     /**
265        \param msg Message displayed at the end of the block
266                   -- \b IN -- \b optional (default = empty string).
267     */
268     void close_block ( const std::string & msg = "" ) const;
269 
270     /// Operator <<.
271     template <class T>
272     const Display & operator << ( const T & ) const;
273 
274     /// Defines the \c cout type.
275     typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
276 
277     /// Allows the use of \c out \c << \c endl (\c std::endl is used).
278     /**
279        \warning This considers also \c std::flush.
280     */
281     typedef CoutType& (*StandardEndLine)(CoutType&);
282 
283     /// Operator << for \c std::endl.
284     const Display & operator << ( StandardEndLine ) const;
285 
286     /// Set the display precision.
287     /**
288        \param p The display precision -- \b IN.
289     */
precision(int p) const290     void precision ( int p ) const { _out.precision(p); }
291 
292     /// Get the current display precision.
293     /**
294        \return An integer for the current precision.
295     */
precision(void) const296     int precision ( void ) const { return static_cast<int>(_out.precision()); }
297 
298     /// Set the format flags (1/2).
299     /**
300        \param f The flags -- \b IN.
301     */
flags(std::ios_base::fmtflags f) const302     void flags ( std::ios_base::fmtflags f ) const { _out.flags(f); }
303 
304     /// Set the format flags (2/2).
305     /**
306        \param f The flags -- \b IN.
307     */
setf(std::ios_base::fmtflags f) const308     void setf ( std::ios_base::fmtflags f ) const { _out.setf(f); }
309 
310     /// Unset the format flags.
311     /**
312        \param f The flags -- \b IN.
313     */
unsetf(std::ios_base::fmtflags f) const314     void unsetf ( std::ios_base::fmtflags f ) const { _out.unsetf(f); }
315 
316 
317     /// Get the current format flags.
318     /**
319        \return The format flags.
320     */
flags(void) const321     std::ios_base::fmtflags flags ( void ) const { return _out.flags(); }
322 
323     /*--------------------*/
324     /*  type conversions  */
325     /*--------------------*/
326 
327     /// Convert a NOMAD::dd_type to a character.
328     /**
329        \param dd The NOMAD::dd_type -- \b IN.
330        \return   The character.
331      */
332     static char dd_to_char ( NOMAD::dd_type dd );
333 
334     /// Convert a NOMAD::dd_type to an integer.
335     /**
336        \param dd The NOMAD::dd_type -- \b IN.
337        \return   The integer.
338     */
339     static int dd_to_int ( NOMAD::dd_type dd );
340 
341     /// Convert an integer to a NOMAD::dd_type.
342     /**
343        \param dd The integer -- \b IN.
344        \return   The NOMAD::dd_type.
345     */
346     static NOMAD::dd_type int_to_dd ( int dd );
347 
348     /// Display a duration with a smart format.
349     /**
350        \param t Duration as an integer in seconds -- \b IN.
351     */
352     void display_time ( int t ) const;
353 
354     /// Display a boolean with format \c yes/no.
355     /**
356        \param b The boolean -- \b IN.
357     */
display_yes_or_no(bool b) const358     void display_yes_or_no ( bool b ) const { (*this) << ( (b) ? "yes" : "no" ); }
359 
360     /// Display a memory size.
361     /**
362        \param size The memory size.
363     */
364     void display_size_of ( float size ) const;
365 
366     /// Display an integer with a specific width.
367     /**
368        \param i     The integer to display -- \b IN.
369        \param max_i Maximal value of \c i used to determine the display width
370                     -- \b IN -- \b optional (default = \c -1).
371     */
372     void display_int_w ( int i , int max_i = -1 ) const;
373 
374     /// Get the keyword associated with a NOMAD::display_stats_type.
375     /**
376        \param dst The NOMAD::display_stats_type -- \b IN.
377        \return    A string containing the keyword.
378     */
379     static std::string get_display_stats_keyword ( NOMAD::display_stats_type dst );
380 
381     /// Extract display format from a string.
382     /**
383        \param s      The string -- \b IN/OUT.
384        \param format The format -- \b OUT.
385     */
386     static void extract_display_format ( std::string & s , std::string & format );
387 
388     /// Get the NOMAD::display_stats_type from a string.
389     /**
390        \param s The string -- \b IN.
391        \return  The NOMAD::display_stats_type.
392     */
393     static NOMAD::display_stats_type get_display_stats_type ( const std::string & s );
394 
395   };
396 
397   /*-------------------------------------------------------------------------*/
398 
399   /// Open an indented block.
400   /**
401      Allows the use of \c out \c << \c open_block(msg).
402   */
403   class open_block {
404   private:
405     std::string _msg; ///< Message displayed as a block title.
406   public:
407 
408     /// Constructor.
409     /**
410        Open an indented block.
411        \param msg The block title
412                   -- \b IN -- \b optional (default = empty string).
413     */
open_block(const std::string & msg="")414     open_block ( const std::string & msg = "" ) : _msg ( msg ) {}
415 
416     /// Operator ().
operator ()(const Display & out) const417     const Display & operator() ( const Display & out ) const {
418       out.open_block ( _msg );
419       return out;
420     }
421   };
422 
423   /*-------------------------------------------------------------------------*/
424 
425   /// Close an indented block.
426   /**
427      Allows the use of \c out \c << \c close_block(msg).
428   */
429   class close_block {
430   private:
431     std::string _msg; ///< Message displayed at the end of a block.
432   public:
433 
434     /// Constructor.
435     /**
436        Close an indented block.
437        \param msg Message displayed at the end of a block
438                   -- \b IN -- \b optional (default = empty string).
439     */
close_block(const std::string & msg="")440     close_block ( const std::string & msg = "" ) : _msg ( msg ) {}
441 
442     /// Operator ().
operator ()(const Display & out) const443     const Display & operator() ( const Display & out ) const {
444       out.close_block ( _msg );
445       return out;
446     }
447   };
448 
449   /*-------------------------------------------------------------*/
450   /*  display functions for enum types defined in 'defines.hpp'  */
451   /*-------------------------------------------------------------*/
452 
453   /// Operator << for NOMAD::stop_type.
454   std::ostream & operator << ( std::ostream & , NOMAD::stop_type );
455 
456   /// Operator << for NOMAD::dd_type.
457   std::ostream & operator << ( std::ostream & , NOMAD::dd_type );
458 
459   /// Operator << for NOMAD::success_type.
460   std::ostream & operator << ( std::ostream & , NOMAD::success_type );
461 
462   /// Operator << for NOMAD::bb_input_type.
463   std::ostream & operator << ( std::ostream & , NOMAD::bb_input_type );
464 
465   /// Operator << for NOMAD::bb_output_type.
466   std::ostream & operator << ( std::ostream & , NOMAD::bb_output_type );
467 
468   /// Operator << for NOMAD::interpolation_type.
469   std::ostream & operator << ( std::ostream & , NOMAD::interpolation_type );
470 
471   /// Operator << for NOMAD::hnorm_type.
472   std::ostream & operator << ( std::ostream & , NOMAD::hnorm_type );
473 
474   /// Operator << for NOMAD::search_type.
475   std::ostream & operator << ( std::ostream & , NOMAD::search_type );
476 
477   /// Operator << for NOMAD::model_type.
478   std::ostream & operator << ( std::ostream & , NOMAD::model_type );
479 
480   /// Operator << for NOMAD::TGP_mode_type.
481   std::ostream & operator << ( std::ostream & , NOMAD::TGP_mode_type );
482 
483   /// Operator << for NOMAD::direction_type.
484   std::ostream & operator << ( std::ostream & , NOMAD::direction_type );
485 
486   /// Operator << for NOMAD::check_failed_type.
487   std::ostream & operator << ( std::ostream & , NOMAD::check_failed_type );
488 
489   /// Operator << for NOMAD::display_stats_type.
490   std::ostream & operator << ( std::ostream & , NOMAD::display_stats_type );
491 
492   /// Operator << for NOMAD::eval_type.
493   std::ostream & operator << ( std::ostream & , NOMAD::eval_type );
494 
495   /// Operator << for NOMAD::eval_status_type.
496   std::ostream & operator << ( std::ostream & , NOMAD::eval_status_type );
497 
498   /// Operator << for NOMAD::multi_formulation_type.
499   std::ostream & operator << ( std::ostream & , NOMAD::multi_formulation_type );
500 
501   /// Operator << for a vector of NOMAD::bb_intput_type.
502   std::ostream & operator << ( std::ostream                            & ,
503 			       const std::vector<NOMAD::bb_input_type> &   );
504 
505   /// Operator <<.
506   template <class T>
operator <<(const T & t) const507   inline const NOMAD::Display & NOMAD::Display::operator << ( const T & t ) const
508   {
509     if ( _newline ) {
510       _out << _indent_str;
511       _newline = false;
512     }
513     _out << t;
514     return *this;
515   }
516 
517   /// Allows the use of \c out \c << \c endl.
operator <<(StandardEndLine m) const518   inline const NOMAD::Display & NOMAD::Display::operator << ( StandardEndLine m ) const
519   {
520     m ( _out ); // this could be a std::flush, so don't use it: instead use method flush()
521     _newline = true;
522     return *this;
523   }
524 
525   /// Allows the use of \c out \c << \c open_block(msg).
operator <<(const NOMAD::Display & out,const NOMAD::open_block & ob)526   inline const NOMAD::Display & operator << ( const NOMAD::Display    & out ,
527 					      const NOMAD::open_block & ob    )
528   {
529     return ob ( out );
530   }
531 
532   /// Allows the use of \c out \c << \c close_block(msg).
operator <<(const NOMAD::Display & out,const NOMAD::close_block & cb)533   inline const NOMAD::Display & operator << ( const NOMAD::Display     & out ,
534 					      const NOMAD::close_block & cb    )
535   {
536     return cb ( out );
537   }
538 
539 }
540 
541 #endif
542