ROL
ROL_ObjectiveDef.hpp
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 #ifndef ROL_OBJECTIVE_DEF_H
11 #define ROL_OBJECTIVE_DEF_H
12 
17 namespace ROL {
18 
19 template<typename Real>
20 Real Objective<Real>::dirDeriv( const Vector<Real> &x, const Vector<Real> &d, Real &tol) {
21  if (dual_ == nullPtr) dual_ = x.dual().clone();
22  gradient(*dual_,x,tol);
23  //return d.dot(dual_->dual());
24  return d.apply(*dual_);
25  //Real dnorm = d.norm(), zero(0);
26  //if ( dnorm == zero ) {
27  // return zero;
28  //}
29  //Real cbrteps = std::cbrt(ROL_EPSILON<Real>()), one(1), v0(0), v1(0);
30  //Real xnorm = x.norm(), h = cbrteps * std::max(xnorm/dnorm,one);
31  //v0 = value(x,tol);
32  //prim_->set(x); prim_->axpy(h, d);
33  //update(*prim_,UpdateType::Temp);
34  //v1 = value(*prim_,tol);
35  //update(x,UpdateType::Revert);
36  //return (v1 - v0) / h;
37 }
38 
39 template<typename Real>
40 void Objective<Real>::gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
41  if (prim_ == nullPtr) prim_ = x.clone();
42  if (basis_ == nullPtr) basis_ = x.clone();
43 
44  const Real cbrteps = std::cbrt(ROL_EPSILON<Real>()), zero(0), one(1);
45  Real f0 = value(x,tol), h(0), xi(0), gi(0);
46  g.zero();
47  for (int i = 0; i < x.dimension(); i++) {
48  basis_->set(*x.basis(i));
49  xi = x.dot(*basis_);
50  h = cbrteps * std::max(std::abs(xi),one) * (xi < zero ? -one : one);
51  prim_->set(x); prim_->axpy(h,*basis_);
52  h = prim_->dot(*basis_) - xi;
53  update(*prim_,UpdateType::Temp);
54  gi = (value(*prim_,tol) - f0) / h;
55  g.axpy(gi,*g.basis(i));
56  }
58 }
59 
60 template<typename Real>
61 void Objective<Real>::hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
62  const Real zero(0), vnorm = v.norm();
63  // Get Step Length
64  if ( vnorm == zero ) {
65  hv.zero();
66  }
67  else {
68  if (prim_ == nullPtr) prim_ = x.clone();
69  if (dual_ == nullPtr) dual_ = hv.clone();
70 
71  //Real h = 2.0/(v.norm()*v.norm())*tol;
72  const Real one(1), h(std::max(one,x.norm()/vnorm)*tol);
73 
74  gradient(*dual_,x,tol); // Compute gradient at x
75  prim_->set(x); prim_->axpy(h,v); // Set prim = x + hv
76  update(*prim_,UpdateType::Temp); // Temporarily update objective at x + hv
77  gradient(hv,*prim_,tol); // Compute gradient at x + hv
78  hv.axpy(-one,*dual_); // Compute difference (f'(x+hv)-f'(x))
79  hv.scale(one/h); // Compute Newton quotient (f'(x+hv)-f'(x))/h
80  update(x,UpdateType::Revert); // Reset objective to x
81  }
82 }
83 
84 template<typename Real>
85 std::vector<std::vector<Real>> Objective<Real>::checkGradient( const Vector<Real> &x,
86  const Vector<Real> &g,
87  const Vector<Real> &d,
88  const bool printToStream,
89  std::ostream & outStream,
90  const int numSteps,
91  const int order ) {
92 
93  const Real ten(10);
94  std::vector<Real> steps(numSteps);
95  for(int i=0;i<numSteps;++i) {
96  steps[i] = pow(ten,static_cast<Real>(-i));
97  }
98 
99  return checkGradient(x,g,d,steps,printToStream,outStream,order);
100 
101 } // checkGradient
102 
103 template<typename Real>
104 std::vector<std::vector<Real>> Objective<Real>::checkGradient( const Vector<Real> &x,
105  const Vector<Real> &g,
106  const Vector<Real> &d,
107  const std::vector<Real> &steps,
108  const bool printToStream,
109  std::ostream & outStream,
110  const int order ) {
111 
112  ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
113  "Error: finite difference order must be 1,2,3, or 4" );
114 
117 
118  Real tol = std::sqrt(ROL_EPSILON<Real>());
119 
120  int numSteps = steps.size();
121  int numVals = 4;
122  std::vector<Real> tmp(numVals);
123  std::vector<std::vector<Real>> gCheck(numSteps, tmp);
124 
125  // Save the format state of the original outStream.
126  nullstream oldFormatState;
127  oldFormatState.copyfmt(outStream);
128 
129  // Evaluate objective value at x.
131  Real val = value(x,tol);
132 
133  // Compute gradient at x.
134  Ptr<Vector<Real>> gtmp = g.clone();
135  gradient(*gtmp, x, tol);
136  //Real dtg = d.dot(gtmp->dual());
137  Real dtg = d.apply(*gtmp);
138 
139  // Temporary vectors.
140  Ptr<Vector<Real>> xnew = x.clone();
141 
142  for (int i=0; i<numSteps; i++) {
143 
144  Real eta = steps[i];
145 
146  xnew->set(x);
147 
148  // Compute gradient, finite-difference gradient, and absolute error.
149  gCheck[i][0] = eta;
150  gCheck[i][1] = dtg;
151 
152  gCheck[i][2] = weights[order-1][0] * val;
153 
154  for(int j=0; j<order; ++j) {
155  // Evaluate at x <- x+eta*c_i*d.
156  xnew->axpy(eta*shifts[order-1][j], d);
157 
158  // Only evaluate at shifts where the weight is nonzero
159  if( weights[order-1][j+1] != 0 ) {
160  update(*xnew,UpdateType::Temp);
161  gCheck[i][2] += weights[order-1][j+1] * this->value(*xnew,tol);
162  }
163  }
164 
165  gCheck[i][2] /= eta;
166 
167  gCheck[i][3] = std::abs(gCheck[i][2] - gCheck[i][1]);
168 
169  if (printToStream) {
170  if (i==0) {
171  outStream << std::right
172  << std::setw(20) << "Step size"
173  << std::setw(20) << "grad'*dir"
174  << std::setw(20) << "FD approx"
175  << std::setw(20) << "abs error"
176  << "\n"
177  << std::setw(20) << "---------"
178  << std::setw(20) << "---------"
179  << std::setw(20) << "---------"
180  << std::setw(20) << "---------"
181  << "\n";
182  }
183  outStream << std::scientific << std::setprecision(11) << std::right
184  << std::setw(20) << gCheck[i][0]
185  << std::setw(20) << gCheck[i][1]
186  << std::setw(20) << gCheck[i][2]
187  << std::setw(20) << gCheck[i][3]
188  << "\n";
189  }
190 
191  }
192 
193  // Reset format state of outStream.
194  outStream.copyfmt(oldFormatState);
195 
196  return gCheck;
197 } // checkGradient
198 
199 template<typename Real>
200 std::vector<std::vector<Real>> Objective<Real>::checkHessVec( const Vector<Real> &x,
201  const Vector<Real> &hv,
202  const Vector<Real> &v,
203  const bool printToStream,
204  std::ostream & outStream,
205  const int numSteps,
206  const int order ) {
207  const Real ten(10);
208  std::vector<Real> steps(numSteps);
209  for(int i=0;i<numSteps;++i) {
210  steps[i] = pow(ten,static_cast<Real>(-i));
211  }
212 
213  return checkHessVec(x,hv,v,steps,printToStream,outStream,order);
214 } // checkHessVec
215 
216 
217 
218 template<typename Real>
219 std::vector<std::vector<Real>> Objective<Real>::checkHessVec( const Vector<Real> &x,
220  const Vector<Real> &hv,
221  const Vector<Real> &v,
222  const std::vector<Real> &steps,
223  const bool printToStream,
224  std::ostream & outStream,
225  const int order ) {
226 
227  ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
228  "Error: finite difference order must be 1,2,3, or 4" );
229 
232 
233  const Real one(1);
234  Real tol = std::sqrt(ROL_EPSILON<Real>());
235 
236  int numSteps = steps.size();
237  int numVals = 4;
238  std::vector<Real> tmp(numVals);
239  std::vector<std::vector<Real>> hvCheck(numSteps, tmp);
240 
241  // Save the format state of the original outStream.
242  nullstream oldFormatState;
243  oldFormatState.copyfmt(outStream);
244 
245  // Compute gradient at x.
246  Ptr<Vector<Real>> g = hv.clone();
248  gradient(*g, x, tol);
249 
250  // Compute (Hessian at x) times (vector v).
251  Ptr<Vector<Real>> Hv = hv.clone();
252  hessVec(*Hv, v, x, tol);
253  Real normHv = Hv->norm();
254 
255  // Temporary vectors.
256  Ptr<Vector<Real>> gdif = hv.clone();
257  Ptr<Vector<Real>> gnew = hv.clone();
258  Ptr<Vector<Real>> xnew = x.clone();
259 
260  for (int i=0; i<numSteps; i++) {
261  Real eta = steps[i];
262  // Evaluate objective value at x+eta*d.
263  xnew->set(x);
264  gdif->set(*g);
265  gdif->scale(weights[order-1][0]);
266  for (int j=0; j<order; ++j) {
267  // Evaluate at x <- x+eta*c_i*d.
268  xnew->axpy(eta*shifts[order-1][j], v);
269  // Only evaluate at shifts where the weight is nonzero
270  if ( weights[order-1][j+1] != 0 ) {
271  update(*xnew,UpdateType::Temp);
272  gradient(*gnew, *xnew, tol);
273  gdif->axpy(weights[order-1][j+1],*gnew);
274  }
275  }
276  gdif->scale(one/eta);
277 
278  // Compute norms of hessvec, finite-difference hessvec, and error.
279  hvCheck[i][0] = eta;
280  hvCheck[i][1] = normHv;
281  hvCheck[i][2] = gdif->norm();
282  gdif->axpy(-one, *Hv);
283  hvCheck[i][3] = gdif->norm();
284 
285  if (printToStream) {
286  if (i==0) {
287  outStream << std::right
288  << std::setw(20) << "Step size"
289  << std::setw(20) << "norm(Hess*vec)"
290  << std::setw(20) << "norm(FD approx)"
291  << std::setw(20) << "norm(abs error)"
292  << "\n"
293  << std::setw(20) << "---------"
294  << std::setw(20) << "--------------"
295  << std::setw(20) << "---------------"
296  << std::setw(20) << "---------------"
297  << "\n";
298  }
299  outStream << std::scientific << std::setprecision(11) << std::right
300  << std::setw(20) << hvCheck[i][0]
301  << std::setw(20) << hvCheck[i][1]
302  << std::setw(20) << hvCheck[i][2]
303  << std::setw(20) << hvCheck[i][3]
304  << "\n";
305  }
306 
307  }
308 
309  // Reset format state of outStream.
310  outStream.copyfmt(oldFormatState);
311 
312  return hvCheck;
313 } // checkHessVec
314 
315 template<typename Real>
316 std::vector<Real> Objective<Real>::checkHessSym( const Vector<Real> &x,
317  const Vector<Real> &hv,
318  const Vector<Real> &v,
319  const Vector<Real> &w,
320  const bool printToStream,
321  std::ostream & outStream ) {
322 
323  Real tol = std::sqrt(ROL_EPSILON<Real>());
324 
325  // Compute (Hessian at x) times (vector v).
326  Ptr<Vector<Real>> h = hv.clone();
328  hessVec(*h, v, x, tol);
329  //Real wHv = w.dot(h->dual());
330  Real wHv = w.apply(*h);
331 
332  hessVec(*h, w, x, tol);
333  //Real vHw = v.dot(h->dual());
334  Real vHw = v.apply(*h);
335 
336  std::vector<Real> hsymCheck(3, 0);
337 
338  hsymCheck[0] = wHv;
339  hsymCheck[1] = vHw;
340  hsymCheck[2] = std::abs(vHw-wHv);
341 
342  // Save the format state of the original outStream.
343  nullstream oldFormatState;
344  oldFormatState.copyfmt(outStream);
345 
346  if (printToStream) {
347  outStream << std::right
348  << std::setw(20) << "<w, H(x)v>"
349  << std::setw(20) << "<v, H(x)w>"
350  << std::setw(20) << "abs error"
351  << "\n";
352  outStream << std::scientific << std::setprecision(11) << std::right
353  << std::setw(20) << hsymCheck[0]
354  << std::setw(20) << hsymCheck[1]
355  << std::setw(20) << hsymCheck[2]
356  << "\n";
357  }
358 
359  // Reset format state of outStream.
360  outStream.copyfmt(oldFormatState);
361 
362  return hsymCheck;
363 
364 } // checkHessSym
365 
366 } // namespace ROL
367 
368 #endif
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:192
virtual void scale(const Real alpha)=0
Compute where .
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual int dimension() const
Return dimension of the vector space.
Definition: ROL_Vector.hpp:162
virtual Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
Definition: ROL_Vector.hpp:204
virtual ROL::Ptr< Vector > basis(const int i) const
Return i-th basis vector.
Definition: ROL_Vector.hpp:148
const double weights[4][5]
Definition: ROL_Types.hpp:834
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:119
virtual void update(const Vector< Real > &u, const Vector< Real > &z, bool flag=true, int iter=-1) override
virtual Real dirDeriv(const Vector< Real > &x, const Vector< Real > &d, Real &tol)
Compute directional derivative.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
ROL::Objective_SimOpt value
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
virtual Real dot(const Vector &x) const =0
Compute where .
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.
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:38
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.
virtual Real norm() const =0
Returns where .
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.