MOOCHO (Single Doxygen Collection) Version of the Day
MoochoPack_NLPAlgoContainer.cpp
Go to the documentation of this file.
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 //#define RELEASE_TRACE
00043 
00044 #include <assert.h>
00045 
00046 #include <iostream> // used for debugging the Release version.
00047 
00048 #include "MoochoPack_NLPAlgoContainer.hpp"
00049 #include "MoochoPack_NLPAlgoInterface.hpp"
00050 #include "MoochoPack_NLPAlgoState.hpp"
00051 #include "NLPInterfacePack_NLP.hpp"
00052 #include "Teuchos_Assert.hpp"
00053 
00054 namespace {
00055 
00056 void report_final_point( const MoochoPack::NLPAlgoState& s, const bool optimal, NLPInterfacePack::NLP* nlp )
00057 {
00058   const AbstractLinAlgPack::size_type
00059     m  = nlp->m(),
00060     nb = nlp->num_bounded_x();
00061   const IterationPack::IterQuantityAccess<AbstractLinAlgPack::VectorMutable>
00062     &x_iq = s.x();
00063   if( x_iq.updated_k(0) ) {
00064     nlp->report_final_solution(
00065       x_iq.get_k(0)                                                      // x
00066       ,( m  && s.lambda().updated_k(0) ) ? &s.lambda().get_k(0)  : NULL  // lambda
00067       ,( nb && s.nu().updated_k(0)     ) ? &s.nu().get_k(0)      : NULL  // nu
00068       , optimal                                                          // optimal = false
00069       );
00070   }
00071 }
00072 
00073 } // end namespace
00074 
00075 namespace MoochoPack {
00076 
00077 // Overridden from rSQPAlgoClient interface
00078 
00079 void NLPAlgoContainer::set_config(const config_ptr_t& config)
00080 {
00081   algo_ = Teuchos::null; // Remove our reference to the current (configured?) algorithm.
00082   config_ = config;
00083 }
00084 
00085 NLPAlgoContainer::config_ptr_t&
00086 NLPAlgoContainer::get_config()
00087 { 
00088   return config_;
00089 }
00090 
00091 const NLPAlgoContainer::config_ptr_t&
00092 NLPAlgoContainer::get_config() const
00093 { 
00094   return config_;
00095 }
00096 
00097 NLPAlgoConfig&
00098 NLPAlgoContainer::config()
00099 { 
00100   return *config_;
00101 }
00102 
00103 const NLPAlgoConfig&
00104 NLPAlgoContainer::config() const
00105 { 
00106   return *config_;
00107 }
00108 
00109 NLPSolverClientInterface::EFindMinReturn
00110 NLPAlgoContainer::find_min()
00111 {
00112   config().init_algo(&algo());
00113   EFindMinReturn solve_return;
00114   try {
00115     solve_return = algo().dispatch();
00116   }
00117   catch(...) {
00118     report_final_point(algo().retrieve_state(),false,&nlp());
00119     throw;
00120   }
00121   report_final_point(algo().retrieve_state(),solve_return==NLPSolverClientInterface::SOLUTION_FOUND,&nlp());
00122   return solve_return;
00123 }
00124 
00125 void NLPAlgoContainer::configure_algorithm(std::ostream* trase_out)
00126 {
00127   assert_valid_setup();
00128   if(!get_algo().get())
00129     config().config_algo_cntr(this,trase_out);
00130 }
00131 
00132 void NLPAlgoContainer::print_algorithm(std::ostream& out) const
00133 {
00134   algo().interface_print_algorithm(out);
00135 }
00136 
00137 void NLPAlgoContainer::set_algo_timing( bool algo_timing )
00138 {
00139   algo().interface_set_algo_timing(algo_timing);
00140 }
00141 
00142 bool NLPAlgoContainer::algo_timing() const
00143 {
00144   return algo().interface_algo_timing();
00145 }
00146 
00147 void NLPAlgoContainer::print_algorithm_times(
00148   std::ostream& out ) const
00149 {
00150   algo().interface_print_algorithm_times(out);
00151 }
00152 
00153 void NLPAlgoContainer::assert_valid_setup() const {
00154   TEUCHOS_TEST_FOR_EXCEPTION(
00155     !get_nlp().get(), NLPSolverClientInterface::InvalidSetup
00156     ,"NLPAlgoContainer::assert_valid_setup() : The NLP object has not been set" );
00157   TEUCHOS_TEST_FOR_EXCEPTION(
00158     !get_track().get(), NLPSolverClientInterface::InvalidSetup
00159     ,"NLPAlgoContainer::assert_valid_setup() : The AlgorithmTracker object has not been set" );
00160   TEUCHOS_TEST_FOR_EXCEPTION(
00161     !get_config().get(), NLPSolverClientInterface::InvalidSetup
00162     ,"NLPAlgoContainer::assert_valid_setup() : The NLPAlgoConfig object has not been set" );
00163 }
00164 
00165 } // end namespace MoochoPack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines