10 #ifndef ROL_OBJECTIVE_DEF_H
11 #define ROL_OBJECTIVE_DEF_H
19 template<
typename Real>
21 if (dual_ == nullPtr) dual_ = x.
dual().
clone();
22 gradient(*dual_,x,tol);
24 return d.
apply(*dual_);
39 template<
typename Real>
41 if (prim_ == nullPtr) prim_ = x.
clone();
42 if (basis_ == nullPtr) basis_ = x.
clone();
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);
48 basis_->set(*x.
basis(i));
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;
54 gi = (
value(*prim_,tol) - f0) / h;
60 template<
typename Real>
62 const Real
zero(0), vnorm = v.
norm();
64 if ( vnorm ==
zero ) {
68 if (prim_ == nullPtr) prim_ = x.
clone();
69 if (dual_ == nullPtr) dual_ = hv.
clone();
72 const Real one(1), h(std::max(one,x.
norm()/vnorm)*tol);
74 gradient(*dual_,x,tol);
75 prim_->set(x); prim_->axpy(h,v);
77 gradient(hv,*prim_,tol);
84 template<
typename Real>
88 const bool printToStream,
89 std::ostream & outStream,
94 std::vector<Real> steps(numSteps);
95 for(
int i=0;i<numSteps;++i) {
96 steps[i] = pow(ten,static_cast<Real>(-i));
99 return checkGradient(x,g,d,steps,printToStream,outStream,order);
103 template<
typename Real>
107 const std::vector<Real> &steps,
108 const bool printToStream,
109 std::ostream & outStream,
112 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
113 "Error: finite difference order must be 1,2,3, or 4" );
118 Real tol = std::sqrt(ROL_EPSILON<Real>());
120 int numSteps = steps.size();
122 std::vector<Real> tmp(numVals);
123 std::vector<std::vector<Real>> gCheck(numSteps, tmp);
127 oldFormatState.copyfmt(outStream);
131 Real val =
value(x,tol);
134 Ptr<Vector<Real>> gtmp = g.
clone();
135 gradient(*gtmp, x, tol);
137 Real dtg = d.
apply(*gtmp);
140 Ptr<Vector<Real>> xnew = x.
clone();
142 for (
int i=0; i<numSteps; i++) {
152 gCheck[i][2] =
weights[order-1][0] * val;
154 for(
int j=0; j<order; ++j) {
156 xnew->axpy(eta*
shifts[order-1][j], d);
159 if(
weights[order-1][j+1] != 0 ) {
161 gCheck[i][2] +=
weights[order-1][j+1] * this->
value(*xnew,tol);
167 gCheck[i][3] = std::abs(gCheck[i][2] - gCheck[i][1]);
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"
177 << std::setw(20) <<
"---------"
178 << std::setw(20) <<
"---------"
179 << std::setw(20) <<
"---------"
180 << std::setw(20) <<
"---------"
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]
194 outStream.copyfmt(oldFormatState);
199 template<
typename Real>
203 const bool printToStream,
204 std::ostream & outStream,
208 std::vector<Real> steps(numSteps);
209 for(
int i=0;i<numSteps;++i) {
210 steps[i] = pow(ten,static_cast<Real>(-i));
213 return checkHessVec(x,hv,v,steps,printToStream,outStream,order);
218 template<
typename Real>
222 const std::vector<Real> &steps,
223 const bool printToStream,
224 std::ostream & outStream,
227 ROL_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
228 "Error: finite difference order must be 1,2,3, or 4" );
234 Real tol = std::sqrt(ROL_EPSILON<Real>());
236 int numSteps = steps.size();
238 std::vector<Real> tmp(numVals);
239 std::vector<std::vector<Real>> hvCheck(numSteps, tmp);
243 oldFormatState.copyfmt(outStream);
246 Ptr<Vector<Real>> g = hv.
clone();
248 gradient(*g, x, tol);
251 Ptr<Vector<Real>> Hv = hv.
clone();
252 hessVec(*Hv, v, x, tol);
253 Real normHv = Hv->norm();
256 Ptr<Vector<Real>> gdif = hv.
clone();
257 Ptr<Vector<Real>> gnew = hv.
clone();
258 Ptr<Vector<Real>> xnew = x.
clone();
260 for (
int i=0; i<numSteps; i++) {
265 gdif->scale(
weights[order-1][0]);
266 for (
int j=0; j<order; ++j) {
268 xnew->axpy(eta*
shifts[order-1][j], v);
270 if (
weights[order-1][j+1] != 0 ) {
272 gradient(*gnew, *xnew, tol);
273 gdif->axpy(
weights[order-1][j+1],*gnew);
276 gdif->scale(one/eta);
280 hvCheck[i][1] = normHv;
281 hvCheck[i][2] = gdif->norm();
282 gdif->axpy(-one, *Hv);
283 hvCheck[i][3] = gdif->norm();
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)"
293 << std::setw(20) <<
"---------"
294 << std::setw(20) <<
"--------------"
295 << std::setw(20) <<
"---------------"
296 << std::setw(20) <<
"---------------"
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]
310 outStream.copyfmt(oldFormatState);
315 template<
typename Real>
320 const bool printToStream,
321 std::ostream & outStream ) {
323 Real tol = std::sqrt(ROL_EPSILON<Real>());
326 Ptr<Vector<Real>> h = hv.
clone();
328 hessVec(*h, v, x, tol);
330 Real wHv = w.
apply(*h);
332 hessVec(*h, w, x, tol);
334 Real vHw = v.
apply(*h);
336 std::vector<Real> hsymCheck(3, 0);
340 hsymCheck[2] = std::abs(vHw-wHv);
344 oldFormatState.copyfmt(outStream);
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"
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]
360 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.