ROL
ROL_Objective_SimOpt.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_OBJECTIVE_SIMOPT_H
45 #define ROL_OBJECTIVE_SIMOPT_H
46 
47 #include "ROL_Objective.hpp"
48 #include "ROL_Vector_SimOpt.hpp"
49 
56 namespace ROL {
57 
58 template <class Real>
59 class Objective_SimOpt : public virtual Objective<Real> {
60 public:
61 
68  virtual void update( const Vector<Real> &u, const Vector<Real> &z, bool flag = true, int iter = -1 ) {}
69 
70  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
71  const ROL::Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const ROL::Vector_SimOpt<Real> >(
72  Teuchos::dyn_cast<const ROL::Vector<Real> >(x));
73  this->update(*(xs.get_1()),*(xs.get_2()),flag,iter);
74  }
75 
76 
79  virtual Real value( const Vector<Real> &u, const Vector<Real> &z, Real &tol ) = 0;
80 
81  Real value( const Vector<Real> &x, Real &tol ) {
82  const ROL::Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const ROL::Vector_SimOpt<Real> >(
83  Teuchos::dyn_cast<const ROL::Vector<Real> >(x));
84  return this->value(*(xs.get_1()),*(xs.get_2()),tol);
85  }
86 
87 
90  virtual void gradient_1( Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
91  Real ftol = std::sqrt(ROL_EPSILON<Real>());
92  Real h = 0.0;
93  this->update(u,z);
94  Real v = this->value(u,z,ftol);
95  Real deriv = 0.0;
96  Teuchos::RCP<Vector<Real> > unew = u.clone();
97  g.zero();
98  for (int i = 0; i < g.dimension(); i++) {
99  h = u.dot(*u.basis(i))*tol;
100  unew->set(u);
101  unew->axpy(h,*(u.basis(i)));
102  this->update(*unew,z);
103  deriv = (this->value(*unew,z,ftol) - v)/h;
104  g.axpy(deriv,*(g.basis(i)));
105  }
106  this->update(u,z);
107  }
110  virtual void gradient_2( Vector<Real> &g, const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
111  Real ftol = std::sqrt(ROL_EPSILON<Real>());
112  Real h = 0.0;
113  this->update(u,z);
114  Real v = this->value(u,z,ftol);
115  Real deriv = 0.0;
116  Teuchos::RCP<Vector<Real> > znew = z.clone();
117  g.zero();
118  for (int i = 0; i < g.dimension(); i++) {
119  h = z.dot(*z.basis(i))*tol;
120  znew->set(z);
121  znew->axpy(h,*(z.basis(i)));
122  this->update(u,*znew);
123  deriv = (this->value(u,*znew,ftol) - v)/h;
124  g.axpy(deriv,*(g.basis(i)));
125  }
126  this->update(u,z);
127  }
128 
129  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
130  ROL::Vector_SimOpt<Real> &gs = Teuchos::dyn_cast<ROL::Vector_SimOpt<Real> >(
131  Teuchos::dyn_cast<ROL::Vector<Real> >(g));
132  const ROL::Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const ROL::Vector_SimOpt<Real> >(
133  Teuchos::dyn_cast<const ROL::Vector<Real> >(x));
134  Teuchos::RCP<Vector<Real> > g1 = gs.get_1()->clone();
135  Teuchos::RCP<Vector<Real> > g2 = gs.get_2()->clone();
136  this->gradient_1(*g1,*(xs.get_1()),*(xs.get_2()),tol);
137  this->gradient_2(*g2,*(xs.get_1()),*(xs.get_2()),tol);
138  gs.set_1(*g1);
139  gs.set_2(*g2);
140  }
141 
142 
145  virtual void hessVec_11( Vector<Real> &hv, const Vector<Real> &v,
146  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
147  Real gtol = std::sqrt(ROL_EPSILON<Real>());
148  // Compute step length
149  Real h = tol;
150  if (v.norm() > std::sqrt(ROL_EPSILON<Real>())) {
151  h = std::max(1.0,u.norm()/v.norm())*tol;
152  }
153  // Evaluate gradient of first component at (u+hv,z)
154  Teuchos::RCP<Vector<Real> > unew = u.clone();
155  unew->set(u);
156  unew->axpy(h,v);
157  this->update(*unew,z);
158  hv.zero();
159  this->gradient_1(hv,*unew,z,gtol);
160  // Evaluate gradient of first component at (u,z)
161  Teuchos::RCP<Vector<Real> > g = hv.clone();
162  this->update(u,z);
163  this->gradient_1(*g,u,z,gtol);
164  // Compute Newton quotient
165  hv.axpy(-1.0,*g);
166  hv.scale(1.0/h);
167  }
168 
169  virtual void hessVec_12( Vector<Real> &hv, const Vector<Real> &v,
170  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
171  Real gtol = std::sqrt(ROL_EPSILON<Real>());
172  // Compute step length
173  Real h = tol;
174  if (v.norm() > std::sqrt(ROL_EPSILON<Real>())) {
175  h = std::max(1.0,u.norm()/v.norm())*tol;
176  }
177  // Evaluate gradient of first component at (u,z+hv)
178  Teuchos::RCP<Vector<Real> > znew = z.clone();
179  znew->set(z);
180  znew->axpy(h,v);
181  this->update(u,*znew);
182  hv.zero();
183  this->gradient_1(hv,u,*znew,gtol);
184  // Evaluate gradient of first component at (u,z)
185  Teuchos::RCP<Vector<Real> > g = hv.clone();
186  this->update(u,z);
187  this->gradient_1(*g,u,z,gtol);
188  // Compute Newton quotient
189  hv.axpy(-1.0,*g);
190  hv.scale(1.0/h);
191  }
192 
193  virtual void hessVec_21( Vector<Real> &hv, const Vector<Real> &v,
194  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
195  Real gtol = std::sqrt(ROL_EPSILON<Real>());
196  // Compute step length
197  Real h = tol;
198  if (v.norm() > std::sqrt(ROL_EPSILON<Real>())) {
199  h = std::max(1.0,u.norm()/v.norm())*tol;
200  }
201  // Evaluate gradient of first component at (u+hv,z)
202  Teuchos::RCP<Vector<Real> > unew = u.clone();
203  unew->set(u);
204  unew->axpy(h,v);
205  this->update(*unew,z);
206  hv.zero();
207  this->gradient_2(hv,*unew,z,gtol);
208  // Evaluate gradient of first component at (u,z)
209  Teuchos::RCP<Vector<Real> > g = hv.clone();
210  this->update(u,z);
211  this->gradient_2(*g,u,z,gtol);
212  // Compute Newton quotient
213  hv.axpy(-1.0,*g);
214  hv.scale(1.0/h);
215  }
216 
217  virtual void hessVec_22( Vector<Real> &hv, const Vector<Real> &v,
218  const Vector<Real> &u, const Vector<Real> &z, Real &tol ) {
219  Real gtol = std::sqrt(ROL_EPSILON<Real>());
220  // Compute step length
221  Real h = tol;
222  if (v.norm() > std::sqrt(ROL_EPSILON<Real>())) {
223  h = std::max(1.0,u.norm()/v.norm())*tol;
224  }
225  // Evaluate gradient of first component at (u,z+hv)
226  Teuchos::RCP<Vector<Real> > znew = z.clone();
227  znew->set(z);
228  znew->axpy(h,v);
229  this->update(u,*znew);
230  hv.zero();
231  this->gradient_2(hv,u,*znew,gtol);
232  // Evaluate gradient of first component at (u,z)
233  Teuchos::RCP<Vector<Real> > g = hv.clone();
234  this->update(u,z);
235  this->gradient_2(*g,u,z,gtol);
236  // Compute Newton quotient
237  hv.axpy(-1.0,*g);
238  hv.scale(1.0/h);
239  }
240 
241  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
242  ROL::Vector_SimOpt<Real> &hvs = Teuchos::dyn_cast<ROL::Vector_SimOpt<Real> >(
243  Teuchos::dyn_cast<ROL::Vector<Real> >(hv));
244  const ROL::Vector_SimOpt<Real> &vs = Teuchos::dyn_cast<const ROL::Vector_SimOpt<Real> >(
245  Teuchos::dyn_cast<const ROL::Vector<Real> >(v));
246  const ROL::Vector_SimOpt<Real> &xs = Teuchos::dyn_cast<const ROL::Vector_SimOpt<Real> >(
247  Teuchos::dyn_cast<const ROL::Vector<Real> >(x));
248  Teuchos::RCP<Vector<Real> > h11 = (hvs.get_1())->clone();
249  this->hessVec_11(*h11,*(vs.get_1()),*(xs.get_1()),*(xs.get_2()),tol);
250  Teuchos::RCP<Vector<Real> > h12 = (hvs.get_1())->clone();
251  this->hessVec_12(*h12,*(vs.get_2()),*(xs.get_1()),*(xs.get_2()),tol);
252  Teuchos::RCP<Vector<Real> > h21 = (hvs.get_2())->clone();
253  this->hessVec_21(*h21,*(vs.get_1()),*(xs.get_1()),*(xs.get_2()),tol);
254  Teuchos::RCP<Vector<Real> > h22 = (hvs.get_2())->clone();
255  this->hessVec_22(*h22,*(vs.get_2()),*(xs.get_1()),*(xs.get_2()),tol);
256  h11->plus(*h12);
257  hvs.set_1(*h11);
258  h22->plus(*h21);
259  hvs.set_2(*h22);
260  }
261 
262 }; // class Objective_SimOpt
263 
264 } // namespace ROL
265 
266 #endif
Provides the interface to evaluate objective functions.
Provides the interface to evaluate simulation-based objective functions.
virtual void scale(const Real alpha)=0
Compute where .
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:143
Defines the linear algebra or vector space interface for simulation-based optimization.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
virtual void hessVec_12(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Teuchos::RCP< const Vector< Real > > get_2() const
void set_1(const Vector< Real > &vec)
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void gradient_2(Vector< Real > &g, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Compute gradient with respect to second component.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
virtual Real dot(const Vector &x) const =0
Compute where .
virtual Teuchos::RCP< Vector > basis(const int i) const
Return i-th basis vector.
Definition: ROL_Vector.hpp:172
virtual void hessVec_21(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
virtual Real value(const Vector< Real > &u, const Vector< Real > &z, Real &tol)=0
Compute value.
virtual int dimension() const
Return dimension of the vector space.
Definition: ROL_Vector.hpp:183
Teuchos::RCP< const Vector< Real > > get_1() const
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
virtual void gradient_1(Vector< Real > &g, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Compute gradient with respect to first component.
virtual void hessVec_22(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
virtual void update(const Vector< Real > &u, const Vector< Real > &z, bool flag=true, int iter=-1)
Update objective function. u is an iterate, z is an iterate, flag = true if the iterate has changed...
virtual Real norm() const =0
Returns where .
Real value(const Vector< Real > &x, Real &tol)
Compute value.
virtual void hessVec_11(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol)
Apply Hessian approximation to vector.
void set_2(const Vector< Real > &vec)