MoochoPack : Framework for Large-Scale Optimization Algorithms  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MoochoPack_MoochoSolver.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 "MoochoPack_MoochoSolver.hpp"
43 
44 #include "MoochoPack_NLPAlgoConfigMamaJama.hpp"
45 #include "MoochoPack_NLPAlgoConfigIP.hpp"
46 
47 #include "MoochoPack_NLPSolverClientInterfaceSetOptions.hpp"
48 #include "MoochoPack_NLPAlgoClientInterface.hpp"
49 #include "MoochoPack_NLPAlgoContainer.hpp"
50 #include "MoochoPack_NLPAlgoState.hpp"
51 #include "MoochoPack_MoochoTrackerSummaryStd.hpp"
52 #include "MoochoPack_MoochoTrackerConsoleStd.hpp"
53 #include "MoochoPack_MoochoTrackerStatsStd.hpp"
54 #include "IterationPack_AlgorithmTrackerComposite.hpp"
55 #include "NLPInterfacePack_NLPFirstOrder.hpp"
56 #include "NLPInterfacePack_NLPDirect.hpp"
57 #include "NLPInterfacePack_test_nlp_first_order.hpp"
58 #include "NLPInterfacePack_test_nlp_direct.hpp"
59 #include "OptionsFromStreamPack_OptionsFromStream.hpp"
60 #include "StopWatchPack_stopwatch.hpp"
61 #include "OptionsFromStreamPack_StringToIntMap.hpp"
62 #include "OptionsFromStreamPack_StringToBool.hpp"
63 #include "Teuchos_Workspace.hpp"
64 #include "Teuchos_Assert.hpp"
65 #include "Teuchos_oblackholestream.hpp"
66 #include "Teuchos_VerboseObject.hpp"
68 #include "Teuchos_TimeMonitor.hpp"
69 #include "Teuchos_Utils.hpp"
70 
71 namespace MoochoPack {
72 
73 // Initialization and algorithm configuration
74 
76  const std::string &options_file_name
77  ,const std::string &extra_options_str
78  )
79  :reconfig_solver_(true)
80  ,workspace_MB_(-1.0)
81  ,obj_scale_(1.0)
82  ,test_nlp_(true)
83  ,print_algo_(true)
84  ,algo_timing_(true)
85  ,generate_stats_file_(false)
86  ,print_opt_grp_not_accessed_(true)
87  ,throw_exceptions_(false)
88  ,output_file_tag_("")
89  ,do_console_outputting_(true)
90  ,do_summary_outputting_(true)
91  ,do_journal_outputting_(true)
92  ,do_algo_outputting_(true)
93  ,configuration_(MAMA_JAMA)
94  ,output_to_black_hole_(OUTPUT_TO_BLACK_HOLE_DEFAULT)
95  ,file_context_postfix_("")
96  ,file_proc_postfix_("")
97 {
98 
101  error_out_used_ = defaultOut;
102 
103  set_output_context("");
104 
105  commandLineOptionsFromStreamProcessor_.options_file_name_opt_name(
106  "moocho-options-file");
107  commandLineOptionsFromStreamProcessor_.options_file_name_opt_doc(
108  "Set the name of the MOOCHO options file in the OptionsFromStream format."
109  " File is ignored if it does not exist!");
110  commandLineOptionsFromStreamProcessor_.options_file_name(options_file_name);
111 
112  commandLineOptionsFromStreamProcessor_.extra_options_str_opt_name(
113  "moocho-extra-options");
114  commandLineOptionsFromStreamProcessor_.extra_options_str_opt_doc(
115  "Extra MOOCHO options specified in the format"
116  " \"OptGrp1{name1=val1,...,namen=valn}:OptGr2{name1=val1,...,namen=valn}:...\"");
117  commandLineOptionsFromStreamProcessor_.extra_options_str(extra_options_str);
118 
119 }
120 
123 {
124  return commandLineOptionsFromStreamProcessor_;
125 }
126 
129 {
130  return commandLineOptionsFromStreamProcessor_;
131 }
132 
135  )
136 {
137  commandLineOptionsFromStreamProcessor_.setup_commandline_processor(clp);
138 }
139 
141  const std::string &file_context_postfix
142  ,EOutputToBlackHole output_to_black_hole
143  ,const int procRank_in
144  ,const int numProcs_in
145  )
146 {
147 
148  file_context_postfix_ = file_context_postfix;
149 
150  output_to_black_hole_ = output_to_black_hole;
151 
154 
155  const int procRank
156  = ( procRank_in >= 0 ? procRank_in : Teuchos::GlobalMPISession::getRank() );
157  const int numProcs
158  = ( numProcs_in > 0 ? numProcs_in : Teuchos::GlobalMPISession::getNProc() );
159 
160  if(output_to_black_hole_==OUTPUT_TO_BLACK_HOLE_DEFAULT) {
161  if( numProcs > 1 && defaultOut->getOutputToRootOnly() >= 0 ) {
162  output_to_black_hole_
163  = ( defaultOut->getOutputToRootOnly()==procRank
164  ? OUTPUT_TO_BLACK_HOLE_FALSE
165  : OUTPUT_TO_BLACK_HOLE_TRUE
166  );
167  }
168  else {
169  output_to_black_hole_ = OUTPUT_TO_BLACK_HOLE_FALSE;
170  }
171  }
172 
173  if( numProcs > 1 ) {
174  file_proc_postfix_ = Teuchos::Utils::getParallelExtension(procRank,numProcs);
175  }
176  else {
177  file_proc_postfix_ = "";
178  }
179 
180  summary_out_ = Teuchos::null;
181  journal_out_ = Teuchos::null;
182  algo_out_ = Teuchos::null;
183 
184  summary_out_used_ = Teuchos::null;
185  journal_out_used_ = Teuchos::null;
186  algo_out_used_ = Teuchos::null;
187  stats_out_used_ = Teuchos::null;
188 
189 }
190 
192 {
193  nlp_ = nlp;
194  reconfig_solver_ = true;
195 }
196 
199 {
200  return nlp_;
201 }
202 
204 {
205  track_ = track;
206  solver_.set_track(Teuchos::null); // Force the track objects to be rebuilt and added!
207 }
208 
211 {
212  return track_;
213 }
214 
216 {
217  config_ = config;
218  solver_.set_config(Teuchos::null); // Must unset the config object.
219  reconfig_solver_ = true;
220 }
221 
224 {
225  return config_;
226 }
227 
229 {
230  options_ = options; // Must totally free all of the references we
231  const config_ptr_t // have to the current options. That includes
232  &config = solver_.get_config(); // removing the options object for the configuration
233  if(config.get()) // object.
234  config->set_options(Teuchos::null); // ...
235  options_used_ = options;
236  commandLineOptionsFromStreamProcessor_.set_options(options);
237  reconfig_solver_ = true;
238 }
239 
242 {
243  return options_;
244 }
245 
247  bool throw_exceptions
248  ,const ostream_ptr_t& error_out
249  )
250 
251 {
252  if( error_out_.get() != NULL ) {
253  if( error_out.get() == NULL )
255  else
256  error_out_used_ = error_out;
257  }
258  else if( error_out.get() != NULL ) {
259  error_out_used_ = error_out;
260  }
261  throw_exceptions_ = throw_exceptions;
262  error_out_ = error_out;
263 }
264 
266 {
267  return throw_exceptions_;
268 }
269 
272 {
273  return error_out_;
274 }
275 
277 {
278  console_out_ = console_out;
279  console_out_used_ = Teuchos::null; // Remove every reference to this ostream object!
280  solver_.set_track(Teuchos::null);
281 }
282 
285 {
286  return console_out_;
287 }
288 
290 {
291  summary_out_ = summary_out;
292  summary_out_used_ = Teuchos::null;
293  solver_.set_track(Teuchos::null); // Remove every reference to this ostream object!
294 }
295 
298 {
299  return summary_out_;
300 }
301 
303 {
304  journal_out_ = journal_out;
305  journal_out_used_ = Teuchos::null;
306  solver_.set_track(Teuchos::null); // Remove every reference to this ostream object!
307 }
308 
311 {
312  return journal_out_;
313 }
314 
316 {
317  algo_out_ = algo_out;
318  algo_out_used_ = Teuchos::null;
319 }
320 
323 {
324  return algo_out_;
325 }
326 
328 MoochoSolver::generate_output_file(const std::string &fileNameBase) const
329 {
330  if( output_to_black_hole_ == OUTPUT_TO_BLACK_HOLE_TRUE )
332  std::string fileName = fileNameBase;
333  if(file_context_postfix_.length())
334  fileName += "." + file_context_postfix_;
335  if(file_proc_postfix_.length())
336  fileName += "." + file_proc_postfix_;
337  if(output_file_tag_.length())
338  fileName += ( "." + output_file_tag_ );
339  fileName += ".out";
340  return Teuchos::rcp(new std::ofstream(fileName.c_str()));
341 }
342 
343 // Solve the NLP
344 
346 {
347 
348  using std::endl;
349  using std::setw;
351  using Teuchos::RCP;
352  namespace ofsp = OptionsFromStreamPack;
353  using ofsp::OptionsFromStream;
354  using ofsp::StringToIntMap;
355  using ofsp::StringToBool;
356 
358  // Validate the input
359  //
360 
362  nlp_.get() == NULL, std::logic_error
363  ,"MoochoSolver::update_solver() : Error, this->get_nlp().get() can not be NULL!" );
364 
365 
366  //
367  // Get the options (or lack of)
368  //
369 
370  bool MoochoSolver_opt_grp_existed = true;
371 
372  if(reconfig_solver_) {
373 
374  if( options_used_.get() == NULL ) {
375  if( options_.get() == NULL ) {
376  options_used_ = commandLineOptionsFromStreamProcessor_.process_and_get_options();
377  }
378  else
379  options_used_ = options_;
380  }
381 
382  //
383  // Read in some options for "MoochoSolver" if needed
384  //
385 
386  if( options_used_.get() ) {
387 
388  options_used_->reset_unaccessed_options_groups();
389 
390  const std::string optgrp_name = "MoochoSolver";
391  OptionsFromStream::options_group_t optgrp = options_used_->options_group( optgrp_name );
392  if( OptionsFromStream::options_group_exists( optgrp ) ) {
393 
394  const int num_opt = 12;
395  enum EOptions {
396  WORKSPACE_MB
397  ,OBJ_SCALE
398  ,TEST_NLP
399  ,CONSOLE_OUTPUTTING
400  ,SUMMARY_OUTPUTTING
401  ,JOURNAL_OUTPUTTING
402  ,ALGO_OUTPUTTING
403  ,PRINT_ALGO
404  ,ALGO_TIMING
405  ,GENERATE_STATS_FILE
406  ,PRINT_OPT_GRP_NOT_ACCESSED
407  ,CONFIGURATION
408  };
409  const char* SOptions[num_opt] = {
410  "workspace_MB"
411  ,"obj_scale"
412  ,"test_nlp"
413  ,"console_outputting"
414  ,"summary_outputting"
415  ,"journal_outputting"
416  ,"algo_outputting"
417  ,"print_algo"
418  ,"algo_timing"
419  ,"generate_stats_file"
420  ,"print_opt_grp_not_accessed"
421  ,"configuration"
422  };
423 
424  const int num_config_opt = 2;
425 
426  const char* SConfigOptions[num_config_opt] = {
427  "mama_jama"
428  ,"interior_point"
429  };
430 
431  StringToIntMap config_map( optgrp_name, num_config_opt, SConfigOptions );
432 
433  StringToIntMap opt_map( optgrp_name, num_opt, SOptions );
434 
435  OptionsFromStream::options_group_t::const_iterator itr = optgrp.begin();
436  for( ; itr != optgrp.end(); ++itr ) {
437  switch( (EOptions)opt_map( ofsp::option_name(itr) ) ) {
438  case WORKSPACE_MB:
439  workspace_MB_ = std::atof( ofsp::option_value(itr).c_str() );
440  break;
441  case OBJ_SCALE:
442  obj_scale_ = std::atof( ofsp::option_value(itr).c_str() );
443  break;
444  case TEST_NLP:
445  test_nlp_ = StringToBool( "test_nlp", ofsp::option_value(itr).c_str() );
446  break;
447  case CONSOLE_OUTPUTTING:
448  do_console_outputting_ = StringToBool( "console_outputting", ofsp::option_value(itr).c_str() );
449  break;
450  case SUMMARY_OUTPUTTING:
451  do_summary_outputting_ = StringToBool( "summary_outputting", ofsp::option_value(itr).c_str() );
452  break;
453  case JOURNAL_OUTPUTTING:
454  do_journal_outputting_ = StringToBool( "journal_outputting", ofsp::option_value(itr).c_str() );
455  break;
456  case ALGO_OUTPUTTING:
457  do_algo_outputting_ = StringToBool( "algo_outputting", ofsp::option_value(itr).c_str() );
458  break;
459  case PRINT_ALGO:
460  print_algo_ = StringToBool( "print_algo", ofsp::option_value(itr).c_str() );
461  break;
462  case ALGO_TIMING:
463  algo_timing_ = StringToBool( "algo_timing", ofsp::option_value(itr).c_str() );
464  break;
465  case GENERATE_STATS_FILE:
466  generate_stats_file_ = StringToBool( "generate_stats_file", ofsp::option_value(itr).c_str() );
467  break;
468  case PRINT_OPT_GRP_NOT_ACCESSED:
469  print_opt_grp_not_accessed_ = StringToBool( "algo_timing", ofsp::option_value(itr).c_str() );
470  break;
471  case CONFIGURATION:
472  configuration_ = config_map( ofsp::option_value(itr).c_str() );
473  break;
474  default:
475  TEUCHOS_TEST_FOR_EXCEPT(true); // this would be a local programming error only.
476  }
477  }
478  }
479  else {
480  MoochoSolver_opt_grp_existed = false;
481  }
482 
483  }
484 
485  }
486 
487  //
488  // Get the output streams if needed
489  //
490 
491  generate_output_streams();
492 
493  if( do_algo_outputting() && !MoochoSolver_opt_grp_existed )
494  *algo_out_used_
495  << "\nWarning! The options group \'MoochoSolver\' was not found.\n"
496  "Using a default set of options ...\n";
497 
498  //
499  // Configure the algorithm
500  //
501 
502  if(reconfig_solver_) {
503 
504  //
505  // Print the headers for the output files
506  //
507 
508  int prec = 8;
510  summary_out_used_->precision(prec);
512  *journal_out_used_ << std::setprecision(prec) << std::scientific;
513 
514  if(do_algo_outputting())
515  *algo_out_used_
516  << "\n********************************************************************"
517  << "\n*** Algorithm information output ***"
518  << "\n*** ***"
519  << "\n*** Below, information about how the the MOOCHO algorithm is ***"
520  << "\n*** setup is given and is followed by detailed printouts of the ***"
521  << "\n*** contents of the algorithm state object (i.e. iteration ***"
522  << "\n*** quantities) and the algorithm description printout ***"
523  << "\n*** (if the option MoochoSolver::print_algo = true is set). ***"
524  << "\n********************************************************************\n";
526  *summary_out_used_
527  << "\n********************************************************************"
528  << "\n*** Algorithm iteration summary output ***"
529  << "\n*** ***"
530  << "\n*** Below, a summary table of the SQP iterations is given as ***"
531  << "\n*** well as a table of the CPU times for each step (if the ***"
532  << "\n*** option MoochoSolver::algo_timing = true is set). ***"
533  << "\n********************************************************************\n";
535  *journal_out_used_
536  << "\n********************************************************************"
537  << "\n*** Algorithm iteration detailed journal output ***"
538  << "\n*** ***"
539  << "\n*** Below, detailed information about the SQP algorithm is given ***"
540  << "\n*** while it is running. The amount of information that is ***"
541  << "\n*** produced can be specified using the option ***"
542  << "\n*** NLPSolverClientInterface::journal_output_level (the default ***"
543  << "\n*** is PRINT_NOTHING and produces no output ***"
544  << "\n********************************************************************\n";
545 
546  // Echo options.
547  if(do_summary_outputting()) {
548  *summary_out_used_ << "\n*** Echoing input options ...\n";
549  if(options_used_.get())
550  options_used_->print_options( *summary_out_used_ );
551  summary_out_used_->flush();
552  }
553  if(do_algo_outputting()) {
554  *algo_out_used_ << "\n*** Echoing input options ...\n";
555  if(options_used_.get())
556  options_used_->print_options( *algo_out_used_ );
557  algo_out_used_->flush();
558  }
559  if(do_journal_outputting()) {
560  *journal_out_used_ << "\n*** Echoing input options ...\n";
561  if(options_used_.get())
562  options_used_->print_options( *journal_out_used_ );
563  journal_out_used_->flush();
564  }
565 
566  //
567  // Allocate the workspace
568  //
569 
570  nlp_->set_options(options_used_);
571  nlp_->initialize();
572  const int default_ws_scale = 10;
573  if( workspace_MB_ < 0.0 ) {
574  workspace_MB_ = nlp_->n() * default_ws_scale * 1e-6 * sizeof(value_type);
575  if(do_algo_outputting())
576  *algo_out_used_
577  << "\nworkspace_MB < 0.0:\n"
578  << "Setting workspace_MB = n * default_ws_scale * 1e-6 * sizeof(value_type) = "
579  << nlp_->n() << " * " << default_ws_scale << " * 1e-6 * " << sizeof(value_type)
580  << " = " << workspace_MB_ << " MB\n";
581  }
583  *summary_out_used_
584  << "\nAllocating workspace_MB = " << workspace_MB_ << " megabytes of temporary "
585  "workspace for automatic arrays only ...\n";
587  Teuchos::rcp(new Teuchos::WorkspaceStoreInitializeable(static_cast<size_t>(1e+6*workspace_MB_)))
588  );
589 
590  //
591  // Reconfigure the algorithm
592  //
593 
594  // Get and set up the configuration object
595  config_ptr_t _config;
596  if(config_.get() == NULL) {
597  if (configuration_ == (EConfigOptions) INTERIOR_POINT) {
598  _config = Teuchos::rcp(new NLPAlgoConfigIP());
599  }
600  else {
601  _config = Teuchos::rcp(new NLPAlgoConfigMamaJama());
602  }
603  }
604  else
605  _config = config_;
606  _config->set_options( options_used_ );
607 
609  std::ostringstream msg;
610  msg << "\n*** Setting up to run MOOCHO on the NLP using a "
611  << "configuration object of type \'" << typeName(*_config) << "\' ...\n";
613  *summary_out_used_ << msg.str();
615  *journal_out_used_ << msg.str();
616  if(do_algo_outputting())
617  *algo_out_used_ << msg.str();
618  }
619 
620  // Set up the solver
621 
622  solver_.set_nlp(nlp_); // Set the NLP object
623  solver_.set_config(_config); // Set the configuration object
624 
625  // Set the client interface options
626  if(options_used_.get()) {
628  solver_options_setter( &solver_ );
629  solver_options_setter.set_options( *options_used_ );
630  }
631 
632  reconfig_solver_ = false;
633 
634  //
635  // Set up the track objects
636  //
637 
639  composite_track = Teuchos::rcp(new AlgorithmTrackerComposite(journal_out_used_));
641  composite_track->tracks().push_back(
642  Teuchos::rcp(new MoochoTrackerConsoleStd(console_out_used_,journal_out_used_)) );
644  composite_track->tracks().push_back(
645  Teuchos::rcp(new MoochoTrackerSummaryStd(summary_out_used_,journal_out_used_)) );
646  if(stats_out_used_.get()) {
647  composite_track->tracks().push_back(
648  Teuchos::rcp(new MoochoTrackerStatsStd(stats_out_used_,stats_out_used_)) );
649  }
650  if( track_.get() ) {
651  track_->set_journal_out(journal_out_used_);
652  composite_track->tracks().push_back( track_ );
653  }
654  solver_.set_track( composite_track );
655 
656  //
657  // Configure and print the algorithm if needed
658  //
659 
660  solver_.configure_algorithm(algo_out_used_.get());
661  if( do_algo_outputting() && print_algo_ )
662  solver_.print_algorithm(*algo_out_used_);
663 
664  }
665 
666 }
667 
669 {
670  using std::endl;
671  using std::setw;
672  using std::flush;
674  using Teuchos::RCP;
675 
676  stopwatch timer;
677  bool threw_exception = false;
678  ESolutionStatus solve_return = SOLVE_RETURN_EXCEPTION;
680  r_find_min = NLPSolverClientInterface::SOLUTION_FOUND;
681 
682  try {
683 
684  update_solver();
685 
686  {
687  std::ostringstream os;
688  os
689  << "\n*****************************"
690  << "\n*** MoochoSolver::solve() ***"
691  << "\n*****************************\n";
692  *this->get_console_out() << os.str();
693  *this->get_summary_out() << os.str();
694  *this->get_journal_out() << os.str();
695  }
696  //
697  // Direct any output from the NLP to the journal output file
698  //
699 
700  EJournalOutputLevel olevel = solver_.journal_output_level();
702  nlpOutputTempState(nlp_,Teuchos::getFancyOStream(journal_out_used_),convertToVerbLevel(olevel));
703 
704  //
705  // Scale the NLP objective function
706  //
707 
708  nlp_->scale_f(obj_scale_);
709 
710  //
711  // Test the nlp if needed
712  //
713 
714  if(test_nlp_) {
715 
716  const char msg1[] = "\ntest_nlp = true: Testing the NLP! ...\n";
718  *console_out_used_ << msg1;
720  *summary_out_used_ << msg1;
722  *journal_out_used_ << msg1;
723  if(NLPFirstOrder* nlp_foi = dynamic_cast<NLPFirstOrder*>(nlp_.get())) {
724  const char msg[] = "\nTesting the supported NLPFirstOrder interface ...\n";
726  *console_out_used_ << msg;
728  *summary_out_used_ << msg;
730  *journal_out_used_ << msg;
731  const bool
732  result = NLPInterfacePack::test_nlp_first_order(
733  nlp_foi,options_used_.get()
734  ,do_journal_outputting() ? journal_out_used_.get() : NULL
735  );
736  if(!result) {
737  const char msg[] = "\nNLPFirstOrder test failed (see journal file)! exiting!\n";
739  *console_out_used_ << msg;
741  *summary_out_used_ << msg;
743  *journal_out_used_ << msg;
744  solve_return = SOLVE_RETURN_NLP_TEST_FAILED;
745  return solve_return;
746  }
747  }
748  else if(NLPDirect* nlp_fod = dynamic_cast<NLPDirect*>(nlp_.get())) {
749  const char msg[] = "\nTesting the supported NLPDirect interface ...\n";
751  *console_out_used_ << msg;
753  *summary_out_used_ << msg;
755  *journal_out_used_ << msg;
756  const bool
757  result = NLPInterfacePack::test_nlp_direct(
758  nlp_fod,options_used_.get()
759  ,do_journal_outputting() ? journal_out_used_.get() : NULL
760  );
761  if(!result) {
762  const char msg[] = "\nNLPDirect test failed (see journal file)! exiting!\n";
764  *console_out_used_ << msg;
766  *summary_out_used_ << msg;
768  *journal_out_used_ << msg;
769  solve_return = SOLVE_RETURN_NLP_TEST_FAILED;
770  return solve_return;
771  }
772  }
773  const char msg2[] = "\nSuccessful end of testing of the nlp\n";
775  *console_out_used_ << msg2;
777  *summary_out_used_ << msg2;
779  *journal_out_used_ << msg2;
780 
781  }
782 
783  //
784  // Solve the NLP
785  //
786 
788  *journal_out_used_
789  << "\n************************************"
790  << "\n*** MoochoSolver::solve_nlp() ***"
791  << "\n************************************\n"
792  << "\n*** Starting iterations ...\n\n";
793 
794  solver_.set_algo_timing(algo_timing_);
795  timer.start();
796  r_find_min = solver_.find_min();
797 
798  }
799  catch(const std::exception& excpt) {
800  std::ostringstream msg;
801  msg << "\nMoochoSolver: Caught an std::exception of type "
802  << typeName(excpt) << " described as : " << excpt.what() << endl;
803  *error_out_used_ << msg.str() << flush;
805  *summary_out_used_ << msg.str() << flush;
807  *journal_out_used_ << msg.str() << flush;
808  if(throw_exceptions_)
809  throw;
810  threw_exception = true;
811  }
812  catch(...) {
813  std::ostringstream msg;
814  msg << "\nMoochoSolver: Caught an unknown exception (i.e. ...)\n";
815  *error_out_used_ << msg.str() << flush;
817  *summary_out_used_ << msg.str() << flush;
819  *journal_out_used_ << msg.str() << flush;
820  if(throw_exceptions_)
821  throw;
822  threw_exception = true;
823  }
824 
825  timer.stop();
826 
827  if(threw_exception) {
829  *summary_out_used_ << "\n\n****************************\n"
830  << "**** Threw an exception ****\n";
831  solve_return = SOLVE_RETURN_EXCEPTION;
832  }
833  else {
834  switch( r_find_min ) {
835  case NLPSolverClientInterface::SOLUTION_FOUND: {
837  *summary_out_used_ << "\n\n************************\n"
838  << "**** Solution Found ****\n";
839  *error_out_used_ << "Solution Found!\n";
840  solve_return = SOLVE_RETURN_SOLVED;
841  break;
842  }
843  case NLPSolverClientInterface::MAX_ITER_EXCEEDED: {
845  *summary_out_used_ << "\n\n**********************************************\n"
846  << "**** Maximun number of iteration exceeded ****\n";
847  *error_out_used_ << "Maximun number of iteration exceeded!\n";
848  solve_return = SOLVE_RETURN_MAX_ITER;
849  break;
850  }
851  case NLPSolverClientInterface::MAX_RUN_TIME_EXCEEDED: {
853  *summary_out_used_ << "\n\n**********************************\n"
854  << "**** Maximun runtime exceeded ****\n";
855  *error_out_used_ << "Maximun runtime exceeded!\n";
856  solve_return = SOLVE_RETURN_MAX_RUN_TIME;
857  break;
858  }
859  case NLPSolverClientInterface::ALGORITHMIC_ERROR: {
861  *summary_out_used_ << "\n\n*********************************************\n"
862  << "**** Some error occurred in the algorithm ****\n";
863  *error_out_used_ << "Some algorithmic error occurred!\n";
864  solve_return = SOLVE_RETURN_EXCEPTION;
865  break;
866  }
867  }
868  }
869 
870  if(do_summary_outputting()) {
871  *summary_out_used_ << "\n total time = " << timer.read() << " sec.\n";
872  if( solver_.algo_timing() ) {
874  solver_.print_algorithm_times( *summary_out_used_ );
875  *summary_out_used_ << "\n\n";
876  Teuchos::TimeMonitor::summarize(*summary_out_used_);
877  }
878  }
879 
880  // Print workspace usage statistics
882  *summary_out_used_
883  << "\n*** Statistics for automatic array workspace:"
884  << "\nNumber of megabytes of preallocated workspace = "
885  << workspace_MB_
886  << "\nNumber of allocations using preallocated workspace = "
887  << Teuchos::get_default_workspace_store()->num_static_allocations()
888  << "\nNumber of dynamic allocations beyond preallocated workspace = "
889  << Teuchos::get_default_workspace_store()->num_dyn_allocations();
890 
891  // Print which options groups were not read
892  if( do_algo_outputting() && print_opt_grp_not_accessed_ ) {
893  *algo_out_used_
894  << "\n***************************************************************\n"
895  "Warning, the following options groups where not accessed.\n"
896  "An options group may not be accessed if it is not looked for\n"
897  "or if an \"optional\" options group was looked from and the user\n"
898  "spelled it incorrectly:\n\n";
899  if(options_used_.get())
900  options_used_->print_unaccessed_options_groups(*algo_out_used_);
901  }
902 
904  console_out_used_->flush();
906  summary_out_used_->flush();
908  journal_out_used_->flush();
909  if(do_algo_outputting())
910  algo_out_used_->flush();
911 
912  return solve_return;
913 
914 }
915 
916 // Get the underlying solver object
917 
919 {
920  update_solver();
921  return solver_;
922 }
923 
925 {
926  update_solver();
927  return solver_;
928 }
929 
930 // private
931 
932 void MoochoSolver::generate_output_streams() const
933 {
934  if( do_console_outputting() && console_out_used_.get() == NULL ) {
935  if( console_out_.get() == NULL ) {
936  console_out_used_ = Teuchos::VerboseObjectBase::getDefaultOStream();
937  console_out_ = console_out_used_;
938  }
939  else {
940  console_out_used_ = console_out_;
941  }
942  }
943  if( do_summary_outputting() && summary_out_used_.get()==NULL ) {
944  if( summary_out_.get() == NULL ) {
945  summary_out_used_ = generate_output_file("MoochoSummary");
946  summary_out_ = summary_out_used_;
947  }
948  else {
949  summary_out_used_ = summary_out_;
950  }
951  }
952  if( do_journal_outputting() && journal_out_used_.get() == NULL ) {
953  if( journal_out_.get() == NULL ) {
954  journal_out_used_ = generate_output_file("MoochoJournal");
955  journal_out_ = journal_out_used_;
956  }
957  else {
958  journal_out_used_ = journal_out_;
959  }
960  }
961  else {
962  journal_out_used_ = Teuchos::rcp(new Teuchos::oblackholestream());
963  }
964  if( do_algo_outputting() && algo_out_used_.get() == NULL ) {
965  if( algo_out_.get() == NULL ) {
966  algo_out_used_ = generate_output_file("MoochoAlgo");
967  algo_out_ = algo_out_used_;
968  }
969  else {
970  algo_out_used_ = algo_out_;
971  }
972  }
973  if( generate_stats_file_ && stats_out_used_.get() == NULL ) {
974  stats_out_used_ = generate_output_file("MoochoStats");
975  }
976 }
977 
978 } // end namespace MoochoPack
const ostream_ptr_t & get_console_out() const
Get the non-const smart pointer to the set output stream for console outputting.
void set_options(const options_ptr_t &options)
Set the various options to use.
void setup_commandline_processor(Teuchos::CommandLineProcessor *clp)
Setup the commandline processor to process commandline options.
TEUCHOSCORE_LIB_DLL_EXPORT void set_default_workspace_store(const Teuchos::RCP< WorkspaceStore > &default_workspace_store)
void set_output_context(const std::string &file_context_postfix, EOutputToBlackHole output_to_black_hole=OUTPUT_TO_BLACK_HOLE_DEFAULT, const int procRank=-1, const int numProcs=-1)
Setup the context for outputting.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void set_options(const OptionsFromStream &options)
Set options for NLPSolverClientInterface from an OptionsFromStream object.
T * get() const
This is a do all configuration class for NLPAlgo.
void update_solver() const
Setup the state of the solver and get ready for a solve.
const ostream_ptr_t & get_summary_out() const
Get the non-const smart pointer to the set output stream for summary outputting.
void set_journal_out(const ostream_ptr_t &journal_out)
Set the std::ostream object to use for journal output by the MOOCHO step objects. ...
const config_ptr_t & get_config() const
Return the configuration object being used.
void setup_commandline_processor(Teuchos::CommandLineProcessor *clp)
const ostream_ptr_t & get_journal_out() const
Get the non-const smart pointer to the set output stream for journal outputting.
bool do_console_outputting() const
Return if console outputting is performed or not.
RCP< NLPInterfacePack::NLP > nlp_ptr_t
bool do_summary_outputting() const
Return if summary outputting is performed or not.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
const track_ptr_t & get_track() const
Get the non-const smart pointer to the set AlgorithmTracker object.
const options_ptr_t & get_options() const
Get the OptionsFromStream object being used to extract the options from.
bool do_journal_outputting() const
Return if journal outputting is performed or not.
This rSQP iteration class provides a tablular output suitable for an 80 char wide console...
MoochoSolver(const std::string &options_file_name="Moocho.opt", const std::string &extra_options_str="")
Constructs to uninitialized.
static RCP< FancyOStream > getDefaultOStream()
int getOutputToRootOnly() const
const ostream_ptr_t & error_out() const
Return the std::ostream object used for error reporting on exceptions.
RCP< std::ostream > generate_output_file(const std::string &fileNameBase) const
Generate an output file given a base file name.
void set_options(Teuchos::RCP< OptionsFromStream > const &options)
void set_config(const config_ptr_t &config)
Set the algorithm configuration object.
const ostream_ptr_t & get_algo_out() const
Get the non-const smart pointer to the set output stream for algo outputting.
void set_console_out(const ostream_ptr_t &console_out)
Set the std::ostream object to use for console output by a MoochoTrackerConsoleStd object...
This is the most basic interface that clients use to solve an NLP.
bool do_algo_outputting() const
Return if algo outputting is performed or not.
static std::string getParallelExtension(int procRank=-1, int numProcs=-1)
void set_summary_out(const ostream_ptr_t &summary_out)
Set the std::ostream object to use for summary output.
NLPSolverClientInterface & get_solver()
Get the underlying NLPSolverClientInterface object.
ESolutionStatus solve_nlp() const
Solve the NLP.
This class simply outputs the convergence information for each iteration.
void setRowsBetweenLines(int lineInterval)
const nlp_ptr_t & get_nlp() const
Get the non-const smart pointer to the set NLP object.
This is a do all configuration class for NLPAlgo.
void set_nlp(const nlp_ptr_t &nlp)
Set the NLP to be solved.
This is a simple track class for getting statistics about a solved (or not solved) NLP...
void set_track(const track_ptr_t &track)
Teuchos::RCP< OptionsFromStream > process_and_get_options()
OptionsFromStreamPack::CommandLineOptionsFromStreamProcessor & commandLineOptionsFromStreamProcessor()
void set_algo_out(const ostream_ptr_t &algo_out)
Set the std::ostream object to use for algorithm output.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
TEUCHOSCORE_LIB_DLL_EXPORT Teuchos::RCP< WorkspaceStore > get_default_workspace_store()
void set_error_handling(bool throw_exceptions, const ostream_ptr_t &error_out)
Set the error output and whether exceptions will be thrown from these functions or not...
bool throw_exceptions() const
Return if exceptions will be thrown out of this->solve_nlp().
static TableFormat & format()
std::string typeName(const T &t)