MoochoPack_MoochoTrackerConsoleStd.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include <assert.h>
00030 
00031 #include <iomanip>
00032 
00033 #include "MoochoPack_MoochoTrackerConsoleStd.hpp"
00034 #include "MoochoPack_NLPAlgoState.hpp"
00035 #include "MoochoPack_moocho_algo_conversion.hpp"
00036 #include "NLPInterfacePack_NLPFirstOrder.hpp"
00037 #include "AbstractLinAlgPack_Vector.hpp"
00038 #include "Teuchos_dyn_cast.hpp"
00039 
00040 namespace MoochoPack {
00041 
00042 using std::endl;
00043 using std::setw;
00044 using std::left;
00045 using std::right;
00046 using std::setprecision;
00047 
00048 // Static members
00049 int   MoochoTrackerConsoleStd::w_i2_     = 2;
00050 char  MoochoTrackerConsoleStd::ul_i2_[]  = "--";
00051 int   MoochoTrackerConsoleStd::w_i4_     = 4;
00052 char  MoochoTrackerConsoleStd::ul_i4_[]  = "----";
00053 int   MoochoTrackerConsoleStd::p2_       = 1;
00054 int   MoochoTrackerConsoleStd::w_p2_     = 8;
00055 char  MoochoTrackerConsoleStd::ul_p2_[]  = "--------";
00056 int   MoochoTrackerConsoleStd::p3_       = 2;
00057 int   MoochoTrackerConsoleStd::w_p3_     = 9;
00058 char  MoochoTrackerConsoleStd::ul_p3_[]  = "---------";
00059 
00060 MoochoTrackerConsoleStd::MoochoTrackerConsoleStd(
00061   const ostream_ptr_t&   o
00062   ,const ostream_ptr_t&  journal_out
00063   )
00064   :AlgorithmTracker(journal_out)
00065   ,o_(o)
00066   ,printed_lines_(NUM_PRINT_LINES)
00067 {}
00068 
00069 void MoochoTrackerConsoleStd::set_output_stream(const ostream_ptr_t& o)
00070 {
00071   o_ = o;
00072 }
00073 
00074 const MoochoTrackerConsoleStd::ostream_ptr_t&
00075 MoochoTrackerConsoleStd::get_output_stream() const
00076 {
00077   return o_;
00078 }
00079 
00080 void MoochoTrackerConsoleStd::initialize()
00081 {
00082   timer_.reset();
00083   timer_.start();
00084 }
00085 
00086 void MoochoTrackerConsoleStd::output_iteration(const Algorithm& p_algo) const
00087 {
00088   const NLPAlgo      &algo = rsqp_algo(p_algo);
00089   const NLPAlgoState &s    = algo.rsqp_state();
00090   const NLP          &nlp  = algo.nlp(); 
00091   
00092   const size_type
00093     m = nlp.m();
00094 
00095   const int
00096     nb = nlp.num_bounded_x();
00097 
00098   if(s.k() == 0) {
00099     print_top_header(s,algo);
00100     printed_lines_ = NUM_PRINT_LINES;
00101   }
00102   
00103   // Output the table's header
00104   if(printed_lines_ == NUM_PRINT_LINES) {
00105     printed_lines_ = 0;
00106     print_header(s,algo);
00107   }
00108 
00109   // ///////////////////////////////
00110   // Output a row for the iteration
00111   
00112   // Get a Quasi-Newton statistics.
00113   const QuasiNewtonStats  *quasi_newt_stats =
00114     ( quasi_newton_stats_.exists_in(s) && quasi_newton_stats_(s).updated_k(0)
00115       ? &quasi_newton_stats_(s).get_k(0)
00116       : NULL );
00117 
00118   std::ostream& o = this->o();
00119 
00120   // k
00121   o << " " << right << setw(w_i4_) << s.k();
00122   // f
00123   if( s.f().updated_k(0) )
00124     o << " " << setprecision(p3_) << right << setw(w_p3_) << s.f().get_k(0);
00125   else
00126     o << " " << right << setw(w_p3_) << "-";
00127   // ||c||s
00128   if( m && s.feas_kkt_err().updated_k(0) )
00129     o << " " << setprecision(p3_) << right << setw(w_p3_) << s.feas_kkt_err().get_k(0);
00130   else
00131     o << " " << right << setw(w_p3_) << "-";
00132   // ||rGL||s
00133   if( s.opt_kkt_err().updated_k(0) )
00134     o << " " << setprecision(p3_) << right << setw(w_p3_) << s.opt_kkt_err().get_k(0);
00135   else
00136     o << " " << right << setw(w_p3_) << "-";
00137   // QN
00138   if( quasi_newt_stats ) {
00139     o << " " << right << setw(w_i2_);
00140     switch( quasi_newt_stats->updated() ) {
00141       case QuasiNewtonStats::UNKNOWN:
00142         o << "-";
00143         break;
00144       case QuasiNewtonStats:: REINITIALIZED:
00145         o << "IN";
00146         break;
00147       case QuasiNewtonStats::DAMPENED_UPDATED:
00148         o << "DU";
00149         break;
00150       case QuasiNewtonStats::UPDATED:
00151         o << "UP";
00152         break;
00153       case QuasiNewtonStats::SKIPED:
00154         o << "SK";
00155         break;
00156       case QuasiNewtonStats::INDEF_SKIPED:
00157         o << "IS";
00158         break;
00159       default:
00160         TEST_FOR_EXCEPT(true);
00161     }
00162   }
00163   else {
00164     o << " " << right << setw(w_i2_) << "-";
00165   }
00166   // #act
00167   if(nb) {
00168     if( s.nu().updated_k(0) )
00169       o << " " << right << setw(w_i4_) << s.nu().get_k(0).nz();
00170     else
00171       o  << " " << right << setw(w_i4_) << "-";
00172   }
00173   // ||Ypy||2
00174   if( m && s.Ypy().updated_k(0) )
00175     o << " "<< setprecision(p2_)  << right << setw(w_p2_) << s.Ypy().get_k(0).norm_2();
00176   else
00177     o << " " << right << setw(w_p2_) << "-";
00178   // ||Zpz||2
00179   if( s.Zpz().updated_k(0) )
00180     o << " " << setprecision(p2_) << right << setw(w_p2_) << s.Zpz().get_k(0).norm_2();
00181   else
00182     o << " " << right << setw(w_p2_) << "-";
00183   // ||d||inf
00184   if( s.d().updated_k(0) )
00185     o << " " << setprecision(p2_) << right << setw(w_p2_) << s.d().get_k(0).norm_inf();
00186   else
00187     o << " " << right << setw(w_p2_) << "-";
00188   // alpha
00189   if( s.alpha().updated_k(0) )
00190     o << " " << setprecision(p2_) << right << setw(w_p2_) << s.alpha().get_k(0);
00191   else
00192     o << " " << right << setw(w_p2_) << "-";
00193   // time(sec)
00194   o << " " << setprecision(7) << right << setw(w_p3_) << timer_.read();
00195 
00196   o << std::endl;
00197 
00198   ++printed_lines_;
00199 }
00200 
00201 void MoochoTrackerConsoleStd::output_final( const Algorithm& p_algo
00202   , EAlgoReturn algo_return ) const
00203 {
00204   using Teuchos::dyn_cast;
00205 
00206   const NLPAlgo        &algo    = rsqp_algo(p_algo);
00207   const NLPAlgoState   &s       = algo.rsqp_state();
00208   const NLPObjGrad     &nlp     = dyn_cast<const NLPObjGrad>(algo.nlp()); 
00209   const NLPFirstOrder  *nlp_foi = dynamic_cast<const NLPFirstOrder*>(&nlp); 
00210   
00211   const size_type
00212     m = nlp.m();
00213 
00214   const int
00215     nb = nlp.num_bounded_x();
00216 
00217   std::ostream& o = this->o();
00218 
00219   // Output the table's header for the first iteration
00220   if(s.k() == 0) {
00221     print_top_header(s,algo);
00222     print_header(s,algo);
00223   }
00224   else {
00225     o
00226       << " " << right << ul_i4_    // "k"
00227       << " " << right << ul_p3_    // "f"
00228       << " " << right << ul_p3_    // "||c||s"
00229       << " " << right << ul_p3_    // "||rGL||s"
00230       << " " << right << ul_i2_    // "QN"
00231       ;
00232     if(nb)
00233       o << " " << right << ul_i4_; // "#act"
00234     o << endl;
00235   }
00236 
00237   // //////////////////////////////////////////
00238   // Output a row for the final iteration
00239   
00240   // Get a Quasi-Newton statistics.
00241   const QuasiNewtonStats  *quasi_newt_stats =
00242     ( quasi_newton_stats_.exists_in(s) && quasi_newton_stats_(s).updated_k(0)
00243       ? &quasi_newton_stats_(s).get_k(0)
00244       : NULL );
00245 
00246   // k
00247   o << " " << right << setw(w_i4_) << s.k();
00248   // f
00249   if( s.f().updated_k(0) )
00250     o << " " << setprecision(p3_) << right << setw(w_p3_) << s.f().get_k(0);
00251   else
00252     o << " " << right << setw(w_p3_) << "-";
00253   // ||c||s
00254   if( m && s.feas_kkt_err().updated_k(0) )
00255     o << " " << setprecision(p3_) << right << setw(w_p3_) << s.feas_kkt_err().get_k(0);
00256   else
00257     o << " " << right << setw(w_p3_) << "-";
00258   // ||rGL||s
00259   if( s.opt_kkt_err().updated_k(0) )
00260     o << " " << setprecision(p3_) << right << setw(w_p3_) << s.opt_kkt_err().get_k(0);
00261   else
00262     o << " " << right << setw(w_p3_) << "-";
00263   // QN
00264   if( quasi_newt_stats ) {
00265     o << " " << right << setw(w_i2_);
00266     switch( quasi_newt_stats->updated() ) {
00267       case QuasiNewtonStats::UNKNOWN:
00268         o << "-";
00269         break;
00270       case QuasiNewtonStats:: REINITIALIZED:
00271         o << "IN";
00272         break;
00273       case QuasiNewtonStats::DAMPENED_UPDATED:
00274         o << "DU";
00275         break;
00276       case QuasiNewtonStats::UPDATED:
00277         o << "UP";
00278         break;
00279       case QuasiNewtonStats::SKIPED:
00280         o << "SK";
00281         break;
00282       case QuasiNewtonStats::INDEF_SKIPED:
00283         o << "IS";
00284         break;
00285       default:
00286         TEST_FOR_EXCEPT(true);
00287     }
00288   }
00289   else {
00290     o << " " << right << setw(w_i2_) << "-";
00291   }
00292   // #act
00293   if(nb) {
00294     if( s.nu().updated_k(0) )
00295       o << " " << right << setw(w_i4_) << s.nu().get_k(0).nz();
00296     else
00297       o  << " " << right << setw(w_i4_) << "-";
00298   }
00299   // ||Ypy||2
00300   if( m && s.Ypy().updated_k(0) )
00301     o << " "<< setprecision(p2_)  << right << setw(w_p2_) << s.Ypy().get_k(0).norm_2();
00302   else
00303     o << " " << right << setw(w_p2_) << "-";
00304   // ||Zpz||2
00305   if( s.Zpz().updated_k(0) )
00306     o << " " << setprecision(p2_) << right << setw(w_p2_) << s.Zpz().get_k(0).norm_2();
00307   else
00308     o << " " << right << setw(w_p2_) << "-";
00309   // ||d||inf
00310   if( s.d().updated_k(0) )
00311     o << " " << setprecision(p2_) << right << setw(w_p2_) << s.d().get_k(0).norm_inf();
00312   else
00313     o << " " << right << setw(w_p2_) << "-";
00314   // alpha
00315   if( s.alpha().updated_k(0) )
00316     o << " " << setprecision(p2_) << right << setw(w_p2_) << s.alpha().get_k(0);
00317   else
00318     o << " " << right << setw(w_p2_) << "-";
00319   // time(sec)
00320   o << " " << setprecision(7) << right << setw(w_p3_) << timer_.read();
00321 
00322   o << endl;
00323 
00324   // Print total time
00325   o << setprecision(5) << "\nTotal time = " << timer_.read() << " sec\n";
00326 
00327   switch( algo_return ) {
00328     case IterationPack::TERMINATE_TRUE:
00329       o << "\nJackpot! You have found the solution!!!!!!\n";
00330       break;
00331     case IterationPack::TERMINATE_FALSE:
00332       o << "\nOops!  Not the solution.  Some error has occured!\n";
00333       break;
00334     case IterationPack::MAX_ITER_EXCEEDED:
00335       o << "\nOops!  Not the solution.  Maximum number of SQP iteration exceeded!\n";
00336       break;
00337     case IterationPack::MAX_RUN_TIME_EXCEEDED:
00338       o << "\nOops!  Not the solution.  Maximum runtime exceeded!\n";
00339       break;
00340     case IterationPack::INTERRUPTED_TERMINATE_TRUE:
00341       o << "\nJackpot? The user terminated the algorithm but said to return optimal!!!!!!\n";
00342       break;
00343     case IterationPack::INTERRUPTED_TERMINATE_FALSE:
00344       o << "\nOops!  Not the solution.  The user terminated the algorithm and said to return non-optimal!\n";
00345       break;
00346     default:
00347       TEST_FOR_EXCEPT(true);
00348   }
00349 
00350   o  << "\nNumber of function evaluations:\n"
00351     <<     "-------------------------------\n"
00352     << "f(x)  : " << nlp.num_f_evals() << endl
00353     << "c(x)  : " << ( m ? nlp.num_c_evals() : 0 ) << endl
00354     << "Gf(x) : " << nlp.num_Gf_evals() << endl
00355     << "Gc(x) : ";
00356   if(m){
00357     if( nlp_foi )
00358       o << nlp_foi->num_Gc_evals();
00359     else
00360       o << "?";
00361   }
00362   else {
00363     o << 0;
00364   }
00365   o << endl;
00366 }
00367 
00368 void MoochoTrackerConsoleStd::print_top_header(const NLPAlgoState &s
00369   , const NLPAlgo &algo) const
00370 {
00371   std::ostream& o = this->o();
00372 
00373   NLPAlgoState::space_c_ptr_t
00374     space_c = s.get_space_c();
00375 
00376   o  << "\n\n********************************\n"
00377     << "*** Start of rSQP Iterations ***\n"
00378     << "n = " << s.space_x().dim()
00379     << ", m = " << ( space_c.get() ? space_c->dim() : 0 )
00380     << ", nz = ";
00381   try {
00382     if(space_c.get()) {
00383       if( s.Gc().updated_k(0) )
00384         o  << s.Gc().get_k(0).nz() << endl;
00385       else
00386         o  << "?\n";
00387     }
00388     else {
00389       o  << 0 << endl;
00390     }
00391   }
00392   catch( const AlgorithmState::DoesNotExist& ) {
00393       o  << "?\n";
00394   }
00395   if( algo.nlp().scale_f() != 1.0 ) {
00396     o  << "f(x) is scaled by : " << algo.nlp().scale_f() << endl;
00397   }
00398 }
00399 
00400 void MoochoTrackerConsoleStd::print_header(const NLPAlgoState &s
00401   , const NLPAlgo &algo) const
00402 {
00403   std::ostream& o = this->o();
00404 
00405   const NLP  &nlp = algo.nlp(); 
00406 
00407   const int
00408     nb = nlp.num_bounded_x();
00409 
00410   o
00411     << endl
00412     << " " << left << setw(w_i4_) << "k"
00413     << " " << left << setw(w_p3_) << "f"
00414     << " " << left << setw(w_p3_) << "||c||s"
00415     << " " << left << setw(w_p3_) << "||rGL||s"
00416     << " " << left << setw(w_i2_) << "QN";
00417   if(nb)
00418     o << " " << left << setw(w_i4_) << "#act";
00419   o
00420     << " " << left << setw(w_p2_) << "||Ypy||2"
00421     << " " << left << setw(w_p2_) << "||Zpz||2"
00422     << " " << left << setw(w_p2_) << "||d||inf"
00423     << " " << left << setw(w_p2_) << "alpha"
00424     << " " << left << setw(w_p3_) << "time(sec)"
00425     << endl
00426     << " " << right << ul_i4_    // "k"
00427     << " " << right << ul_p3_    // "f"
00428     << " " << right << ul_p3_    // "||c||s"
00429     << " " << right << ul_p3_    // "||rGL||s"
00430     << " " << right << ul_i2_;   // "QN"
00431     if(nb)
00432       o << " " << right << ul_i4_; // "#act"
00433   o
00434     << " " << right << ul_p2_    // "||Ypy||2"
00435     << " " << right << ul_p2_    // "||Zpz||2"
00436     << " " << right << ul_p2_    // "||d||inf"
00437     << " " << right << ul_p2_    // "alpha"
00438     << " " << right << ul_p3_    // "time(s)"
00439     << endl;
00440 }
00441 
00442 } // end namespace MoochoPack

Generated on Tue Oct 20 12:51:47 2009 for MOOCHO (Single Doxygen Collection) by doxygen 1.4.7