MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NLPInterfacePack_test_nlp_direct.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 
62  NLPDirect *nlp
64  ,std::ostream *out
65  )
66 {
68 
69  bool result;
70  bool success = true;
71 
73  nlpOutputTempState(Teuchos::rcp(nlp,false),Teuchos::getFancyOStream(Teuchos::rcp(out,false)),Teuchos::VERB_LOW);
74 
75  if(out)
76  *out
77  << "\n****************************"
78  << "\n*** test_nlp_direct(...) ***"
79  << "\n*****************************\n";
80 
81  nlp->initialize(true);
82 
83  // Test the DVector spaces
84  if(out)
85  *out << "\nTesting the vector spaces ...\n";
86 
87  VectorSpaceTester vec_space_tester;
88  if(options) {
89  VectorSpaceTesterSetOptions
90  opt_setter(&vec_space_tester);
91  opt_setter.set_options(*options);
92  }
93 
94  if(out)
95  *out << "\nTesting nlp->space_x() ...\n";
96  result = vec_space_tester.check_vector_space(*nlp->space_x(),out);
97  if(out) {
98  if(result)
99  *out << "nlp->space_x() checks out!\n";
100  else
101  *out << "nlp->space_x() check failed!\n";
102  }
103  update_success( result, &success );
104  if(out)
105  *out << "\nTesting nlp->space_x()->sub_space(nlp->var_dep()) ...\n";
106  result = vec_space_tester.check_vector_space(
107  *nlp->space_x()->sub_space(nlp->var_dep()),out);
108  if(out) {
109  if(result)
110  *out << "nlp->space_x()->sub_space(nlp->var_dep()) checks out!\n";
111  else
112  *out << "nlp->space_x()->sub_space(nlp->var_dep()) check failed!\n";
113  }
114  update_success( result, &success );
115  if(out)
116  *out << "\nTesting nlp->space_x()->sub_space(nlp->var_indep()) ...\n";
117  result = vec_space_tester.check_vector_space(
118  *nlp->space_x()->sub_space(nlp->var_indep()),out);
119  if(out) {
120  if(result)
121  *out << "nlp->space_x()->sub_space(nlp->var_indep()) checks out!\n";
122  else
123  *out << "nlp->space_x()->sub_space(nlp->var_indep()) check failed!\n";
124  }
125  update_success( result, &success );
126  if(out)
127  *out << "\nTesting nlp->space_c() ...\n";
128  result = vec_space_tester.check_vector_space(*nlp->space_c(),out);
129  if(out) {
130  if(result)
131  *out << "nlp->space_c() checks out!\n";
132  else
133  *out << "nlp->space_c() check failed!\n";
134  }
135  update_success( result, &success );
136  if(out)
137  *out << "\nTesting nlp->space_c()->sub_space(nlp->con_decomp()) ...\n";
138  result = vec_space_tester.check_vector_space(
139  *nlp->space_c()->sub_space(nlp->con_decomp()),out);
140  if(out) {
141  if(result)
142  *out << "nlp->space_c()->sub_space(nlp->con_decomp()) checks out!\n";
143  else
144  *out << "nlp->space_c()->sub_space(nlp->con_decomp()) check failed!\n";
145  }
146  update_success( result, &success );
147  if( nlp->con_decomp().size() < nlp->m() ) {
148  if(out)
149  *out << "\nTesting nlp->space_c()->sub_space(nlp->con_undecomp()) ...\n";
150  result = vec_space_tester.check_vector_space(
151  *nlp->space_c()->sub_space(nlp->con_undecomp()),out);
152  if(out) {
153  if(result)
154  *out << "nlp->space_c()->sub_space(nlp->con_undecomp()) checks out!\n";
155  else
156  *out << "nlp->space_c()->sub_space(nlp->con_undecomp()) check failed!\n";
157  }
158  update_success( result, &success );
159  }
160 
161  // Test the NLP interface first!
162 
163  NLPTester nlp_tester;
164  if(options) {
166  nlp_tester_opt_setter(&nlp_tester);
167  nlp_tester_opt_setter.set_options(*options);
168  }
169  const bool print_all_warnings = nlp_tester.print_all();
170 
171  result = nlp_tester.test_interface(
172  nlp, nlp->xinit(), print_all_warnings, out );
173  update_success( result, &success );
174 
175  // Test the NLPDirect interface now!
176 
177  const bool supports_Gf = nlp->supports_Gf();
178 
179  if(out)
180  *out << "\nCalling nlp->calc_point(...) at nlp->xinit() ...\n";
181  const size_type
182  n = nlp->n(),
183  m = nlp->m();
184  const Range1D
185  var_dep = nlp->var_dep(),
186  var_indep = nlp->var_indep(),
187  con_decomp = nlp->con_decomp(),
188  con_undecomp = nlp->con_undecomp();
189  VectorSpace::vec_mut_ptr_t
190  c = nlp->space_c()->create_member(),
191  Gf = nlp->space_x()->create_member(),
192  py = nlp->space_x()->sub_space(var_dep)->create_member(),
193  rGf = nlp->space_x()->sub_space(var_indep)->create_member();
194  NLPDirect::mat_fcty_ptr_t::element_type::obj_ptr_t
195  GcU = con_decomp.size() < m ? nlp->factory_GcU()->create() : Teuchos::null,
196  D = nlp->factory_D()->create(),
197  Uz = con_decomp.size() < m ? nlp->factory_Uz()->create() : Teuchos::null;
198  nlp->calc_point(
199  nlp->xinit(), NULL, c.get(), true, supports_Gf?Gf.get():NULL, py.get(), rGf.get()
200  ,GcU.get(), D.get(), Uz.get() );
201  if(out) {
202  if(supports_Gf) {
203  *out << "\n||Gf||inf = " << Gf->norm_inf();
204  if(nlp_tester.print_all())
205  *out << "\nGf =\n" << *Gf;
206  }
207  *out << "\n||py||inf = " << py->norm_inf();
208  if(nlp_tester.print_all())
209  *out << "\npy =\n" << *py;
210  *out << "\n||rGf||inf = " << rGf->norm_inf();
211  if(nlp_tester.print_all())
212  *out << "\nrGf =\n" << *rGf;
213  if(nlp_tester.print_all())
214  *out << "\nD =\n" << *D;
215  if( con_decomp.size() < m ) {
216  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Print GcU and Uz
217  }
218  *out << "\n";
219  }
220 
222  calc_fd_prod;
223  if(options) {
225  options_setter( &calc_fd_prod );
226  options_setter.set_options(*options);
227  }
229  nlp_first_order_direct_tester(Teuchos::rcp(&calc_fd_prod,false));
230  if(options) {
232  nlp_tester_opt_setter(&nlp_first_order_direct_tester);
233  nlp_tester_opt_setter.set_options(*options);
234  }
235  result = nlp_first_order_direct_tester.finite_diff_check(
236  nlp, nlp->xinit()
237  ,nlp->num_bounded_x() ? &nlp->xl() : NULL
238  ,nlp->num_bounded_x() ? &nlp->xu() : NULL
239  ,c.get()
240  ,supports_Gf?Gf.get():NULL,py.get(),rGf.get(),GcU.get(),D.get(),Uz.get()
241  ,print_all_warnings, out
242  );
243  update_success( result, &success );
244 
245  return success;
246 }
Extracts options from a text stream and then allows convenient access to them.
AbstractLinAlgPack::size_type size_type
virtual Range1D con_decomp() const
Return the range of decomposed equality constraints.
virtual const mat_fcty_ptr_t factory_GcU() const
Return a matrix factory object for creating GcU.
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 const mat_fcty_ptr_t factory_Uz() const
Return a matrix factory object for Uz = F + E * D.
Set options for NLPDirectTester from an OptionsFromStream object.
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(...).
virtual void calc_point(const Vector &x, value_type *f, VectorMutable *c, bool recalc_c, VectorMutable *Gf, VectorMutable *py, VectorMutable *rGf, MatrixOp *GcU, MatrixOp *D, MatrixOp *Uz) const =0
Compute all of the needed quanities for direct sensitivities.
Set options for NLPTester from an OptionsFromStream object.
Interface providing only direct first order sensitivity information.
Set options for CalcFiniteDiffProd from an OptionsFromStream object.
bool test_nlp_direct(NLPDirect *nlp, OptionsFromStreamPack::OptionsFromStream *options, std::ostream *out)
Test an NLPDirect object.
Testing class for base NLP interface.
virtual Range1D con_undecomp() const
Return the range of undecomposed equality constraints.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
bool finite_diff_check(NLPDirect *nlp, const Vector &xo, const Vector *xl, const Vector *xu, const Vector *c, const Vector *Gf, const Vector *py, const Vector *rGf, const MatrixOp *GcU, const MatrixOp *D, const MatrixOp *Uz, bool print_all_warnings, std::ostream *out) const
This function takes an NLP object and its computed derivatives and function values and validates the ...
void initialize(bool test_setup)
Initialize the NLP for its first use.
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.
virtual Range1D var_dep() const
Return the range of dependent (i.e. basic) variables.
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...
Concrete class that tests the computed values of the NLPDirect interface using finite differences...
virtual const Vector & xu() const =0
Returns a reference to the vector of upper bounds on the variables x.
virtual Range1D var_indep() const
Return the range of independent (i.e. nonbasic) variables.
virtual const mat_fcty_ptr_t factory_D() const =0
Return a matrix factory object for D = -inv(C)*N {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.
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
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
virtual bool supports_Gf() const
Determine if the objective gradient is supported or not.