MOOCHO (Single Doxygen Collection) Version of the Day
MoochoPack_CheckDescentQuasiNormalStep_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 // 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 
00031 #include "MoochoPack_CheckDescentQuasiNormalStep_Step.hpp"
00032 #include "MoochoPack_Exceptions.hpp"
00033 #include "MoochoPack_moocho_algo_conversion.hpp"
00034 #include "IterationPack_print_algorithm_step.hpp"
00035 #include "AbstractLinAlgPack_VectorSpace.hpp"
00036 #include "AbstractLinAlgPack_VectorMutable.hpp"
00037 #include "AbstractLinAlgPack_VectorOut.hpp"
00038 #include "AbstractLinAlgPack_VectorStdOps.hpp"
00039 #include "AbstractLinAlgPack_MatrixOp.hpp"
00040 #include "AbstractLinAlgPack_LinAlgOpPack.hpp"
00041 #include "Teuchos_TestForException.hpp"
00042 
00043 namespace MoochoPack {
00044 
00045 CheckDescentQuasiNormalStep_Step::CheckDescentQuasiNormalStep_Step(
00046   const calc_fd_prod_ptr_t&   calc_fd_prod
00047   )
00048   :calc_fd_prod_(calc_fd_prod)
00049 {}
00050 
00051 bool CheckDescentQuasiNormalStep_Step::do_step(
00052   Algorithm& _algo, poss_type step_poss, IterationPack::EDoStepType type
00053   ,poss_type assoc_step_poss
00054   )
00055 {
00056   using BLAS_Cpp::no_trans;
00057   using AbstractLinAlgPack::dot;
00058   using LinAlgOpPack::V_MtV;
00059 
00060   NLPAlgo         &algo        = rsqp_algo(_algo);
00061   NLPAlgoState    &s           = algo.rsqp_state();
00062   NLP             &nlp         = algo.nlp();
00063   const Range1D   equ_decomp   = s.equ_decomp();
00064 
00065   EJournalOutputLevel olevel = algo.algo_cntr().journal_output_level();
00066   std::ostream& out = algo.track().journal_out();
00067 
00068   // print step header.
00069   if( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) ) {
00070     using IterationPack::print_algorithm_step;
00071     print_algorithm_step( algo, step_poss, type, assoc_step_poss, out );
00072   }
00073   
00074   const size_type
00075     nb = nlp.num_bounded_x();
00076 
00077   // Get iteration quantities
00078   IterQuantityAccess<VectorMutable>
00079     &c_iq   = s.c(),
00080     &Ypy_iq = s.Ypy();
00081   const Vector::vec_ptr_t
00082     cd_k = c_iq.get_k(0).sub_view(equ_decomp);
00083   const Vector
00084     &Ypy_k = Ypy_iq.get_k(0);
00085   
00086   value_type descent_c = -1.0;
00087   if( s.get_iter_quant_id( Gc_name ) != AlgorithmState::DOES_NOT_EXIST ) {
00088     if( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) ) {
00089       out << "\nGc_k exists; compute descent_c = c_k(equ_decomp)'*Gc_k(:,equ_decomp)'*Ypy_k ...\n";
00090     }
00091     const MatrixOp::mat_ptr_t
00092       Gcd_k = s.Gc().get_k(0).sub_view(Range1D(),equ_decomp);
00093     VectorSpace::vec_mut_ptr_t
00094       t = cd_k->space().create_member();
00095     V_MtV( t.get(), *Gcd_k, BLAS_Cpp::trans, Ypy_k );
00096     if( static_cast<int>(olevel) >= static_cast<int>(PRINT_VECTORS) ) {
00097       out << "\nGc_k(:,equ_decomp)'*Ypy_k =\n" << *t;
00098     }
00099     descent_c = dot( *cd_k, *t );
00100   }
00101   else {
00102     if( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) ) {
00103       out << "\nGc_k does not exist; compute descent_c = c_k(equ_decomp)'*FDGc_k(:,equ_decomp)'*Ypy_k "
00104         << "using finite differences ...\n";
00105     }
00106     VectorSpace::vec_mut_ptr_t
00107       t = nlp.space_c()->create_member();
00108     calc_fd_prod().calc_deriv_product(
00109       s.x().get_k(0),nb?&nlp.xl():NULL,nb?&nlp.xu():NULL
00110       ,Ypy_k,NULL,&c_iq.get_k(0),true,&nlp
00111       ,NULL,t.get()
00112       ,static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) ? &out : NULL
00113       );
00114     if( static_cast<int>(olevel) >= static_cast<int>(PRINT_VECTORS) ) {
00115       out << "\nFDGc_k(:,equ_decomp)'*Ypy_k =\n" << *t->sub_view(equ_decomp);
00116     }
00117     descent_c = dot( *cd_k, *t->sub_view(equ_decomp) );
00118   }
00119 
00120   if( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) ) {
00121     out << "\ndescent_c = " << descent_c << std::endl;
00122   }
00123 
00124   if( descent_c > 0.0 ) { // ToDo: add some allowance for > 0.0 for finite difference errors!
00125     if( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) ) {
00126       out << "\nError, descent_c > 0.0; this is not a descent direction\n"
00127         << "Throw TestFailed and terminate the algorithm ...\n";
00128     }
00129     TEST_FOR_EXCEPTION(
00130       true, TestFailed
00131       ,"CheckDescentQuasiNormalStep_Step::do_step(...) : Error, descent for the decomposed constraints "
00132       "with respect to the quasi-normal step c_k(equ_decomp)'*FDGc_k(:,equ_decomp)'*Ypy_k = "
00133       << descent_c << " > 0.0;  This is not a descent direction!\n" );
00134   }
00135 
00136   return true;
00137 }
00138 
00139 void CheckDescentQuasiNormalStep_Step::print_step(
00140   const Algorithm& algo, poss_type step_poss, IterationPack::EDoStepType type
00141   ,poss_type assoc_step_poss, std::ostream& out, const std::string& L
00142   ) const
00143 {
00144   out
00145     << L << "*** Check for descent in the decomposed equality constraints for the quasi-normal step\n"
00146     << L << "if Gc_k exists then\n"
00147     << L << "  descent_c = c_k(equ_decomp)'*Gc_k(:,equ_decomp)'*Ypy_k\n"
00148     << L << "else\n"
00149     << L << "  descent_c = c_k(equ_decomp)'*FDGc(:,equ_decomp)'*Ypy_k (finite diff.)\n"
00150     << L << "end\n"
00151     << L << "if descent > 0.0 then\n"
00152     << L << "  throw TestFailed exception!\n"
00153     << L << "end\n"
00154     ;
00155 }
00156 
00157 } // end namespace MoochoPack
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines