source: branches/dev/Common/IpJournalist.hpp @ 546

Last change on this file since 546 was 546, checked in by andreasw, 15 years ago
  • added missing svn:keyword for a few files
  • increased factor for theta reduction in restoration convergence check from 1e1 to 1e2
  • trigger restoration phase in expect_infeasible_problem mode if multipliers become larger than 1e8
  • revert to regular restoration phase if more than max_soft_resto_iters (new option) iterations are taken in soft restoration phase
  • made quality_function default for adaptive barrier strategy
  • added new print level to avoid one line of output per iteration
  • fixed TNLPAdapter, so that only some parts of the problem (objective, constraints, variables) are scaled if desired
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 KB
Line 
1// Copyright (C) 2004, 2005 International Business Machines and others.
2// All Rights Reserved.
3// This code is published under the Common Public License.
4//
5// $Id: IpJournalist.hpp 546 2005-10-20 22:40:15Z andreasw $
6//
7// Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13
8
9#ifndef __IPJOURNALIST_HPP__
10#define __IPJOURNALIST_HPP__
11
12#include <string>
13#include <vector>
14#include "IpTypes.hpp"
15#include "IpReferenced.hpp"
16#include "IpSmartPtr.hpp"
17
18#ifdef HAVE_CSTDARG
19# include <cstdarg>
20#else
21# ifdef HAVE_STDARG_H
22#  include <stdarg.h>
23# else
24#  error "don't have header file for stdarg"
25# endif
26#endif
27
28namespace Ipopt
29{
30
31  // forward declarations
32  class Journal;
33  class FileJournal;
34
35  /**@name Journalist Enumerations. */
36  //@{
37  /** Print Level Enum. */
38  enum EJournalLevel {
39    J_INSUPPRESSIBLE=-1,
40    J_NONE=0,
41    J_ERROR,
42    J_WARNING,
43    J_SUMMARY,
44    J_ITERSUMMARY,
45    J_DETAILED,
46    J_MOREDETAILED,
47    J_VECTOR,
48    J_MOREVECTOR,
49    J_MATRIX,
50    J_MOREMATRIX,
51    J_ALL,
52    J_LAST_LEVEL
53  };
54
55  /** Category Selection Enum. */
56  enum EJournalCategory {
57    J_DBG=0,
58    J_STATISTICS,
59    J_MAIN,
60    J_INITIALIZATION,
61    J_BARRIER_UPDATE,
62    J_SOLVE_PD_SYSTEM,
63    J_FRAC_TO_BOUND,
64    J_LINEAR_ALGEBRA,
65    J_LINE_SEARCH,
66    J_SOLUTION,
67    J_DOCUMENTATION,
68    J_NLP,
69    J_TIMING_STATISTICS,
70    J_LAST_CATEGORY
71  };
72  //@}
73
74  /** Class responsible for all message output.
75   * This class is responsible for all messaging and output.
76   * The "printing" code or "author" should send ALL messages to the
77   * Journalist, indicating an appropriate category and print level.
78   * The journalist then decides, based on reader specified
79   * acceptance criteria, which message is actually printed in which
80   * journals.
81   * This allows the printing code to send everything, while the
82   * "reader" can decide what they really want to see.
83   *
84   * Authors:
85   * Authors use the
86   * Journals: You can add as many Journals as you like to the
87   * Journalist with the AddJournal or the AddFileJournal methods.
88   * Each one represents a different printing location (or file). 
89   * Then, you can call the "print" methods of the Journalist to output
90   * information to each of the journals.
91   *
92   * Acceptance Criteria: Each print message should be flagged
93   * appropriately with an EJournalCategory and EJournalLevel.
94   *
95   * The AddFileJournal
96   * method returns a pointer to the newly created Journal object
97   * (if successful) so you can set Acceptance criteria for that
98   * particular location.
99   *
100   */
101  class Journalist : public ReferencedObject
102  {
103  public:
104    /**@name Constructor / Desructor. */
105    //@{
106    /** Constructor. */
107    Journalist();
108
109    /** Destructor... */
110    virtual ~Journalist();
111    //@}
112
113    /**@name Author Methods.
114     * These methods are used by authoring code, or code that wants
115     * to report some information.
116     */
117    //@{
118    /** Method to print a formatted string */
119    void Printf(EJournalLevel level, EJournalCategory category,
120                const char* format, ...) const;
121
122    /** Method to print a long string including indentation.  The
123     *  string is printed starting at the current position.  If the
124     *  position (counting started at the current position) exceeds
125     *  max_length, a new line is inserted, and indent_spaces many
126     *  spaces are printed before the string is continued.  This is
127     *  for example used during the printing of the option
128     *  documentation. */
129    void PrintStringOverLines(EJournalLevel level, EJournalCategory category,
130                              Index indent_spaces, Index max_length,
131                              const std::string& line) const;
132
133    /** Method to print a formatted string with indentation */
134    void PrintfIndented(EJournalLevel level,
135                        EJournalCategory category,
136                        Index indent_level,
137                        const char* format, ...) const;
138
139    /** Method to print a formatted string
140     * using the va_list argument. */
141    void VPrintf(EJournalLevel level,
142                 EJournalCategory category,
143                 const char* pformat,
144                 va_list ap) const;
145
146    /** Method to print a formatted string with indentation,
147     * using the va_list argument. */
148    void VPrintfIndented(EJournalLevel level,
149                         EJournalCategory category,
150                         Index indent_level,
151                         const char* pformat,
152                         va_list ap) const;
153
154    /** Method that returns true if there is a Journal that would
155     *  write output for the given JournalLevel and JournalCategory.
156     *  This is useful if expensive computation would be required for
157     *  a particular output.  The author code can check with this
158     *  method if the computations are indeed required.
159     */
160    bool ProduceOutput(EJournalLevel level,
161                       EJournalCategory category) const;
162
163
164    /** Method that flushes the current buffer for all Journalists.
165     Calling this method after one optimization run helps to avoid
166     cluttering output with that produced by other parts of the
167     program (e.g. written in Fortran) */
168    void FlushBuffer() const;
169    //@}
170
171    /**@name Reader Methods.
172     * These methods are used by the reader. The reader will setup the
173     * journalist with each output file and the acceptance
174     * criteria for that file.
175     *
176     * Use these methods to setup the journals (files or other output).
177     * These are the internal objects that keep track of the print levels
178     * for each category. Then use the internal Journal objects to
179     * set specific print levels for each category (or keep defaults).
180     * 
181     */
182    //@{
183    /** Add a new journal.  The location_name is a string identifier,
184     *  which can be used to obtain the pointer to the new Journal at
185     *  a later point using the GetJournal method.
186     *  The default_level is
187     *  used to initialize the * printing level for all categories.
188     */
189    bool AddJournal(const SmartPtr<Journal> jrnl);
190
191    /** Add a new FileJournal. fname is the name
192     *  of the * file to which this Journal corresponds.  Use
193     *  fname="stdout" * for stdout, and use fname="stderr" for
194     *  stderr.  This method * returns the Journal pointer so you can
195     *  set specific acceptance criteria.  It returns NULL if there
196     *  was a problem creating a new Journal.   
197     */
198    SmartPtr<Journal> AddFileJournal(
199      const std::string& location_name,    /** identifier */
200      const std::string& fname,
201      EJournalLevel default_level = J_WARNING
202    );
203
204    /** Get an existing journal.  You can use this method to change
205     *  the acceptance criteria at runtime.
206     */
207    SmartPtr<Journal> GetJournal(const std::string& location_name);
208    //@}
209
210  private:
211    /**@name Default Compiler Generated Methods
212     * (Hidden to avoid implicit creation/calling).
213     * These methods are not implemented and
214     * we do not want the compiler to implement
215     * them for us, so we declare them private
216     * and do not define them. This ensures that
217     * they will not be implicitly created/called. */
218    //@{
219    /** Copy Constructor */
220    Journalist(const Journalist&);
221
222    /** Overloaded Equals Operator */
223    void operator=(const Journalist&);
224    //@}
225
226    //** Private Data Members. */
227    //@{
228    std::vector< SmartPtr<Journal> > journals_;
229    //@}
230  };
231
232  /** Journal class (part of the Journalist implementation.). This
233   *  class is the base class for all Journals. It controls the
234   *  acceptance criteria for print statements etc. Derived classes
235   *  like the FileJournal - output those messages to specific locations
236   */
237  class Journal : public ReferencedObject
238  {
239  public:
240    /** Constructor. */
241    Journal(const std::string& name, EJournalLevel default_level);
242
243    /** Destructor. */
244    virtual ~Journal();
245
246    /** Get the name of the Journal */
247    std::string Name();
248
249    /** Set the print level for a particular category. */
250    void SetPrintLevel(
251      EJournalCategory category, EJournalLevel level
252    );
253
254    /** Set the print level for all category. */
255    void SetAllPrintLevels(
256      EJournalLevel level
257    );
258
259    /**@name Journal Output Methods. These methods are called by the
260     *  Journalist who first checks if the output print level and category
261     *  are acceptable.
262     *  Calling the Print methods explicitly (instead of through the
263     *  Journalist will output the message regardless of print level
264     *  and category. You should use the Journalist to print & flush instead
265     */
266    //@{
267    /** Ask if a particular print level/category is accepted by the
268     * journal.
269     */
270    bool IsAccepted(
271      EJournalCategory category, EJournalLevel level
272    ) const;
273
274    /** Print to the designated output location */
275    void Print(const char* str)
276    {
277      PrintImpl(str);
278    }
279
280    /** Printf to the designated output location */
281    void Printf(const char* pformat, va_list ap)
282    {
283      PrintfImpl(pformat, ap);
284    }
285
286    /** Flush output buffer.*/
287    void FlushBuffer()
288    {
289      FlushBufferImpl();
290    }
291    //@}
292
293  protected:
294    /**@name Implementation version of Print methods. Derived classes
295     * should overload the Impl methods.
296     */
297    //@{
298    /** Print to the designated output location */
299    virtual void PrintImpl(const char* str)=0;
300
301    /** Printf to the designated output location */
302    virtual void PrintfImpl(const char* pformat, va_list ap)=0;
303
304    /** Flush output buffer.*/
305    virtual void FlushBufferImpl()=0;
306    //@}
307
308  private:
309    /**@name Default Compiler Generated Methods
310     * (Hidden to avoid implicit creation/calling).
311     * These methods are not implemented and
312     * we do not want the compiler to implement
313     * them for us, so we declare them private
314     * and do not define them. This ensures that
315     * they will not be implicitly created/called. */
316    //@{
317    /** Default Constructor */
318    Journal();
319
320    /** Copy Constructor */
321    Journal(const Journal&);
322
323    /** Overloaded Equals Operator */
324    void operator=(const Journal&);
325    //@}
326
327    /** Name of the output location */
328    std::string name_;
329
330    /** vector of integers indicating the level for each category */
331    Index print_levels_[J_LAST_CATEGORY];
332  };
333
334
335  /** FileJournal class. This is a particular Journal implementation that
336   *  writes to a file for output. It can write to (stdout, stderr, or disk)
337   *  by using "stdout" and "stderr" as filenames.
338   */
339  class FileJournal : public Journal
340  {
341  public:
342    /** Constructor. */
343    FileJournal(const std::string& name, EJournalLevel default_level);
344
345    /** Destructor. */
346    virtual ~FileJournal();
347
348    /** Open a new file for the output location.
349     *  Special Names: stdout means stdout,
350     *               : stderr means stderr.
351     *
352     *  Return code is false only if the file with the given name
353     *  could not be opened.
354     */
355    bool Open(const char* fname);
356
357  protected:
358    /**@name Implementation version of Print methods - Overloaded from
359     * Journal base class.
360     */
361    //@{
362    /** Print to the designated output location */
363    virtual void PrintImpl(const char* str);
364
365    /** Printf to the designated output location */
366    virtual void PrintfImpl(const char* pformat, va_list ap);
367
368    /** Flush output buffer.*/
369    virtual void FlushBufferImpl();
370    //@}
371
372  private:
373    /**@name Default Compiler Generated Methods
374     * (Hidden to avoid implicit creation/calling).
375     * These methods are not implemented and
376     * we do not want the compiler to implement
377     * them for us, so we declare them private
378     * and do not define them. This ensures that
379     * they will not be implicitly created/called. */
380    //@{
381    /** Default Constructor */
382    FileJournal();
383
384    /** Copy Constructor */
385    FileJournal(const FileJournal&);
386
387    /** Overloaded Equals Operator */
388    void operator=(const FileJournal&);
389    //@}
390
391    /** FILE pointer for the output destination */
392    FILE* file_;
393  };
394}
395
396#endif
Note: See TracBrowser for help on using the repository browser.