44 #ifndef ROL_OBJECTIVE_DEF_H
45 #define ROL_OBJECTIVE_DEF_H
56 if ( dnorm ==
zero ) {
59 Real cbrteps = std::cbrt(ROL_EPSILON<Real>()), one(1), v0(0), v1(0);
60 Real xnorm = x.
norm(), h = cbrteps * std::max(xnorm/dnorm,one);
61 ROL::Ptr<Vector<Real>> y = x.
clone();
62 y->set(x); y->axpy(h, d);
73 Real cbrteps = std::cbrt(ROL_EPSILON<Real>()),
zero(0), one(1);
74 Real f0 =
value(x,tol), h(0), xi(0), gi(0);
75 Ptr<Vector<Real>> y = x.
clone(), ei = x.
clone();
79 h = cbrteps * std::max(std::abs(xi),one) * (xi <
zero ? -one : one);
80 y->set(x); y->axpy(h,*ei);
83 gi = (
value(*y,tol) - f0) / h;
93 if ( vnorm ==
zero ) {
97 Real gtol = std::sqrt(ROL_EPSILON<Real>()), one(1);
99 Real h = std::max(one,x.
norm()/vnorm)*tol;
103 ROL::Ptr<Vector<Real>> g = hv.
clone();
107 ROL::Ptr<Vector<Real>> y = x.
clone();
108 y->set(x); y->axpy(h,v);
113 gradient(hv,*y,gtol);
126 template <
class Real>
130 const bool printToStream,
131 std::ostream & outStream,
135 std::vector<Real> steps(numSteps);
136 for(
int i=0;i<numSteps;++i) {
137 steps[i] = pow(10,-i);
140 return checkGradient(x,g,d,steps,printToStream,outStream,order);
146 template <
class Real>
150 const std::vector<Real> &steps,
151 const bool printToStream,
152 std::ostream & outStream,
155 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
156 "Error: finite difference order must be 1,2,3, or 4" );
161 Real tol = std::sqrt(ROL_EPSILON<Real>());
163 int numSteps = steps.size();
165 std::vector<Real> tmp(numVals);
166 std::vector<std::vector<Real> > gCheck(numSteps, tmp);
170 oldFormatState.copyfmt(outStream);
174 Real val = this->
value(x,tol);
177 ROL::Ptr<Vector<Real> > gtmp = g.
clone();
178 this->gradient(*gtmp, x, tol);
179 Real dtg = d.
dot(gtmp->dual());
182 ROL::Ptr<Vector<Real> > xnew = x.
clone();
184 for (
int i=0; i<numSteps; i++) {
194 gCheck[i][2] =
weights[order-1][0] * val;
196 for(
int j=0; j<order; ++j) {
198 xnew->axpy(eta*
shifts[order-1][j], d);
201 if(
weights[order-1][j+1] != 0 ) {
203 gCheck[i][2] +=
weights[order-1][j+1] * this->
value(*xnew,tol);
209 gCheck[i][3] = std::abs(gCheck[i][2] - gCheck[i][1]);
213 outStream << std::right
214 << std::setw(20) <<
"Step size"
215 << std::setw(20) <<
"grad'*dir"
216 << std::setw(20) <<
"FD approx"
217 << std::setw(20) <<
"abs error"
219 << std::setw(20) <<
"---------"
220 << std::setw(20) <<
"---------"
221 << std::setw(20) <<
"---------"
222 << std::setw(20) <<
"---------"
225 outStream << std::scientific << std::setprecision(11) << std::right
226 << std::setw(20) << gCheck[i][0]
227 << std::setw(20) << gCheck[i][1]
228 << std::setw(20) << gCheck[i][2]
229 << std::setw(20) << gCheck[i][3]
236 outStream.copyfmt(oldFormatState);
248 template <
class Real>
252 const bool printToStream,
253 std::ostream & outStream,
256 std::vector<Real> steps(numSteps);
257 for(
int i=0;i<numSteps;++i) {
258 steps[i] = pow(10,-i);
261 return checkHessVec(x,hv,v,steps,printToStream,outStream,order);
266 template <
class Real>
270 const std::vector<Real> &steps,
271 const bool printToStream,
272 std::ostream & outStream,
275 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
276 "Error: finite difference order must be 1,2,3, or 4" );
282 Real tol = std::sqrt(ROL_EPSILON<Real>());
284 int numSteps = steps.size();
286 std::vector<Real> tmp(numVals);
287 std::vector<std::vector<Real> > hvCheck(numSteps, tmp);
291 oldFormatState.copyfmt(outStream);
294 ROL::Ptr<Vector<Real> > g = hv.
clone();
296 this->gradient(*g, x, tol);
299 ROL::Ptr<Vector<Real> > Hv = hv.
clone();
300 this->hessVec(*Hv, v, x, tol);
301 Real normHv = Hv->norm();
304 ROL::Ptr<Vector<Real> > gdif = hv.
clone();
305 ROL::Ptr<Vector<Real> > gnew = hv.
clone();
306 ROL::Ptr<Vector<Real> > xnew = x.
clone();
308 for (
int i=0; i<numSteps; i++) {
316 gdif->scale(
weights[order-1][0]);
318 for(
int j=0; j<order; ++j) {
321 xnew->axpy(eta*
shifts[order-1][j], v);
324 if(
weights[order-1][j+1] != 0 ) {
326 this->gradient(*gnew, *xnew, tol);
327 gdif->axpy(
weights[order-1][j+1],*gnew);
332 gdif->scale(1.0/eta);
336 hvCheck[i][1] = normHv;
337 hvCheck[i][2] = gdif->norm();
338 gdif->axpy(-1.0, *Hv);
339 hvCheck[i][3] = gdif->norm();
343 outStream << std::right
344 << std::setw(20) <<
"Step size"
345 << std::setw(20) <<
"norm(Hess*vec)"
346 << std::setw(20) <<
"norm(FD approx)"
347 << std::setw(20) <<
"norm(abs error)"
349 << std::setw(20) <<
"---------"
350 << std::setw(20) <<
"--------------"
351 << std::setw(20) <<
"---------------"
352 << std::setw(20) <<
"---------------"
355 outStream << std::scientific << std::setprecision(11) << std::right
356 << std::setw(20) << hvCheck[i][0]
357 << std::setw(20) << hvCheck[i][1]
358 << std::setw(20) << hvCheck[i][2]
359 << std::setw(20) << hvCheck[i][3]
366 outStream.copyfmt(oldFormatState);
378 const bool printToStream,
379 std::ostream & outStream ) {
381 Real tol = std::sqrt(ROL_EPSILON<Real>());
384 ROL::Ptr<Vector<Real> > h = hv.
clone();
385 this->hessVec(*h, v, x, tol);
386 Real wHv = w.
dot(h->dual());
388 this->hessVec(*h, w, x, tol);
389 Real vHw = v.
dot(h->dual());
391 std::vector<Real> hsymCheck(3, 0);
395 hsymCheck[2] = std::abs(vHw-wHv);
399 oldFormatState.copyfmt(outStream);
402 outStream << std::right
403 << std::setw(20) <<
"<w, H(x)v>"
404 << std::setw(20) <<
"<v, H(x)w>"
405 << std::setw(20) <<
"abs error"
407 outStream << std::scientific << std::setprecision(11) << std::right
408 << std::setw(20) << hsymCheck[0]
409 << std::setw(20) << hsymCheck[1]
410 << std::setw(20) << hsymCheck[2]
415 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.