MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NLPInterfacePack_test_nlp_first_order.cpp
Go to the documentation of this file.
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 <assert.h>
43 
60 #include "Teuchos_FancyOStream.hpp"
61 
63  NLPFirstOrder *nlp
65  ,std::ostream *out
66  )
67 {
68  namespace rcp = MemMngPack;
70 
71  bool result;
72  bool success = true;
73 
75  nlpOutputTempState(Teuchos::rcp(nlp,false),Teuchos::getFancyOStream(Teuchos::rcp(out,false)),Teuchos::VERB_LOW);
76 
77  if(out)
78  *out << "\n*********************************"
79  << "\n*** test_nlp_first_order(...) ***"
80  << "\n*********************************\n";
81 
82  nlp->initialize(true);
83 
84  // Test the DVector spaces
85  if(out)
86  *out << "\nTesting the vector spaces ...\n";
87 
88  VectorSpaceTester vec_space_tester;
89  if(options) {
90  VectorSpaceTesterSetOptions
91  opt_setter(&vec_space_tester);
92  opt_setter.set_options(*options);
93  }
94 
95  if(out)
96  *out << "\nTesting nlp->space_x() ...\n";
97  result = vec_space_tester.check_vector_space(*nlp->space_x(),out);
98  if(out) {
99  if(result)
100  *out << "nlp->space_x() checks out!\n";
101  else
102  *out << "nlp->space_x() check failed!\n";
103  }
104  update_success( result, &success );
105 
106  if( nlp->m() ) {
107  if(out)
108  *out << "\nTesting nlp->space_c() ...\n";
109  result = vec_space_tester.check_vector_space(*nlp->space_c(),out);
110  if(out) {
111  if(result)
112  *out << "nlp->space_c() checks out!\n";
113  else
114  *out << "nlp->space_c() check failed!\n";
115  }
116  update_success( result, &success );
117  }
118 
119  // Test the NLP interface first!
120 
121  NLPTester nlp_tester;
122  if(options) {
124  nlp_tester_opt_setter(&nlp_tester);
125  nlp_tester_opt_setter.set_options(*options);
126  }
127  const bool print_all_warnings = nlp_tester.print_all();
128 
129  result = nlp_tester.test_interface(
130  nlp, nlp->xinit(), print_all_warnings, out );
131  update_success( result, &success );
132 
133  // Test the NLPFirstOrder interface now!
134 
135  const size_type
136  n = nlp->n(),
137  m = nlp->m();
138  VectorSpace::vec_mut_ptr_t
139  c = m ? nlp->space_c()->create_member() : Teuchos::null,
140  Gf = nlp->space_x()->create_member();
141  NLPFirstOrder::mat_fcty_ptr_t::element_type::obj_ptr_t
142  Gc = m ? nlp->factory_Gc()->create() : Teuchos::null;
143 
144  if(m) {
145  if(out)
146  *out << "\nCalling nlp->calc_Gc(...) at nlp->xinit() ...\n";
147  nlp->set_Gc( Gc.get() );
148  nlp->calc_Gc( nlp->xinit(), true );
149  if(nlp_tester.print_all()) {
150  *out << "\nGc =\n" << *Gc;
151  }
152  }
153 
154  if(out)
155  *out << "\nCalling nlp->calc_Gf(...) at nlp->xinit() ...\n";
156  nlp->set_Gf( Gf.get() );
157  nlp->calc_Gf( nlp->xinit(), m == 0 );
158  if(nlp_tester.print_all())
159  *out << "\nGf =\n" << *Gf;
160 
162  calc_fd_prod;
163  if(options) {
165  options_setter( &calc_fd_prod );
166  options_setter.set_options(*options);
167  }
169  nlp_first_derivatives_tester(Teuchos::rcp(&calc_fd_prod,false));
170  if(options) {
172  nlp_tester_opt_setter(&nlp_first_derivatives_tester);
173  nlp_tester_opt_setter.set_options(*options);
174  }
175  result = nlp_first_derivatives_tester.finite_diff_check(
176  nlp, nlp->xinit()
177  ,nlp->num_bounded_x() ? &nlp->xl() : NULL
178  ,nlp->num_bounded_x() ? &nlp->xu() : NULL
179  ,Gc.get(), Gf.get()
180  ,print_all_warnings, out
181  );
182  update_success( result, &success );
183 
184  return success;
185 }
Extracts options from a text stream and then allows convenient access to them.
AbstractLinAlgPack::size_type size_type
virtual void calc_Gc(const Vector &x, bool newx=true) const
Update the matrix for Gc at the point x and put it in the stored reference.
void initialize(bool test_setup)
Initialize the NLP for its first use.
bool test_interface(NLP *nlp, const Vector &xo, bool print_all_warnings, std::ostream *out)
Test the NLP interface as the given base point xo.
virtual vec_space_ptr_t space_x() const =0
Vector space object for unknown variables x (dimension n).
void set_options(const OptionsFromStream &options)
Overridden from SetOptionsFromStream and calls setOption(...).
Set options for NLPTester from an OptionsFromStream object.
bool test_nlp_first_order(NLPFirstOrder *nlp, OptionsFromStreamPack::OptionsFromStream *options, std::ostream *out)
Test an NLPFirstOrder object.
Set options for CalcFiniteDiffProd from an OptionsFromStream object.
Teuchos::AbstractFactory< AbstractLinAlgPack::MatrixOp > * factory_Gc
Testing class for base NLP interface.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Strategy interface for computing the product of the derivatives of the functions of an NLP along give...
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).
virtual const Vector & xl() const =0
Returns the lower bounds on the variables x.
bool finite_diff_check(NLP *nlp, const Vector &xo, const Vector *xl, const Vector *xu, const MatrixOp *Gc, const Vector *Gf, bool print_all_warnings, std::ostream *out) const
This function takes an NLP object and its computed derivatives and function values and validates the ...
std::ostream * out
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 const Vector & xu() const =0
Returns a reference to the vector of upper bounds on the variables x.
Set options for NLPFirstDerivTester from an OptionsFromStream object.
NLP first order information interface class {abstract}.
virtual obj_ptr_t create() const =0
bool update_success(bool result_check, bool *success)
Helper function for updating a flag for if an operation returned false.
Concrete class that tests the derivatives using finite differences.
virtual const Vector & xinit() const =0
Returns a reference to the vector of the initial guess for the solution x.
virtual size_type m() const
Return the number of general equality constraints.
int n
virtual void set_Gf(VectorMutable *Gf)
Set a pointer to a vector to be updated when this->calc_Gf() is called.
virtual void calc_Gf(const Vector &x, bool newx=true) const
Update the vector for Gf at the point x and put it in the stored reference.
virtual void set_Gc(MatrixOp *Gc)
Set a pointer to a matrix object to be updated when this->calc_Gc() is called.