ROL
ROL_StdObjective_Def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_STDOBJECTIVE_DEF_H
45 #define ROL_STDOBJECTIVE_DEF_H
46 
47 namespace ROL {
48 
49 template<typename Real>
50 void StdObjective<Real>::update( const Vector<Real> &x, bool flag, int iter ) {
51  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
52  update(*(xs.getVector()),flag,iter);
53 }
54 
55 template<typename Real>
56 void StdObjective<Real>::update( const Vector<Real> &x, UpdateType type, int iter ) {
57  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
58  update(*(xs.getVector()),type,iter);
59 }
60 
61 template<typename Real>
62 Real StdObjective<Real>::value( const Vector<Real> &x, Real &tol ) {
63  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
64  return value(*(xs.getVector()),tol);
65 }
66 
67 template<typename Real>
68 void StdObjective<Real>::gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
69  const unsigned size = x.size();
70  std::vector<Real> y; y.assign(x.begin(),x.end());
71  const Real cbrteps = std::cbrt(ROL::ROL_EPSILON<Real>()), one(1);
72  Real h(1), xi(0);
73  const Real val = value(x,tol);
74  for (unsigned i = 0; i < size; ++i) {
75  xi = x[i];
76  h = cbrteps * std::max(std::abs(xi),one) * sgn(xi);
77  y[i] = xi + h;
78  h = y[i] - xi;
79  update(y);
80  g[i] = (value(y,tol) - val)/h;
81  y[i] = xi;
82  }
83  update(x);
84 }
85 
86 template<typename Real>
87 void StdObjective<Real>::gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
88  StdVector<Real> gs = dynamic_cast<StdVector<Real>&>(g);
89  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
90  gradient(*(gs.getVector()),*(xs.getVector()),tol);
91 }
92 
93 template<typename Real>
94 Real StdObjective<Real>::dirDeriv( const std::vector<Real> &x, const std::vector<Real> &d, Real &tol ) {
95  ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
96  ">>> ERROR (ROL::StdObjective): dirDeriv not implemented!");
97 }
98 
99 template<typename Real>
100 Real StdObjective<Real>::dirDeriv( const Vector<Real> &x, const Vector<Real> &d, Real &tol ) {
101  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
102  const StdVector<Real> ds = dynamic_cast<const StdVector<Real>&>(d);
103  try {
104  return dirDeriv(*(xs.getVector()),*(ds.getVector()),tol);
105  }
106  catch (std::exception &e) {
107  return Objective<Real>::dirDeriv(x,d,tol);
108  }
109 }
110 
111 template<typename Real>
112 void StdObjective<Real>::hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
113  ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
114  ">>> ERROR (ROL::StdObjective): hessVec not implemented!");
115 }
116 
117 template<typename Real>
118 void StdObjective<Real>::hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
119  try {
120  StdVector<Real> hvs = dynamic_cast<StdVector<Real>&>(hv);
121  const StdVector<Real> vs = dynamic_cast<const StdVector<Real>&>(v);
122  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
123  hessVec(*(hvs.getVector()),*(vs.getVector()),*(xs.getVector()),tol);
124  }
125  catch (std::exception &e) {
126  Objective<Real>::hessVec(hv,v,x,tol);
127  }
128 }
129 
130 template<typename Real>
131 void StdObjective<Real>::invHessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
132  ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
133  ">>> ERROR (ROL::StdObjective): invHessVec not implemented!");
134 }
135 
136 template<typename Real>
137 void StdObjective<Real>::invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
138  StdVector<Real> hvs = dynamic_cast<StdVector<Real>&>(hv);
139  const StdVector<Real> vs = dynamic_cast<const StdVector<Real>&>(v);
140  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
141  invHessVec(*(hvs.getVector()),*(vs.getVector()),*(xs.getVector()),tol);
142 }
143 
144 template<typename Real>
145 void StdObjective<Real>::precond( std::vector<Real> &Pv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
146  Pv.assign(v.begin(),v.end());
147 }
148 
149 template<typename Real>
150 void StdObjective<Real>::precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
151  StdVector<Real> Pvs = dynamic_cast<StdVector<Real>&>(Pv);
152  const StdVector<Real> vs = dynamic_cast<const StdVector<Real>&>(v);
153  const StdVector<Real> xs = dynamic_cast<const StdVector<Real>&>(x);
154  precond(*(Pvs.getVector()),*(vs.getVector()),*(xs.getVector()),tol);
155 }
156 
157 template<typename Real>
158 Real StdObjective<Real>::sgn(Real x) const {
159  const Real zero(0), one(1);
160  return (x < zero ? -one : one);
161 }
162 
163 } // namespace ROL
164 
165 #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:80
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)