71 namespace MoochoPack {
76 const std::string &options_file_name
77 ,
const std::string &extra_options_str
79 :reconfig_solver_(true)
85 ,generate_stats_file_(false)
86 ,print_opt_grp_not_accessed_(true)
87 ,throw_exceptions_(false)
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_(
"")
100 defaultOut = Teuchos::VerboseObjectBase::getDefaultOStream();
101 error_out_used_ = defaultOut;
106 "moocho-options-file");
108 "Set the name of the MOOCHO options file in the OptionsFromStream format."
109 " File is ignored if it does not exist!");
113 "moocho-extra-options");
115 "Extra MOOCHO options specified in the format"
116 " \"OptGrp1{name1=val1,...,namen=valn}:OptGr2{name1=val1,...,namen=valn}:...\"");
141 const std::string &file_context_postfix
143 ,
const int procRank_in
144 ,
const int numProcs_in
148 file_context_postfix_ = file_context_postfix;
150 output_to_black_hole_ = output_to_black_hole;
153 defaultOut = Teuchos::VerboseObjectBase::getDefaultOStream();
162 output_to_black_hole_
177 file_proc_postfix_ =
"";
232 &config = solver_.get_config();
235 options_used_ = options;
247 bool throw_exceptions
252 if( error_out_.get() != NULL ) {
253 if( error_out.get() == NULL )
254 error_out_used_ = Teuchos::VerboseObjectBase::getDefaultOStream();
258 else if( error_out.get() != NULL ) {
278 console_out_ = console_out;
291 summary_out_ = summary_out;
304 journal_out_ = journal_out;
317 algo_out_ = algo_out;
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_;
340 return Teuchos::rcp(
new std::ofstream(fileName.c_str()));
352 namespace ofsp = OptionsFromStreamPack;
353 using ofsp::OptionsFromStream;
354 using ofsp::StringToIntMap;
362 nlp_.get() == NULL, std::logic_error
363 ,
"MoochoSolver::update_solver() : Error, this->get_nlp().get() can not be NULL!" );
370 bool MoochoSolver_opt_grp_existed =
true;
374 if( options_used_.get() == NULL ) {
375 if( options_.get() == NULL ) {
379 options_used_ = options_;
386 if( options_used_.get() ) {
388 options_used_->reset_unaccessed_options_groups();
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 ) ) {
394 const int num_opt = 12;
406 ,PRINT_OPT_GRP_NOT_ACCESSED
409 const char* SOptions[num_opt] = {
413 ,
"console_outputting"
414 ,
"summary_outputting"
415 ,
"journal_outputting"
419 ,
"generate_stats_file"
420 ,
"print_opt_grp_not_accessed"
424 const int num_config_opt = 2;
426 const char* SConfigOptions[num_config_opt] = {
431 StringToIntMap config_map( optgrp_name, num_config_opt, SConfigOptions );
433 StringToIntMap opt_map( optgrp_name, num_opt, SOptions );
435 OptionsFromStream::options_group_t::const_iterator itr = optgrp.begin();
436 for( ; itr != optgrp.end(); ++itr ) {
447 case CONSOLE_OUTPUTTING:
450 case SUMMARY_OUTPUTTING:
453 case JOURNAL_OUTPUTTING:
456 case ALGO_OUTPUTTING:
465 case GENERATE_STATS_FILE:
468 case PRINT_OPT_GRP_NOT_ACCESSED:
480 MoochoSolver_opt_grp_existed =
false;
495 <<
"\nWarning! The options group \'MoochoSolver\' was not found.\n"
496 "Using a default set of options ...\n";
510 summary_out_used_->precision(prec);
512 *journal_out_used_ << std::setprecision(prec) << std::scientific;
516 <<
"\n********************************************************************"
517 <<
"\n*** Algorithm information output ***"
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";
527 <<
"\n********************************************************************"
528 <<
"\n*** Algorithm iteration summary output ***"
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";
536 <<
"\n********************************************************************"
537 <<
"\n*** Algorithm iteration detailed journal output ***"
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";
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();
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();
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();
570 nlp_->set_options(options_used_);
572 const int default_ws_scale = 10;
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)
584 <<
"\nAllocating workspace_MB = " <<
workspace_MB_ <<
" megabytes of temporary "
585 "workspace for automatic arrays only ...\n";
596 if(config_.get() == NULL) {
606 _config->set_options( options_used_ );
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();
617 *algo_out_used_ << msg.str();
622 solver_.set_nlp(nlp_);
623 solver_.set_config(_config);
626 if(options_used_.get()) {
628 solver_options_setter( &solver_ );
629 solver_options_setter.
set_options( *options_used_ );
639 composite_track =
Teuchos::rcp(
new AlgorithmTrackerComposite(journal_out_used_));
641 composite_track->tracks().push_back(
644 composite_track->tracks().push_back(
646 if(stats_out_used_.get()) {
647 composite_track->tracks().push_back(
651 track_->set_journal_out(journal_out_used_);
652 composite_track->tracks().push_back( track_ );
654 solver_.set_track( composite_track );
660 solver_.configure_algorithm(algo_out_used_.get());
662 solver_.print_algorithm(*algo_out_used_);
677 bool threw_exception =
false;
687 std::ostringstream os;
689 <<
"\n*****************************"
690 <<
"\n*** MoochoSolver::solve() ***"
691 <<
"\n*****************************\n";
702 nlpOutputTempState(nlp_,Teuchos::getFancyOStream(journal_out_used_),
convertToVerbLevel(olevel));
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;
733 nlp_foi,options_used_.get()
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;
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;
758 nlp_fod,options_used_.get()
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;
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;
789 <<
"\n************************************"
790 <<
"\n*** MoochoSolver::solve_nlp() ***"
791 <<
"\n************************************\n"
792 <<
"\n*** Starting iterations ...\n\n";
796 r_find_min = solver_.find_min();
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;
810 threw_exception =
true;
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;
822 threw_exception =
true;
827 if(threw_exception) {
829 *summary_out_used_ <<
"\n\n****************************\n"
830 <<
"**** Threw an exception ****\n";
834 switch( r_find_min ) {
837 *summary_out_used_ <<
"\n\n************************\n"
838 <<
"**** Solution Found ****\n";
839 *error_out_used_ <<
"Solution Found!\n";
845 *summary_out_used_ <<
"\n\n**********************************************\n"
846 <<
"**** Maximun number of iteration exceeded ****\n";
847 *error_out_used_ <<
"Maximun number of iteration exceeded!\n";
853 *summary_out_used_ <<
"\n\n**********************************\n"
854 <<
"**** Maximun runtime exceeded ****\n";
855 *error_out_used_ <<
"Maximun runtime exceeded!\n";
861 *summary_out_used_ <<
"\n\n*********************************************\n"
862 <<
"**** Some error occurred in the algorithm ****\n";
863 *error_out_used_ <<
"Some algorithmic error occurred!\n";
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";
883 <<
"\n*** Statistics for automatic array workspace:"
884 <<
"\nNumber of megabytes of preallocated workspace = "
886 <<
"\nNumber of allocations using preallocated workspace = "
888 <<
"\nNumber of dynamic allocations beyond preallocated workspace = "
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_);
904 console_out_used_->flush();
906 summary_out_used_->flush();
908 journal_out_used_->flush();
910 algo_out_used_->flush();
935 if( console_out_.get() == NULL ) {
936 console_out_used_ = Teuchos::VerboseObjectBase::getDefaultOStream();
937 console_out_ = console_out_used_;
940 console_out_used_ = console_out_;
944 if( summary_out_.get() == NULL ) {
946 summary_out_ = summary_out_used_;
949 summary_out_used_ = summary_out_;
953 if( journal_out_.get() == NULL ) {
955 journal_out_ = journal_out_used_;
958 journal_out_used_ = journal_out_;
965 if( algo_out_.get() == NULL ) {
967 algo_out_ = algo_out_used_;
970 algo_out_used_ = algo_out_;
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.
Reads from a file and/or parses from the commandline to initalize an OptionsFromStream object...
void setup_commandline_processor(Teuchos::CommandLineProcessor *clp)
Setup the commandline processor to process commandline options.
std::string typeName(const T &t)
bool StringToBool(const char *opt_name, const char *str)
Convert a string "true" or "false" into bool #true# or #false#.
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.
Teuchos::EVerbosityLevel convertToVerbLevel(const EJournalOutputLevel output_level)
Conver to Teuchos::EVerbosityLevel.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
const std::string & option_name(OptionsGroup::const_iterator &itr)
void set_options(const OptionsFromStream &options)
Overridden from SetOptionsFromStream and calls setOption(...).
bool do_console_outputting_
Set options for NLPSolverClientInterface from an OptionsFromStream object.
void generate_output_streams() const
This is a do all configuration class for NLPAlgo.
bool test_nlp_first_order(NLPFirstOrder *nlp, OptionsFromStreamPack::OptionsFromStream *options, std::ostream *out)
Test an NLPFirstOrder object.
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.
bool do_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)
Setup a comandline processor before it processes commandline options or reads form a file...
std::string output_file_tag_
bool test_nlp_direct(NLPDirect *nlp, OptionsFromStreamPack::OptionsFromStream *options, std::ostream *out)
Test an NLPDirect object.
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.
bool do_journal_outputting_
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)
bool print_opt_grp_not_accessed_
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)
EJournalOutputLevel
enum for journal output.
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.
bool generate_stats_file_
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)
Set the OptionsFromStream object that will be used to fill options in to.
RCP< std::ostream > ostream_ptr_t
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.
OptionsFromStreamPack::CommandLineOptionsFromStreamProcessor commandLineOptionsFromStreamProcessor_
ESolutionStatus solve_nlp() const
Solve the NLP.
This class simply outputs the convergence information for each iteration.
AbstractLinAlgPack::value_type value_type
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)
RCP< OptionsFromStreamPack::OptionsFromStream > options_ptr_t
Teuchos::RCP< OptionsFromStream > process_and_get_options()
Calls process_options() and returns 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().
const std::string & option_value(OptionsGroup::const_iterator &itr)