ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
example
sacado
example_01b.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
17
18
19
20
#include "
ROL_StdVector.hpp
"
21
#include "Sacado.hpp"
22
23
using namespace
ROL
;
24
25
template
<
class
ScalarT>
26
class
FunctionZakharov
{
27
28
29
30
public
:
31
32
ScalarT
eval
(
const
std::vector<ScalarT> &x);
33
34
};
35
46
template
<
class
ScalarT>
47
ScalarT
FunctionZakharov<ScalarT>::eval
(
const
std::vector<ScalarT> & x) {
48
49
typedef
typename
std::vector<ScalarT>::size_type luint;
50
51
ScalarT xdotx = 0;
52
ScalarT kdotx = 0;
53
ScalarT J = 0;
54
55
// Compute dot products
56
for
(luint i=0; i<x.size(); ++i) {
57
xdotx += pow(x[i],2);
// (k,x)
58
kdotx += ScalarT(i+1)*x[i];
// (x,x)
59
}
60
61
// Sum terms in objective function
62
J = xdotx + pow(kdotx,2)/4.0 + pow(kdotx,4)/16.0;
63
64
return
J;
65
}
66
67
68
template
<
class
Real>
69
class
Zakharov_Sacado_Objective
:
public
Objective
<Real> {
70
71
typedef
std::vector<Real>
vector
;
72
typedef
Vector<Real>
V
;
73
typedef
StdVector<Real>
SV
;
74
75
typedef
Sacado::Fad::DFad<Real>
GradType
;
76
typedef
Sacado::Fad::SFad<Real,1>
DirDerivType
;
77
typedef
Sacado::Fad::DFad<DirDerivType>
HessVecType
;
78
79
typedef
typename
vector::size_type
luint
;
80
81
// In C++11, we could use template typedefs:
82
// template <typename T> using GradTypeT = Sacado::Fad::DFad<T>;
83
// typedef Sacado::Fad::SFad<Real,1> DirDerivType;
84
// typedef GradTypeT<Real> GradType;
85
// typedef GradTypeT<DirDerivType> HessVecType;
86
87
private
:
88
89
FunctionZakharov<Real>
zfunc_
;
90
FunctionZakharov<GradType>
zfuncGrad_
;
91
FunctionZakharov<HessVecType>
zfuncHessVec_
;
92
93
ROL::Ptr<const vector>
getVector
(
const
V
& x ) {
94
95
return
dynamic_cast<
const
SV
&
>
(x).
getVector
();
96
}
97
98
ROL::Ptr<vector>
getVector
(
V
& x ) {
99
100
return
dynamic_cast<
SV
&
>
(x).
getVector
();
101
}
102
103
public
:
104
105
Zakharov_Sacado_Objective
() {}
106
107
/* Evaluate the objective function at x */
108
Real
value
(
const
Vector<Real>
&x, Real &tol ) {
109
110
ROL::Ptr<const vector> xp =
getVector
(x);
111
return
zfunc_
.eval(*xp);
112
}
113
114
/* Evaluate the gradient at x */
115
void
gradient
(
Vector<Real>
&g,
const
Vector<Real>
&x, Real &tol ) {
116
117
ROL::Ptr<const vector> xp =
getVector
(x);
118
ROL::Ptr<vector> gp =
getVector
(g);
119
120
luint
n = xp->size();
121
122
std::vector<GradType> x_grad(n);
123
124
for
(
luint
i=0; i<n; ++i) {
125
x_grad[i] = (*xp)[i];
// Set values x(i).
126
x_grad[i].diff(i,n);
// Choose canonical directions.
127
}
128
129
GradType
J_grad =
zfuncGrad_
.eval(x_grad);
130
131
for
(
luint
i=0; i<n; ++i) {
132
(*gp)[i] = J_grad.dx(i);
133
}
134
135
}
136
137
/* Compute the action of the Hessian evaluated at x on a vector v */
138
void
hessVec
(
Vector<Real>
&hv,
const
Vector<Real>
&v,
const
Vector<Real>
&x, Real &tol ) {
139
140
141
ROL::Ptr<vector> hvp =
getVector
(hv);
142
ROL::Ptr<const vector> vp =
getVector
(v);
143
ROL::Ptr<const vector> xp =
getVector
(x);
144
145
luint
n = xp->size();
146
147
std::vector<HessVecType> x_hessvec(n);
148
149
for
(
luint
i=0; i<n; ++i) {
150
DirDerivType
tmp(1,(*xp)[i]);
// Set values x(i).
151
tmp.fastAccessDx(0)= (*vp)[i];
// Set direction values v(i).
152
x_hessvec[i] = tmp;
// Use tmp to define hessvec-able x.
153
x_hessvec[i].diff(i,n);
// Choose directions.
154
}
155
156
// Compute Hessian-vector product (and other currently irrelevant things).
157
HessVecType
J_hessvec =
zfuncHessVec_
.eval(x_hessvec);
158
159
for
(
luint
i=0; i<n; ++i) {
160
(*hvp)[i] = (J_hessvec.dx(i)).fastAccessDx(0);
161
// hessvec = get gradient get dir deriv
162
}
163
}
164
};
165
ROL_StdVector.hpp
FunctionZakharov
Definition
example_01b.hpp:26
FunctionZakharov::eval
ScalarT eval(const std::vector< ScalarT > &x)
A Sacado-accessible version of the Zakharov function to differentiate.
Definition
example_01b.hpp:47
ROL::Objective::Objective
Objective()
Definition
ROL_Objective.hpp:53
ROL::StdVector
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Definition
ROL_StdVector.hpp:27
ROL::Vector
Defines the linear algebra or vector space interface.
Definition
ROL_Vector.hpp:51
Zakharov_Sacado_Objective::getVector
ROL::Ptr< const vector > getVector(const V &x)
Definition
example_01b.hpp:93
Zakharov_Sacado_Objective::hessVec
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Definition
example_01b.hpp:138
Zakharov_Sacado_Objective::Zakharov_Sacado_Objective
Zakharov_Sacado_Objective()
Definition
example_01b.hpp:105
Zakharov_Sacado_Objective::zfuncHessVec_
FunctionZakharov< HessVecType > zfuncHessVec_
Definition
example_01b.hpp:91
Zakharov_Sacado_Objective::V
Vector< Real > V
Definition
example_01b.hpp:72
Zakharov_Sacado_Objective::GradType
Sacado::Fad::DFad< Real > GradType
Definition
example_01b.hpp:75
Zakharov_Sacado_Objective::gradient
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition
example_01b.hpp:115
Zakharov_Sacado_Objective::DirDerivType
Sacado::Fad::SFad< Real, 1 > DirDerivType
Definition
example_01b.hpp:76
Zakharov_Sacado_Objective::value
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition
example_01b.hpp:108
Zakharov_Sacado_Objective::SV
StdVector< Real > SV
Definition
example_01b.hpp:73
Zakharov_Sacado_Objective::vector
std::vector< Real > vector
Definition
example_01b.hpp:71
Zakharov_Sacado_Objective::zfuncGrad_
FunctionZakharov< GradType > zfuncGrad_
Definition
example_01b.hpp:90
Zakharov_Sacado_Objective::zfunc_
FunctionZakharov< Real > zfunc_
Definition
example_01b.hpp:89
Zakharov_Sacado_Objective::getVector
ROL::Ptr< vector > getVector(V &x)
Definition
example_01b.hpp:98
Zakharov_Sacado_Objective::HessVecType
Sacado::Fad::DFad< DirDerivType > HessVecType
Definition
example_01b.hpp:77
Zakharov_Sacado_Objective::luint
vector::size_type luint
Definition
example_01b.hpp:79
ROL
Definition
ROL_ElementwiseVector.hpp:27
Generated by
1.17.0