ROL
function/test_06.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 
44 #include "ROL_Stream.hpp"
45 #include "Teuchos_GlobalMPISession.hpp"
46 
47 #include "ROL_StdVector.hpp"
48 #include "ROL_StdObjective.hpp"
49 
50 typedef double RealT;
51 
52 template<class Real>
54 public:
55  Real value( const std::vector<Real> &x, Real &tol ) {
56  Real quad(0), lin(0);
57  unsigned size = x.size();
58  std::vector<Real> p(size+2,1);
59  for ( unsigned i = 0; i < size; i++ ) {
60  quad += x[i]*x[i];
61  lin += x[i]*p[i+1];
62  }
63  return std::exp(p[0])*quad + lin + p[size+1];
64  }
65 
66  void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
67  unsigned size = x.size();
68  std::vector<Real> p(size+2,1);
69  const Real two(2);
70  for ( unsigned i = 0; i < size; i++ ) {
71  g[i] = two*std::exp(p[0])*x[i] + p[i+1];
72  }
73  }
74 
75  void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
76  unsigned size = x.size();
77  std::vector<Real> p(size+2,1);
78  const Real two(2);
79  for ( unsigned i = 0; i < size; i++ ) {
80  hv[i] = two*std::exp(p[0])*v[i];
81  }
82  }
83 };
84 
85 void setRandomVector(std::vector<RealT> &x) {
86  unsigned dim = x.size();
87  for ( unsigned i = 0; i < dim; i++ ) {
88  x[i] = (RealT)rand()/(RealT)RAND_MAX;
89  }
90 }
91 
92 int main(int argc, char* argv[]) {
93 
94  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
95 
96  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
97  int iprint = argc - 1;
98  ROL::Ptr<std::ostream> outStream;
99  ROL::nullstream bhs; // outputs nothing
100  if (iprint > 0)
101  outStream = ROL::makePtrFromRef(std::cout);
102  else
103  outStream = ROL::makePtrFromRef(bhs);
104 
105  int errorFlag = 0;
106 
107  try {
108  /**********************************************************************************************/
109  /************************* CONSTRUCT SOL COMPONENTS *******************************************/
110  /**********************************************************************************************/
111  // Build vectors
112  unsigned dim = 4;
113  ROL::Ptr<std::vector<RealT> > x_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
114  ROL::Ptr<ROL::Vector<RealT> > x = ROL::makePtr<ROL::StdVector<RealT>>(x_ptr);
115  setRandomVector(*x_ptr);
116  ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
117  ROL::Ptr<ROL::Vector<RealT> > d = ROL::makePtr<ROL::StdVector<RealT>>(d_ptr);
118  setRandomVector(*d_ptr);
119  // Build objective function
120  ROL::Ptr<ROL::StdObjective<RealT> > obj =
121  ROL::makePtr<ObjectiveFunctionTest06<RealT>>();
122  // Test parametrized objective functions
123  *outStream << "Check Derivatives of StdObjective\n";
124  obj->checkGradient(*x,*d,true,*outStream);
125  obj->checkHessVec(*x,*d,true,*outStream);
126  }
127  catch (std::logic_error& err) {
128  *outStream << err.what() << "\n";
129  errorFlag = -1000;
130  }; // end try
131 
132  if (errorFlag != 0)
133  std::cout << "End Result: TEST FAILED\n";
134  else
135  std::cout << "End Result: TEST PASSED\n";
136 
137  return 0;
138 }
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:72
int main(int argc, char *argv[])
constexpr auto dim