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