MoochoPack : Framework for Large-Scale Optimization Algorithms  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MoochoPack_NLPAlgoState.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include <sstream>
43 #include <typeinfo>
44 
45 #include "MoochoPack_NLPAlgoState.hpp"
46 #include "ConstrainedOptPack_MeritFuncNLP.hpp"
47 #include "AbstractLinAlgPack_MatrixSymOp.hpp"
48 #include "AbstractLinAlgPack_MatrixOpNonsing.hpp"
49 #include "Teuchos_dyn_cast.hpp"
50 
51 #include "IterationPack_IterQuantityAccess.hpp"
52 #include "IterationPack_cast_iq.hpp"
53 #include "IterationPack_IterQuantityAccessContiguous.hpp"
54 
55 // NLPAlgoState iteration quantities names
56 
57 // Iteration Info
58 const std::string MoochoPack::num_basis_name = "num_basis";
59 // NLP Problem Info
60 const std::string MoochoPack::x_name = "x";
61 const std::string MoochoPack::f_name = "f";
62 const std::string MoochoPack::Gf_name = "Gf";
63 const std::string MoochoPack::HL_name = "HL";
64 const std::string MoochoPack::c_name = "c";
65 const std::string MoochoPack::Gc_name = "Gc";
66 // Constraint Gradient Null Space / Range Space Decomposition Info
67 const std::string MoochoPack::Y_name = "Y";
68 const std::string MoochoPack::Z_name = "Z";
69 const std::string MoochoPack::R_name = "R";
70 const std::string MoochoPack::Uy_name = "Uy";
71 const std::string MoochoPack::Uz_name = "Uz";
72 // Search Direction Info
73 const std::string MoochoPack::py_name = "py";
74 const std::string MoochoPack::Ypy_name = "Ypy";
75 const std::string MoochoPack::pz_name = "pz";
76 const std::string MoochoPack::Zpz_name = "Zpz";
77 const std::string MoochoPack::d_name = "d";
78 // Reduced QP Subproblem Info
79 const std::string MoochoPack::rGf_name = "rGf";
80 const std::string MoochoPack::rHL_name = "rHL";
81 const std::string MoochoPack::w_name = "w";
82 const std::string MoochoPack::zeta_name = "zeta";
83 const std::string MoochoPack::qp_grad_name = "qp_grad";
84 const std::string MoochoPack::eta_name = "eta";
85 // Global Convergence Info
86 const std::string MoochoPack::alpha_name = "alpha";
87 const std::string MoochoPack::merit_func_nlp_name = "merit_func_nlp";
88 const std::string MoochoPack::mu_name = "mu";
89 const std::string MoochoPack::phi_name = "phi";
90 // KKT Info
91 const std::string MoochoPack::opt_kkt_err_name = "opt_kkt_err";
92 const std::string MoochoPack::feas_kkt_err_name = "feas_kkt_err";
93 const std::string MoochoPack::comp_kkt_err_name = "comp_kkt_err";
94 const std::string MoochoPack::GL_name = "GL";
95 const std::string MoochoPack::rGL_name = "rGL";
96 const std::string MoochoPack::lambda_name = "lambda";
97 const std::string MoochoPack::nu_name = "nu";
98 
99 namespace MoochoPack {
100 
101 // Constructors / initializers
102 
104 {
105  space_range_ = space_range;
106  update_vector_factories(VST_SPACE_RANGE,space_range);
107 }
108 
110 {
111  space_null_ = space_null;
112  update_vector_factories(VST_SPACE_NULL,space_null);
113 }
114 
116  const decomp_sys_ptr_t& decomp_sys
117  ,const vec_space_ptr_t& space_x
118  ,const vec_space_ptr_t& space_c
119  ,const vec_space_ptr_t& space_range
120  ,const vec_space_ptr_t& space_null
121  )
122  :decomp_sys_(decomp_sys)
123  ,space_x_(space_x)
124  ,space_c_(space_c)
125  ,space_range_(space_range)
126  ,space_null_(space_null)
127 {}
128 
129 // Iteration Info
130 
131 STATE_INDEX_IQ_DEF( NLPAlgoState, num_basis, num_basis_name )
132 
133 // NLP Problem Info
134 
135 STATE_VECTOR_IQ_DEF( NLPAlgoState, x, x_name, get_space_x(), VST_SPACE_X )
136 STATE_SCALAR_IQ_DEF( NLPAlgoState, f, f_name )
137 STATE_IQ_DEF( NLPAlgoState, MatrixSymOp, HL, HL_name )
138 STATE_VECTOR_IQ_DEF( NLPAlgoState, Gf, Gf_name, get_space_x(), VST_SPACE_X )
139 STATE_VECTOR_IQ_DEF( NLPAlgoState, c, c_name, get_space_c(), VST_SPACE_C )
140 STATE_IQ_DEF( NLPAlgoState, MatrixOp, Gc, Gc_name )
141 
142 // Constraint Gradient Null Space / Range Space Decomposition Info
143 
144 STATE_IQ_DEF( NLPAlgoState, MatrixOp, Y, Y_name )
145 STATE_IQ_DEF( NLPAlgoState, MatrixOp, Z, Z_name )
146 STATE_IQ_DEF( NLPAlgoState, MatrixOpNonsing, R, R_name )
147 STATE_IQ_DEF( NLPAlgoState, MatrixOp, Uy, Uy_name )
148 STATE_IQ_DEF( NLPAlgoState, MatrixOp, Uz, Uz_name )
149 
150 // Search Direction Info
151 
152 STATE_VECTOR_IQ_DEF( NLPAlgoState, py, py_name, get_space_range(), VST_SPACE_RANGE )
153 STATE_VECTOR_IQ_DEF( NLPAlgoState, Ypy, Ypy_name, get_space_x(), VST_SPACE_X )
154 STATE_VECTOR_IQ_DEF( NLPAlgoState, pz, pz_name, get_space_null(), VST_SPACE_NULL )
155 STATE_VECTOR_IQ_DEF( NLPAlgoState, Zpz, Zpz_name, get_space_x(), VST_SPACE_X )
156 STATE_VECTOR_IQ_DEF( NLPAlgoState, d, d_name, get_space_x(), VST_SPACE_X )
157 
158 // QP Subproblem Info
159 
160 STATE_VECTOR_IQ_DEF( NLPAlgoState, rGf, rGf_name, get_space_null(), VST_SPACE_NULL )
161 STATE_IQ_DEF( NLPAlgoState, MatrixSymOp, rHL, rHL_name )
162 STATE_VECTOR_IQ_DEF( NLPAlgoState, w, w_name, get_space_null(), VST_SPACE_NULL )
163 STATE_SCALAR_IQ_DEF( NLPAlgoState, zeta, zeta_name )
164 STATE_VECTOR_IQ_DEF( NLPAlgoState, qp_grad, qp_grad_name, get_space_null(), VST_SPACE_NULL )
165 STATE_SCALAR_IQ_DEF( NLPAlgoState, eta, eta_name )
166 
167 // Global Convergence Info
168 
169 STATE_SCALAR_IQ_DEF( NLPAlgoState, alpha, alpha_name )
170 STATE_IQ_DEF( NLPAlgoState, MeritFuncNLP, merit_func_nlp, merit_func_nlp_name )
171 STATE_SCALAR_IQ_DEF( NLPAlgoState, mu, mu_name )
172 STATE_SCALAR_IQ_DEF( NLPAlgoState, phi, phi_name )
173 
174 // KKT Info
175 
176 STATE_SCALAR_IQ_DEF( NLPAlgoState, opt_kkt_err, opt_kkt_err_name )
177 STATE_SCALAR_IQ_DEF( NLPAlgoState, feas_kkt_err, feas_kkt_err_name )
178 STATE_SCALAR_IQ_DEF( NLPAlgoState, comp_kkt_err, comp_kkt_err_name )
179 STATE_VECTOR_IQ_DEF( NLPAlgoState, GL, GL_name, get_space_x(), VST_SPACE_X )
180 STATE_VECTOR_IQ_DEF( NLPAlgoState, rGL, rGL_name, get_space_null(), VST_SPACE_NULL )
181 STATE_VECTOR_IQ_DEF( NLPAlgoState, lambda, lambda_name, get_space_c(), VST_SPACE_C )
182 STATE_VECTOR_IQ_DEF( NLPAlgoState, nu, nu_name, get_space_x(), VST_SPACE_X )
183 
184 // protected
185 
186 void NLPAlgoState::update_iq_id(
187  const std::string& iq_name
188  ,iq_id_encap* iq_id
189  ) const
190 {
191  namespace rcp = MemMngPack;
192  if(iq_id->iq_id == DOES_NOT_EXIST)
193  iq_id->iq_id = this->get_iter_quant_id(iq_name);
195  iq_id->iq_id == DOES_NOT_EXIST, DoesNotExist
196  ,"NLPAlgoState::update_iq_id(iq_name,iq_id) : Error, "
197  " The iteration quantity with name \'" << iq_name <<
198  "\' does not exist!" );
199 }
200 
202  const std::string& iq_name
203  ,iq_id_encap* iq_id
204  )
205 {
206  namespace rcp = MemMngPack;
207  if(iq_id->iq_id == DOES_NOT_EXIST) {
208  iq_id_type
209  _iq_id = this->get_iter_quant_id(iq_name);
210  if(_iq_id == DOES_NOT_EXIST) {
211  iq_id->iq_id = this->set_iter_quant(
212  iq_name
213  ,Teuchos::rcp(
214  new IterQuantityAccessContiguous<index_type>(
215  1
216  ,iq_name
217 #ifdef _MIPS_CXX
220 #endif
221  )
222  )
223  );
224  }
225  else {
226  iq_id->iq_id = _iq_id;
227  }
228  }
229 }
230 
232  const std::string& iq_name
233  ,iq_id_encap* iq_id
234  )
235 {
236  namespace rcp = MemMngPack;
237  if(iq_id->iq_id == DOES_NOT_EXIST) {
238  iq_id_type
239  _iq_id = this->get_iter_quant_id(iq_name);
240  if(_iq_id == DOES_NOT_EXIST) {
241  iq_id->iq_id = this->set_iter_quant(
242  iq_name
243  ,Teuchos::rcp(
244  new IterQuantityAccessContiguous<value_type>(
245  1
246  ,iq_name
247 #ifdef _MIPS_CXX
250 #endif
251  )
252  )
253  );
254  }
255  else {
256  iq_id->iq_id = _iq_id;
257  }
258  }
259 }
260 
262  const std::string& iq_name
263  ,const VectorSpace::space_ptr_t& vec_space
264  ,EVecSpaceType vec_space_type
265  ,iq_id_encap* iq_id
266  )
267 {
268  namespace rcp = MemMngPack;
269  if(iq_id->iq_id == DOES_NOT_EXIST) {
270  iq_id_type
271  _iq_id = this->get_iter_quant_id(iq_name);
272  if(_iq_id == DOES_NOT_EXIST) {
273  iq_id->iq_id = this->set_iter_quant(
274  iq_name
275  ,Teuchos::rcp(
276  new IterQuantityAccessContiguous<VectorMutable>(
277  1
278  ,iq_name
279  ,vec_space
280  )
281  )
282  );
283  }
284  else {
285  iq_id->iq_id = _iq_id;
286  }
287  // Record the list of vectors for a given vector space.
288  vector_iqs_lists_[vec_space_type].push_back(iq_id->iq_id);
289  }
290 }
291 
292 // private
293 
294 void NLPAlgoState::update_vector_factories(
295  EVecSpaceType vec_space_type
296  ,const vec_space_ptr_t& vec_space
297  )
298 {
299  using Teuchos::dyn_cast;
300  iq_vector_list_t &iq_vector_list = vector_iqs_lists_[vec_space_type];
301  for( iq_vector_list_t::const_iterator iq_itr = iq_vector_list.begin(); iq_itr != iq_vector_list.end(); ++iq_itr )
302  dyn_cast<IterQuantityAccessContiguous<VectorMutable> >(this->iter_quant(*iq_itr)).set_factory(vec_space);
303 }
304 
305 } // end namespace MoochoPack
#define STATE_SCALAR_IQ_DEF(CLASS, NAME, NAME_STR)
Add class definitions for a value_type iteration quantity.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
T_To & dyn_cast(T_From &from)
#define STATE_INDEX_IQ_DEF(CLASS, NAME, NAME_STR)
Add class definitions for a index_type iteration quantity.
void update_vector_iq_id(const std::string &iq_name, const VectorSpace::space_ptr_t &vec_space, EVecSpaceType vec_space_type, iq_id_encap *iq_id)
virtual iq_id_type set_iter_quant(const std::string &iq_name, const IQ_ptr &iq)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
#define STATE_VECTOR_IQ_DEF(CLASS, NAME, NAME_STR, VEC_SPC, VEC_RN)
Add class definitions for a VectorMutable iteration quantity.
virtual IterQuantity & iter_quant(const std::string &iq_name)
void update_value_type_iq_id(const std::string &iq_name, iq_id_encap *iq_id)
Reduced space SQP state encapsulation interface.
virtual iq_id_type get_iter_quant_id(const std::string &iq_name) const
#define STATE_IQ_DEF(CLASS, TYPE, NAME, NAME_STR)
Add class definitions for an arbitrary iteration quantity.
void set_space_null(const vec_space_ptr_t &space_null)
Set the VectorSpace of the null space (pz).
void update_index_type_iq_id(const std::string &iq_name, iq_id_encap *iq_id)
NLPAlgoState(const decomp_sys_ptr_t &decomp_sys=Teuchos::null, const vec_space_ptr_t &space_x=Teuchos::null, const vec_space_ptr_t &space_c=Teuchos::null, const vec_space_ptr_t &space_range=Teuchos::null, const vec_space_ptr_t &space_null=Teuchos::null)
Construct.
void set_space_range(const vec_space_ptr_t &space_range)
Set the VectorSpace of the range space (py).