queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
QUESO::OptimizerMonitor Class Reference

Object to monitor convergence of optimizers. More...

#include <OptimizerMonitor.h>

Public Member Functions

 OptimizerMonitor (const BaseEnvironment &env, unsigned int n_iters=100)
 Constructor. More...
 
 ~OptimizerMonitor ()
 
void set_display_output (bool enable_output, bool print_xmin)
 Enabling output to std::cout everytime append is called. More...
 
void append (std::vector< double > &x_min, double objective, double norm)
 Add current value of minimizer, objective, and error norm. More...
 
void reset ()
 Clears internal datastructures and resets display variables to false. More...
 
void print (std::ostream &output, bool print_xmin=false) const
 Prints entire convergence history. More...
 

Private Member Functions

void print_header (std::ostream &output, bool print_xmin) const
 
void print_iteration (unsigned int iter, std::ostream &output, bool print_xmin) const
 

Private Attributes

const BaseEnvironmentm_env
 
bool m_display_conv
 
bool m_print_xmin
 
std::vector< std::vector
< double > > 
m_minimizer_hist
 
std::vector< double > m_objective_hist
 
std::vector< double > m_norm_hist
 

Detailed Description

Object to monitor convergence of optimizers.

Definition at line 36 of file OptimizerMonitor.h.

Constructor & Destructor Documentation

QUESO::OptimizerMonitor::OptimizerMonitor ( const BaseEnvironment env,
unsigned int  n_iters = 100 
)

Constructor.

Input paramter n_iters will reserve space for monitor data for n_iters. This is for efficiency - if the number of iterations goes beyond n_iters, there will not be an error, just less performance by the monitor. Defaults to reserving space for 100 iterations

Definition at line 34 of file OptimizerMonitor.C.

References m_minimizer_hist, m_norm_hist, and m_objective_hist.

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  }
std::vector< double > m_objective_hist
const BaseEnvironment & m_env
std::vector< std::vector< double > > m_minimizer_hist
std::vector< double > m_norm_hist
QUESO::OptimizerMonitor::~OptimizerMonitor ( )

Definition at line 44 of file OptimizerMonitor.C.

45  {}

Member Function Documentation

void QUESO::OptimizerMonitor::append ( std::vector< double > &  x_min,
double  objective,
double  norm 
)

Add current value of minimizer, objective, and error norm.

Definition at line 53 of file OptimizerMonitor.C.

References m_display_conv, m_minimizer_hist, m_norm_hist, m_objective_hist, m_print_xmin, print_header(), and print_iteration().

Referenced by QUESO::GslOptimizer::minimize_no_gradient(), and QUESO::GslOptimizer::minimize_with_gradient().

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  }
void print_header(std::ostream &output, bool print_xmin) const
std::vector< double > m_objective_hist
void print_iteration(unsigned int iter, std::ostream &output, bool print_xmin) const
std::vector< std::vector< double > > m_minimizer_hist
std::vector< double > m_norm_hist
void QUESO::OptimizerMonitor::print ( std::ostream &  output,
bool  print_xmin = false 
) const

Prints entire convergence history.

The print_xmin argument controls whether or not the estimate of the minimizer (all components) is printed.

Definition at line 147 of file OptimizerMonitor.C.

References m_norm_hist, print_header(), print_iteration(), and QUESO::size.

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  }
void print_header(std::ostream &output, bool print_xmin) const
void print_iteration(unsigned int iter, std::ostream &output, bool print_xmin) const
std::vector< double > m_norm_hist
void QUESO::OptimizerMonitor::print_header ( std::ostream &  output,
bool  print_xmin 
) const
private

Definition at line 76 of file OptimizerMonitor.C.

References QUESO::BaseEnvironment::fullRank(), m_env, m_minimizer_hist, and QUESO::size.

Referenced by append(), and print().

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  }
const BaseEnvironment & m_env
int fullRank() const
Returns the rank of the MPI process in QUESO&#39;s full communicator.
Definition: Environment.C:268
std::vector< std::vector< double > > m_minimizer_hist
void QUESO::OptimizerMonitor::print_iteration ( unsigned int  iter,
std::ostream &  output,
bool  print_xmin 
) const
private

Definition at line 106 of file OptimizerMonitor.C.

References QUESO::BaseEnvironment::fullRank(), m_env, m_minimizer_hist, m_norm_hist, and m_objective_hist.

Referenced by append(), and print().

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  }
std::vector< double > m_objective_hist
const BaseEnvironment & m_env
int fullRank() const
Returns the rank of the MPI process in QUESO&#39;s full communicator.
Definition: Environment.C:268
std::vector< std::vector< double > > m_minimizer_hist
std::vector< double > m_norm_hist
void QUESO::OptimizerMonitor::reset ( )

Clears internal datastructures and resets display variables to false.

Definition at line 137 of file OptimizerMonitor.C.

References m_display_conv, m_minimizer_hist, m_norm_hist, m_objective_hist, and m_print_xmin.

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  }
std::vector< double > m_objective_hist
std::vector< std::vector< double > > m_minimizer_hist
std::vector< double > m_norm_hist
void QUESO::OptimizerMonitor::set_display_output ( bool  enable_output,
bool  print_xmin 
)

Enabling output to std::cout everytime append is called.

Helpful when wanting to monitor convergence progress in real time. The print_xmin argument controls whether or not the current estimate of the minimizer is printed. Not recommended if you have more than a handful of dimensions over which you are optimizing.

Definition at line 47 of file OptimizerMonitor.C.

References m_display_conv, and m_print_xmin.

Referenced by QUESO::StatisticalInverseProblem< P_V, P_M >::solveWithBayesMetropolisHastings().

48  {
49  m_display_conv = enable_output;
50  m_print_xmin = print_xmin;
51  }

Member Data Documentation

bool QUESO::OptimizerMonitor::m_display_conv
private

Definition at line 77 of file OptimizerMonitor.h.

Referenced by append(), reset(), and set_display_output().

const BaseEnvironment& QUESO::OptimizerMonitor::m_env
private

Definition at line 75 of file OptimizerMonitor.h.

Referenced by print_header(), and print_iteration().

std::vector<std::vector<double> > QUESO::OptimizerMonitor::m_minimizer_hist
private

Definition at line 80 of file OptimizerMonitor.h.

Referenced by append(), OptimizerMonitor(), print_header(), print_iteration(), and reset().

std::vector<double> QUESO::OptimizerMonitor::m_norm_hist
private

Definition at line 82 of file OptimizerMonitor.h.

Referenced by append(), OptimizerMonitor(), print(), print_iteration(), and reset().

std::vector<double> QUESO::OptimizerMonitor::m_objective_hist
private

Definition at line 81 of file OptimizerMonitor.h.

Referenced by append(), OptimizerMonitor(), print_iteration(), and reset().

bool QUESO::OptimizerMonitor::m_print_xmin
private

Definition at line 78 of file OptimizerMonitor.h.

Referenced by append(), reset(), and set_display_output().


The documentation for this class was generated from the following files:

Generated on Tue Jun 5 2018 19:49:11 for queso-0.57.1 by  doxygen 1.8.5