ROL
zakharov/example_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 1
49 
50 #include "ROL_Algorithm.hpp"
51 #include "ROL_LineSearchStep.hpp"
52 #include "ROL_StatusTest.hpp"
53 #include "ROL_StdVector.hpp"
54 #include "ROL_Zakharov.hpp"
56 #include "Teuchos_oblackholestream.hpp"
57 #include "Teuchos_GlobalMPISession.hpp"
58 #include "Teuchos_XMLParameterListHelpers.hpp"
59 
60 #include <iostream>
61 
62 typedef double RealT;
63 
64 int main(int argc, char *argv[]) {
65 
66  using namespace Teuchos;
67 
68  typedef std::vector<RealT> vector;
69  typedef ROL::Vector<RealT> V; // Abstract vector
70  typedef ROL::StdVector<RealT> SV; // Concrete vector containing std::vector data
71 
72  GlobalMPISession mpiSession(&argc, &argv);
73 
74  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
75  int iprint = argc - 1;
76  Teuchos::RCP<std::ostream> outStream;
77  Teuchos::oblackholestream bhs; // outputs nothing
78  if (iprint > 0)
79  outStream = Teuchos::rcp(&std::cout, false);
80  else
81  outStream = Teuchos::rcp(&bhs, false);
82 
83  int errorFlag = 0;
84 
85  // *** Example body.
86 
87  try {
88 
89  int dim = 10; // Set problem dimension.
90 
91  RCP<ParameterList> parlist = rcp(new ParameterList());
92  std::string paramfile = "parameters.xml";
93  updateParametersFromXmlFile(paramfile,parlist.ptr());
94 
95  // Define algorithm.
96  ROL::Algorithm<RealT> algo("Line Search",*parlist);
97 
98  // Iteration vector.
99  RCP<vector> x_rcp = rcp( new vector(dim, 0.0) );
100 
101  // Vector of natural numbers.
102  RCP<vector> k_rcp = rcp( new vector(dim, 0.0) );
103 
104  // For gradient and Hessian checks.
105  RCP<vector> xtest_rcp = rcp( new vector(dim, 0.0) );
106  RCP<vector> d_rcp = rcp( new vector(dim, 0.0) );
107  RCP<vector> v_rcp = rcp( new vector(dim, 0.0) );
108  RCP<vector> hv_rcp = rcp( new vector(dim, 0.0) );
109  RCP<vector> ihhv_rcp = rcp( new vector(dim, 0.0) );
110 
111  RealT left = -1e0, right = 1e0;
112  for (int i=0; i<dim; i++) {
113  (*x_rcp)[i] = 2;
114  (*k_rcp)[i] = i+1.0;
115 
116  (*xtest_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
117  (*d_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
118  (*v_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
119  }
120 
121  RCP<V> k = rcp(new SV(k_rcp) );
122  SV x(x_rcp);
123 
124  // Check gradient and Hessian.
125  SV xtest(xtest_rcp);
126  SV d(d_rcp);
127  SV v(v_rcp);
128  SV hv(hv_rcp);
129  SV ihhv(ihhv_rcp);
130 
132 
133  obj.checkGradient(xtest, d, true, *outStream); *outStream << "\n";
134  obj.checkHessVec(xtest, v, true, *outStream); *outStream << "\n";
135  obj.checkHessSym(xtest, d, v, true, *outStream); *outStream << "\n";
136 
137  // Check inverse Hessian.
138  RealT tol=0;
139  obj.hessVec(hv,v,xtest,tol);
140  obj.invHessVec(ihhv,hv,xtest,tol);
141  ihhv.axpy(-1,v);
142  *outStream << "Checking inverse Hessian" << std::endl;
143  *outStream << "||H^{-1}Hv-v|| = " << ihhv.norm() << std::endl;
144 
145 
146  // Run algorithm.
147  algo.run(x, obj, true, *outStream);
148 
149  // Get True Solution
150  RCP<vector> xtrue_rcp = rcp( new vector(dim, 0.0) );
151  SV xtrue(xtrue_rcp);
152 
153 
154  // Compute Error
155  x.axpy(-1.0, xtrue);
156  RealT abserr = x.norm();
157  *outStream << std::scientific << "\n Absolute Error: " << abserr << std::endl;
158  if ( abserr > sqrt(ROL::ROL_EPSILON) ) {
159  errorFlag += 1;
160  }
161  }
162  catch (std::logic_error err) {
163  *outStream << err.what() << "\n";
164  errorFlag = -1000;
165  }; // end try
166 
167  if (errorFlag != 0)
168  std::cout << "End Result: TEST FAILED\n";
169  else
170  std::cout << "End Result: TEST PASSED\n";
171 
172  return 0;
173 
174 }
175 
void invHessVec(Vector< Real > &ihv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
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.
Provides an interface to run optimization algorithms.
Contains definitions for the Zakharov function as evaluated using only the ROL::Vector interface...
double RealT
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.
double RealT
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.
static const double ROL_EPSILON
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:118