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
47namespace ROL {
48
49template<typename Real>
50void 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
55template<typename Real>
56void 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
61template<typename Real>
62Real 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
67template<typename Real>
68void 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
86template<typename Real>
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
93template<typename Real>
94Real 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
99template<typename Real>
100Real 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
111template<typename Real>
112void 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
117template<typename Real>
118void 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
130template<typename Real>
131void 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
136template<typename Real>
137void 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
144template<typename Real>
145void 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
149template<typename Real>
150void 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
157template<typename Real>
158Real 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
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