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();
162 this->gradient(*gtmp, x, tol);
163 Real dtg = d.
dot(gtmp->dual());
166 ROL::Ptr<Vector<Real> > xnew = x.
clone();
168 for (
int i=0; i<numSteps; i++) {
178 gCheck[i][2] =
weights[order-1][0] * val;
180 for(
int j=0; j<order; ++j) {
182 xnew->axpy(eta*
shifts[order-1][j], d);
185 if(
weights[order-1][j+1] != 0 ) {
187 gCheck[i][2] +=
weights[order-1][j+1] * this->
value(*xnew,tol);
193 gCheck[i][3] = std::abs(gCheck[i][2] - gCheck[i][1]);
197 outStream << std::right
198 << std::setw(20) <<
"Step size"
199 << std::setw(20) <<
"grad'*dir"
200 << std::setw(20) <<
"FD approx"
201 << std::setw(20) <<
"abs error"
203 << std::setw(20) <<
"---------"
204 << std::setw(20) <<
"---------"
205 << std::setw(20) <<
"---------"
206 << std::setw(20) <<
"---------"
209 outStream << std::scientific << std::setprecision(11) << std::right
210 << std::setw(20) << gCheck[i][0]
211 << std::setw(20) << gCheck[i][1]
212 << std::setw(20) << gCheck[i][2]
213 << std::setw(20) << gCheck[i][3]
220 outStream.copyfmt(oldFormatState);
232 template <
class Real>
236 const bool printToStream,
237 std::ostream & outStream,
240 std::vector<Real> steps(numSteps);
241 for(
int i=0;i<numSteps;++i) {
242 steps[i] = pow(10,-i);
245 return checkHessVec(x,hv,v,steps,printToStream,outStream,order);
250 template <
class Real>
254 const std::vector<Real> &steps,
255 const bool printToStream,
256 std::ostream & outStream,
259 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
260 "Error: finite difference order must be 1,2,3, or 4" );
266 Real tol = std::sqrt(ROL_EPSILON<Real>());
268 int numSteps = steps.size();
270 std::vector<Real> tmp(numVals);
271 std::vector<std::vector<Real> > hvCheck(numSteps, tmp);
275 oldFormatState.copyfmt(outStream);
278 ROL::Ptr<Vector<Real> > g = hv.
clone();
280 this->gradient(*g, x, tol);
283 ROL::Ptr<Vector<Real> > Hv = hv.
clone();
284 this->hessVec(*Hv, v, x, tol);
285 Real normHv = Hv->norm();
288 ROL::Ptr<Vector<Real> > gdif = hv.
clone();
289 ROL::Ptr<Vector<Real> > gnew = hv.
clone();
290 ROL::Ptr<Vector<Real> > xnew = x.
clone();
292 for (
int i=0; i<numSteps; i++) {
300 gdif->scale(
weights[order-1][0]);
302 for(
int j=0; j<order; ++j) {
305 xnew->axpy(eta*
shifts[order-1][j], v);
308 if(
weights[order-1][j+1] != 0 ) {
310 this->gradient(*gnew, *xnew, tol);
311 gdif->axpy(
weights[order-1][j+1],*gnew);
316 gdif->scale(1.0/eta);
320 hvCheck[i][1] = normHv;
321 hvCheck[i][2] = gdif->norm();
322 gdif->axpy(-1.0, *Hv);
323 hvCheck[i][3] = gdif->norm();
327 outStream << std::right
328 << std::setw(20) <<
"Step size"
329 << std::setw(20) <<
"norm(Hess*vec)"
330 << std::setw(20) <<
"norm(FD approx)"
331 << std::setw(20) <<
"norm(abs error)"
333 << std::setw(20) <<
"---------"
334 << std::setw(20) <<
"--------------"
335 << std::setw(20) <<
"---------------"
336 << std::setw(20) <<
"---------------"
339 outStream << std::scientific << std::setprecision(11) << std::right
340 << std::setw(20) << hvCheck[i][0]
341 << std::setw(20) << hvCheck[i][1]
342 << std::setw(20) << hvCheck[i][2]
343 << std::setw(20) << hvCheck[i][3]
350 outStream.copyfmt(oldFormatState);
362 const bool printToStream,
363 std::ostream & outStream ) {
365 Real tol = std::sqrt(ROL_EPSILON<Real>());
368 ROL::Ptr<Vector<Real> > h = hv.
clone();
369 this->hessVec(*h, v, x, tol);
370 Real wHv = w.
dot(h->dual());
372 this->hessVec(*h, w, x, tol);
373 Real vHw = v.
dot(h->dual());
375 std::vector<Real> hsymCheck(3, 0);
379 hsymCheck[2] = std::abs(vHw-wHv);
383 oldFormatState.copyfmt(outStream);
386 outStream << std::right
387 << std::setw(20) <<
"<w, H(x)v>"
388 << std::setw(20) <<
"<v, H(x)w>"
389 << std::setw(20) <<
"abs error"
391 outStream << std::scientific << std::setprecision(11) << std::right
392 << std::setw(20) << hsymCheck[0]
393 << std::setw(20) << hsymCheck[1]
394 << std::setw(20) << hsymCheck[2]
399 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.