NLPInterfacePack: C++ Interfaces and Implementation for Non-Linear Programs  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
NLPInterfacePack_NLP.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 <limits>
43 
44 #include "NLPInterfacePack_NLP.hpp"
45 #include "AbstractLinAlgPack_VectorMutable.hpp"
46 #include "AbstractLinAlgPack_VectorSpace.hpp"
47 #include "Teuchos_Assert.hpp"
48 
49 namespace {
50 const char name_f[] = "f";
51 const char name_c[] = "c";
52 const char name_c_breve[] = "c_breve";
53 const char name_h_breve[] = "h_breve";
54 NLPInterfacePack::NLP::options_ptr_t null_options = Teuchos::null;
55 } // end namespace
56 
57 namespace NLPInterfacePack {
58 
59 // static
60 
61 value_type NLP::infinite_bound()
62 {
63 // return std::numeric_limits<value_type>::max();
64  return 1e+50;
65 }
66 
67 // constructors
68 
70 {}
71 
72 // destructor
73 
75 {}
76 
77 void NLP::set_options( const options_ptr_t& options )
78 {}
79 
80 const NLP::options_ptr_t&
82 {
83  return null_options;
84 }
85 
86 void NLP::initialize(bool test_setup)
87 {
88  num_f_evals_ = num_c_evals_ = 0;
89 }
90 
91 // dimensionality
92 
94 NLP::n() const
95 {
96  return this->space_x()->dim();
97 }
98 
99 size_type
100 NLP::m() const
101 {
102  VectorSpace::space_ptr_t spc = this->space_c();
103  return spc.get() ? spc->dim() : 0;
104 }
105 
106 // initial guess
107 
109  VectorMutable* lambda
110  ,VectorMutable* nu
111  ) const
112 {
113 #ifdef TEUCHOS_DEBUG
114  TEUCHOS_TEST_FOR_EXCEPTION( lambda && this->m() == 0, std::logic_error, "" );
115  TEUCHOS_TEST_FOR_EXCEPTION( nu && this->num_bounded_x() == 0, std::logic_error, "" );
116 #endif
117  if(lambda) {
118 #ifdef TEUCHOS_DEBUG
119  TEUCHOS_TEST_FOR_EXCEPTION( !this->space_c()->is_compatible(lambda->space()), VectorSpace::IncompatibleVectorSpaces, "" );
120 #endif
121  *lambda = 0.0;
122  }
123  if(nu) {
124 #ifdef TEUCHOS_DEBUG
125  TEUCHOS_TEST_FOR_EXCEPTION( !this->space_x()->is_compatible(nu->space()), VectorSpace::IncompatibleVectorSpaces, "" );
126 #endif
127  *nu = 0.0;
128  }
129 }
130 
131 // <<std comp>> members for f
132 
133 void NLP::set_f(value_type* f)
134 {
135  first_order_info_.f = f;
136 }
137 
138 value_type* NLP::get_f()
139 {
140  return StandardCompositionRelationshipsPack::get_role_name(first_order_info_.f, false, name_f);
141 }
142 
143 value_type& NLP::f()
144 {
145  return StandardCompositionRelationshipsPack::role_name(first_order_info_.f, false, name_f);
146 }
147 
148 const value_type& NLP::f() const
149 {
150  return StandardCompositionRelationshipsPack::role_name(first_order_info_.f, false, name_f);
151 }
152 
153 // <<std comp>> members for c
154 
156 {
157 #ifdef TEUCHOS_DEBUG
158  TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
159  TEUCHOS_TEST_FOR_EXCEPTION( c && !this->space_c()->is_compatible(c->space()), VectorSpace::IncompatibleVectorSpaces, "" );
160 #endif
161  first_order_info_.c = c;
162 }
163 
165 {
166 #ifdef TEUCHOS_DEBUG
167  TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
168 #endif
169  return StandardCompositionRelationshipsPack::get_role_name(first_order_info_.c, false, name_c);
170 }
171 
173 {
174 #ifdef TEUCHOS_DEBUG
175  TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
176 #endif
177  return StandardCompositionRelationshipsPack::role_name(first_order_info_.c, false, name_c);
178 }
179 
180 const Vector& NLP::c() const
181 {
182 #ifdef TEUCHOS_DEBUG
183  TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
184 #endif
185  return StandardCompositionRelationshipsPack::role_name(first_order_info_.c, false, name_c);
186 }
187 
189 {
190  set_f(NULL);
191  if(m()) set_c(NULL);
192  if(m()-ns()) set_c_breve(NULL);
193  if(m()-ns()) set_h_breve(NULL);
194 }
195 
196 // calculations
197 
198 void NLP::calc_f(const Vector& x, bool newx) const
199 {
200  StandardCompositionRelationshipsPack::assert_role_name_set(first_order_info_.f, "NLP::calc_f()", name_f);
201  imp_calc_f(x,newx,zero_order_info());
202  num_f_evals_++;
203 }
204 
205 void NLP::calc_c(const Vector& x, bool newx) const
206 {
207 #ifdef TEUCHOS_DEBUG
208  TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
209 #endif
210  StandardCompositionRelationshipsPack::assert_role_name_set(first_order_info_.c, "NLP::calc_c()", name_c);
211  imp_calc_c(x,newx,zero_order_info());
212  num_c_evals_++;
213 }
214 
216  const Vector& x
217  ,const Vector* lambda
218  ,const Vector* nu
219  ,bool optimal
220  )
221 {
222  // The default behavior is just to ignore the solution!
223 }
224 
226 {
227  return num_f_evals_;
228 }
229 
231 {
232 #ifdef TEUCHOS_DEBUG
233  TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0, std::logic_error, "" );
234 #endif
235  return num_c_evals_;
236 }
237 
238 // General inequalities and slack variables
239 
241 {
243  return space_h_breve.get() ? space_h_breve->dim() : 0;
244 }
245 
247 {
248  return this->space_c();
249 }
250 
252 {
253  return Teuchos::null;
254 }
255 
256 const Vector& NLP::hl_breve() const
257 {
259  true, std::logic_error
260  ,"NLP::hl_breve(): Error, this method must be overridden if space_h_breve is defined" );
261 
262  //execution should never reach this point, but compilers expect a non-void
263  //function to return something, so we'll create a dummy value to use in a
264  //return statement.
265  //(a better design would not require function bodies for unimplemented
266  //functions like this...)
267  Vector* dummy = NULL;
268  return(*dummy);
269 }
270 
271 const Vector& NLP::hu_breve() const
272 {
274  true, std::logic_error
275  ,"NLP::hl_breve(): Error, this method must be overridden if space_h_breve is defined" );
276 
277  //execution should never reach this point, but compilers expect a non-void
278  //function to return something, so we'll create a dummy value to use in a
279  //return statement.
280  //(a better design would not require function bodies for unimplemented
281  //functions like this...)
282  Vector* dummy = NULL;
283  return(*dummy);
284 }
285 
287 {
288 #ifdef TEUCHOS_DEBUG
289  TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
290  TEUCHOS_TEST_FOR_EXCEPTION( c_breve && !this->space_c_breve()->is_compatible(c_breve->space()), VectorSpace::IncompatibleVectorSpaces, "" );
291 #endif
292  first_order_info_breve_.c = c_breve;
293 }
294 
296 {
297 #ifdef TEUCHOS_DEBUG
298  TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
299 #endif
300  return first_order_info_breve_.c;
301 }
302 
304 {
305 #ifdef TEUCHOS_DEBUG
306  TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
307 #endif
308  return StandardCompositionRelationshipsPack::role_name(first_order_info_breve_.c, false, name_c_breve);
309 }
310 
311 const Vector& NLP::c_breve() const
312 {
313 #ifdef TEUCHOS_DEBUG
314  TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
315 #endif
316  return StandardCompositionRelationshipsPack::role_name(first_order_info_breve_.c, false, name_c_breve);
317 }
318 
320 {
321 #ifdef TEUCHOS_DEBUG
322  TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
323  TEUCHOS_TEST_FOR_EXCEPTION( h_breve && !this->space_h_breve()->is_compatible(h_breve->space()), VectorSpace::IncompatibleVectorSpaces, "" );
324 #endif
325  first_order_info_breve_.c = h_breve;
326 }
327 
329 {
330 #ifdef TEUCHOS_DEBUG
331  TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
332 #endif
333  return first_order_info_breve_.h;
334 }
335 
337 {
338 #ifdef TEUCHOS_DEBUG
339  TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
340 #endif
341  return StandardCompositionRelationshipsPack::role_name(first_order_info_breve_.c, false, name_h_breve);
342 }
343 
344 const Vector& NLP::h_breve() const
345 {
346 #ifdef TEUCHOS_DEBUG
347  TEUCHOS_TEST_FOR_EXCEPTION( this->m() - this->ns() == 0, std::logic_error, "" );
348 #endif
349  return StandardCompositionRelationshipsPack::role_name(first_order_info_breve_.c, false, name_h_breve);
350 }
351 
352 const Permutation& NLP::P_var() const
353 {
355 // if(!P_var_.get()) P_var_ = Teuchos::rcp(new PermutationSerial(this->space_x());
356  return *P_var_;
357 }
358 
359 const Permutation& NLP::P_equ() const
360 {
362 // if(!P_equ_.get()) P_equ = Teuchos::rcp(new PermutationSerial(this->space_c());
363  return *P_equ_;
364 }
365 
366 void NLP::calc_c_breve(const Vector& x, bool newx) const
367 {
368 #ifdef TEUCHOS_DEBUG
369  TEUCHOS_TEST_FOR_EXCEPTION( this->m() == 0 || this->ns() > 0, std::logic_error, "" );
370 #endif
371  StandardCompositionRelationshipsPack::assert_role_name_set(first_order_info_breve_.c, "NLP::calc_c_breve()", name_c_breve);
373  num_c_evals_++;
374 }
375 
376 void NLP::calc_h_breve(const Vector& x, bool newx) const
377 {
378 #ifdef TEUCHOS_DEBUG
379  TEUCHOS_TEST_FOR_EXCEPTION( this->ns() == 0, std::logic_error, "" );
380 #endif
381  StandardCompositionRelationshipsPack::assert_role_name_set(first_order_info_breve_.h, "NLP::calc_h_breve()", name_h_breve);
383  num_c_evals_++;
384 }
385 
386 // protected
387 
389  const Vector &x
390  ,bool newx
391  ,const ZeroOrderInfo &zero_order_info_breve
392  ) const
393 {
394  imp_calc_c(x,newx,zero_order_info_breve);
395 }
396 
398  const Vector &x
399  ,bool newx
400  ,const ZeroOrderInfo &zero_order_info_breve
401  ) const
402 {
404  true, std::logic_error
405  ,"NLP::hl_breve(): Error, this method must be overridden if space_h_breve is defined" );
406 }
407 
408 } // namespace NLPInterfacePack
virtual value_type & f()
Returns non-const *this->get_f().
virtual void calc_c(const Vector &x, bool newx=true) const
Update the constraint residual vector for c at the point x and put it in the stored reference...
VectorMutable * c
Pointer to constraints residual c (Will be NULL if not set)
virtual size_type ns() const
Return the number of slack variables (i.e. number of general inequalities).
virtual const VectorSpace & space() const =0
virtual void report_final_solution(const Vector &x, const Vector *lambda, const Vector *nu, bool is_optimal)
Used by the solver to report the final solution and multipliers.
virtual size_type num_c_evals() const
Gives the number of constraint function c(x) evaluations called by the solver since initialize() was ...
virtual VectorMutable & c_breve()
Returns non-const *this->get_c_breve().
virtual VectorMutable * get_c_breve()
Return pointer passed to this->set_c_breve().
virtual vec_space_ptr_t space_x() const =0
Vector space object for unknown variables x (dimension n).
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual vec_space_ptr_t space_c_breve() const
Vector space object for the original equalities c_breve(x_breve)
virtual void get_init_lagrange_mult(VectorMutable *lambda, VectorMutable *nu) const
Get the initial value of the Lagrange multipliers lambda.
T * get() const
const ZeroOrderInfo zero_order_info() const
Return pointer to set quantities.
virtual void imp_calc_c_breve(const Vector &x, bool newx, const ZeroOrderInfo &zero_order_info_breve) const
Overridden to compute c_breve(x_breve) and perhaps f(x) and/or h_breve(x_breve)
virtual void imp_calc_f(const Vector &x, bool newx, const ZeroOrderInfo &zero_order_info) const =0
Overridden to compute f(x) (and perhaps other quantities if set).
virtual void set_options(const options_ptr_t &options)
Set the options that this NLP may be interested in.
virtual void imp_calc_h_breve(const Vector &x, bool newx, const ZeroOrderInfo &zero_order_info_breve) const
Overridden to compute h_breve(x_breve) and perhaps f(x) and/or c_breve(x_breve).
virtual void imp_calc_c(const Vector &x, bool newx, const ZeroOrderInfo &zero_order_info) const =0
Overridden to compute c(x) and perhaps f(x) and/or h(x) (if multiple calculaiton = true)...
virtual void unset_quantities()
Call to unset all storage quantities (both in this class and all subclasses).
virtual const Vector & hl_breve() const
Returns a reference to the vector of lower bounds on the general inequality constraints h_breve(x_bre...
virtual void calc_f(const Vector &x, bool newx=true) const
Update the value for the objective f at the point x and put it in the stored reference.
virtual size_type n() const
Return the number of variables.
virtual vec_space_ptr_t space_c() const =0
Vector space object for general equality constraints c(x) (dimension m).
size_t size_type
const ZeroOrderInfo zero_order_info_breve() const
Return pointer to set hat quantities.
virtual size_type num_bounded_x() const =0
Returns the number of variables in x(i) for which xl(i)> -infinite_bound() or xu(i) < +infinite_bound...
virtual void set_c(VectorMutable *c)
Set a pointer to a vector to be updated when this->calc_c() is called.
virtual value_type * get_f()
Return pointer passed to this->set_f().
Teuchos::RCP< const VectorSpace > vec_space_ptr_t
virtual ~NLP()
Destructor that cleans all the memory it owns.
Struct for objective and constriants (pointer).
virtual const options_ptr_t & get_options() const
Get the OptionsFromStream object being used to extract the options from.
virtual vec_space_ptr_t space_h_breve() const
Vector space object for the original inequalities h_breve(x_breve)
virtual const Permutation & P_var() const
Return the permutation object for the variables.
NLP()
Initialize to no reference set to calculation quanities.
virtual void set_c_breve(VectorMutable *c_breve)
Set a pointer to a vector to be updated when this->calc_c_breve() is called.
virtual const Permutation & P_equ() const
Return the permutation object for the constraints.
virtual const Vector & hu_breve() const
Returns a reference to the vector of upper bounds on the general inequality constraints h_breve(x_bre...
virtual void calc_h_breve(const Vector &x, bool newx=true) const
Update the constraint residual vector for h_breve at the point x and put it in the stored reference...
value_type * f
Pointer to objective function f (Will be NULL if not set)
virtual void set_h_breve(VectorMutable *h_breve)
Set a pointer to a vector to be updated when this->calc_h_breve() is called.
virtual void calc_c_breve(const Vector &x, bool newx=true) const
Update the constraint residual vector for c_breve at the point x and put it in the stored reference...
virtual size_type m() const
Return the number of general equality constraints.
virtual void initialize(bool test_setup=false)
Initialize the NLP before it is used.
virtual size_type num_f_evals() const
Gives the number of object function f(x) evaluations called by the solver since initialize() was call...
virtual VectorMutable * get_h_breve()
Return pointer passed to this->set_h_breve().
static value_type infinite_bound()
Value for an infinite bound.
virtual VectorMutable * get_c()
Return pointer passed to this->set_c().
VectorMutable * h
Pointer to inequality constraints h (Will be NULL if not set)
virtual void set_f(value_type *f)
Set a pointer to an value to be updated when this->calc_f() is called.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
virtual VectorMutable & c()
Returns non-const *this->get_c().
virtual VectorMutable & h_breve()
Returns non-const *this->get_h_breve().