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_test_nlp_direct.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 <assert.h>
43 
44 #include "NLPInterfacePack_test_nlp_direct.hpp"
45 #include "NLPInterfacePack_CalcFiniteDiffProd.hpp"
46 #include "NLPInterfacePack_CalcFiniteDiffProdSetOptions.hpp"
47 #include "NLPInterfacePack_NLPTester.hpp"
48 #include "NLPInterfacePack_NLPTesterSetOptions.hpp"
49 #include "NLPInterfacePack_NLPDirectTester.hpp"
50 #include "NLPInterfacePack_NLPDirectTesterSetOptions.hpp"
51 #include "NLPInterfacePack_NLPDirect.hpp"
52 #include "AbstractLinAlgPack_VectorSpace.hpp"
53 #include "AbstractLinAlgPack_VectorSpaceTester.hpp"
54 #include "AbstractLinAlgPack_VectorSpaceTesterSetOptions.hpp"
55 #include "AbstractLinAlgPack_VectorMutable.hpp"
56 #include "AbstractLinAlgPack_VectorOut.hpp"
57 #include "AbstractLinAlgPack_MatrixOp.hpp"
58 #include "AbstractLinAlgPack_MatrixOpOut.hpp"
59 #include "TestingHelperPack_update_success.hpp"
60 
61 bool NLPInterfacePack::test_nlp_direct(
62  NLPDirect *nlp
64  ,std::ostream *out
65  )
66 {
67  using TestingHelperPack::update_success;
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) {
165  NLPTesterSetOptions
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 
221  CalcFiniteDiffProd
222  calc_fd_prod;
223  if(options) {
224  CalcFiniteDiffProdSetOptions
225  options_setter( &calc_fd_prod );
226  options_setter.set_options(*options);
227  }
228  NLPDirectTester
229  nlp_first_order_direct_tester(Teuchos::rcp(&calc_fd_prod,false));
230  if(options) {
231  NLPDirectTesterSetOptions
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 }
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
size_t size_type
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)