44 #ifndef ROL_OBJECTIVE_DEF_H
45 #define ROL_OBJECTIVE_DEF_H
55 Real ftol = std::sqrt(ROL_EPSILON<Real>());
57 ROL::Ptr<Vector<Real> > xd = d.
clone();
60 return (this->
value(*xd,ftol) - this->
value(x,ftol)) / tol;
66 Real deriv = 0.0, h = 0.0, xi = 0.0;
69 h = ((xi < ROL_EPSILON<Real>()) ? 1. : xi)*tol;
70 deriv = this->dirDeriv(x,*x.
basis(i),h);
83 Real gtol = std::sqrt(ROL_EPSILON<Real>());
85 Real h = std::max(one,x.
norm()/v.
norm())*tol;
89 ROL::Ptr<Vector<Real> > g = hv.
clone();
90 this->gradient(*g,x,gtol);
93 ROL::Ptr<Vector<Real> > xnew = x.
clone();
100 this->gradient(hv,*xnew,gtol);
110 template <
class Real>
114 const bool printToStream,
115 std::ostream & outStream,
119 std::vector<Real> steps(numSteps);
120 for(
int i=0;i<numSteps;++i) {
121 steps[i] = pow(10,-i);
124 return checkGradient(x,g,d,steps,printToStream,outStream,order);
130 template <
class Real>
134 const std::vector<Real> &steps,
135 const bool printToStream,
136 std::ostream & outStream,
139 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
140 "Error: finite difference order must be 1,2,3, or 4" );
145 Real tol = std::sqrt(ROL_EPSILON<Real>());
147 int numSteps = steps.size();
149 std::vector<Real> tmp(numVals);
150 std::vector<std::vector<Real> > gCheck(numSteps, tmp);
154 oldFormatState.copyfmt(outStream);
158 Real val = this->
value(x,tol);
161 ROL::Ptr<Vector<Real> > gtmp = g.
clone();
163 this->gradient(*gtmp, x, tol);
164 Real dtg = d.
dot(gtmp->dual());
167 ROL::Ptr<Vector<Real> > xnew = x.
clone();
169 for (
int i=0; i<numSteps; i++) {
179 gCheck[i][2] =
weights[order-1][0] * val;
181 for(
int j=0; j<order; ++j) {
183 xnew->axpy(eta*
shifts[order-1][j], d);
186 if(
weights[order-1][j+1] != 0 ) {
188 gCheck[i][2] +=
weights[order-1][j+1] * this->
value(*xnew,tol);
194 gCheck[i][3] = std::abs(gCheck[i][2] - gCheck[i][1]);
198 outStream << std::right
199 << std::setw(20) <<
"Step size"
200 << std::setw(20) <<
"grad'*dir"
201 << std::setw(20) <<
"FD approx"
202 << std::setw(20) <<
"abs error"
204 << std::setw(20) <<
"---------"
205 << std::setw(20) <<
"---------"
206 << std::setw(20) <<
"---------"
207 << std::setw(20) <<
"---------"
210 outStream << std::scientific << std::setprecision(11) << std::right
211 << std::setw(20) << gCheck[i][0]
212 << std::setw(20) << gCheck[i][1]
213 << std::setw(20) << gCheck[i][2]
214 << std::setw(20) << gCheck[i][3]
221 outStream.copyfmt(oldFormatState);
233 template <
class Real>
237 const bool printToStream,
238 std::ostream & outStream,
241 std::vector<Real> steps(numSteps);
242 for(
int i=0;i<numSteps;++i) {
243 steps[i] = pow(10,-i);
246 return checkHessVec(x,hv,v,steps,printToStream,outStream,order);
251 template <
class Real>
255 const std::vector<Real> &steps,
256 const bool printToStream,
257 std::ostream & outStream,
260 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
261 "Error: finite difference order must be 1,2,3, or 4" );
267 Real tol = std::sqrt(ROL_EPSILON<Real>());
269 int numSteps = steps.size();
271 std::vector<Real> tmp(numVals);
272 std::vector<std::vector<Real> > hvCheck(numSteps, tmp);
276 oldFormatState.copyfmt(outStream);
279 ROL::Ptr<Vector<Real> > g = hv.
clone();
281 this->gradient(*g, x, tol);
284 ROL::Ptr<Vector<Real> > Hv = hv.
clone();
285 this->hessVec(*Hv, v, x, tol);
286 Real normHv = Hv->norm();
289 ROL::Ptr<Vector<Real> > gdif = hv.
clone();
290 ROL::Ptr<Vector<Real> > gnew = hv.
clone();
291 ROL::Ptr<Vector<Real> > xnew = x.
clone();
293 for (
int i=0; i<numSteps; i++) {
301 gdif->scale(
weights[order-1][0]);
303 for(
int j=0; j<order; ++j) {
306 xnew->axpy(eta*
shifts[order-1][j], v);
309 if(
weights[order-1][j+1] != 0 ) {
311 this->gradient(*gnew, *xnew, tol);
312 gdif->axpy(
weights[order-1][j+1],*gnew);
317 gdif->scale(1.0/eta);
321 hvCheck[i][1] = normHv;
322 hvCheck[i][2] = gdif->norm();
323 gdif->axpy(-1.0, *Hv);
324 hvCheck[i][3] = gdif->norm();
328 outStream << std::right
329 << std::setw(20) <<
"Step size"
330 << std::setw(20) <<
"norm(Hess*vec)"
331 << std::setw(20) <<
"norm(FD approx)"
332 << std::setw(20) <<
"norm(abs error)"
334 << std::setw(20) <<
"---------"
335 << std::setw(20) <<
"--------------"
336 << std::setw(20) <<
"---------------"
337 << std::setw(20) <<
"---------------"
340 outStream << std::scientific << std::setprecision(11) << std::right
341 << std::setw(20) << hvCheck[i][0]
342 << std::setw(20) << hvCheck[i][1]
343 << std::setw(20) << hvCheck[i][2]
344 << std::setw(20) << hvCheck[i][3]
351 outStream.copyfmt(oldFormatState);
363 const bool printToStream,
364 std::ostream & outStream ) {
366 Real tol = std::sqrt(ROL_EPSILON<Real>());
369 ROL::Ptr<Vector<Real> > h = hv.
clone();
370 this->hessVec(*h, v, x, tol);
371 Real wHv = w.
dot(h->dual());
373 this->hessVec(*h, w, x, tol);
374 Real vHw = v.
dot(h->dual());
376 std::vector<Real> hsymCheck(3, 0);
380 hsymCheck[2] = std::abs(vHw-wHv);
384 oldFormatState.copyfmt(outStream);
387 outStream << std::right
388 << std::setw(20) <<
"<w, H(x)v>"
389 << std::setw(20) <<
"<v, H(x)w>"
390 << std::setw(20) <<
"abs error"
392 outStream << std::scientific << std::setprecision(11) << std::right
393 << std::setw(20) << hsymCheck[0]
394 << std::setw(20) << hsymCheck[1]
395 << std::setw(20) << hsymCheck[2]
400 outStream.copyfmt(oldFormatState);
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.
virtual ROL::Ptr< Vector > basis(const int i) const
Return i-th basis vector.
const double weights[4][5]
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
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.
Defines the linear algebra or vector space interface.
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
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.