MOOCHO (Single Doxygen Collection) Version of the Day
MoochoPack_LineSearchNLE_Step.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 #include <ostream>
00043 #include <typeinfo>
00044 
00045 #include "MoochoPack_LineSearchNLE_Step.hpp"
00046 #include "MoochoPack_Exceptions.hpp"
00047 #include "MoochoPack_moocho_algo_conversion.hpp"
00048 #include "IterationPack_print_algorithm_step.hpp"
00049 #include "ConstrainedOptPack_MeritFuncNLESqrResid.hpp"
00050 #include "ConstrainedOptPack_MeritFuncCalcNLE.hpp"
00051 #include "ConstrainedOptPack_MeritFuncCalc1DQuadratic.hpp"
00052 #include "AbstractLinAlgPack_VectorMutable.hpp"
00053 #include "AbstractLinAlgPack_VectorStdOps.hpp"
00054 #include "AbstractLinAlgPack_VectorOut.hpp"
00055 #include "AbstractLinAlgPack_assert_print_nan_inf.hpp"
00056 #include "AbstractLinAlgPack_LinAlgOpPack.hpp"
00057 #include "Teuchos_Assert.hpp"
00058 
00059 namespace MoochoPack {
00060 
00061 LineSearchNLE_Step::LineSearchNLE_Step(
00062   const direct_line_search_ptr_t& direct_line_search
00063   )
00064   :direct_line_search_(direct_line_search)
00065 {}
00066 
00067 bool LineSearchNLE_Step::do_step(
00068   Algorithm& _algo, poss_type step_poss, IterationPack::EDoStepType type
00069   ,poss_type assoc_step_poss
00070   )
00071 {
00072   using AbstractLinAlgPack::Vp_StV;
00073   using LinAlgOpPack::V_VpV;
00074 
00075   NLPAlgo         &algo   = rsqp_algo(_algo);
00076   NLPAlgoState    &s      = algo.rsqp_state();
00077   NLP             &nlp    = algo.nlp();
00078 
00079   EJournalOutputLevel olevel = algo.algo_cntr().journal_output_level();
00080   std::ostream& out = algo.track().journal_out();
00081 
00082   // print step header.
00083   if( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) ) {
00084     using IterationPack::print_algorithm_step;
00085     print_algorithm_step( algo, step_poss, type, assoc_step_poss, out );
00086   }
00087 
00088   const size_type
00089     //n  = nlp.n(),
00090     m  = nlp.m();
00091 
00092   TEUCHOS_TEST_FOR_EXCEPTION( m == 0, std::logic_error, "LineSearchNLE_Step::do_step(...) : Error!" );
00093 
00094   // /////////////////////////////////////////
00095   // Set references to iteration quantities
00096   //
00097   // Set k+1 first then go back to get k+0 to ensure
00098   // we have backward storage!
00099 
00100   IterQuantityAccess<value_type>
00101     &alpha_iq = s.alpha();
00102   IterQuantityAccess<VectorMutable>
00103     &x_iq    = s.x(),
00104     &d_iq    = s.d(),
00105     &c_iq    = s.c();
00106   
00107   VectorMutable        &x_kp1   = x_iq.get_k(+1);
00108   const Vector         &x_k     = x_iq.get_k(0);
00109   VectorMutable        &c_kp1   = c_iq.get_k(+1);
00110   const Vector         &c_k     = c_iq.get_k(0);
00111   const Vector         &d_k     = d_iq.get_k(0);
00112   value_type           &alpha_k = alpha_iq.get_k(0);
00113 
00114   // //////////////////////////////////////
00115   // Build the merit function
00116   
00117   ConstrainedOptPack::MeritFuncNLESqrResid  phi_c;
00118 
00119   // /////////////////////////////////////
00120   // Compute Dphi_k, phi_kp1 and phi_k
00121 
00122   // phi_k, phi_kp1
00123   const value_type  phi_k   = phi_c.value(c_k);
00124   value_type        phi_kp1 = phi_c.value(c_kp1);
00125   if( (int)olevel >= (int)PRINT_ALGORITHM_STEPS ) {
00126     out << "\nBegin definition of NLP merit function phi_c.value(c(x)):\n";
00127     phi_c.print_merit_func( out, "    " );
00128     out << "end definition of the NLP merit funciton\n";
00129   }
00130   // Dphi_k
00131   phi_c.calc_deriv(c_k);
00132   const value_type
00133     Dphi_k  = phi_c.deriv();
00134   if( (int)olevel >= (int)PRINT_ALGORITHM_STEPS ) {
00135     out << "\nDphi_k = "  << Dphi_k << std::endl;
00136   }
00137   TEUCHOS_TEST_FOR_EXCEPTION(
00138     Dphi_k >= 0, LineSearchFailure
00139     ,"LineSearchNLE_Step::do_step(...) : " 
00140     "Error, d_k is not a descent direction for the merit function "
00141     "since Dphi_k = " << Dphi_k << " >= 0" );
00142 
00143   // //////////////////////
00144   // Do the line search
00145   
00146   nlp.unset_quantities();
00147   nlp.set_c( &c_kp1 );
00148   ConstrainedOptPack::MeritFuncCalcNLE    phi_c_calc( &phi_c, &nlp );
00149   const Vector*                           xd[2] = { &x_k, &d_k };
00150   MeritFuncCalc1DQuadratic                phi_calc_1d( phi_c_calc, 1, xd, &x_kp1 );
00151   
00152   if( !direct_line_search().do_line_search(
00153       phi_calc_1d, phi_k, &alpha_k, &phi_kp1
00154       ,( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS)
00155          ? &out : static_cast<std::ostream*>(0) )
00156       )
00157     )
00158   {
00159     // The line search failed!
00160     if( static_cast<int>(olevel) >= static_cast<int>(PRINT_BASIC_ALGORITHM_INFO) )
00161       out
00162         << "\nThe maximum number of linesearch iterations has been exceeded "
00163         << "(k = " << algo.state().k() << ")\n"
00164         << "(phi_k - phi_kp1)/phi_k = " << ((phi_k - phi_kp1)/phi_k)
00165         << "\nso we will reject the step and declare a line search failure.\n";
00166     TEUCHOS_TEST_FOR_EXCEPTION(
00167       true, LineSearchFailure
00168       ,"LineSearchNLE_Step::do_step(): Line search failure" );
00169   }
00170 
00171   nlp.unset_quantities();
00172   
00173   if( (int)olevel >= (int)PRINT_ALGORITHM_STEPS ) {
00174     out << "\nalpha_k                = " << alpha_k;
00175     out << "\n||x_kp1||inf           = " << x_kp1.norm_inf();
00176     out << "\n||c_kp1||inf           = " << c_kp1.norm_inf();
00177     out << "\nphi_kp1                = " << phi_kp1;
00178     out << std::endl;
00179   }
00180   
00181   if( (int)olevel >= (int)PRINT_VECTORS ) {
00182     out << "\nx_kp1 =\n" << x_kp1;
00183     out << "\nc_kp1 =\n" << c_kp1;
00184   }
00185 
00186   return true;
00187 }
00188 
00189 void LineSearchNLE_Step::print_step(
00190   const Algorithm& algo, poss_type step_poss, IterationPack::EDoStepType type, poss_type assoc_step_poss
00191   ,std::ostream& out, const std::string& L
00192   ) const
00193 {
00194   out
00195     << L << "*** Preform a line search for c(x_k + alpha_k*d_k) along the full space search direction d_k.\n"
00196     << L << "ToDo: Fill this in!\n";
00197 }
00198 
00199 } // end namespace MoochoPack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines