|
MoochoPack : Framework for Large-Scale Optimization Algorithms Version of the Day
|
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 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 00028 00029 #include <ostream> 00030 #include <typeinfo> 00031 00032 #include "MoochoPack_LineSearchNLE_Step.hpp" 00033 #include "MoochoPack_Exceptions.hpp" 00034 #include "MoochoPack_moocho_algo_conversion.hpp" 00035 #include "IterationPack_print_algorithm_step.hpp" 00036 #include "ConstrainedOptPack_MeritFuncNLESqrResid.hpp" 00037 #include "ConstrainedOptPack_MeritFuncCalcNLE.hpp" 00038 #include "ConstrainedOptPack_MeritFuncCalc1DQuadratic.hpp" 00039 #include "AbstractLinAlgPack_VectorMutable.hpp" 00040 #include "AbstractLinAlgPack_VectorStdOps.hpp" 00041 #include "AbstractLinAlgPack_VectorOut.hpp" 00042 #include "AbstractLinAlgPack_assert_print_nan_inf.hpp" 00043 #include "AbstractLinAlgPack_LinAlgOpPack.hpp" 00044 #include "Teuchos_TestForException.hpp" 00045 00046 namespace MoochoPack { 00047 00048 LineSearchNLE_Step::LineSearchNLE_Step( 00049 const direct_line_search_ptr_t& direct_line_search 00050 ) 00051 :direct_line_search_(direct_line_search) 00052 {} 00053 00054 bool LineSearchNLE_Step::do_step( 00055 Algorithm& _algo, poss_type step_poss, IterationPack::EDoStepType type 00056 ,poss_type assoc_step_poss 00057 ) 00058 { 00059 using AbstractLinAlgPack::Vp_StV; 00060 using LinAlgOpPack::V_VpV; 00061 00062 NLPAlgo &algo = rsqp_algo(_algo); 00063 NLPAlgoState &s = algo.rsqp_state(); 00064 NLP &nlp = algo.nlp(); 00065 00066 EJournalOutputLevel olevel = algo.algo_cntr().journal_output_level(); 00067 std::ostream& out = algo.track().journal_out(); 00068 00069 // print step header. 00070 if( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) ) { 00071 using IterationPack::print_algorithm_step; 00072 print_algorithm_step( algo, step_poss, type, assoc_step_poss, out ); 00073 } 00074 00075 const size_type 00076 //n = nlp.n(), 00077 m = nlp.m(); 00078 00079 TEST_FOR_EXCEPTION( m == 0, std::logic_error, "LineSearchNLE_Step::do_step(...) : Error!" ); 00080 00081 // ///////////////////////////////////////// 00082 // Set references to iteration quantities 00083 // 00084 // Set k+1 first then go back to get k+0 to ensure 00085 // we have backward storage! 00086 00087 IterQuantityAccess<value_type> 00088 &alpha_iq = s.alpha(); 00089 IterQuantityAccess<VectorMutable> 00090 &x_iq = s.x(), 00091 &d_iq = s.d(), 00092 &c_iq = s.c(); 00093 00094 VectorMutable &x_kp1 = x_iq.get_k(+1); 00095 const Vector &x_k = x_iq.get_k(0); 00096 VectorMutable &c_kp1 = c_iq.get_k(+1); 00097 const Vector &c_k = c_iq.get_k(0); 00098 const Vector &d_k = d_iq.get_k(0); 00099 value_type &alpha_k = alpha_iq.get_k(0); 00100 00101 // ////////////////////////////////////// 00102 // Build the merit function 00103 00104 ConstrainedOptPack::MeritFuncNLESqrResid phi_c; 00105 00106 // ///////////////////////////////////// 00107 // Compute Dphi_k, phi_kp1 and phi_k 00108 00109 // phi_k, phi_kp1 00110 const value_type phi_k = phi_c.value(c_k); 00111 value_type phi_kp1 = phi_c.value(c_kp1); 00112 if( (int)olevel >= (int)PRINT_ALGORITHM_STEPS ) { 00113 out << "\nBegin definition of NLP merit function phi_c.value(c(x)):\n"; 00114 phi_c.print_merit_func( out, " " ); 00115 out << "end definition of the NLP merit funciton\n"; 00116 } 00117 // Dphi_k 00118 phi_c.calc_deriv(c_k); 00119 const value_type 00120 Dphi_k = phi_c.deriv(); 00121 if( (int)olevel >= (int)PRINT_ALGORITHM_STEPS ) { 00122 out << "\nDphi_k = " << Dphi_k << std::endl; 00123 } 00124 TEST_FOR_EXCEPTION( 00125 Dphi_k >= 0, LineSearchFailure 00126 ,"LineSearchNLE_Step::do_step(...) : " 00127 "Error, d_k is not a descent direction for the merit function " 00128 "since Dphi_k = " << Dphi_k << " >= 0" ); 00129 00130 // ////////////////////// 00131 // Do the line search 00132 00133 nlp.unset_quantities(); 00134 nlp.set_c( &c_kp1 ); 00135 ConstrainedOptPack::MeritFuncCalcNLE phi_c_calc( &phi_c, &nlp ); 00136 const Vector* xd[2] = { &x_k, &d_k }; 00137 MeritFuncCalc1DQuadratic phi_calc_1d( phi_c_calc, 1, xd, &x_kp1 ); 00138 00139 if( !direct_line_search().do_line_search( 00140 phi_calc_1d, phi_k, &alpha_k, &phi_kp1 00141 ,( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) 00142 ? &out : static_cast<std::ostream*>(0) ) 00143 ) 00144 ) 00145 { 00146 // The line search failed! 00147 if( static_cast<int>(olevel) >= static_cast<int>(PRINT_BASIC_ALGORITHM_INFO) ) 00148 out 00149 << "\nThe maximum number of linesearch iterations has been exceeded " 00150 << "(k = " << algo.state().k() << ")\n" 00151 << "(phi_k - phi_kp1)/phi_k = " << ((phi_k - phi_kp1)/phi_k) 00152 << "\nso we will reject the step and declare a line search failure.\n"; 00153 TEST_FOR_EXCEPTION( 00154 true, LineSearchFailure 00155 ,"LineSearchNLE_Step::do_step(): Line search failure" ); 00156 } 00157 00158 nlp.unset_quantities(); 00159 00160 if( (int)olevel >= (int)PRINT_ALGORITHM_STEPS ) { 00161 out << "\nalpha_k = " << alpha_k; 00162 out << "\n||x_kp1||inf = " << x_kp1.norm_inf(); 00163 out << "\n||c_kp1||inf = " << c_kp1.norm_inf(); 00164 out << "\nphi_kp1 = " << phi_kp1; 00165 out << std::endl; 00166 } 00167 00168 if( (int)olevel >= (int)PRINT_VECTORS ) { 00169 out << "\nx_kp1 =\n" << x_kp1; 00170 out << "\nc_kp1 =\n" << c_kp1; 00171 } 00172 00173 return true; 00174 } 00175 00176 void LineSearchNLE_Step::print_step( 00177 const Algorithm& algo, poss_type step_poss, IterationPack::EDoStepType type, poss_type assoc_step_poss 00178 ,std::ostream& out, const std::string& L 00179 ) const 00180 { 00181 out 00182 << L << "*** Preform a line search for c(x_k + alpha_k*d_k) along the full space search direction d_k.\n" 00183 << L << "ToDo: Fill this in!\n"; 00184 } 00185 00186 } // end namespace MoochoPack
1.7.4