00001 #if 0
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <typeinfo>
00032
00033 #include "MoochoPack/src/std/CheckBasisFromCPy_Step.h"
00034 #include "MoochoPack_moocho_algo_conversion.hpp"
00035 #include "IterationPack_print_algorithm_step.hpp"
00036 #include "ConstrainedOptPack_DecompositionSystemVarReduct.hpp"
00037 #include "ConstrainedOptPack/src/VectorWithNorms.h"
00038 #include "AbstractLinAlgPack/src/AbstractLinAlgPack_MatrixOp.hpp"
00039 #include "DenseLinAlgPack_DVectorClass.hpp"
00040 #include "DenseLinAlgPack_DVectorOp.hpp"
00041 #include "DenseLinAlgPack_LinAlgOpPack.hpp"
00042 #include "Midynamic_cast_verbose.h"
00043 #include "MiWorkspacePack.h"
00044
00045 namespace LinAlgOpPack {
00046 using AbstractLinAlgPack::Vp_StMtV;
00047 }
00048
00049 namespace MoochoPack {
00050
00051 CheckBasisFromCPy_Step::CheckBasisFromCPy_Step(
00052 const new_basis_strategy_ptr_t& new_basis_strategy
00053 ,value_type max_basis_cond_change_frac
00054 )
00055 : new_basis_strategy_(new_basis_strategy)
00056 , max_basis_cond_change_frac_( max_basis_cond_change_frac )
00057 {
00058 reset();
00059 }
00060
00061 void CheckBasisFromCPy_Step::reset() {
00062 beta_min_ = std::numeric_limits<value_type>::max();
00063 }
00064
00065
00066
00067 bool CheckBasisFromCPy_Step::do_step( Algorithm& _algo, poss_type step_poss
00068 , IterationPack::EDoStepType type, poss_type assoc_step_poss )
00069 {
00070 using DenseLinAlgPack::norm_inf;
00071 using LinAlgOpPack::V_MtV;
00072 using LinAlgOpPack::Vp_V;
00073 using Teuchos::dyn_cast;
00074 using Teuchos::Workspace;
00075 Teuchos::WorkspaceStore* wss = Teuchos::get_default_workspace_store().get();
00076
00077 NLPAlgo &algo = rsqp_algo(_algo);
00078 NLPAlgoState &s = algo.rsqp_state();
00079 Range1D decomp = s.con_decomp();
00080
00081 EJournalOutputLevel olevel = algo.algo_cntr().journal_output_level();
00082 std::ostream& out = algo.track().journal_out();
00083
00084
00085 if( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) ) {
00086 using IterationPack::print_algorithm_step;
00087 print_algorithm_step( algo, step_poss, type, assoc_step_poss, out );
00088 }
00089
00090 bool select_new_basis = false;
00091
00092
00093 DVectorSlice py_k = s.py().get_k(0)();
00094 DVectorSlice c_k = s.c().get_k(0)();
00095 DVectorSlice c_decomp_k = c_k(decomp);
00096 Workspace<value_type> resid_ws(wss,py_k.size());
00097 DVectorSlice resid(&resid_ws[0],resid_ws.size());
00098 {
00099 #ifdef _WINDOWS
00100 DecompositionSystemVarReduct
00101 &decomp_sys = dynamic_cast<DecompositionSystemVarReduct&>(s.decomp_sys());
00102 #else
00103 DecompositionSystemVarReduct
00104 &decomp_sys = dyn_cast<DecompositionSystemVarReduct>(s.decomp_sys());
00105 #endif
00106 V_MtV( &resid, decomp_sys.C(), BLAS_Cpp::no_trans, py_k );
00107 Vp_V( &resid, c_decomp_k );
00108 }
00109
00110 const value_type
00111 small_num = std::numeric_limits<value_type>::min(),
00112 nrm_resid = norm_inf(resid),
00113 nrm_c_decomp = norm_inf(c_decomp_k),
00114 beta = nrm_resid / (nrm_c_decomp+small_num);
00115
00116 if( (int)olevel >= (int)PRINT_ALGORITHM_STEPS ) {
00117 out << "\nbeta = ||(Gc(decomp)'*Y)*py_k + c_k(decomp)||inf / (||c_k(decomp)||inf + small_number)"
00118 << "\n = "<<nrm_resid<<" / ("<<nrm_c_decomp<<" + "<<small_num<<")"
00119 << "\n = " << beta << std::endl;
00120 }
00121
00122 if( beta != 0.0 ) {
00123 if( beta < beta_min_ ) {
00124 beta_min_ = beta;
00125 }
00126 else {
00127 if( beta / beta_min_ > max_basis_cond_change_frac() ) {
00128 if( (int)olevel >= (int)PRINT_BASIC_ALGORITHM_INFO ) {
00129 out
00130 << "\nbeta_change = ( ||R*py+c||/||c|| = " << beta
00131 << " ) / ( min ||R*py+c||/||c|| = " << beta_min_ << " )\n"
00132 << " = " << (beta/beta_min_) << " > max_basis_cond_change_frac = "
00133 << max_basis_cond_change_frac()
00134 << "\n\nSelecting a new basis"
00135 << " (k = " << algo.state().k() << ") ...\n";
00136 }
00137 select_new_basis = true;
00138 }
00139 }
00140 if(select_new_basis) {
00141
00142
00143
00144 beta_min_ = std::numeric_limits<value_type>::max();
00145 return new_basis_strategy().transistion_basis(algo,step_poss,type,assoc_step_poss);
00146 }
00147 }
00148 return true;
00149 }
00150
00151 void CheckBasisFromCPy_Step::print_step( const Algorithm& algo, poss_type step_poss
00152 , IterationPack::EDoStepType type, poss_type assoc_step_poss
00153 , std::ostream& out, const std::string& L ) const
00154 {
00155 out
00156 << L << "*** Try to detect when the basis is becomming illconditioned\n"
00157 << L << "default: beta_min = inf\n"
00158 << L << " max_basis_cond_change_frac = " << max_basis_cond_change_frac() << std::endl
00159 << L << "beta = norm_inf((Gc(decomp)'*Y)*py_k + c_k(decomp)) / (norm_inf(c_k(decomp))+small_number)\n"
00160 << L << "select_new_basis = false\n"
00161 << L << "if beta < beta_min then\n"
00162 << L << " beta_min = beta\n"
00163 << L << "else\n"
00164 << L << " if beta/ beta_min > max_basis_cond_change_frac then\n"
00165 << L << " select_new_basis = true\n"
00166 << L << " end\n"
00167 << L << "end\n"
00168 << L << "if select_new_basis == true then\n"
00169 << L << " new basis selection : " << typeName(new_basis_strategy()) << std::endl;
00170 new_basis_strategy().print_method( rsqp_algo(algo),step_poss,type,assoc_step_poss,out
00171 ,L+" " );
00172 out
00173 << L << " end new basis selection\n"
00174 << L << "end\n";
00175 }
00176
00177 }
00178
00179 #endif // 0