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_first_order.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_first_order.hpp"
45 #include "NLPInterfacePack_CalcFiniteDiffProd.hpp"
46 #include "NLPInterfacePack_CalcFiniteDiffProdSetOptions.hpp"
47 #include "NLPInterfacePack_NLPTester.hpp"
48 #include "NLPInterfacePack_NLPTesterSetOptions.hpp"
49 #include "NLPInterfacePack_NLPFirstDerivTester.hpp"
50 #include "NLPInterfacePack_NLPFirstDerivTesterSetOptions.hpp"
51 #include "NLPInterfacePack_NLPFirstOrder.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 #include "Teuchos_FancyOStream.hpp"
61 
62 bool NLPInterfacePack::test_nlp_first_order(
63  NLPFirstOrder *nlp
65  ,std::ostream *out
66  )
67 {
68  namespace rcp = MemMngPack;
69  using TestingHelperPack::update_success;
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) {
123  NLPTesterSetOptions
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 
161  CalcFiniteDiffProd
162  calc_fd_prod;
163  if(options) {
164  CalcFiniteDiffProdSetOptions
165  options_setter( &calc_fd_prod );
166  options_setter.set_options(*options);
167  }
168  NLPFirstDerivTester
169  nlp_first_derivatives_tester(Teuchos::rcp(&calc_fd_prod,false));
170  if(options) {
171  NLPFirstDerivTesterSetOptions
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 }
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
size_t size_type