ROL
function/test_02.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 #include "ROL_StdVector.hpp"
50 #include "Teuchos_oblackholestream.hpp"
51 #include "Teuchos_GlobalMPISession.hpp"
52 
53 typedef double RealT;
54 
55 int main(int argc, char *argv[]) {
56 
57  using Teuchos::RCP;
58  using Teuchos::rcp;
59 
60  typedef std::vector<RealT> vector;
61  typedef ROL::StdVector<RealT> StdVector;
62  typedef Teuchos::RCP<ROL::Vector<RealT> > PVector;
63 
64  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
65 
66  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
67  int iprint = argc - 1;
68  Teuchos::RCP<std::ostream> outStream;
69  Teuchos::oblackholestream bhs; // outputs nothing
70  if (iprint > 0)
71  outStream = Teuchos::rcp(&std::cout, false);
72  else
73  outStream = Teuchos::rcp(&bhs, false);
74 
75  // Save the format state of the original std::cout.
76  Teuchos::oblackholestream oldFormatState;
77  oldFormatState.copyfmt(std::cout);
78 
79  int errorFlag = 0;
80 
81  // Specify interval on which to generate uniform random numbers.
82  RealT left = 0.1, right = 1.1;
83 
84  // *** Test body.
85 
86  try {
87 
88  int dim = 1;
89  RCP<vector> x_rcp = rcp( new vector(dim,0.0) );
90  RCP<vector> y_rcp = rcp( new vector(dim,0.0) );
91  RCP<vector> v_rcp = rcp( new vector(dim,0.0) );
92  RCP<vector> d_rcp = rcp( new vector(dim,0.0) );
93  RCP<vector> gx_rcp = rcp( new vector(dim,0.0) );
94  RCP<vector> gy_rcp = rcp( new vector(dim,0.0) );
95  RCP<vector> hv_rcp = rcp( new vector(dim,0.0) );
96 
97  for( int i=0; i<dim; ++i ) {
98  (*x_rcp)[i] = 2+( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
99  (*d_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
100  (*v_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
101  }
102 
103  StdVector x( x_rcp);
104  StdVector y( y_rcp);
105  StdVector v( v_rcp);
106  StdVector d( d_rcp);
107  StdVector gx(gx_rcp);
108  StdVector gy(gy_rcp);
109  StdVector hv(hv_rcp);
110 
111  // Fixed difference step size
112  RealT delta = 1.e-7;
113 
114  y.set(x); // y = x
115  y.axpy(delta,d); // y = x+delta*d
116 
118 
119  // Do step size sweep
120  obj.checkGradient(x, d, true, *outStream); *outStream << "\n";
121  obj.checkHessVec(x, v, true, *outStream); *outStream << "\n";
122 
123 
124 
125  RealT tol = 0;
126 
127  // Compute objective at x and y
128  RealT fx = obj.value(x,tol);
129  RealT fy = obj.value(y,tol);
130 
131  // Compute gradient at x and y
132  obj.gradient(gx,x,tol);
133  obj.gradient(gy,y,tol);
134 
135  // Compute action of Hessian on v at x
136  obj.hessVec(hv,v,x,tol);
137 
138  // FD gradient error
139  RealT graderr = (fy - fx)/delta - gx.dot(d);
140 
141  // FD Hessian error
142  PVector dg = gx.clone();
143  dg->set(gy);
144  dg->axpy(-1.0,gx);
145 
146  RealT hesserr = ( dg->dot(v) )/delta - hv.dot(d);
147 
148  if( std::abs(graderr) > 1e-8 ) {
149  ++errorFlag;
150  }
151 
152  if( std::abs(hesserr) > 1e-8 ) {
153  ++errorFlag;
154  }
155 
156  }
157  catch (std::logic_error err) {
158  *outStream << err.what() << "\n";
159  errorFlag = -1000;
160  }; // end try
161 
162  if (errorFlag != 0)
163  std::cout << "End Result: TEST FAILED\n";
164  else
165  std::cout << "End Result: TEST PASSED\n";
166 
167  return 0;
168 
169 
170 }
171 
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
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 std::vector implementation of the ROL::Vector interface.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
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.
Log barrier objective for interior point methods.
double RealT
double RealT