ROL
example_01c.hpp
Go to the documentation of this file.
1 #include "Sacado.hpp"
2 #include "ROL_StdVector.hpp"
3 #include "ROL_Objective.hpp"
4 
5 using namespace ROL;
6 
7 // Nested class variation on example_01
8 
9 template<class Real>
10 class Zakharov : public Objective<Real> {
11 
12  public:
13  Real value(const Vector<Real> &x, Real &tol) {
14 
15  Teuchos::RCP<const std::vector<Real> > xp =
16  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
17 
18  int n = xp->size();
19 
20  Real xdotx = 0;
21 
22  Real kdotx = 0;
23  Real J = 0;
24 
25  // Compute dot products
26  for(int i=0; i<n; ++i) {
27  xdotx += pow((*xp)[i],2); // (k,x)
28  kdotx += double(i+1)*(*xp)[i]; // (x,x)
29  }
30 
31  // Sum terms in objective function
32  J = xdotx + pow(kdotx,2)/4.0 + pow(kdotx,4)/16.0;
33 
34  return J;
35  }
36 };
37 
38 
39 template<class Real>
40 class GradientEnabler : public Objective<Real> {
41 
42  typedef Sacado::Fad::DFad<Real> FadType;
43 
44  Teuchos::RCP<Objective<FadType> > obj_;
45 
46  public:
47  GradientEnabler(Teuchos::RCP<Objective<FadType> > obj) : obj_(obj) {}
48 
49  Real value(const Vector<Real> &x, Real &tol) {
50 
51  Teuchos::RCP<const std::vector<Real> > xp =
52  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
53 
54  int n = xp->size();
55 
56  Teuchos::RCP<std::vector<FadType> > x_fad_rcp = Teuchos::rcp(new std::vector<FadType>);
57  x_fad_rcp->reserve(n);
58  for(int i=0; i<n; ++i) {
59  x_fad_rcp->push_back((*xp)[i]);
60  }
61  StdVector<FadType> x_fad(x_fad_rcp);
62 
63  FadType tol_fad = tol;
64  FadType J_fad = obj_->value(x_fad,tol_fad);
65 
66  return J_fad.val();
67  }
68 
69  void gradient(Vector<Real> &g, const Vector<Real> &x, Real &tol) {
70 
71  Teuchos::RCP<const std::vector<Real> > xp =
72  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
73 
74  Teuchos::RCP<std::vector<Real> > gp =
75  Teuchos::rcp_const_cast<std::vector<Real> > ((Teuchos::dyn_cast<StdVector<Real> > (g)).getVector());
76 
77  int n = xp->size();
78 
79  Teuchos::RCP<std::vector<FadType> > x_fad_rcp = Teuchos::rcp( new std::vector<FadType> );
80  x_fad_rcp->reserve(n);
81 
82  for(int i=0; i<n; ++i) {
83  x_fad_rcp->push_back(FadType(n,i,(*xp)[i]));
84  }
85 
86  StdVector<FadType> x_fad(x_fad_rcp);
87 
88  FadType tol_fad = tol;
89  FadType J_fad = obj_->value(x_fad,tol_fad);
90 
91  for(int i=0; i<n; ++i) {
92  (*gp)[i] = J_fad.dx(i);
93  }
94  }
95 };
96 
97 
98 
99 
100 template<class Real>
101 class HessianEnabler : public Objective<Real> {
102 
103  typedef Sacado::Fad::SFad<Real,1> FadType;
104 
105  Teuchos::RCP<Objective<FadType> > obj_;
106 
107  public:
108  HessianEnabler(Teuchos::RCP<Objective<FadType> > obj) : obj_(obj) {}
109 
110  Real value(const Vector<Real> &x, Real &tol) {
111 
112  Teuchos::RCP<const std::vector<Real> > xp =
113  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
114 
115  int n = xp->size();
116 
117  Teuchos::RCP<std::vector<FadType> > x_fad_rcp = Teuchos::rcp(new std::vector<FadType>);
118  x_fad_rcp->reserve(n);
119  for(int i=0; i<n; ++i) {
120  x_fad_rcp->push_back((*xp)[i]);
121  }
122  StdVector<FadType> x_fad(x_fad_rcp);
123 
124  FadType tol_fad = tol;
125  FadType J_fad = obj_->value(x_fad,tol_fad);
126 
127  return J_fad.val();
128  }
129 
130  void gradient(Vector<Real> &g, const Vector<Real> &x, Real &tol) {
131 
132  Teuchos::RCP<const std::vector<Real> > xp =
133  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
134 
135  Teuchos::RCP<std::vector<Real> > gp =
136  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(g)).getVector());
137 
138  int n = xp->size();
139 
140  Teuchos::RCP<std::vector<FadType> > x_fad_rcp = Teuchos::rcp(new std::vector<FadType>);
141  Teuchos::RCP<std::vector<FadType> > g_fad_rcp = Teuchos::rcp(new std::vector<FadType>);
142 
143  x_fad_rcp->reserve(n);
144  g_fad_rcp->reserve(n);
145 
146  for(int i=0; i<n; ++i) {
147  x_fad_rcp->push_back((*xp)[i]);
148  g_fad_rcp->push_back((*xp)[i]);
149  }
150  StdVector<FadType> x_fad(x_fad_rcp);
151  StdVector<FadType> g_fad(g_fad_rcp);
152  FadType tol_fad = tol;
153 
154  obj_->gradient(g_fad,x_fad,tol_fad);
155 
156  for(int i=0; i<n; ++i) {
157  (*gp)[i] = (*g_fad_rcp)[i].val();
158  }
159  }
160 
161 
162  void hessVec(Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol) {
163  Teuchos::RCP<const std::vector<Real> > xp =
164  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
165  Teuchos::RCP<const std::vector<Real> > vp =
166  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(v))).getVector();
167  Teuchos::RCP<std::vector<Real> > hvp =
168  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(hv)).getVector());
169 
170  int n = xp->size();
171 
172  // Create a vector of independent variables
173  Teuchos::RCP<std::vector<FadType> > x_fad_rcp = Teuchos::rcp( new std::vector<FadType> );
174  x_fad_rcp->reserve(n);
175 
176  // Allocate for gradient
177  Teuchos::RCP<std::vector<FadType> > g_fad_rcp = Teuchos::rcp( new std::vector<FadType> );
178  g_fad_rcp->reserve(n);
179 
180  for(int i=0; i<n; ++i) {
181  x_fad_rcp->push_back(FadType(1,(*xp)[i]));
182  g_fad_rcp->push_back(0);
183  (*x_fad_rcp)[i].fastAccessDx(0) = (*vp)[i];
184  }
185 
186  StdVector<FadType> x_fad(x_fad_rcp);
187  StdVector<FadType> g_fad(g_fad_rcp);
188  FadType tol_fad = tol;
189  obj_->gradient(g_fad,x_fad,tol_fad);
190 
191  for(int i=0; i<n; ++i) {
192  (*hvp)[i] = (*g_fad_rcp)[i].dx(0);
193  }
194  }
195 
196 };
197 
198 
199 
200 
Provides the interface to evaluate objective functions.
GradientEnabler(Teuchos::RCP< Objective< FadType > > obj)
Definition: example_01c.hpp:47
Teuchos::RCP< Objective< FadType > > obj_
Sacado::Fad::DFad< Real > FadType
Definition: example_01c.hpp:42
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: example_01c.hpp:69
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
HessianEnabler(Teuchos::RCP< Objective< FadType > > obj)
Provides the std::vector implementation of the ROL::Vector interface.
Sacado::Fad::SFad< Real, 1 > FadType
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: example_01c.hpp:13
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: example_01c.hpp:49
Teuchos::RCP< Objective< FadType > > obj_
Definition: example_01c.hpp:44
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Real value(const Vector< Real > &x, Real &tol)
Compute value.