ROL
function/test_06.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 
10 #include "ROL_Stream.hpp"
11 #include "Teuchos_GlobalMPISession.hpp"
12 
13 #include "ROL_StdVector.hpp"
14 #include "ROL_StdObjective.hpp"
15 
16 typedef double RealT;
17 
18 template<class Real>
20 public:
21  Real value( const std::vector<Real> &x, Real &tol ) {
22  Real quad(0), lin(0);
23  unsigned size = x.size();
24  std::vector<Real> p(size+2,1);
25  for ( unsigned i = 0; i < size; i++ ) {
26  quad += x[i]*x[i];
27  lin += x[i]*p[i+1];
28  }
29  return std::exp(p[0])*quad + lin + p[size+1];
30  }
31 
32  void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
33  unsigned size = x.size();
34  std::vector<Real> p(size+2,1);
35  const Real two(2);
36  for ( unsigned i = 0; i < size; i++ ) {
37  g[i] = two*std::exp(p[0])*x[i] + p[i+1];
38  }
39  }
40 
41  void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
42  unsigned size = x.size();
43  std::vector<Real> p(size+2,1);
44  const Real two(2);
45  for ( unsigned i = 0; i < size; i++ ) {
46  hv[i] = two*std::exp(p[0])*v[i];
47  }
48  }
49 };
50 
51 void setRandomVector(std::vector<RealT> &x) {
52  unsigned dim = x.size();
53  for ( unsigned i = 0; i < dim; i++ ) {
54  x[i] = (RealT)rand()/(RealT)RAND_MAX;
55  }
56 }
57 
58 int main(int argc, char* argv[]) {
59 
60  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
61 
62  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
63  int iprint = argc - 1;
64  ROL::Ptr<std::ostream> outStream;
65  ROL::nullstream bhs; // outputs nothing
66  if (iprint > 0)
67  outStream = ROL::makePtrFromRef(std::cout);
68  else
69  outStream = ROL::makePtrFromRef(bhs);
70 
71  int errorFlag = 0;
72 
73  try {
74  /**********************************************************************************************/
75  /************************* CONSTRUCT SOL COMPONENTS *******************************************/
76  /**********************************************************************************************/
77  // Build vectors
78  unsigned dim = 4;
79  ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
80  ROL::Ptr<ROL::Vector<RealT> > x = ROL::makePtr<ROL::StdVector<RealT>>(x_ptr);
81  setRandomVector(*x_ptr);
82  ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
83  ROL::Ptr<ROL::Vector<RealT> > d = ROL::makePtr<ROL::StdVector<RealT>>(d_ptr);
84  setRandomVector(*d_ptr);
85  // Build objective function
86  ROL::Ptr<ROL::StdObjective<RealT> > obj =
87  ROL::makePtr<ObjectiveFunctionTest06<RealT>>();
88  // Test parametrized objective functions
89  *outStream << "Check Derivatives of StdObjective\n";
90  obj->checkGradient(*x,*d,true,*outStream);
91  obj->checkHessVec(*x,*d,true,*outStream);
92  }
93  catch (std::logic_error& err) {
94  *outStream << err.what() << "\n";
95  errorFlag = -1000;
96  }; // end try
97 
98  if (errorFlag != 0)
99  std::cout << "End Result: TEST FAILED\n";
100  else
101  std::cout << "End Result: TEST PASSED\n";
102 
103  return 0;
104 }
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
void setRandomVector(std::vector< RealT > &x)
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector&#39;s.
Real value(const std::vector< Real > &x, Real &tol)
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
constexpr auto dim