ROL
function/test_07.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"
16 
17 typedef double RealT;
18 
19 template<class Real>
21 public:
22  Real value( const std::vector<Real> &x, Real &tol ) {
23  Real half(0.5), quad(0);
24  unsigned size = x.size();
25  for ( unsigned i = 0; i < size; i++ ) {
26  quad += x[i]*x[i];
27  }
28  return half*quad;
29  }
30 
31  void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
32  g.assign(x.begin(),x.end());
33  }
34 
35  void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
36  hv.assign(v.begin(),v.end());
37  }
38 };
39 
40 template<class Real>
42 public:
43  Real value( const std::vector<Real> &x, Real &tol ) {
44  Real lin(0);
45  unsigned size = x.size();
46  for ( unsigned i = 0; i < size; i++ ) {
47  lin += x[i];
48  }
49  return lin;
50  }
51 
52  void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
53  g.assign(x.size(),1);
54  }
55 
56  void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
57  hv.assign(x.size(),0);
58  }
59 };
60 
61 template<class Real>
63 public:
64  Real value( const std::vector<Real> &x, Real &tol ) {
65  return std::log(x[0]) * std::exp(x[1]);
66  }
67 
68  void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
69  g[0] = std::exp(x[1])/x[0];
70  g[1] = std::exp(x[1]) * std::log(x[0]);
71  }
72 
73  void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
74  Real H11 = -std::exp(x[1])/(x[0]*x[0]);
75  Real H12 = std::exp(x[1])/x[0];
76  Real H21 = std::exp(x[1])/x[0];
77  Real H22 = std::exp(x[1]) * std::log(x[0]);
78  hv[0] = H11*v[0] + H12*v[1];
79  hv[1] = H21*v[0] + H22*v[1];
80  }
81 };
82 
83 void setRandomVector(std::vector<RealT> &x) {
84  unsigned dim = x.size();
85  for ( unsigned i = 0; i < dim; i++ ) {
86  x[i] = (RealT)rand()/(RealT)RAND_MAX;
87  }
88 }
89 
90 int main(int argc, char* argv[]) {
91 
92  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
93 
94  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
95  int iprint = argc - 1;
96  ROL::Ptr<std::ostream> outStream;
97  ROL::nullstream bhs; // outputs nothing
98  if (iprint > 0)
99  outStream = ROL::makePtrFromRef(std::cout);
100  else
101  outStream = ROL::makePtrFromRef(bhs);
102 
103  int errorFlag = 0;
104 
105  try {
106  /**********************************************************************************************/
107  /************************* CONSTRUCT SOL COMPONENTS *******************************************/
108  /**********************************************************************************************/
109  // Build vectors
110  unsigned dim = 4;
111  ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
112  ROL::Ptr<ROL::Vector<RealT> > x = ROL::makePtr<ROL::StdVector<RealT>>(x_ptr);
113  setRandomVector(*x_ptr);
114  ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
115  ROL::Ptr<ROL::Vector<RealT> > d = ROL::makePtr<ROL::StdVector<RealT>>(d_ptr);
116  setRandomVector(*d_ptr);
117  // Build objective function
118  std::vector<ROL::Ptr<ROL::Objective<RealT> > > vec_obj(2,ROL::nullPtr);
119  vec_obj[0] = ROL::makePtr<ObjectiveFunctionTest07_1<RealT>>();
120  vec_obj[1] = ROL::makePtr<ObjectiveFunctionTest07_2<RealT>>();
121  ROL::Ptr<ROL::StdObjective<RealT> > obj_scalarize
122  = ROL::makePtr<ObjectiveFunctionTest07_scalarize<RealT>>();
123  ROL::Ptr<ROL::Objective<RealT> > obj
124  = ROL::makePtr<ROL::CompositeObjective<RealT>>(vec_obj,obj_scalarize);
125  // Test parametrized objective functions
126  *outStream << "Check Derivatives of CompositeObjective\n";
127  obj->checkGradient(*x,*d,true,*outStream);
128  obj->checkHessVec(*x,*d,true,*outStream);
129  }
130  catch (std::logic_error& err) {
131  *outStream << err.what() << "\n";
132  errorFlag = -1000;
133  }; // end try
134 
135  if (errorFlag != 0)
136  std::cout << "End Result: TEST FAILED\n";
137  else
138  std::cout << "End Result: TEST PASSED\n";
139 
140  return 0;
141 }
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
void setRandomVector(std::vector< RealT > &x)
Real value(const std::vector< Real > &x, Real &tol)
Real value(const std::vector< Real > &x, Real &tol)
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
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)
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector&#39;s.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
int main(int argc, char *argv[])
Real value(const std::vector< Real > &x, Real &tol)
constexpr auto dim
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)