MoochoPack_MoochoTrackerXMLSummary.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 <fstream>
00032 #include <iostream>
00033 #include <iomanip>
00034 #include <time.h>
00035 
00036 #include "MoochoPack_MoochoTrackerXMLSummary.hpp"
00037 #include "MoochoPack_NLPAlgoState.hpp"
00038 #include "MoochoPack_moocho_algo_conversion.hpp"
00039 #include "NLPInterfacePack_NLPFirstOrder.hpp"
00040 //#include "AbstractLinAlgPack_Vector.hpp"
00041 //#include "AbstractLinAlgPack_MatrixSymOp.hpp"
00042 #include "Teuchos_dyn_cast.hpp"
00043 
00044 using std::endl;
00045 using std::setw;
00046 
00047 namespace MoochoPack {
00048 
00049 MoochoTrackerXMLSummary::MoochoTrackerXMLSummary(
00050   const Teuchos::RCP<std::ostream> &journal_out
00051   ,const std::string xml_filename
00052   ,const std::string problem_name
00053   ,const std::string algorithm_description
00054   )
00055   :AlgorithmTracker(journal_out)
00056   ,obj_value_(0.0)
00057   ,c_norm_value_(0.0)
00058   ,xml_filename_(xml_filename)
00059   ,problem_name_(problem_name)
00060   ,algorithm_description_(algorithm_description)
00061 {
00062 } 
00063 
00064 void MoochoTrackerXMLSummary::output_iteration(const Algorithm& algo) const
00065 {
00066 
00067   using Teuchos::dyn_cast;
00068 
00069   const NLPAlgo         &_algo   = rsqp_algo(algo);
00070   const NLPAlgoState    &s       = _algo.rsqp_state();
00071   const NLPObjGrad      &nlp     = dyn_cast<const NLPObjGrad>(_algo.nlp()); 
00072 
00073   if (s.k() == 0) {
00074     // first iteration...
00075     // write out a dummy file that will be overwritten if the algorithm completes
00076     // this way there will be an xml file even if the algorithm exceptions...
00077     Teuchos::RCP<std::ofstream> xml_out = Teuchos::rcp( new std::ofstream(xml_filename_.c_str(), std::ios::out));
00078     std::ofstream &out = *xml_out.get();
00079     
00080     // Print the Problem Element
00081     open_problem_element(out, algo);
00082     
00083     char ind[] = "   ";
00084 
00085     out << ind << "<Solution status=\"UNKNOWN_FAILURE\" objective_value=\"?\" constraint_norm=\"?\"/>\n";
00086 
00087     out << ind << "<Iterations overall=\"?\"/>\n";
00088 
00089     close_problem_element(out);
00090 
00091     out.close();
00092   }
00093 }
00094 
00095 void MoochoTrackerXMLSummary::output_final(const Algorithm& algo
00096   , EAlgoReturn algo_return) const
00097 {
00098   using Teuchos::dyn_cast;
00099 
00100   const NLPAlgo            &_algo  = rsqp_algo(algo);
00101   const NLPAlgoState           &s      =_algo.rsqp_state();
00102   const NLPObjGrad      &nlp    = dyn_cast<const NLPObjGrad>(_algo.nlp()); 
00103   const NLPFirstOrder  *nlp_foi = dynamic_cast<const NLPFirstOrder*>(&nlp); 
00104 
00105   const size_type
00106     m = nlp.m();
00107 
00108   Teuchos::RCP<std::ofstream> xml_out = Teuchos::rcp( new std::ofstream(xml_filename_.c_str(), std::ios::out));
00109   std::ofstream &out = *xml_out.get();
00110 
00111   // Print the Problem Element
00112   open_problem_element(out, algo);
00113 
00114   char ind[] = "   ";
00115 
00116   char soln_status[256];
00117   switch (algo_return) 
00118     {
00119     case IterationPack::TERMINATE_TRUE:
00120       std::strcpy(soln_status, "SOLVED");
00121       break;
00122     case IterationPack::TERMINATE_FALSE:
00123       std::strcpy(soln_status, "FAILED");
00124       break;
00125     case IterationPack::MAX_ITER_EXCEEDED:
00126       std::strcpy(soln_status, "MAX_ITER");
00127       break;
00128     case IterationPack::MAX_RUN_TIME_EXCEEDED:
00129       std::strcpy(soln_status, "MAX_RUN_TIME");
00130       break;
00131     case IterationPack::INTERRUPTED_TERMINATE_TRUE:
00132       std::strcpy(soln_status, "INTERRUPTED_SOLVED");
00133       break;
00134     case IterationPack::INTERRUPTED_TERMINATE_FALSE:
00135       std::strcpy(soln_status, "INTERRUPTED_FAILED");
00136       break;
00137     default:
00138       std::strcpy(soln_status, "UNKNOWN_STATUS");
00139       break;
00140     }
00141 
00142   // Output the solution element
00143   out << ind << "<Solution status=\"" << soln_status;
00144 
00145   if (s.f().updated_k(0)) {
00146     out << "\" objective_value=\"" << s.f().get_k(0) << "\"";
00147   }
00148   else {
00149     out << "\" objective_value=\"?\"";    
00150   }
00151 
00152   if (m && s.c().updated_k(0)) {
00153     out << " constraint_norm=\"" << s.c().get_k(0).norm_inf() << "\"";
00154   }
00155   else {
00156     out << " constraint_norm=\"?\"";
00157   }
00158     
00159   out << "/>\n";
00160 
00161   // Output the Iterations element
00162   out << ind << "<Iterations overall=\"" << s.k() << "\"/>\n";
00163 
00164   // Output the Evaluations element
00165   out << ind << "<Evaluations>\n"
00166     << ind << ind << "<Objective evaluations=\"" << nlp.num_f_evals() << "\"/>\n"
00167       << ind << ind << "<Constraint evaluations=\"" << ( m ? nlp.num_c_evals() : 0 ) << "\"/>\n"
00168       << ind << ind << "<Objective_Gradient evaluations=\"" << nlp.num_Gf_evals() << "\"/>\n"
00169     << ind << ind << "<Constraint_Gradient evaluations=\"";
00170 
00171   if(m) {
00172     if (nlp_foi) {
00173       out << nlp_foi->num_Gc_evals();
00174     }
00175     else {
00176       out << "?";
00177     }
00178   }
00179   else {
00180     out << 0;
00181   }
00182 
00183   out << "\"/>\n"
00184     << ind << "</Evaluations>\n";
00185 
00186   // Output the Timing element
00187   /*  out << ind << "<Timing>\n";
00188   const int n = _algo.num_steps();
00189   const int final_iter = s.k();
00190   const int n_iters = final_iter+1;
00191   std::vector<double>* step_times = new std::vector<double>[n_iters](n+1);
00192   for (int k=0; k<n_iters; k++) {
00193     _algo.get_step_times_k(k-(final_iter), &(step_times[k])[0]);
00194     }
00195 
00196   for (int i=0; i<n+1; i++) {
00197     if (i != n) {
00198       out << ind << ind << "<Step name=\"" << _algo.get_step_name(i+1) << "\">\n";
00199     }
00200     else {
00201       // overall step
00202     out << ind << ind << "<Overall>\n";
00203       
00204     }
00205 
00206       
00207     for (int k=0; k<final_iter; k++) {
00208       out << ind << ind << ind << "<IterationTime iteration=\"" << k << "\" seconds=\"" << (step_times[k])[i] << "\"/>\n";
00209     }
00210     double total, average, min, max, percent;
00211     _algo.get_final_step_stats(i, &total, &average, &min, &max, &percent);
00212     
00213     out << ind << ind << ind << "<TotalTime seconds=\"" << total << "\" percent=\"" << percent*100.0 << "\"/>\n"
00214       << ind << ind << ind << "<AverageTime seconds=\"" << average << "\"/>\n"
00215       << ind << ind << ind << "<MinTime seconds=\"" << min << "\"/>\n"
00216       << ind << ind << ind << "<MaxTime seconds=\"" << max << "\"/>\n";
00217 
00218     if (i != n) {
00219       out << ind << ind << "</Step>\n";
00220     }
00221     else {
00222       // overall step
00223     out << ind << ind << "</Overall>\n";
00224       
00225     }
00226   }
00227 
00228   delete [] step_times;
00229       
00230   out << ind << "</Timing>\n";
00231 
00232   */
00233   // Close the problem element
00234   close_problem_element(out);
00235 
00236   out.close();
00237 }
00238 
00239 
00240 void MoochoTrackerXMLSummary::output_pre_file() const
00241 {
00242   Teuchos::RCP<std::ofstream> xml_out = Teuchos::rcp( new std::ofstream(xml_filename_.c_str(), std::ios::out));
00243   std::ofstream &out = *xml_out.get();
00244   
00245   char ind[] = "   ";
00246   
00247   // get a string representation of the current date/time
00248   time_t current_time = time(NULL);
00249   char time_str[26];
00250   std::strcpy(time_str, ctime(&current_time));
00251   time_str[24]='\0';
00252   out << "<Problem name=\"" << problem_name_  << "\" time=\"" << time_str << "\">\n";
00253 
00254   out << ind << "<Dimension n=\"?\" m=\"?\"/>\n";
00255 
00256   TEST_FOR_EXCEPTION(
00257     true, std::logic_error
00258     ,"Error!, this function stopped compiling so RAB commented "
00259     "it out on 2/4/2005.  Sorry!"
00260     );
00261 
00262   /* RAB: 2005/03/04: This stopped compiling so I disabled this for now
00263   
00264   // get the machine name - NOTE: this is not portable, may need to
00265   // look into a way to do this on multiple platforms
00266   char machine_name[256];
00267   strcpy(machine_name, "UnknownMachine");
00268   FILE* machine_file = popen("uname -n", "r");
00269   if (machine_file) {
00270     fread(machine_name, sizeof(char), 255, machine_file);
00271     char* end = strchr(machine_name, '\n');
00272     *end = '\0';
00273     pclose(machine_file);
00274   }
00275   
00276   out << ind << "<Machine name=\"" << machine_name << "\"/>\n";
00277 
00278   out << ind << "<Solution status=\"UNKNOWN_FAILURE\" objective_value=\"?\" constraint_norm=\"?\"/>\n";
00279   
00280   out << ind << "<Algorithm description=\"" << algorithm_description_ << "\"/>\n";
00281 
00282   out << ind << "<Iterations overall=\"?\"/>\n";
00283 
00284   out << "</Problem>\n";
00285     
00286   out.close();  
00287 
00288   */
00289 
00290 }
00291 
00292 void MoochoTrackerXMLSummary::open_problem_element( std::ostream& out, const Algorithm& algo) const
00293 {
00294   if (out) {
00295     const NLPAlgo  &_algo  = rsqp_algo(algo);
00296     const NLP      &nlp    = _algo.nlp(); 
00297 
00298     char ind[] = "   ";
00299 
00300 
00301   TEST_FOR_EXCEPTION(
00302     true, std::logic_error
00303     ,"Error!, this function stopped compiling so RAB commented "
00304     "it out on 2/4/2005.  Sorry!"
00305     );
00306 
00307   /* RAB: 2005/03/04: This stopped compiling so I disabled this for now
00308 
00309     // get a string representation of the current date/time
00310     time_t current_time = time(NULL);
00311     char time_str[26];
00312     strcpy(time_str, ctime(&current_time));
00313     time_str[24]='\0';
00314     out << "<Problem name=\"" << problem_name_  << "\" time=\"" << time_str << "\">\n";
00315 
00316     out << ind << "<Dimension n=\"" << nlp.n() << "\" m=\"" << nlp.m() << "\"/>\n";
00317 
00318     // get the machine name - NOTE: this is not portable, may need to
00319     // look into a way to do this on multiple platforms
00320     char machine_name[256];
00321     strcpy(machine_name, "UnknownMachine");
00322     FILE* machine_file = popen("uname -n", "r");
00323     if (machine_file) {
00324       fread(machine_name, sizeof(char), 255, machine_file);
00325       char* end = strchr(machine_name, '\n');
00326       *end = '\0';
00327       pclose(machine_file);
00328     }
00329 
00330     out << ind << "<Machine name=\"" << machine_name << "\"/>\n";
00331     //out << ind << "<File location=\"" << file_location_ << "\"/>\n";
00332     
00333     out << ind << "<Algorithm description=\"" << algorithm_description_ << "\">\n";
00334     // output some description of the algorithm and 
00335     // its options
00336     out << ind << "</Algorithm>\n";
00337 
00338   */
00339 
00340   }
00341 
00342 }
00343 
00344 void MoochoTrackerXMLSummary::close_problem_element( std::ostream& out) const
00345 {
00346   if (out) {
00347     out << "</Problem>\n";
00348   }
00349 }
00350 
00351 } // end namespace MoochoPack

Generated on Tue Jul 13 09:30:53 2010 for MOOCHO (Single Doxygen Collection) by  doxygen 1.4.7