42 #include "MoochoPack_MoochoSolver.hpp"
44 #include "MoochoPack_NLPAlgoConfigMamaJama.hpp"
45 #include "MoochoPack_NLPAlgoConfigIP.hpp"
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"
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_(
"")
101 error_out_used_ = defaultOut;
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);
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);
124 return commandLineOptionsFromStreamProcessor_;
130 return commandLineOptionsFromStreamProcessor_;
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;
160 if(output_to_black_hole_==OUTPUT_TO_BLACK_HOLE_DEFAULT) {
162 output_to_black_hole_
164 ? OUTPUT_TO_BLACK_HOLE_FALSE
165 : OUTPUT_TO_BLACK_HOLE_TRUE
169 output_to_black_hole_ = OUTPUT_TO_BLACK_HOLE_FALSE;
177 file_proc_postfix_ =
"";
180 summary_out_ = Teuchos::null;
181 journal_out_ = Teuchos::null;
182 algo_out_ = Teuchos::null;
184 summary_out_used_ = Teuchos::null;
185 journal_out_used_ = Teuchos::null;
186 algo_out_used_ = Teuchos::null;
187 stats_out_used_ = Teuchos::null;
194 reconfig_solver_ =
true;
206 solver_.set_track(Teuchos::null);
218 solver_.set_config(Teuchos::null);
219 reconfig_solver_ =
true;
232 &config = solver_.get_config();
234 config->set_options(Teuchos::null);
235 options_used_ = options;
236 commandLineOptionsFromStreamProcessor_.
set_options(options);
237 reconfig_solver_ =
true;
247 bool throw_exceptions
252 if( error_out_.get() != NULL ) {
253 if( error_out.
get() == NULL )
258 else if( error_out.
get() != NULL ) {
267 return throw_exceptions_;
278 console_out_ = console_out;
279 console_out_used_ = Teuchos::null;
280 solver_.set_track(Teuchos::null);
291 summary_out_ = summary_out;
292 summary_out_used_ = Teuchos::null;
293 solver_.set_track(Teuchos::null);
304 journal_out_ = journal_out;
305 journal_out_used_ = Teuchos::null;
306 solver_.set_track(Teuchos::null);
317 algo_out_ = algo_out;
318 algo_out_used_ = Teuchos::null;
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_ );
340 return Teuchos::rcp(
new std::ofstream(fileName.c_str()));
352 namespace ofsp = OptionsFromStreamPack;
353 using ofsp::OptionsFromStream;
354 using ofsp::StringToIntMap;
355 using ofsp::StringToBool;
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;
372 if(reconfig_solver_) {
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 ) {
437 switch( (EOptions)opt_map( ofsp::option_name(itr) ) ) {
439 workspace_MB_ = std::atof( ofsp::option_value(itr).c_str() );
442 obj_scale_ = std::atof( ofsp::option_value(itr).c_str() );
445 test_nlp_ = StringToBool(
"test_nlp", ofsp::option_value(itr).c_str() );
447 case CONSOLE_OUTPUTTING:
448 do_console_outputting_ = StringToBool(
"console_outputting", ofsp::option_value(itr).c_str() );
450 case SUMMARY_OUTPUTTING:
451 do_summary_outputting_ = StringToBool(
"summary_outputting", ofsp::option_value(itr).c_str() );
453 case JOURNAL_OUTPUTTING:
454 do_journal_outputting_ = StringToBool(
"journal_outputting", ofsp::option_value(itr).c_str() );
456 case ALGO_OUTPUTTING:
457 do_algo_outputting_ = StringToBool(
"algo_outputting", ofsp::option_value(itr).c_str() );
460 print_algo_ = StringToBool(
"print_algo", ofsp::option_value(itr).c_str() );
463 algo_timing_ = StringToBool(
"algo_timing", ofsp::option_value(itr).c_str() );
465 case GENERATE_STATS_FILE:
466 generate_stats_file_ = StringToBool(
"generate_stats_file", ofsp::option_value(itr).c_str() );
468 case PRINT_OPT_GRP_NOT_ACCESSED:
469 print_opt_grp_not_accessed_ = StringToBool(
"algo_timing", ofsp::option_value(itr).c_str() );
472 configuration_ = config_map( ofsp::option_value(itr).c_str() );
480 MoochoSolver_opt_grp_existed =
false;
491 generate_output_streams();
495 <<
"\nWarning! The options group \'MoochoSolver\' was not found.\n"
496 "Using a default set of options ...\n";
502 if(reconfig_solver_) {
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;
573 if( workspace_MB_ < 0.0 ) {
574 workspace_MB_ = nlp_->n() * default_ws_scale * 1e-6 *
sizeof(value_type);
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";
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_ );
632 reconfig_solver_ =
false;
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;
680 r_find_min = NLPSolverClientInterface::SOLUTION_FOUND;
687 std::ostringstream os;
689 <<
"\n*****************************"
690 <<
"\n*** MoochoSolver::solve() ***"
691 <<
"\n*****************************\n";
700 EJournalOutputLevel olevel = solver_.journal_output_level();
702 nlpOutputTempState(nlp_,Teuchos::getFancyOStream(journal_out_used_),convertToVerbLevel(olevel));
708 nlp_->scale_f(obj_scale_);
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;
732 result = NLPInterfacePack::test_nlp_first_order(
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;
744 solve_return = SOLVE_RETURN_NLP_TEST_FAILED;
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;
757 result = NLPInterfacePack::test_nlp_direct(
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;
769 solve_return = SOLVE_RETURN_NLP_TEST_FAILED;
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";
794 solver_.set_algo_timing(algo_timing_);
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;
808 if(throw_exceptions_)
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;
820 if(throw_exceptions_)
822 threw_exception =
true;
827 if(threw_exception) {
829 *summary_out_used_ <<
"\n\n****************************\n"
830 <<
"**** Threw an exception ****\n";
831 solve_return = SOLVE_RETURN_EXCEPTION;
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;
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;
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;
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;
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();
932 void MoochoSolver::generate_output_streams()
const
935 if( console_out_.get() == NULL ) {
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_;
973 if( generate_stats_file_ && stats_out_used_.get() == NULL ) {
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.
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.
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().
std::string typeName(const T &t)