MoochoPack : Framework for Large-Scale Optimization Algorithms  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MoochoPack_MoochoTrackerStatsStd.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include <assert.h>
43 
44 #include <iomanip>
45 
46 #include "MoochoPack_MoochoTrackerStatsStd.hpp"
47 #include "MoochoPack_NLPAlgoState.hpp"
48 #include "MoochoPack_moocho_algo_conversion.hpp"
49 #include "NLPInterfacePack_NLPFirstOrder.hpp"
50 #include "AbstractLinAlgPack_Vector.hpp"
51 #include "Teuchos_dyn_cast.hpp"
52 
53 namespace {
54 template< class T >
55 inline
56 T my_max( const T& v1, const T& v2 ) { return v1 > v2 ? v1 : v2; }
57 } // end namespace
58 
59 namespace MoochoPack {
60 
61 using std::endl;
62 using std::setw;
63 using std::left;
64 using std::right;
65 using std::setprecision;
66 
67 MoochoTrackerStatsStd::MoochoTrackerStatsStd(
68  const ostream_ptr_t& o, const ostream_ptr_t& journal_out
69  )
70  :AlgorithmTracker(journal_out)
71 {
73 }
74 
75 void MoochoTrackerStatsStd::set_output_stream(const ostream_ptr_t& o)
76 {
77  o_ = o;
78 }
79 
82 {
83  return o_;
84 }
85 
87 {
88  num_QN_updates_ = 0;
89  timer_.reset();
90  timer_.start();
91 }
92 
94 {
95  const NLPAlgo &algo = rsqp_algo(p_algo);
96  const NLPAlgoState &s = algo.rsqp_state();
97 
98  // All we have to do here is to just to count the number of quasi-newton updates
99  const QuasiNewtonStats *quasi_newt_stats =
100  ( quasi_newton_stats_.exists_in(s) && quasi_newton_stats_(s).updated_k(0)
101  ? &quasi_newton_stats_(s).get_k(0)
102  : NULL );
103  if( quasi_newt_stats ) {
104  QuasiNewtonStats::EUpdate updated = quasi_newt_stats->updated();
105  if( updated == QuasiNewtonStats::DAMPENED_UPDATED || updated == QuasiNewtonStats::UPDATED )
106  num_QN_updates_++;
107  }
108 }
109 
111  , EAlgoReturn algo_return ) const
112 {
113  using Teuchos::dyn_cast;
114 
115  const NLPAlgo &algo = rsqp_algo(p_algo);
116  const NLPAlgoState &s = algo.rsqp_state();
117  const NLPObjGrad &nlp = dyn_cast<const NLPObjGrad>(algo.nlp());
118  const NLPFirstOrder *nlp_foi = dynamic_cast<const NLPFirstOrder*>(&nlp);
119 
120  const size_type
121  m = nlp.m();
122 
123  std::ostream& o = this->o();
124 
125  // Stop the timer
126  timer_.stop();
127 
128  // Formating info
129  const int
130  p = 18,
131  stat_w = 15,
132  val_w = p + 10;
133 
134  // Get a Quasi-Newton statistics.
135  const QuasiNewtonStats *quasi_newt_stats =
136  ( quasi_newton_stats_.exists_in(s) && quasi_newton_stats_(s).updated_k(0)
137  ? &quasi_newton_stats_(s).get_k(0)
138  : NULL );
139  if( quasi_newt_stats ) {
140  QuasiNewtonStats::EUpdate updated = quasi_newt_stats->updated();
141  if( updated == QuasiNewtonStats::DAMPENED_UPDATED || updated == QuasiNewtonStats::UPDATED )
142  num_QN_updates_++;
143  }
144 
145  // status
146  o << left << setw(stat_w) << "status" << "= "
147  << right << setw(val_w);
148  switch( algo_return ) {
149  case IterationPack::TERMINATE_TRUE:
150  o << "solved";
151  break;
152  case IterationPack::TERMINATE_FALSE:
153  o << "except";
154  break;
155  case IterationPack::MAX_ITER_EXCEEDED:
156  o << "max_iter";
157  break;
158  case IterationPack::MAX_RUN_TIME_EXCEEDED:
159  o << "max_run_time";
160  break;
161  case IterationPack::INTERRUPTED_TERMINATE_TRUE:
162  o << "interrupted_solved";
163  break;
164  case IterationPack::INTERRUPTED_TERMINATE_FALSE:
165  o << "interrupted_not_solved";
166  break;
167  default:
169  }
170  o << "; # solved, except, max_iter, max_run_time\n";
171  // niter
172  o << left << setw(stat_w) << "niter" << "= "
173  << right << setw(val_w) << s.k()
174  << "; # Number of rSQP iterations (plus 1?)\n";
175  // nfunc
176  o << left << setw(stat_w) << "nfunc" << "= "
177  << right << setw(val_w) << my_max(nlp.num_f_evals(),(m? nlp.num_c_evals():0) )
178  << "; # max( number f(x) evals, number c(x) evals )\n";
179  // ngrad
180  o << left << setw(stat_w) << "ngrad" << "= "
181  << right << setw(val_w) << my_max(nlp.num_Gf_evals(),(m?(nlp_foi?nlp_foi->num_Gc_evals():s.k()+1):0))
182  << "; # max( number Gf(x) evals, number Gc(x) evals )\n";
183  // CPU
184  o << left << setw(stat_w) << "CPU" << "= "
185  << right << setw(val_w) << timer_.read()
186  << "; # Number of CPU seconds total\n";
187  // obj_func
188  o << left << setw(stat_w) << "obj_func" << "= "
189  << right << setw(val_w);
190  if(s.f().updated_k(0))
191  o << s.f().get_k(0);
192  else
193  o << "-";
194  o << "; # Objective function value f(x) at final point\n";
195  // feas_kkt_err
196  o << left << setw(stat_w) << "feas_kkt_err" << "= "
197  << right << setw(val_w);
198  if(s.feas_kkt_err().updated_k(0))
199  o << s.feas_kkt_err().get_k(0);
200  else if(s.c().updated_k(0))
201  o << s.c().get_k(0).norm_inf();
202  else
203  o << "-";
204  o << "; # Feasibility error at final point (scaled ||c(x)||inf, feas_err_k)\n";
205  // opt_kkt_err
206  o << left << setw(stat_w) << "opt_kkt_err" << "= "
207  << right << setw(val_w);
208  if(s.opt_kkt_err().updated_k(0))
209  o << s.opt_kkt_err().get_k(0);
210  else if(s.rGL().updated_k(0))
211  o << s.rGL().get_k(0).norm_inf();
212  else if(s.rGL().updated_k(-1))
213  o << s.rGL().get_k(-1).norm_inf();
214  else
215  o << "-";
216  o << "; # Optimality error at final point (scaled ||rGL||inf, opt_err_k)\n";
217  // nact
218  o << left << setw(stat_w) << "nact" << "= "
219  << right << setw(val_w);
220  if(s.nu().updated_k(0))
221  o << s.nu().get_k(0).nz();
222  else if(s.nu().updated_k(-1))
223  o << s.nu().get_k(-1).nz();
224  else
225  o << "-";
226  o << "; # Number of total active constraints at the final point\n";
227  // nbasis_change
228  const IterQuantityAccess<index_type> &num_basis = s.num_basis();
229  const int lu_k = num_basis.last_updated();
230  o << left << setw(stat_w) << "nbasis_change" << "= "
231  << right << setw(val_w) << ( lu_k != IterQuantity::NONE_UPDATED
232  ? num_basis.get_k(lu_k)
233  : 0 )
234  << "; # Number of basis changes\n";
235  // nquasi_newton
236  o << left << setw(stat_w) << "nquasi_newton" << "= "
237  << right << setw(val_w) << num_QN_updates_
238  << "; # Number of quasi-newton updates\n";
239 
240 }
241 
242 } // end namespace MoochoPack
const ostream_ptr_t & get_output_stream() const
Get the output stream for statistics outputting.
Class for storing statistics about the Quasi-Newton updating.
T_To & dyn_cast(T_From &from)
rSQP Algorithm control class.
EUpdate
Set to this value if a statistic is not known.
void output_final(const Algorithm &algo, EAlgoReturn algo_return) const
Reduced space SQP state encapsulation interface.
size_t size_type
void output_iteration(const Algorithm &algo) const
NLPAlgoState & rsqp_state()
<<std aggr>="">> members for algo_cntr
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)