queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
OptimizerMonitor.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2017 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <string>
26 #include <iomanip>
27 #include <queso/asserts.h>
28 #include <queso/Environment.h>
29 #include <queso/OptimizerMonitor.h>
30 
31 namespace QUESO
32 {
33 
34  OptimizerMonitor::OptimizerMonitor( const BaseEnvironment& env, unsigned int n_iters )
35  : m_env(env),
36  m_display_conv(false),
37  m_print_xmin(false)
38  {
39  m_minimizer_hist.reserve(n_iters);
40  m_objective_hist.reserve(n_iters);
41  m_norm_hist.reserve(n_iters);
42  }
43 
45  {}
46 
47  void OptimizerMonitor::set_display_output( bool enable_output, bool print_xmin )
48  {
49  m_display_conv = enable_output;
50  m_print_xmin = print_xmin;
51  }
52 
53  void OptimizerMonitor::append( std::vector<double>& x_min, double objective, double norm )
54  {
55  // This needs to be done before trying to print
56  m_minimizer_hist.push_back(x_min);
57  m_objective_hist.push_back(objective);
58  m_norm_hist.push_back(norm);
59 
60  // Print out to screen if the user set the option
61  if( m_display_conv )
62  {
63  // If we are appending the first entry, print a nice header
64  if( m_minimizer_hist.size() == 1 )
65  {
66  this->print_header(std::cout, m_print_xmin);
67  }
68 
69  /* We're assuming here the size of the array is the current iteration.
70  Shift by -1 since we count from 0 in C++. */
71  this->print_iteration(m_norm_hist.size()-1,std::cout,m_print_xmin);
72  }
73 
74  }
75 
76  void OptimizerMonitor::print_header( std::ostream& output, bool print_xmin ) const
77  {
78  unsigned int width = 37;
79 
80  if( print_xmin) width += (m_minimizer_hist[0]).size()*15;
81 
82  // Only print output on processor 0
83  if( m_env.fullRank() == 0 )
84  {
85  output.width(5);
86  output << "i";
87 
88  if( print_xmin)
89  {
90  for( unsigned int i = 0; i < m_minimizer_hist[0].size(); i++ )
91  {
92  output.width(9);
93  output << "x" << i << std::string(5,' ');
94  }
95  }
96 
97  output.width(9);
98  output << "f" << std::string(5,' ');
99 
100  output.width(12);
101  output << "norm" << std::endl;
102  output << std::string(width,'-') << std::endl;
103  }
104  }
105 
106  void OptimizerMonitor::print_iteration( unsigned int iter, std::ostream& output,
107  bool print_xmin ) const
108  {
109  if( m_env.fullRank() == 0 )
110  {
111  output.width(5);
112  output << iter;
113 
114  if( print_xmin)
115  {
116  for( unsigned int i = 0; i < m_minimizer_hist[iter].size(); i++ )
117  {
118  output.width(2);
119  output << " ";
120  output.width(13);
121  output << std::scientific << m_minimizer_hist[iter][i];
122  }
123  }
124 
125  output.width(2);
126  output << " ";
127  output.width(13);
128  output << std::scientific << m_objective_hist[iter];
129 
130  output.width(2);
131  output << " ";
132  output.width(13);
133  output << m_norm_hist[iter] << std::endl;
134  }
135  }
136 
138  {
139  m_display_conv = false;
140  m_print_xmin = false;
141 
142  m_minimizer_hist.clear();
143  m_objective_hist.clear();
144  m_norm_hist.clear();
145  }
146 
147  void OptimizerMonitor::print( std::ostream& output, bool print_xmin ) const
148  {
149  // First check that there's something to print.
150  if( m_norm_hist.empty() )
151  {
152  std::cerr << "Nothing to print from OptimizerMonitor!" << std::endl;
153  queso_error();
154  }
155 
156  this->print_header(output,print_xmin);
157 
158  unsigned int size = m_norm_hist.size();
159  for(unsigned int i = 0; i < size; i++ )
160  {
161  this->print_iteration(i,output,print_xmin);
162  }
163  }
164 
165 } // end namespace QUESO
void set_display_output(bool enable_output, bool print_xmin)
Enabling output to std::cout everytime append is called.
void print_header(std::ostream &output, bool print_xmin) const
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:198
std::vector< double > m_objective_hist
const BaseEnvironment & m_env
void print_iteration(unsigned int iter, std::ostream &output, bool print_xmin) const
void print(std::ostream &output, bool print_xmin=false) const
Prints entire convergence history.
int fullRank() const
Returns the rank of the MPI process in QUESO&#39;s full communicator.
Definition: Environment.C:268
OptimizerMonitor(const BaseEnvironment &env, unsigned int n_iters=100)
Constructor.
void reset()
Clears internal datastructures and resets display variables to false.
std::vector< std::vector< double > > m_minimizer_hist
void append(std::vector< double > &x_min, double objective, double norm)
Add current value of minimizer, objective, and error norm.
std::vector< double > m_norm_hist

Generated on Tue Jun 5 2018 19:48:55 for queso-0.57.1 by  doxygen 1.8.5