MoochoPack : Framework for Large-Scale Optimization Algorithms Version of the Day
MoochoPack_MoochoTrackerConsoleStd.cpp
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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include <assert.h>
00043 
00044 #include <iomanip>
00045 
00046 #include "MoochoPack_MoochoTrackerConsoleStd.hpp"
00047 #include "MoochoPack_NLPAlgoState.hpp"
00048 #include "MoochoPack_moocho_algo_conversion.hpp"
00049 #include "NLPInterfacePack_NLPFirstOrder.hpp"
00050 #include "AbstractLinAlgPack_Vector.hpp"
00051 #include "Teuchos_dyn_cast.hpp"
00052 
00053 namespace MoochoPack {
00054 
00055 using std::endl;
00056 using std::setw;
00057 using std::left;
00058 using std::right;
00059 using std::setprecision;
00060 
00061 // Static members
00062 int   MoochoTrackerConsoleStd::w_i2_     = 2;
00063 char  MoochoTrackerConsoleStd::ul_i2_[]  = "--";
00064 int   MoochoTrackerConsoleStd::w_i4_     = 4;
00065 char  MoochoTrackerConsoleStd::ul_i4_[]  = "----";
00066 int   MoochoTrackerConsoleStd::p2_       = 1;
00067 int   MoochoTrackerConsoleStd::w_p2_     = 8;
00068 char  MoochoTrackerConsoleStd::ul_p2_[]  = "--------";
00069 int   MoochoTrackerConsoleStd::p3_       = 2;
00070 int   MoochoTrackerConsoleStd::w_p3_     = 9;
00071 char  MoochoTrackerConsoleStd::ul_p3_[]  = "---------";
00072 
00073 MoochoTrackerConsoleStd::MoochoTrackerConsoleStd(
00074   const ostream_ptr_t&   o
00075   ,const ostream_ptr_t&  journal_out
00076   )
00077   :AlgorithmTracker(journal_out)
00078   ,o_(o)
00079   ,printed_lines_(NUM_PRINT_LINES)
00080 {}
00081 
00082 void MoochoTrackerConsoleStd::set_output_stream(const ostream_ptr_t& o)
00083 {
00084   o_ = o;
00085 }
00086 
00087 const MoochoTrackerConsoleStd::ostream_ptr_t&
00088 MoochoTrackerConsoleStd::get_output_stream() const
00089 {
00090   return o_;
00091 }
00092 
00093 void MoochoTrackerConsoleStd::initialize()
00094 {
00095   timer_.reset();
00096   timer_.start();
00097 }
00098 
00099 void MoochoTrackerConsoleStd::output_iteration(const Algorithm& p_algo) const
00100 {
00101   const NLPAlgo      &algo = rsqp_algo(p_algo);
00102   const NLPAlgoState &s    = algo.rsqp_state();
00103   const NLP          &nlp  = algo.nlp(); 
00104   
00105   const size_type
00106     m = nlp.m();
00107 
00108   const int
00109     nb = nlp.num_bounded_x();
00110 
00111   if(s.k() == 0) {
00112     print_top_header(s,algo);
00113     printed_lines_ = NUM_PRINT_LINES;
00114   }
00115   
00116   // Output the table's header
00117   if(printed_lines_ == NUM_PRINT_LINES) {
00118     printed_lines_ = 0;
00119     print_header(s,algo);
00120   }
00121 
00122   // ///////////////////////////////
00123   // Output a row for the iteration
00124   
00125   // Get a Quasi-Newton statistics.
00126   const QuasiNewtonStats  *quasi_newt_stats =
00127     ( quasi_newton_stats_.exists_in(s) && quasi_newton_stats_(s).updated_k(0)
00128       ? &quasi_newton_stats_(s).get_k(0)
00129       : NULL );
00130 
00131   std::ostream& o = this->o();
00132 
00133   // k
00134   o << " " << right << setw(w_i4_) << s.k();
00135   // f
00136   if( s.f().updated_k(0) )
00137     o << " " << setprecision(p3_) << right << setw(w_p3_) << s.f().get_k(0);
00138   else
00139     o << " " << right << setw(w_p3_) << "-";
00140   // ||c||s
00141   if( m && s.feas_kkt_err().updated_k(0) )
00142     o << " " << setprecision(p3_) << right << setw(w_p3_) << s.feas_kkt_err().get_k(0);
00143   else
00144     o << " " << right << setw(w_p3_) << "-";
00145   // ||rGL||s
00146   if( s.opt_kkt_err().updated_k(0) )
00147     o << " " << setprecision(p3_) << right << setw(w_p3_) << s.opt_kkt_err().get_k(0);
00148   else
00149     o << " " << right << setw(w_p3_) << "-";
00150   // QN
00151   if( quasi_newt_stats ) {
00152     o << " " << right << setw(w_i2_);
00153     switch( quasi_newt_stats->updated() ) {
00154       case QuasiNewtonStats::UNKNOWN:
00155         o << "-";
00156         break;
00157       case QuasiNewtonStats:: REINITIALIZED:
00158         o << "IN";
00159         break;
00160       case QuasiNewtonStats::DAMPENED_UPDATED:
00161         o << "DU";
00162         break;
00163       case QuasiNewtonStats::UPDATED:
00164         o << "UP";
00165         break;
00166       case QuasiNewtonStats::SKIPED:
00167         o << "SK";
00168         break;
00169       case QuasiNewtonStats::INDEF_SKIPED:
00170         o << "IS";
00171         break;
00172       default:
00173         TEUCHOS_TEST_FOR_EXCEPT(true);
00174     }
00175   }
00176   else {
00177     o << " " << right << setw(w_i2_) << "-";
00178   }
00179   // #act
00180   if(nb) {
00181     if( s.nu().updated_k(0) )
00182       o << " " << right << setw(w_i4_) << s.nu().get_k(0).nz();
00183     else
00184       o  << " " << right << setw(w_i4_) << "-";
00185   }
00186   // ||Ypy||2
00187   if( m && s.Ypy().updated_k(0) )
00188     o << " "<< setprecision(p2_)  << right << setw(w_p2_) << s.Ypy().get_k(0).norm_2();
00189   else
00190     o << " " << right << setw(w_p2_) << "-";
00191   // ||Zpz||2
00192   if( s.Zpz().updated_k(0) )
00193     o << " " << setprecision(p2_) << right << setw(w_p2_) << s.Zpz().get_k(0).norm_2();
00194   else
00195     o << " " << right << setw(w_p2_) << "-";
00196   // ||d||inf
00197   if( s.d().updated_k(0) )
00198     o << " " << setprecision(p2_) << right << setw(w_p2_) << s.d().get_k(0).norm_inf();
00199   else
00200     o << " " << right << setw(w_p2_) << "-";
00201   // alpha
00202   if( s.alpha().updated_k(0) )
00203     o << " " << setprecision(p2_) << right << setw(w_p2_) << s.alpha().get_k(0);
00204   else
00205     o << " " << right << setw(w_p2_) << "-";
00206   // time(sec)
00207   o << " " << setprecision(7) << right << setw(w_p3_) << timer_.read();
00208 
00209   o << std::endl;
00210 
00211   ++printed_lines_;
00212 }
00213 
00214 void MoochoTrackerConsoleStd::output_final( const Algorithm& p_algo
00215   , EAlgoReturn algo_return ) const
00216 {
00217   using Teuchos::dyn_cast;
00218 
00219   const NLPAlgo        &algo    = rsqp_algo(p_algo);
00220   const NLPAlgoState   &s       = algo.rsqp_state();
00221   const NLPObjGrad     &nlp     = dyn_cast<const NLPObjGrad>(algo.nlp()); 
00222   const NLPFirstOrder  *nlp_foi = dynamic_cast<const NLPFirstOrder*>(&nlp); 
00223   
00224   const size_type
00225     m = nlp.m();
00226 
00227   const int
00228     nb = nlp.num_bounded_x();
00229 
00230   std::ostream& o = this->o();
00231 
00232   // Output the table's header for the first iteration
00233   if(s.k() == 0) {
00234     print_top_header(s,algo);
00235     print_header(s,algo);
00236   }
00237   else {
00238     o
00239       << " " << right << ul_i4_    // "k"
00240       << " " << right << ul_p3_    // "f"
00241       << " " << right << ul_p3_    // "||c||s"
00242       << " " << right << ul_p3_    // "||rGL||s"
00243       << " " << right << ul_i2_    // "QN"
00244       ;
00245     if(nb)
00246       o << " " << right << ul_i4_; // "#act"
00247     o << endl;
00248   }
00249 
00250   // //////////////////////////////////////////
00251   // Output a row for the final iteration
00252   
00253   // Get a Quasi-Newton statistics.
00254   const QuasiNewtonStats  *quasi_newt_stats =
00255     ( quasi_newton_stats_.exists_in(s) && quasi_newton_stats_(s).updated_k(0)
00256       ? &quasi_newton_stats_(s).get_k(0)
00257       : NULL );
00258 
00259   // k
00260   o << " " << right << setw(w_i4_) << s.k();
00261   // f
00262   if( s.f().updated_k(0) )
00263     o << " " << setprecision(p3_) << right << setw(w_p3_) << s.f().get_k(0);
00264   else
00265     o << " " << right << setw(w_p3_) << "-";
00266   // ||c||s
00267   if( m && s.feas_kkt_err().updated_k(0) )
00268     o << " " << setprecision(p3_) << right << setw(w_p3_) << s.feas_kkt_err().get_k(0);
00269   else
00270     o << " " << right << setw(w_p3_) << "-";
00271   // ||rGL||s
00272   if( s.opt_kkt_err().updated_k(0) )
00273     o << " " << setprecision(p3_) << right << setw(w_p3_) << s.opt_kkt_err().get_k(0);
00274   else
00275     o << " " << right << setw(w_p3_) << "-";
00276   // QN
00277   if( quasi_newt_stats ) {
00278     o << " " << right << setw(w_i2_);
00279     switch( quasi_newt_stats->updated() ) {
00280       case QuasiNewtonStats::UNKNOWN:
00281         o << "-";
00282         break;
00283       case QuasiNewtonStats:: REINITIALIZED:
00284         o << "IN";
00285         break;
00286       case QuasiNewtonStats::DAMPENED_UPDATED:
00287         o << "DU";
00288         break;
00289       case QuasiNewtonStats::UPDATED:
00290         o << "UP";
00291         break;
00292       case QuasiNewtonStats::SKIPED:
00293         o << "SK";
00294         break;
00295       case QuasiNewtonStats::INDEF_SKIPED:
00296         o << "IS";
00297         break;
00298       default:
00299         TEUCHOS_TEST_FOR_EXCEPT(true);
00300     }
00301   }
00302   else {
00303     o << " " << right << setw(w_i2_) << "-";
00304   }
00305   // #act
00306   if(nb) {
00307     if( s.nu().updated_k(0) )
00308       o << " " << right << setw(w_i4_) << s.nu().get_k(0).nz();
00309     else
00310       o  << " " << right << setw(w_i4_) << "-";
00311   }
00312   // ||Ypy||2
00313   if( m && s.Ypy().updated_k(0) )
00314     o << " "<< setprecision(p2_)  << right << setw(w_p2_) << s.Ypy().get_k(0).norm_2();
00315   else
00316     o << " " << right << setw(w_p2_) << "-";
00317   // ||Zpz||2
00318   if( s.Zpz().updated_k(0) )
00319     o << " " << setprecision(p2_) << right << setw(w_p2_) << s.Zpz().get_k(0).norm_2();
00320   else
00321     o << " " << right << setw(w_p2_) << "-";
00322   // ||d||inf
00323   if( s.d().updated_k(0) )
00324     o << " " << setprecision(p2_) << right << setw(w_p2_) << s.d().get_k(0).norm_inf();
00325   else
00326     o << " " << right << setw(w_p2_) << "-";
00327   // alpha
00328   if( s.alpha().updated_k(0) )
00329     o << " " << setprecision(p2_) << right << setw(w_p2_) << s.alpha().get_k(0);
00330   else
00331     o << " " << right << setw(w_p2_) << "-";
00332   // time(sec)
00333   o << " " << setprecision(7) << right << setw(w_p3_) << timer_.read();
00334 
00335   o << endl;
00336 
00337   // Print total time
00338   o << setprecision(5) << "\nTotal time = " << timer_.read() << " sec\n";
00339 
00340   switch( algo_return ) {
00341     case IterationPack::TERMINATE_TRUE:
00342       o << "\nJackpot! You have found the solution!!!!!!\n";
00343       break;
00344     case IterationPack::TERMINATE_FALSE:
00345       o << "\nOops!  Not the solution.  Some error has occured!\n";
00346       break;
00347     case IterationPack::MAX_ITER_EXCEEDED:
00348       o << "\nOops!  Not the solution.  Maximum number of SQP iteration exceeded!\n";
00349       break;
00350     case IterationPack::MAX_RUN_TIME_EXCEEDED:
00351       o << "\nOops!  Not the solution.  Maximum runtime exceeded!\n";
00352       break;
00353     case IterationPack::INTERRUPTED_TERMINATE_TRUE:
00354       o << "\nJackpot? The user terminated the algorithm but said to return optimal!!!!!!\n";
00355       break;
00356     case IterationPack::INTERRUPTED_TERMINATE_FALSE:
00357       o << "\nOops!  Not the solution.  The user terminated the algorithm and said to return non-optimal!\n";
00358       break;
00359     default:
00360       TEUCHOS_TEST_FOR_EXCEPT(true);
00361   }
00362 
00363   o  << "\nNumber of function evaluations:\n"
00364     <<     "-------------------------------\n"
00365     << "f(x)  : " << nlp.num_f_evals() << endl
00366     << "c(x)  : " << ( m ? nlp.num_c_evals() : 0 ) << endl
00367     << "Gf(x) : " << nlp.num_Gf_evals() << endl
00368     << "Gc(x) : ";
00369   if(m){
00370     if( nlp_foi )
00371       o << nlp_foi->num_Gc_evals();
00372     else
00373       o << "?";
00374   }
00375   else {
00376     o << 0;
00377   }
00378   o << endl;
00379 }
00380 
00381 void MoochoTrackerConsoleStd::print_top_header(const NLPAlgoState &s
00382   , const NLPAlgo &algo) const
00383 {
00384   std::ostream& o = this->o();
00385 
00386   NLPAlgoState::space_c_ptr_t
00387     space_c = s.get_space_c();
00388 
00389   o  << "\n\n********************************\n"
00390     << "*** Start of rSQP Iterations ***\n"
00391     << "n = " << s.space_x().dim()
00392     << ", m = " << ( space_c.get() ? space_c->dim() : 0 )
00393     << ", nz = ";
00394   try {
00395     if(space_c.get()) {
00396       if( s.Gc().updated_k(0) )
00397         o  << s.Gc().get_k(0).nz() << endl;
00398       else
00399         o  << "?\n";
00400     }
00401     else {
00402       o  << 0 << endl;
00403     }
00404   }
00405   catch( const AlgorithmState::DoesNotExist& ) {
00406       o  << "?\n";
00407   }
00408   if( algo.nlp().scale_f() != 1.0 ) {
00409     o  << "f(x) is scaled by : " << algo.nlp().scale_f() << endl;
00410   }
00411 }
00412 
00413 void MoochoTrackerConsoleStd::print_header(const NLPAlgoState &s
00414   , const NLPAlgo &algo) const
00415 {
00416   std::ostream& o = this->o();
00417 
00418   const NLP  &nlp = algo.nlp(); 
00419 
00420   const int
00421     nb = nlp.num_bounded_x();
00422 
00423   o
00424     << endl
00425     << " " << left << setw(w_i4_) << "k"
00426     << " " << left << setw(w_p3_) << "f"
00427     << " " << left << setw(w_p3_) << "||c||s"
00428     << " " << left << setw(w_p3_) << "||rGL||s"
00429     << " " << left << setw(w_i2_) << "QN";
00430   if(nb)
00431     o << " " << left << setw(w_i4_) << "#act";
00432   o
00433     << " " << left << setw(w_p2_) << "||Ypy||2"
00434     << " " << left << setw(w_p2_) << "||Zpz||2"
00435     << " " << left << setw(w_p2_) << "||d||inf"
00436     << " " << left << setw(w_p2_) << "alpha"
00437     << " " << left << setw(w_p3_) << "time(sec)"
00438     << endl
00439     << " " << right << ul_i4_    // "k"
00440     << " " << right << ul_p3_    // "f"
00441     << " " << right << ul_p3_    // "||c||s"
00442     << " " << right << ul_p3_    // "||rGL||s"
00443     << " " << right << ul_i2_;   // "QN"
00444     if(nb)
00445       o << " " << right << ul_i4_; // "#act"
00446   o
00447     << " " << right << ul_p2_    // "||Ypy||2"
00448     << " " << right << ul_p2_    // "||Zpz||2"
00449     << " " << right << ul_p2_    // "||d||inf"
00450     << " " << right << ul_p2_    // "alpha"
00451     << " " << right << ul_p3_    // "time(s)"
00452     << endl;
00453 }
00454 
00455 } // end namespace MoochoPack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends