ROL
function/test_08.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 #include "ROL_Objective_SimOpt.hpp"
17 
18 typedef double RealT;
19 
20 template<class Real>
22 public:
23  Real value( const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
24  ROL::Ptr<const std::vector<Real> > up
25  = dynamic_cast<const ROL::StdVector<Real>&>(u).getVector();
26  ROL::Ptr<const std::vector<Real> > zp
27  = dynamic_cast<const ROL::StdVector<Real>&>(z).getVector();
28  Real half(0.5), quadu(0), quadz(0);
29  unsigned usize = up->size();
30  for ( unsigned i = 0; i < usize; i++ ) {
31  quadu += (*up)[i]*(*up)[i];
32  }
33  unsigned zsize = zp->size();
34  for ( unsigned i = 0; i < zsize; i++ ) {
35  quadz += (*zp)[i]*(*zp)[i];
36  }
37  return half*(quadu + quadz);
38  }
39 
40  void gradient_1( ROL::Vector<Real> &g, const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
41  ROL::Ptr<std::vector<Real> > gp
42  = dynamic_cast<ROL::StdVector<Real>&>(g).getVector();
43  ROL::Ptr<const std::vector<Real> > up
44  = dynamic_cast<const ROL::StdVector<Real>&>(u).getVector();
45  gp->assign(up->begin(),up->end());
46  }
47 
48  void gradient_2( ROL::Vector<Real> &g, const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
49  ROL::Ptr<std::vector<Real> > gp
50  = dynamic_cast<ROL::StdVector<Real>&>(g).getVector();
51  ROL::Ptr<const std::vector<Real> > zp
52  = dynamic_cast<const ROL::StdVector<Real>&>(z).getVector();
53  gp->assign(zp->begin(),zp->end());
54  }
55 
57  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
58  ROL::Ptr<std::vector<Real> > hvp
59  = dynamic_cast<ROL::StdVector<Real>&>(hv).getVector();
60  ROL::Ptr<const std::vector<Real> > vp
61  = dynamic_cast<const ROL::StdVector<Real>&>(v).getVector();
62  hvp->assign(vp->begin(),vp->end());
63  }
64 
66  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
67  hv.zero();
68  }
69 
71  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
72  hv.zero();
73  }
74 
76  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
77  ROL::Ptr<std::vector<Real> > hvp
78  = dynamic_cast<ROL::StdVector<Real>&>(hv).getVector();
79  ROL::Ptr<const std::vector<Real> > vp
80  = dynamic_cast<const ROL::StdVector<Real>&>(v).getVector();
81  hvp->assign(vp->begin(),vp->end());
82  }
83 };
84 
85 template<class Real>
87 public:
88  Real value( const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
89  ROL::Ptr<const std::vector<Real> > up
90  = dynamic_cast<const ROL::StdVector<Real>&>(u).getVector();
91  ROL::Ptr<const std::vector<Real> > zp
92  = dynamic_cast<const ROL::StdVector<Real>&>(z).getVector();
93  Real linu(0), linz(0);
94  unsigned usize = up->size();
95  for ( unsigned i = 0; i < usize; i++ ) {
96  linu += (*up)[i];
97  }
98  unsigned zsize = zp->size();
99  for ( unsigned i = 0; i < zsize; i++ ) {
100  linz += (*zp)[i];
101  }
102  return linu + linz;
103  }
104 
105  void gradient_1( ROL::Vector<Real> &g, const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
106  ROL::Ptr<std::vector<Real> > gp
107  = dynamic_cast<ROL::StdVector<Real>&>(g).getVector();
108  ROL::Ptr<const std::vector<Real> > up
109  = dynamic_cast<const ROL::StdVector<Real>&>(u).getVector();
110  gp->assign(up->size(),1);
111  }
112 
113  void gradient_2( ROL::Vector<Real> &g, const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
114  ROL::Ptr<std::vector<Real> > gp
115  = dynamic_cast<ROL::StdVector<Real>&>(g).getVector();
116  ROL::Ptr<const std::vector<Real> > zp
117  = dynamic_cast<const ROL::StdVector<Real>&>(z).getVector();
118  gp->assign(zp->size(),1);
119  }
120 
122  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
123  hv.zero();
124  }
125 
127  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
128  hv.zero();
129  }
130 
132  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
133  hv.zero();
134  }
135 
137  const ROL::Vector<Real> &u, const ROL::Vector<Real> &z, Real &tol ) {
138  hv.zero();
139  }
140 };
141 
142 template<class Real>
144 public:
145  Real value( const std::vector<Real> &x, Real &tol ) {
146  return std::log(x[0]) * std::exp(x[1]);
147  }
148 
149  void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
150  g[0] = std::exp(x[1])/x[0];
151  g[1] = std::exp(x[1]) * std::log(x[0]);
152  }
153 
154  void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
155  Real H11 = -std::exp(x[1])/(x[0]*x[0]);
156  Real H12 = std::exp(x[1])/x[0];
157  Real H21 = std::exp(x[1])/x[0];
158  Real H22 = std::exp(x[1]) * std::log(x[0]);
159  hv[0] = H11*v[0] + H12*v[1];
160  hv[1] = H21*v[0] + H22*v[1];
161  }
162 };
163 
164 void setRandomVector(std::vector<RealT> &x) {
165  unsigned dim = x.size();
166  for ( unsigned i = 0; i < dim; i++ ) {
167  x[i] = (RealT)rand()/(RealT)RAND_MAX;
168  }
169 }
170 
171 int main(int argc, char* argv[]) {
172 
173  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
174 
175  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
176  int iprint = argc - 1;
177  ROL::Ptr<std::ostream> outStream;
178  ROL::nullstream bhs; // outputs nothing
179  if (iprint > 0)
180  outStream = ROL::makePtrFromRef(std::cout);
181  else
182  outStream = ROL::makePtrFromRef(bhs);
183 
184  int errorFlag = 0;
185 
186  try {
187  /**********************************************************************************************/
188  /************************* CONSTRUCT SOL COMPONENTS *******************************************/
189  /**********************************************************************************************/
190  // Build vectors
191  unsigned dim = 2;
192  ROL::Ptr<std::vector<RealT> > u_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
193  ROL::Ptr<ROL::Vector<RealT> > u = ROL::makePtr<ROL::StdVector<RealT>>(u_ptr);
194  setRandomVector(*u_ptr);
195  ROL::Ptr<std::vector<RealT> > z_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
196  ROL::Ptr<ROL::Vector<RealT> > z = ROL::makePtr<ROL::StdVector<RealT>>(z_ptr);
197  setRandomVector(*z_ptr);
198  ROL::Ptr<ROL::Vector<RealT> > x = ROL::makePtr<ROL::Vector_SimOpt<RealT>>(u,z);
199  ROL::Ptr<std::vector<RealT> > du_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
200  ROL::Ptr<ROL::Vector<RealT> > du = ROL::makePtr<ROL::StdVector<RealT>>(du_ptr);
201  setRandomVector(*du_ptr);
202  ROL::Ptr<std::vector<RealT> > dz_ptr = ROL::makePtr<std::vector<RealT>>(dim,0.0);
203  ROL::Ptr<ROL::Vector<RealT> > dz = ROL::makePtr<ROL::StdVector<RealT>>(dz_ptr);
204  setRandomVector(*dz_ptr);
205  ROL::Ptr<ROL::Vector<RealT> > d = ROL::makePtr<ROL::Vector_SimOpt<RealT>>(du,dz);
206  // Build objective function
207  std::vector<ROL::Ptr<ROL::Objective_SimOpt<RealT> > > vec_obj(2,ROL::nullPtr);
208  vec_obj[0] = ROL::makePtr<ObjectiveFunctionTest08_1<RealT>>();
209  vec_obj[1] = ROL::makePtr<ObjectiveFunctionTest08_2<RealT>>();
210  ROL::Ptr<ROL::StdObjective<RealT> > obj_scalarize
211  = ROL::makePtr<ObjectiveFunctionTest08_scalarize<RealT>>();
212  ROL::Ptr<ROL::CompositeObjective_SimOpt<RealT> > obj
213  = ROL::makePtr<ROL::CompositeObjective_SimOpt<RealT>>(vec_obj,obj_scalarize);
214  // Test parametrized objective functions
215  *outStream << "Check Derivatives of CompositeObjective_SimOpt\n";
216  obj->checkGradient(*x,*d,true,*outStream);
217  obj->checkHessVec(*x,*d,true,*outStream);
218  }
219  catch (std::logic_error& err) {
220  *outStream << err.what() << "\n";
221  errorFlag = -1000;
222  }; // end try
223 
224  if (errorFlag != 0)
225  std::cout << "End Result: TEST FAILED\n";
226  else
227  std::cout << "End Result: TEST PASSED\n";
228 
229  return 0;
230 }
Provides the interface to evaluate simulation-based objective functions.
void hessVec_12(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
void setRandomVector(std::vector< RealT > &x)
void gradient_2(ROL::Vector< Real > &g, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute gradient with respect to second component.
void hessVec_21(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
void gradient_2(ROL::Vector< Real > &g, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute gradient with respect to second component.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:133
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36
Real value(const std::vector< Real > &x, Real &tol)
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector&#39;s.
void gradient_1(ROL::Vector< Real > &g, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute gradient with respect to first component.
void hessVec_12(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
void hessVec_11(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply Hessian approximation to vector.
void hessVec_21(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
void hessVec_11(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Apply Hessian approximation to vector.
Real value(const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute value.
int main(int argc, char *argv[])
void gradient_1(ROL::Vector< Real > &g, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute gradient with respect to first component.
void hessVec_22(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
void hessVec_22(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
constexpr auto dim
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Real value(const ROL::Vector< Real > &u, const ROL::Vector< Real > &z, Real &tol)
Compute value.