ROL
ROL_StdObjective_Def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_STDOBJECTIVE_DEF_H
11 #define ROL_STDOBJECTIVE_DEF_H
12 
13 namespace ROL {
14 
15 template<typename Real>
16 void StdObjective<Real>::update( const Vector<Real> &x, bool flag, int iter ) {
17  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
18  update(*(xs.getVector()),flag,iter);
19 }
20 
21 template<typename Real>
22 void StdObjective<Real>::update( const Vector<Real> &x, UpdateType type, int iter ) {
23  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
24  update(*(xs.getVector()),type,iter);
25 }
26 
27 template<typename Real>
28 Real StdObjective<Real>::value( const Vector<Real> &x, Real &tol ) {
29  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
30  return value(*(xs.getVector()),tol);
31 }
32 
33 template<typename Real>
34 void StdObjective<Real>::gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
35  const unsigned size = x.size();
36  std::vector<Real> y; y.assign(x.begin(),x.end());
37  const Real cbrteps = std::cbrt(ROL::ROL_EPSILON<Real>()), one(1);
38  Real h(1), xi(0);
39  const Real val = value(x,tol);
40  for (unsigned i = 0; i < size; ++i) {
41  xi = x[i];
42  h = cbrteps * std::max(std::abs(xi),one) * sgn(xi);
43  y[i] = xi + h;
44  h = y[i] - xi;
45  update(y);
46  g[i] = (value(y,tol) - val)/h;
47  y[i] = xi;
48  }
49  update(x);
50 }
51 
52 template<typename Real>
53 void StdObjective<Real>::gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
54  StdVector<Real> gs = dynamic_cast<StdVector<Real>&>(g);
55  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
56  gradient(*(gs.getVector()),*(xs.getVector()),tol);
57 }
58 
59 template<typename Real>
60 Real StdObjective<Real>::dirDeriv( const std::vector<Real> &x, const std::vector<Real> &d, Real &tol ) {
61  ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
62  ">>> ERROR (ROL::StdObjective): dirDeriv not implemented!");
63 }
64 
65 template<typename Real>
66 Real StdObjective<Real>::dirDeriv( const Vector<Real> &x, const Vector<Real> &d, Real &tol ) {
67  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
68  const StdVector<Real> ds = dynamic_cast<const StdVector<Real>&>(d);
69  try {
70  return dirDeriv(*(xs.getVector()),*(ds.getVector()),tol);
71  }
72  catch (std::exception &e) {
73  return Objective<Real>::dirDeriv(x,d,tol);
74  }
75 }
76 
77 template<typename Real>
78 void StdObjective<Real>::hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
79  ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
80  ">>> ERROR (ROL::StdObjective): hessVec not implemented!");
81 }
82 
83 template<typename Real>
84 void StdObjective<Real>::hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
85  try {
86  StdVector<Real> hvs = dynamic_cast<StdVector<Real>&>(hv);
87  const StdVector<Real> vs = dynamic_cast<const StdVector<Real>&>(v);
88  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
89  hessVec(*(hvs.getVector()),*(vs.getVector()),*(xs.getVector()),tol);
90  }
91  catch (std::exception &e) {
92  Objective<Real>::hessVec(hv,v,x,tol);
93  }
94 }
95 
96 template<typename Real>
97 void StdObjective<Real>::invHessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
98  ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
99  ">>> ERROR (ROL::StdObjective): invHessVec not implemented!");
100 }
101 
102 template<typename Real>
103 void StdObjective<Real>::invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
104  StdVector<Real> hvs = dynamic_cast<StdVector<Real>&>(hv);
105  const StdVector<Real> vs = dynamic_cast<const StdVector<Real>&>(v);
106  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
107  invHessVec(*(hvs.getVector()),*(vs.getVector()),*(xs.getVector()),tol);
108 }
109 
110 template<typename Real>
111 void StdObjective<Real>::precond( std::vector<Real> &Pv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
112  Pv.assign(v.begin(),v.end());
113 }
114 
115 template<typename Real>
116 void StdObjective<Real>::precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
117  StdVector<Real> Pvs = dynamic_cast<StdVector<Real>&>(Pv);
118  const StdVector<Real> vs = dynamic_cast<const StdVector<Real>&>(v);
119  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
120  precond(*(Pvs.getVector()),*(vs.getVector()),*(xs.getVector()),tol);
121 }
122 
123 template<typename Real>
124 Real StdObjective<Real>::sgn(Real x) const {
125  const Real zero(0), one(1);
126  return (x < zero ? -one : one);
127 }
128 
129 } // namespace ROL
130 
131 #endif
virtual Real value(const std::vector< Real > &x, Real &tol)=0
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.
Ptr< const std::vector< Element > > getVector() const
ROL::Objective_SimOpt value
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
virtual void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
virtual void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
virtual void invHessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Real sgn(Real x) const
virtual void update(const std::vector< Real > &x, bool flag=true, int iter=-1)
virtual void precond(std::vector< Real > &Pv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
virtual Real dirDeriv(const std::vector< Real > &x, const std::vector< Real > &d, Real &tol)