MoochoPack : Framework for Large-Scale Optimization Algorithms  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MoochoPack_MoochoTrackerXMLSummary.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 <fstream>
45 #include <iostream>
46 #include <iomanip>
47 #include <time.h>
48 
49 #include "MoochoPack_MoochoTrackerXMLSummary.hpp"
50 #include "MoochoPack_NLPAlgoState.hpp"
51 #include "MoochoPack_moocho_algo_conversion.hpp"
52 #include "NLPInterfacePack_NLPFirstOrder.hpp"
53 //#include "AbstractLinAlgPack_Vector.hpp"
54 //#include "AbstractLinAlgPack_MatrixSymOp.hpp"
55 #include "Teuchos_dyn_cast.hpp"
56 
57 using std::endl;
58 using std::setw;
59 
60 namespace MoochoPack {
61 
62 MoochoTrackerXMLSummary::MoochoTrackerXMLSummary(
63  const Teuchos::RCP<std::ostream> &journal_out
64  ,const std::string xml_filename
65  ,const std::string problem_name
66  ,const std::string algorithm_description
67  )
68  :AlgorithmTracker(journal_out)
69  ,obj_value_(0.0)
70  ,c_norm_value_(0.0)
71  ,xml_filename_(xml_filename)
72  ,problem_name_(problem_name)
73  ,algorithm_description_(algorithm_description)
74 {
75 }
76 
78 {
79 
80  using Teuchos::dyn_cast;
81 
82  const NLPAlgo &_algo = rsqp_algo(algo);
83  const NLPAlgoState &s = _algo.rsqp_state();
84  const NLPObjGrad &nlp = dyn_cast<const NLPObjGrad>(_algo.nlp());
85 
86  if (s.k() == 0) {
87  // first iteration...
88  // write out a dummy file that will be overwritten if the algorithm completes
89  // this way there will be an xml file even if the algorithm exceptions...
90  Teuchos::RCP<std::ofstream> xml_out = Teuchos::rcp( new std::ofstream(xml_filename_.c_str(), std::ios::out));
91  std::ofstream &out = *xml_out.get();
92 
93  // Print the Problem Element
94  open_problem_element(out, algo);
95 
96  char ind[] = " ";
97 
98  out << ind << "<Solution status=\"UNKNOWN_FAILURE\" objective_value=\"?\" constraint_norm=\"?\"/>\n";
99 
100  out << ind << "<Iterations overall=\"?\"/>\n";
101 
102  close_problem_element(out);
103 
104  out.close();
105  }
106 }
107 
109  , EAlgoReturn algo_return) const
110 {
111  using Teuchos::dyn_cast;
112 
113  const NLPAlgo &_algo = rsqp_algo(algo);
114  const NLPAlgoState &s =_algo.rsqp_state();
115  const NLPObjGrad &nlp = dyn_cast<const NLPObjGrad>(_algo.nlp());
116  const NLPFirstOrder *nlp_foi = dynamic_cast<const NLPFirstOrder*>(&nlp);
117 
118  const size_type
119  m = nlp.m();
120 
121  Teuchos::RCP<std::ofstream> xml_out = Teuchos::rcp( new std::ofstream(xml_filename_.c_str(), std::ios::out));
122  std::ofstream &out = *xml_out.get();
123 
124  // Print the Problem Element
125  open_problem_element(out, algo);
126 
127  char ind[] = " ";
128 
129  char soln_status[256];
130  switch (algo_return)
131  {
132  case IterationPack::TERMINATE_TRUE:
133  std::strcpy(soln_status, "SOLVED");
134  break;
135  case IterationPack::TERMINATE_FALSE:
136  std::strcpy(soln_status, "FAILED");
137  break;
138  case IterationPack::MAX_ITER_EXCEEDED:
139  std::strcpy(soln_status, "MAX_ITER");
140  break;
141  case IterationPack::MAX_RUN_TIME_EXCEEDED:
142  std::strcpy(soln_status, "MAX_RUN_TIME");
143  break;
144  case IterationPack::INTERRUPTED_TERMINATE_TRUE:
145  std::strcpy(soln_status, "INTERRUPTED_SOLVED");
146  break;
147  case IterationPack::INTERRUPTED_TERMINATE_FALSE:
148  std::strcpy(soln_status, "INTERRUPTED_FAILED");
149  break;
150  default:
151  std::strcpy(soln_status, "UNKNOWN_STATUS");
152  break;
153  }
154 
155  // Output the solution element
156  out << ind << "<Solution status=\"" << soln_status;
157 
158  if (s.f().updated_k(0)) {
159  out << "\" objective_value=\"" << s.f().get_k(0) << "\"";
160  }
161  else {
162  out << "\" objective_value=\"?\"";
163  }
164 
165  if (m && s.c().updated_k(0)) {
166  out << " constraint_norm=\"" << s.c().get_k(0).norm_inf() << "\"";
167  }
168  else {
169  out << " constraint_norm=\"?\"";
170  }
171 
172  out << "/>\n";
173 
174  // Output the Iterations element
175  out << ind << "<Iterations overall=\"" << s.k() << "\"/>\n";
176 
177  // Output the Evaluations element
178  out << ind << "<Evaluations>\n"
179  << ind << ind << "<Objective evaluations=\"" << nlp.num_f_evals() << "\"/>\n"
180  << ind << ind << "<Constraint evaluations=\"" << ( m ? nlp.num_c_evals() : 0 ) << "\"/>\n"
181  << ind << ind << "<Objective_Gradient evaluations=\"" << nlp.num_Gf_evals() << "\"/>\n"
182  << ind << ind << "<Constraint_Gradient evaluations=\"";
183 
184  if(m) {
185  if (nlp_foi) {
186  out << nlp_foi->num_Gc_evals();
187  }
188  else {
189  out << "?";
190  }
191  }
192  else {
193  out << 0;
194  }
195 
196  out << "\"/>\n"
197  << ind << "</Evaluations>\n";
198 
199  // Output the Timing element
200  /* out << ind << "<Timing>\n";
201  const int n = _algo.num_steps();
202  const int final_iter = s.k();
203  const int n_iters = final_iter+1;
204  std::vector<double>* step_times = new std::vector<double>[n_iters](n+1);
205  for (int k=0; k<n_iters; k++) {
206  _algo.get_step_times_k(k-(final_iter), &(step_times[k])[0]);
207  }
208 
209  for (int i=0; i<n+1; i++) {
210  if (i != n) {
211  out << ind << ind << "<Step name=\"" << _algo.get_step_name(i+1) << "\">\n";
212  }
213  else {
214  // overall step
215  out << ind << ind << "<Overall>\n";
216 
217  }
218 
219 
220  for (int k=0; k<final_iter; k++) {
221  out << ind << ind << ind << "<IterationTime iteration=\"" << k << "\" seconds=\"" << (step_times[k])[i] << "\"/>\n";
222  }
223  double total, average, min, max, percent;
224  _algo.get_final_step_stats(i, &total, &average, &min, &max, &percent);
225 
226  out << ind << ind << ind << "<TotalTime seconds=\"" << total << "\" percent=\"" << percent*100.0 << "\"/>\n"
227  << ind << ind << ind << "<AverageTime seconds=\"" << average << "\"/>\n"
228  << ind << ind << ind << "<MinTime seconds=\"" << min << "\"/>\n"
229  << ind << ind << ind << "<MaxTime seconds=\"" << max << "\"/>\n";
230 
231  if (i != n) {
232  out << ind << ind << "</Step>\n";
233  }
234  else {
235  // overall step
236  out << ind << ind << "</Overall>\n";
237 
238  }
239  }
240 
241  delete [] step_times;
242 
243  out << ind << "</Timing>\n";
244 
245  */
246  // Close the problem element
247  close_problem_element(out);
248 
249  out.close();
250 }
251 
252 
254 {
255  Teuchos::RCP<std::ofstream> xml_out = Teuchos::rcp( new std::ofstream(xml_filename_.c_str(), std::ios::out));
256  std::ofstream &out = *xml_out.get();
257 
258  char ind[] = " ";
259 
260  // get a string representation of the current date/time
261  time_t current_time = time(NULL);
262  char time_str[26];
263  std::strcpy(time_str, ctime(&current_time));
264  time_str[24]='\0';
265  out << "<Problem name=\"" << problem_name_ << "\" time=\"" << time_str << "\">\n";
266 
267  out << ind << "<Dimension n=\"?\" m=\"?\"/>\n";
268 
270  true, std::logic_error
271  ,"Error!, this function stopped compiling so RAB commented "
272  "it out on 2/4/2005. Sorry!"
273  );
274 
275  /* RAB: 2005/03/04: This stopped compiling so I disabled this for now
276 
277  // get the machine name - NOTE: this is not portable, may need to
278  // look into a way to do this on multiple platforms
279  char machine_name[256];
280  strcpy(machine_name, "UnknownMachine");
281  FILE* machine_file = popen("uname -n", "r");
282  if (machine_file) {
283  fread(machine_name, sizeof(char), 255, machine_file);
284  char* end = strchr(machine_name, '\n');
285  *end = '\0';
286  pclose(machine_file);
287  }
288 
289  out << ind << "<Machine name=\"" << machine_name << "\"/>\n";
290 
291  out << ind << "<Solution status=\"UNKNOWN_FAILURE\" objective_value=\"?\" constraint_norm=\"?\"/>\n";
292 
293  out << ind << "<Algorithm description=\"" << algorithm_description_ << "\"/>\n";
294 
295  out << ind << "<Iterations overall=\"?\"/>\n";
296 
297  out << "</Problem>\n";
298 
299  out.close();
300 
301  */
302 
303 }
304 
305 void MoochoTrackerXMLSummary::open_problem_element( std::ostream& out, const Algorithm& algo) const
306 {
307  if (out) {
308  const NLPAlgo &_algo = rsqp_algo(algo);
309  const NLP &nlp = _algo.nlp();
310 
311  char ind[] = " ";
312 
313 
315  true, std::logic_error
316  ,"Error!, this function stopped compiling so RAB commented "
317  "it out on 2/4/2005. Sorry!"
318  );
319 
320  /* RAB: 2005/03/04: This stopped compiling so I disabled this for now
321 
322  // get a string representation of the current date/time
323  time_t current_time = time(NULL);
324  char time_str[26];
325  strcpy(time_str, ctime(&current_time));
326  time_str[24]='\0';
327  out << "<Problem name=\"" << problem_name_ << "\" time=\"" << time_str << "\">\n";
328 
329  out << ind << "<Dimension n=\"" << nlp.n() << "\" m=\"" << nlp.m() << "\"/>\n";
330 
331  // get the machine name - NOTE: this is not portable, may need to
332  // look into a way to do this on multiple platforms
333  char machine_name[256];
334  strcpy(machine_name, "UnknownMachine");
335  FILE* machine_file = popen("uname -n", "r");
336  if (machine_file) {
337  fread(machine_name, sizeof(char), 255, machine_file);
338  char* end = strchr(machine_name, '\n');
339  *end = '\0';
340  pclose(machine_file);
341  }
342 
343  out << ind << "<Machine name=\"" << machine_name << "\"/>\n";
344  //out << ind << "<File location=\"" << file_location_ << "\"/>\n";
345 
346  out << ind << "<Algorithm description=\"" << algorithm_description_ << "\">\n";
347  // output some description of the algorithm and
348  // its options
349  out << ind << "</Algorithm>\n";
350 
351  */
352 
353  }
354 
355 }
356 
357 void MoochoTrackerXMLSummary::close_problem_element( std::ostream& out) const
358 {
359  if (out) {
360  out << "</Problem>\n";
361  }
362 }
363 
364 } // end namespace MoochoPack
void open_problem_element(std::ostream &out, const Algorithm &algo) const
Print the header to the output.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
T_To & dyn_cast(T_From &from)
rSQP Algorithm control class.
void output_final(const Algorithm &algo, EAlgoReturn algo_return) const
T * get() const
void output_iteration(const Algorithm &algo) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Reduced space SQP state encapsulation interface.
size_t size_type
NLPAlgoState & rsqp_state()
<<std aggr>="">> members for algo_cntr
void output_pre_file() const
Set the output stream for summary outputting.