ROL
function/test_01.cpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Rapid Optimization Library (ROL) Package
4//
5// Copyright 2014 NTESS and the ROL contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
14//#define USE_HESSVEC 0
15
16#include "ROL_StdVector.hpp"
19#include "ROL_Stream.hpp"
20#include "Teuchos_GlobalMPISession.hpp"
21
22#include <iostream>
23
24typedef double RealT;
25
26int main(int argc, char *argv[]) {
27
28 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
29
30 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
31 int iprint = argc - 1;
32 ROL::Ptr<std::ostream> outStream;
33 ROL::nullstream bhs; // outputs nothing
34 if (iprint > 0)
35 outStream = ROL::makePtrFromRef(std::cout);
36 else
37 outStream = ROL::makePtrFromRef(bhs);
38
39 // Save the format state of the original std::cout.
40 ROL::nullstream oldFormatState;
41 oldFormatState.copyfmt(std::cout);
42
43 int errorFlag = 0;
44
45 // Specify interval on which to generate uniform random numbers.
46 RealT left = -1.0, right = 1.0;
47
48 // *** Test body.
49
50 try {
51
52 int dim = 128;
53 ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
54 ROL::Ptr<std::vector<RealT> > y_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
55 ROL::Ptr<std::vector<RealT> > z_ptr = ROL::makePtr<std::vector<RealT>>(dim, 0.0);
56 ROL::StdVector<RealT> x(x_ptr);
57 ROL::StdVector<RealT> y(y_ptr);
58 ROL::StdVector<RealT> z(z_ptr);
59
60 // set x,y
61 for (int i=0; i<dim; i++) {
62 (*x_ptr)[i] = 10.0* (1.0 + (RealT)rand() / (RealT)RAND_MAX);
63 (*y_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
64 (*z_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
65 }
66
67 //ROL::ZOO::Objective_Rosenbrock<RealT> obj;
69 //ROL::ZOO::Objective_SumOfSquares<RealT> obj;
70 //ROL::ZOO::Objective_LeastSquares<RealT> obj;
71
72 std::vector<std::vector<RealT> > gCheck = obj.checkGradient(x, y);
73
74 for (unsigned i=0; i<gCheck.size(); i++) {
75 if (i==0) {
76 *outStream << std::right
77 << std::setw(20) << "Step size"
78 << std::setw(20) << "grad'*dir"
79 << std::setw(20) << "FD approx"
80 << std::setw(20) << "abs error"
81 << "\n";
82 }
83 *outStream << std::scientific << std::setprecision(8) << std::right
84 << std::setw(20) << gCheck[i][0]
85 << std::setw(20) << gCheck[i][1]
86 << std::setw(20) << gCheck[i][2]
87 << std::setw(20) << gCheck[i][3]
88 << "\n";
89 }
90
91 *outStream << "\n";
92 std::vector<std::vector<RealT> > hvCheck = obj.checkHessVec(x, y);
93
94 for (unsigned i=0; i<hvCheck.size(); i++) {
95 if (i==0) {
96 *outStream << std::right
97 << std::setw(20) << "Step size"
98 << std::setw(20) << "norm(Hess*vec)"
99 << std::setw(20) << "norm(FD approx)"
100 << std::setw(20) << "norm(abs error)"
101 << "\n";
102 }
103 *outStream << std::scientific << std::setprecision(8) << std::right
104 << std::setw(20) << hvCheck[i][0]
105 << std::setw(20) << hvCheck[i][1]
106 << std::setw(20) << hvCheck[i][2]
107 << std::setw(20) << hvCheck[i][3]
108 << "\n";
109 }
110
111 *outStream << "\n";
112 std::vector<RealT> hsymCheck = obj.checkHessSym(x, y, z);
113
114 *outStream << std::right
115 << std::setw(20) << "<w, H(x)v>"
116 << std::setw(20) << "<v, H(x)w>"
117 << std::setw(20) << "abs error"
118 << "\n";
119 *outStream << std::scientific << std::setprecision(8) << std::right
120 << std::setw(20) << hsymCheck[0]
121 << std::setw(20) << hsymCheck[1]
122 << std::setw(20) << hsymCheck[2]
123 << "\n";
124
125 Teuchos::SerialDenseMatrix<int, RealT> H(x.dimension(), x.dimension());
126 H = ROL::computeDenseHessian(obj, x);
127 //H.print(*outStream);
128
129 std::vector<std::vector<RealT> > eigenvals = ROL::computeEigenvalues(H);
130
131 *outStream << "\n";
132 for (unsigned i=0; i<(eigenvals[0]).size(); i++) {
133 if (i==0) {
134 *outStream << std::right
135 << std::setw(20) << "Real"
136 << std::setw(20) << "Imag"
137 << "\n";
138 }
139 *outStream << std::scientific << std::setprecision(8) << std::right
140 << std::setw(20) << (eigenvals[0])[i]
141 << std::setw(20) << (eigenvals[1])[i]
142 << "\n";
143 }
144
145 }
146 catch (std::logic_error& err) {
147 *outStream << err.what() << "\n";
148 errorFlag = -1000;
149 }; // end try
150
151 if (errorFlag != 0)
152 std::cout << "End Result: TEST FAILED\n";
153 else
154 std::cout << "End Result: TEST PASSED\n";
155
156 // reset format state of std::cout
157 std::cout.copyfmt(oldFormatState);
158
159 return 0;
160
161}
162
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...
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.
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.
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.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
int main(int argc, char *argv[])
double RealT
Teuchos::SerialDenseMatrix< int, Real > computeDenseHessian(Objective< Real > &obj, const Vector< Real > &x)
std::vector< std::vector< Real > > computeEigenvalues(const Teuchos::SerialDenseMatrix< int, Real > &mat)
constexpr auto dim