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_Zakharov.hpp"
51 #include "ROL_LineSearchStep.hpp"
52 #include "ROL_Algorithm.hpp"
53 #include "Teuchos_oblackholestream.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  Teuchos::RCP<std::ostream> outStream;
67  Teuchos::oblackholestream bhs; // outputs nothing
68  if (iprint > 0)
69  outStream = Teuchos::rcp(&std::cout, false);
70  else
71  outStream = Teuchos::rcp(&bhs, false);
72 
73  int errorFlag = 0;
74 
75  // *** Example body.
76 
77  try {
78 
79  int dim = 10; // Set problem dimension.
80 
81  Teuchos::ParameterList parlist;
82  // Enumerations
83  parlist.set("Descent Type", "Nonlinear-CG");
84  parlist.set("Nonlinear CG Type", "Oren-Luenberger");
85  parlist.set("Linesearch Type", "Cubic Interpolation");
86  parlist.set("Linesearch Curvature Condition", "Wolfe");
87  // Linesearch Parameters
88  parlist.set("Maximum Number of Function Evaluations", 20);
89  parlist.set("Sufficient Decrease Parameter", 1.e-4);
90  parlist.set("Curvature Conditions Parameter", 0.9);
91  parlist.set("Backtracking Rate", 0.5);
92  parlist.set("Initial Linesearch Parameter", 1.0);
93  parlist.set("User Defined Linesearch Parameter", false);
94  // Krylov Parameters
95  parlist.set("Absolute Krylov Tolerance", 1.e-4);
96  parlist.set("Relative Krylov Tolerance", 1.e-2);
97  parlist.set("Maximum Number of Krylov Iterations", 10);
98  // Define Step
99  ROL::LineSearchStep<RealT> step(parlist);
100 
101  // Define Status Test
102  RealT gtol = 1e-12; // norm of gradient tolerance
103  RealT stol = 1e-14; // norm of step tolerance
104  int maxit = 100; // maximum number of iterations
105  ROL::StatusTest<RealT> status(gtol, stol, maxit);
106 
107  // Define Algorithm
108  ROL::DefaultAlgorithm<RealT> algo(step,status,false);
109 
110  // Iteration Vector
111  Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
112 
113  // Vector of natural numbers
114  Teuchos::RCP<std::vector<RealT> > k_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
115 
116  // For gradient and Hessian checks
117  Teuchos::RCP<std::vector<RealT> > xtest_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
118  Teuchos::RCP<std::vector<RealT> > d_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
119  Teuchos::RCP<std::vector<RealT> > v_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
120  Teuchos::RCP<std::vector<RealT> > hv_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
121  Teuchos::RCP<std::vector<RealT> > ihhv_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
122 
123  RealT left = -1e0, right = 1e0;
124  for (int i=0; i<dim; i++) {
125  (*x_rcp)[i] = 4;
126  (*k_rcp)[i] = i+1.0;
127 
128  (*xtest_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
129  (*d_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
130  (*v_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
131  }
132 
133  Teuchos::RCP<ROL::Vector<RealT> > k = Teuchos::rcp(new ROL::StdVector<RealT> (k_rcp) );
134  ROL::StdVector<RealT> x(x_rcp);
135 
136  // Check gradient and Hessian
137  ROL::StdVector<RealT> xtest(xtest_rcp);
138  ROL::StdVector<RealT> d(d_rcp);
139  ROL::StdVector<RealT> v(v_rcp);
140  ROL::StdVector<RealT> hv(hv_rcp);
141  ROL::StdVector<RealT> ihhv(ihhv_rcp);
142 
144 
145  obj.checkGradient(xtest, d, true, *outStream); *outStream << "\n";
146  obj.checkHessVec(xtest, v, true, *outStream); *outStream << "\n";
147  obj.checkHessSym(xtest, d, v, true, *outStream); *outStream << "\n";
148 
149  // Check inverse Hessian
150  RealT tol=0;
151  obj.hessVec(hv,v,xtest,tol);
152  obj.invHessVec(ihhv,hv,xtest,tol);
153  ihhv.axpy(-1,v);
154  std::cout << "Checking inverse Hessian" << std::endl;
155  std::cout << "||H^{-1}Hv-v|| = " << ihhv.norm() << std::endl;
156 
157 
158  // Run Algorithm
159  std::vector<std::string> output = algo.run(x, obj, false);
160  for ( unsigned i = 0; i < output.size(); i++ ) {
161  std::cout << output[i];
162  }
163 
164  // Get True Solution
165  Teuchos::RCP<std::vector<RealT> > xtrue_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) );
166  ROL::StdVector<RealT> xtrue(xtrue_rcp);
167 
168 
169  // Compute Error
170  x.axpy(-1.0, xtrue);
171  RealT abserr = x.norm();
172  *outStream << std::scientific << "\n Absolute Error: " << abserr;
173  if ( abserr > sqrt(ROL::ROL_EPSILON) ) {
174  errorFlag += 1;
175  }
176  }
177  catch (std::logic_error err) {
178  *outStream << err.what() << "\n";
179  errorFlag = -1000;
180  }; // end try
181 
182  if (errorFlag != 0)
183  std::cout << "End Result: TEST FAILED\n";
184  else
185  std::cout << "End Result: TEST PASSED\n";
186 
187  return 0;
188 
189 }
190 
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:141
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.
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 interface to compute optimization steps with line search.
Real norm() const
Returns where .
Provides the std::vector implementation of the ROL::Vector interface.
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
Provides an interface to check status of 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:115