MoochoPack : Framework for Large-Scale Optimization Algorithms Version of the Day
MoochoPack_CheckDecompositionFromPy_Step.cpp
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 <typeinfo>
00030 #include <limits>
00031 
00032 #include "MoochoPack_CheckDecompositionFromPy_Step.hpp"
00033 #include "MoochoPack_moocho_algo_conversion.hpp"
00034 #include "IterationPack_print_algorithm_step.hpp"
00035 #include "AbstractLinAlgPack_Vector.hpp"
00036 #include "AbstractLinAlgPack_LinAlgOpPack.hpp"
00037 
00038 namespace MoochoPack {
00039 
00040 CheckDecompositionFromPy_Step::CheckDecompositionFromPy_Step(
00041   const new_decomp_strategy_ptr_t   &new_decomp_strategy
00042   ,value_type                       max_decomposition_cond_change_frac
00043   )
00044   :new_decomp_strategy_(new_decomp_strategy)
00045   ,max_decomposition_cond_change_frac_( max_decomposition_cond_change_frac )
00046   ,max_cond_( 0.01 / std::numeric_limits<value_type>::epsilon() )
00047 {
00048   reset();
00049 }
00050 
00051 void CheckDecompositionFromPy_Step::reset() {
00052   beta_min_ = std::numeric_limits<value_type>::max();
00053 }
00054 
00055 // Overridden
00056 
00057 bool CheckDecompositionFromPy_Step::do_step(
00058   Algorithm& _algo, poss_type step_poss, IterationPack::EDoStepType type, poss_type assoc_step_poss
00059   )
00060 {
00061   NLPAlgo                &algo       = rsqp_algo(_algo);
00062   NLPAlgoState               &s          = algo.rsqp_state();
00063   const Range1D           equ_decomp  = s.equ_decomp();
00064   EJournalOutputLevel     olevel      = algo.algo_cntr().journal_output_level();
00065   std::ostream            &out        = algo.track().journal_out();
00066 
00067   // print step header.
00068   if( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) ) {
00069     using IterationPack::print_algorithm_step;
00070     print_algorithm_step( algo, step_poss, type, assoc_step_poss, out );
00071   }
00072 
00073   bool select_new_decomposition = false;
00074 
00075   const value_type
00076     small_num = std::numeric_limits<value_type>::min(),
00077     beta = s.py().get_k(0).norm_inf() / (s.c().get_k(0).sub_view(equ_decomp)->norm_inf()+small_num);
00078 
00079   if( (int)olevel >= (int)PRINT_ALGORITHM_STEPS ) {
00080     out << "\nbeta = ||py||/||c|| = " << beta << std::endl;
00081   }
00082 
00083   // Check to see if a new basis was selected or not
00084   IterQuantityAccess<index_type>
00085     &num_basis_iq = s.num_basis();
00086   if( num_basis_iq.updated_k(0) ) {
00087     if( (int)olevel >= (int)PRINT_ALGORITHM_STEPS )
00088       out << "\nnum_basis_k was updated so the basis changed so we will skip this check\n"
00089         << "    reset min ||py||/||c|| to current value + 1\n";
00090     beta_min_ = beta + 1.0;
00091     return true;
00092   }
00093 
00094   if( beta + 1.0 < beta_min_ ) {
00095     beta_min_ = beta + 1.0;
00096   }
00097   else {
00098     if( (beta + 1.0)/ beta_min_ > max_decomposition_cond_change_frac() ) {
00099       if( (int)olevel >= (int)PRINT_BASIC_ALGORITHM_INFO ) {
00100         out << "Select a new decomposition"
00101             << " (k = " << algo.state().k() << ")\n"
00102           << "beta_change = ( ||py||/||c|| = " << beta
00103             << " ) / ( min ||py||/||c|| = " << beta_min_ << " )\n"
00104           << "beta_change = " << (beta/beta_min_) << " > max_decomposition_cond_change_frac = "
00105             << max_decomposition_cond_change_frac() << std::endl;
00106       }
00107       select_new_decomposition = true;
00108     }
00109   }
00110   if( !select_new_decomposition && beta > max_cond() ) {
00111     if( (int)olevel >= (int)PRINT_BASIC_ALGORITHM_INFO ) {
00112       out << "\nConditioning of decomposition matrix is > " << beta
00113         << " > max_cond = " << max_cond() << std::endl
00114         << "Selecting a new decomposition ... "
00115         << " (k = " << algo.state().k() << ")\n";
00116     }
00117     select_new_decomposition = true;
00118   }
00119 
00120   if(select_new_decomposition) {
00121     reset();
00122     return new_decomp_strategy().new_decomposition(algo,step_poss,type,assoc_step_poss);
00123   }
00124 
00125   return true;    
00126 }
00127 
00128 void CheckDecompositionFromPy_Step::print_step(
00129   const Algorithm& algo, poss_type step_poss
00130   ,IterationPack::EDoStepType type, poss_type assoc_step_poss
00131   ,std::ostream& out, const std::string& L ) const
00132 {
00133   out
00134     << L << "default: beta_min = inf\n"
00135     << L << "         max_decomposition_cond_change_frac = " << max_decomposition_cond_change_frac() << std::endl
00136     << L << "         max_cond = 0.01 * mach_eps\n"
00137     << L << "beta = norm_inf(py_k) / (norm_inf(c_k(equ_decomp))+small_number)\n"
00138     << L << "select_new_decomposition = false\n"
00139     << L << "if num_basis_k is updated then\n"
00140     << L << "  beta_min = beta + 1\n"
00141     << L << "end\n"
00142     << L << "if beta + 1 < beta_min then\n"
00143     << L << "  beta_min = beta + 1\n"
00144     << L << "else\n"
00145     << L << "  if (beta + 1) / beta_min > max_decomposition_cond_change_frac then\n"
00146     << L << "    select_new_decomposition = true\n"
00147     << L << "  end\n"
00148     << L << "end\n"
00149     << L << "if beta > max_cond then\n"
00150     << L << "  select_new_decomposition = true\n"
00151     << L << "end\n"
00152     << L << "if select_new_decomposition == true then\n"
00153     << L << "  new decomposition selection : " << typeName(new_decomp_strategy()) << std::endl
00154     ;
00155   new_decomp_strategy().print_new_decomposition(
00156     rsqp_algo(algo),step_poss,type,assoc_step_poss,out, L + "    " );
00157   out
00158     << L << "  end new decomposition selection\n"
00159     << L << "end\n"
00160     ;
00161 }
00162 
00163 } // end namespace MoochoPack 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends