44 #ifndef ROL_OBJECTIVE_DEF_H
45 #define ROL_OBJECTIVE_DEF_H
53 template<
typename Real>
55 if (dual_ == nullPtr) dual_ = x.
dual().
clone();
56 gradient(*dual_,x,tol);
58 return d.
apply(*dual_);
73 template<
typename Real>
75 if (prim_ == nullPtr) prim_ = x.
clone();
76 if (basis_ == nullPtr) basis_ = x.
clone();
78 const Real cbrteps = std::cbrt(ROL_EPSILON<Real>()),
zero(0), one(1);
79 Real f0 =
value(x,tol), h(0), xi(0), gi(0);
82 basis_->set(*x.
basis(i));
84 h = cbrteps * std::max(std::abs(xi),one) * (xi <
zero ? -one : one);
85 prim_->set(x); prim_->axpy(h,*basis_);
86 h = prim_->dot(*basis_) - xi;
88 gi = (
value(*prim_,tol) - f0) / h;
94 template<
typename Real>
96 const Real
zero(0), vnorm = v.
norm();
98 if ( vnorm ==
zero ) {
102 if (prim_ == nullPtr) prim_ = x.
clone();
103 if (dual_ == nullPtr) dual_ = hv.
clone();
106 const Real one(1), h(std::max(one,x.
norm()/vnorm)*tol);
108 gradient(*dual_,x,tol);
109 prim_->set(x); prim_->axpy(h,v);
111 gradient(hv,*prim_,tol);
112 hv.
axpy(-one,*dual_);
118 template<
typename Real>
122 const bool printToStream,
123 std::ostream & outStream,
128 std::vector<Real> steps(numSteps);
129 for(
int i=0;i<numSteps;++i) {
130 steps[i] = pow(ten,static_cast<Real>(-i));
133 return checkGradient(x,g,d,steps,printToStream,outStream,order);
137 template<
typename Real>
141 const std::vector<Real> &steps,
142 const bool printToStream,
143 std::ostream & outStream,
146 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
147 "Error: finite difference order must be 1,2,3, or 4" );
152 Real tol = std::sqrt(ROL_EPSILON<Real>());
154 int numSteps = steps.size();
156 std::vector<Real> tmp(numVals);
157 std::vector<std::vector<Real>> gCheck(numSteps, tmp);
161 oldFormatState.copyfmt(outStream);
165 Real val =
value(x,tol);
168 Ptr<Vector<Real>> gtmp = g.
clone();
169 gradient(*gtmp, x, tol);
171 Real dtg = d.
apply(*gtmp);
174 Ptr<Vector<Real>> xnew = x.
clone();
176 for (
int i=0; i<numSteps; i++) {
186 gCheck[i][2] =
weights[order-1][0] * val;
188 for(
int j=0; j<order; ++j) {
190 xnew->axpy(eta*
shifts[order-1][j], d);
193 if(
weights[order-1][j+1] != 0 ) {
195 gCheck[i][2] +=
weights[order-1][j+1] * this->
value(*xnew,tol);
201 gCheck[i][3] = std::abs(gCheck[i][2] - gCheck[i][1]);
205 outStream << std::right
206 << std::setw(20) <<
"Step size"
207 << std::setw(20) <<
"grad'*dir"
208 << std::setw(20) <<
"FD approx"
209 << std::setw(20) <<
"abs error"
211 << std::setw(20) <<
"---------"
212 << std::setw(20) <<
"---------"
213 << std::setw(20) <<
"---------"
214 << std::setw(20) <<
"---------"
217 outStream << std::scientific << std::setprecision(11) << std::right
218 << std::setw(20) << gCheck[i][0]
219 << std::setw(20) << gCheck[i][1]
220 << std::setw(20) << gCheck[i][2]
221 << std::setw(20) << gCheck[i][3]
228 outStream.copyfmt(oldFormatState);
233 template<
typename Real>
237 const bool printToStream,
238 std::ostream & outStream,
242 std::vector<Real> steps(numSteps);
243 for(
int i=0;i<numSteps;++i) {
244 steps[i] = pow(ten,static_cast<Real>(-i));
247 return checkHessVec(x,hv,v,steps,printToStream,outStream,order);
252 template<
typename Real>
256 const std::vector<Real> &steps,
257 const bool printToStream,
258 std::ostream & outStream,
261 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
262 "Error: finite difference order must be 1,2,3, or 4" );
268 Real tol = std::sqrt(ROL_EPSILON<Real>());
270 int numSteps = steps.size();
272 std::vector<Real> tmp(numVals);
273 std::vector<std::vector<Real>> hvCheck(numSteps, tmp);
277 oldFormatState.copyfmt(outStream);
280 Ptr<Vector<Real>> g = hv.
clone();
282 gradient(*g, x, tol);
285 Ptr<Vector<Real>> Hv = hv.
clone();
286 hessVec(*Hv, v, x, tol);
287 Real normHv = Hv->norm();
290 Ptr<Vector<Real>> gdif = hv.
clone();
291 Ptr<Vector<Real>> gnew = hv.
clone();
292 Ptr<Vector<Real>> xnew = x.
clone();
294 for (
int i=0; i<numSteps; i++) {
299 gdif->scale(
weights[order-1][0]);
300 for (
int j=0; j<order; ++j) {
302 xnew->axpy(eta*
shifts[order-1][j], v);
304 if (
weights[order-1][j+1] != 0 ) {
306 gradient(*gnew, *xnew, tol);
307 gdif->axpy(
weights[order-1][j+1],*gnew);
310 gdif->scale(one/eta);
314 hvCheck[i][1] = normHv;
315 hvCheck[i][2] = gdif->norm();
316 gdif->axpy(-one, *Hv);
317 hvCheck[i][3] = gdif->norm();
321 outStream << std::right
322 << std::setw(20) <<
"Step size"
323 << std::setw(20) <<
"norm(Hess*vec)"
324 << std::setw(20) <<
"norm(FD approx)"
325 << std::setw(20) <<
"norm(abs error)"
327 << std::setw(20) <<
"---------"
328 << std::setw(20) <<
"--------------"
329 << std::setw(20) <<
"---------------"
330 << std::setw(20) <<
"---------------"
333 outStream << std::scientific << std::setprecision(11) << std::right
334 << std::setw(20) << hvCheck[i][0]
335 << std::setw(20) << hvCheck[i][1]
336 << std::setw(20) << hvCheck[i][2]
337 << std::setw(20) << hvCheck[i][3]
344 outStream.copyfmt(oldFormatState);
349 template<
typename Real>
354 const bool printToStream,
355 std::ostream & outStream ) {
357 Real tol = std::sqrt(ROL_EPSILON<Real>());
360 Ptr<Vector<Real>> h = hv.
clone();
362 hessVec(*h, v, x, tol);
364 Real wHv = w.
apply(*h);
366 hessVec(*h, w, x, tol);
368 Real vHw = v.
apply(*h);
370 std::vector<Real> hsymCheck(3, 0);
374 hsymCheck[2] = std::abs(vHw-wHv);
378 oldFormatState.copyfmt(outStream);
381 outStream << std::right
382 << std::setw(20) <<
"<w, H(x)v>"
383 << std::setw(20) <<
"<v, H(x)w>"
384 << std::setw(20) <<
"abs error"
386 outStream << std::scientific << std::setprecision(11) << std::right
387 << std::setw(20) << hsymCheck[0]
388 << std::setw(20) << hsymCheck[1]
389 << std::setw(20) << hsymCheck[2]
394 outStream.copyfmt(oldFormatState);
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
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 Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
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.