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
13namespace ROL {
14
15template<typename Real>
16void 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
21template<typename Real>
22void 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
27template<typename Real>
28Real 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
33template<typename Real>
34void 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
52template<typename Real>
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
59template<typename Real>
60Real 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
65template<typename Real>
66Real 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
77template<typename Real>
78void 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
83template<typename Real>
84void 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
96template<typename Real>
97void 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
102template<typename Real>
103void 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
110template<typename Real>
111void 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
115template<typename Real>
116void 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
123template<typename Real>
124Real 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
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
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.
virtual Real dirDeriv(const std::vector< Real > &x, const std::vector< Real > &d, Real &tol)
virtual void invHessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
virtual void precond(std::vector< Real > &Pv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
virtual void update(const std::vector< Real > &x, bool flag=true, int iter=-1)
virtual void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
virtual Real value(const std::vector< Real > &x, Real &tol)=0
virtual void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Ptr< const std::vector< Element > > getVector() const
Defines the linear algebra or vector space interface.
ROL::Objective_SerialSimOpt Objective_SimOpt value(const V &u, const V &z, Real &tol) override
virtual void update(const Vector< Real > &u, const Vector< Real > &z, bool flag=true, int iter=-1) override