MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MoochoPack_CheckBasisFromCPy_Step.cpp
Go to the documentation of this file.
1 #if 0
2 
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
7 // Copyright (2003) Sandia Corporation
8 //
9 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
10 // license for use of this work by or on behalf of the U.S. Government.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
40 //
41 // ***********************************************************************
42 // @HEADER
43 
44 #include <typeinfo>
45 
46 #include "MoochoPack/src/std/CheckBasisFromCPy_Step.h"
50 #include "ConstrainedOptPack/src/VectorWithNorms.h"
51 #include "AbstractLinAlgPack/src/AbstractLinAlgPack_MatrixOp.hpp"
55 #include "Midynamic_cast_verbose.h"
56 #include "MiWorkspacePack.h"
57 
58 namespace LinAlgOpPack {
60 }
61 
62 namespace MoochoPack {
63 
64 CheckBasisFromCPy_Step::CheckBasisFromCPy_Step(
65  const new_basis_strategy_ptr_t& new_basis_strategy
66  ,value_type max_basis_cond_change_frac
67  )
68  : new_basis_strategy_(new_basis_strategy)
69  , max_basis_cond_change_frac_( max_basis_cond_change_frac )
70 {
71  reset();
72 }
73 
74 void CheckBasisFromCPy_Step::reset() {
76 }
77 
78 // Overridden
79 
80 bool CheckBasisFromCPy_Step::do_step( Algorithm& _algo, poss_type step_poss
81  , IterationPack::EDoStepType type, poss_type assoc_step_poss )
82 {
84  using LinAlgOpPack::V_MtV;
85  using LinAlgOpPack::Vp_V;
86  using Teuchos::dyn_cast;
87  using Teuchos::Workspace;
89 
90  NLPAlgo &algo = rsqp_algo(_algo);
91  NLPAlgoState &s = algo.rsqp_state();
92  Range1D decomp = s.con_decomp();
93 
94  EJournalOutputLevel olevel = algo.algo_cntr().journal_output_level();
95  std::ostream& out = algo.track().journal_out();
96 
97  // print step header.
98  if( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) ) {
100  print_algorithm_step( algo, step_poss, type, assoc_step_poss, out );
101  }
102 
103  bool select_new_basis = false;
104 
105  // Compute: resid = (Gc(decomp)'*Y) * py + c(decomp)
106  DVectorSlice py_k = s.py().get_k(0)();
107  DVectorSlice c_k = s.c().get_k(0)();
108  DVectorSlice c_decomp_k = c_k(decomp);
109  Workspace<value_type> resid_ws(wss,py_k.size());
110  DVectorSlice resid(&resid_ws[0],resid_ws.size());
111  {
112 #ifdef _WINDOWS
113  DecompositionSystemVarReduct
114  &decomp_sys = dynamic_cast<DecompositionSystemVarReduct&>(s.decomp_sys());
115 #else
116  DecompositionSystemVarReduct
117  &decomp_sys = dyn_cast<DecompositionSystemVarReduct>(s.decomp_sys());
118 #endif
119  V_MtV( &resid, decomp_sys.C(), BLAS_Cpp::no_trans, py_k );
120  Vp_V( &resid, c_decomp_k );
121  }
122 
123  const value_type
125  nrm_resid = norm_inf(resid),
126  nrm_c_decomp = norm_inf(c_decomp_k),
127  beta = nrm_resid / (nrm_c_decomp+small_num);
128 
129  if( (int)olevel >= (int)PRINT_ALGORITHM_STEPS ) {
130  out << "\nbeta = ||(Gc(decomp)'*Y)*py_k + c_k(decomp)||inf / (||c_k(decomp)||inf + small_number)"
131  << "\n = "<<nrm_resid<<" / ("<<nrm_c_decomp<<" + "<<small_num<<")"
132  << "\n = " << beta << std::endl;
133  }
134 
135  if( beta != 0.0 ) {
136  if( beta < beta_min_ ) {
137  beta_min_ = beta;
138  }
139  else {
140  if( beta / beta_min_ > max_basis_cond_change_frac() ) {
141  if( (int)olevel >= (int)PRINT_BASIC_ALGORITHM_INFO ) {
142  out
143  << "\nbeta_change = ( ||R*py+c||/||c|| = " << beta
144  << " ) / ( min ||R*py+c||/||c|| = " << beta_min_ << " )\n"
145  << " = " << (beta/beta_min_) << " > max_basis_cond_change_frac = "
146  << max_basis_cond_change_frac()
147  << "\n\nSelecting a new basis"
148  << " (k = " << algo.state().k() << ") ...\n";
149  }
150  select_new_basis = true;
151  }
152  }
153  if(select_new_basis) {
154  // reset memory
155  // ToDo: You really need to check to see if someone else
156  // changed the basis also.
158  return new_basis_strategy().transistion_basis(algo,step_poss,type,assoc_step_poss);
159  }
160  }
161  return true;
162 }
163 
164 void CheckBasisFromCPy_Step::print_step( const Algorithm& algo, poss_type step_poss
165  , IterationPack::EDoStepType type, poss_type assoc_step_poss
166  , std::ostream& out, const std::string& L ) const
167 {
168  out
169  << L << "*** Try to detect when the basis is becomming illconditioned\n"
170  << L << "default: beta_min = inf\n"
171  << L << " max_basis_cond_change_frac = " << max_basis_cond_change_frac() << std::endl
172  << L << "beta = norm_inf((Gc(decomp)'*Y)*py_k + c_k(decomp)) / (norm_inf(c_k(decomp))+small_number)\n"
173  << L << "select_new_basis = false\n"
174  << L << "if beta < beta_min then\n"
175  << L << " beta_min = beta\n"
176  << L << "else\n"
177  << L << " if beta/ beta_min > max_basis_cond_change_frac then\n"
178  << L << " select_new_basis = true\n"
179  << L << " end\n"
180  << L << "end\n"
181  << L << "if select_new_basis == true then\n"
182  << L << " new basis selection : " << typeName(new_basis_strategy()) << std::endl;
183  new_basis_strategy().print_method( rsqp_algo(algo),step_poss,type,assoc_step_poss,out
184  ,L+" " );
185  out
186  << L << " end new basis selection\n"
187  << L << "end\n";
188 }
189 
190 } // end namespace MoochoPack
191 
192 #endif // 0
std::string typeName(const T &t)
Not transposed.
EJournalOutputLevel
enum for journal output.
T_To & dyn_cast(T_From &from)
std::ostream * out
void print_algorithm_step(const Algorithm &algo, Algorithm::poss_type step_poss, EDoStepType type, Algorithm::poss_type assoc_step_poss, std::ostream &out)
Prints to 'out' the algorithm step.
void Vp_StMtV(VectorMutable *v_lhs, value_type alpha, const MatrixOp &M_rhs1, BLAS_Cpp::Transp trans_rhs1, const Vector &v_rhs2, value_type beta=1.0)
v_lhs = alpha * op(M_rhs1) * v_rhs2 + beta * v_lhs (BLAS xGEMV)
value_type norm_inf(const SparseVectorSlice< T_Ele > &sv_rhs)
result = ||sv_rhs||inf (BLAS IxAMAX)
DenseLinAlgPack::VectorSliceTmpl< value_type > DVectorSlice
void V_MtV(VectorMutable *v_lhs, const MatrixOp &M_rhs1, BLAS_Cpp::Transp trans_rhs1, const V &V_rhs2)
v_lhs = op(M_rhs1) * V_rhs2.
AbstractLinAlgPack::value_type value_type
value_type norm_inf(const DVectorSlice &vs_rhs)
result = ||vs_rhs||infinity (BLAS IxAMAX)
NLPAlgo & rsqp_algo(Algorithm &algo)
Convert from a Algorithm to a NLPAlgo.
TEUCHOSCORE_LIB_DLL_EXPORT Teuchos::RCP< WorkspaceStore > get_default_workspace_store()
void Vp_V(VectorMutable *v_lhs, const V &V_rhs)
v_lhs += V_rhs.