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