ROL
function/test_01.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) 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 lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
48 //#define USE_HESSVEC 0
49 
50 #include "ROL_StdVector.hpp"
51 #include "ROL_GetTestProblems.hpp"
52 #include "ROL_HelperFunctions.hpp"
53 #include "ROL_Stream.hpp"
54 #include "Teuchos_GlobalMPISession.hpp"
55 
56 #include <iostream>
57 
58 typedef double RealT;
59 
60 int main(int argc, char *argv[]) {
61 
62  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
63 
64  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
65  int iprint = argc - 1;
66  ROL::Ptr<std::ostream> outStream;
67  ROL::nullstream bhs; // outputs nothing
68  if (iprint > 0)
69  outStream = ROL::makePtrFromRef(std::cout);
70  else
71  outStream = ROL::makePtrFromRef(bhs);
72 
73  // Save the format state of the original std::cout.
74  ROL::nullstream oldFormatState;
75  oldFormatState.copyfmt(std::cout);
76 
77  int errorFlag = 0;
78 
79  // Specify interval on which to generate uniform random numbers.
80  RealT left = -1.0, right = 1.0;
81 
82  // *** Test body.
83 
84  try {
85 
86  int dim = 128;
87  ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
88  ROL::Ptr<std::vector<RealT> > y_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
89  ROL::Ptr<std::vector<RealT> > z_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
90  ROL::StdVector<RealT> x(x_ptr);
91  ROL::StdVector<RealT> y(y_ptr);
92  ROL::StdVector<RealT> z(z_ptr);
93 
94  // set x,y
95  for (int i=0; i<dim; i++) {
96  (*x_ptr)[i] = 10.0* (1.0 + (RealT)rand() / (RealT)RAND_MAX);
97  (*y_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
98  (*z_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
99  }
100 
101  //ROL::ZOO::Objective_Rosenbrock<RealT> obj;
103  //ROL::ZOO::Objective_SumOfSquares<RealT> obj;
104  //ROL::ZOO::Objective_LeastSquares<RealT> obj;
105 
106  std::vector<std::vector<RealT> > gCheck = obj.checkGradient(x, y);
107 
108  for (unsigned i=0; i<gCheck.size(); i++) {
109  if (i==0) {
110  *outStream << std::right
111  << std::setw(20) << "Step size"
112  << std::setw(20) << "grad'*dir"
113  << std::setw(20) << "FD approx"
114  << std::setw(20) << "abs error"
115  << "\n";
116  }
117  *outStream << std::scientific << std::setprecision(8) << std::right
118  << std::setw(20) << gCheck[i][0]
119  << std::setw(20) << gCheck[i][1]
120  << std::setw(20) << gCheck[i][2]
121  << std::setw(20) << gCheck[i][3]
122  << "\n";
123  }
124 
125  *outStream << "\n";
126  std::vector<std::vector<RealT> > hvCheck = obj.checkHessVec(x, y);
127 
128  for (unsigned i=0; i<hvCheck.size(); i++) {
129  if (i==0) {
130  *outStream << std::right
131  << std::setw(20) << "Step size"
132  << std::setw(20) << "norm(Hess*vec)"
133  << std::setw(20) << "norm(FD approx)"
134  << std::setw(20) << "norm(abs error)"
135  << "\n";
136  }
137  *outStream << std::scientific << std::setprecision(8) << std::right
138  << std::setw(20) << hvCheck[i][0]
139  << std::setw(20) << hvCheck[i][1]
140  << std::setw(20) << hvCheck[i][2]
141  << std::setw(20) << hvCheck[i][3]
142  << "\n";
143  }
144 
145  *outStream << "\n";
146  std::vector<RealT> hsymCheck = obj.checkHessSym(x, y, z);
147 
148  *outStream << std::right
149  << std::setw(20) << "<w, H(x)v>"
150  << std::setw(20) << "<v, H(x)w>"
151  << std::setw(20) << "abs error"
152  << "\n";
153  *outStream << std::scientific << std::setprecision(8) << std::right
154  << std::setw(20) << hsymCheck[0]
155  << std::setw(20) << hsymCheck[1]
156  << std::setw(20) << hsymCheck[2]
157  << "\n";
158 
159  Teuchos::SerialDenseMatrix<int, RealT> H(x.dimension(), x.dimension());
160  H = ROL::computeDenseHessian(obj, x);
161  //H.print(*outStream);
162 
163  std::vector<std::vector<RealT> > eigenvals = ROL::computeEigenvalues(H);
164 
165  *outStream << "\n";
166  for (unsigned i=0; i<(eigenvals[0]).size(); i++) {
167  if (i==0) {
168  *outStream << std::right
169  << std::setw(20) << "Real"
170  << std::setw(20) << "Imag"
171  << "\n";
172  }
173  *outStream << std::scientific << std::setprecision(8) << std::right
174  << std::setw(20) << (eigenvals[0])[i]
175  << std::setw(20) << (eigenvals[1])[i]
176  << "\n";
177  }
178 
179  }
180  catch (std::logic_error& err) {
181  *outStream << err.what() << "\n";
182  errorFlag = -1000;
183  }; // end try
184 
185  if (errorFlag != 0)
186  std::cout << "End Result: TEST FAILED\n";
187  else
188  std::cout << "End Result: TEST PASSED\n";
189 
190  // reset format state of std::cout
191  std::cout.copyfmt(oldFormatState);
192 
193  return 0;
194 
195 }
196 
Contains definitions of test objective functions.
Contains definitions for helper functions in ROL.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Teuchos::SerialDenseMatrix< int, Real > computeDenseHessian(Objective< Real > &obj, const Vector< Real > &x)
virtual std::vector< std::vector< Real > > checkGradient(const Vector< Real > &x, const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference gradient check.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
std::vector< std::vector< Real > > computeEigenvalues(const Teuchos::SerialDenseMatrix< int, Real > &mat)
int dimension() const
Return dimension of the vector space.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])
virtual std::vector< std::vector< Real > > checkHessVec(const Vector< Real > &x, const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference Hessian-applied-to-vector check.
virtual std::vector< Real > checkHessSym(const Vector< Real > &x, const Vector< Real > &v, const Vector< Real > &w, const bool printToStream=true, std::ostream &outStream=std::cout)
Hessian symmetry check.
constexpr auto dim