ROL
ROL_TruncatedCG.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_TRUNCATEDCG_H
45 #define ROL_TRUNCATEDCG_H
46 
51 #include "ROL_TrustRegion.hpp"
52 #include "ROL_Types.hpp"
53 #include "ROL_HelperFunctions.hpp"
54 
55 namespace ROL {
56 
57 template<class Real>
58 class TruncatedCG : public TrustRegion<Real> {
59 private:
60 
61  Teuchos::RCP<Vector<Real> > s_;
62  Teuchos::RCP<Vector<Real> > g_;
63  Teuchos::RCP<Vector<Real> > v_;
64  Teuchos::RCP<Vector<Real> > p_;
65  Teuchos::RCP<Vector<Real> > Hp_;
66 
67  int maxit_;
68  Real tol1_;
69  Real tol2_;
70 
71  Real pRed_;
72 
73 public:
74 
75  // Constructor
76  TruncatedCG( Teuchos::ParameterList &parlist ) : TrustRegion<Real>(parlist), pRed_(0.0) {
77  // Unravel Parameter List
78  maxit_ = parlist.get("Maximum Number of Krylov Iterations", 20);
79  tol1_ = parlist.get("Absolute Krylov Tolerance", 1.e-4);
80  tol2_ = parlist.get("Relative Krylov Tolerance", 1.e-2);
81  }
82 
83  void initialize( const Vector<Real> &x, const Vector<Real> &s, const Vector<Real> &g) {
85  s_ = s.clone();
86  g_ = g.clone();
87  v_ = s.clone();
88  p_ = s.clone();
89  Hp_ = g.clone();
90  }
91 
92  void run( Vector<Real> &s, Real &snorm, Real &del, int &iflag, int &iter, const Vector<Real> &x,
93  const Vector<Real> &grad, const Real &gnorm, ProjectedObjective<Real> &pObj ) {
94  Real tol = std::sqrt(ROL_EPSILON);
95  const Real gtol = std::min(tol1_,tol2_*gnorm);
96 
97  // Old and New Step Vectors
98  s.zero();
99  snorm = 0.0;
100  Real snorm2 = 0.0;
101  s_->zero();
102  Real s1norm2 = 0.0;
103 
104  // Gradient Vector
105  g_->set(grad);
106  Real normg = gnorm;
107  if ( pObj.isConActivated() ) {
108  pObj.pruneActive(*g_,grad,x);
109  normg = g_->norm();
110  }
111 
112  // Preconditioned Gradient Vector
113  //pObj.precond(*v,*g,x,tol);
114  pObj.reducedPrecond(*v_,*g_,x,grad,x,tol);
115 
116  // Basis Vector
117  p_->set(*v_);
118  p_->scale(-1.0);
119  Real pnorm2 = v_->dot(g_->dual());
120 
121  iter = 0;
122  iflag = 0;
123  Real kappa = 0.0;
124  Real beta = 0.0;
125  Real sigma = 0.0;
126  Real alpha = 0.0;
127  Real tmp = 0.0;
128  Real gv = v_->dot(g_->dual());
129  Real sMp = 0.0;
130  pRed_ = 0.0;
131 
132  for (iter = 0; iter < maxit_; iter++) {
133  //pObj.hessVec(*Hp,*p,x,tol);
134  pObj.reducedHessVec(*Hp_,*p_,x,grad,x,tol);
135 
136  kappa = p_->dot(Hp_->dual());
137  if (kappa <= 0.0) {
138  sigma = (-sMp+sqrt(sMp*sMp+pnorm2*(del*del-snorm2)))/pnorm2;
139  s.axpy(sigma,*p_);
140  iflag = 2;
141  break;
142  }
143 
144  alpha = gv/kappa;
145  s_->set(s);
146  s_->axpy(alpha,*p_);
147  s1norm2 = snorm2 + 2.0*alpha*sMp + alpha*alpha*pnorm2;
148 
149  if (s1norm2 >= del*del) {
150  sigma = (-sMp+sqrt(sMp*sMp+pnorm2*(del*del-snorm2)))/pnorm2;
151  s.axpy(sigma,*p_);
152  iflag = 3;
153  break;
154  }
155 
156  pRed_ += 0.5*alpha*gv;
157 
158  s.set(*s_);
159  snorm2 = s1norm2;
160 
161  g_->axpy(alpha,*Hp_);
162  normg = g_->norm();
163  if (normg < gtol) {
164  break;
165  }
166 
167  //pObj.precond(*v,*g,x,tol);
168  pObj.reducedPrecond(*v_,*g_,x,grad,x,tol);
169  tmp = gv;
170  gv = v_->dot(g_->dual());
171  beta = gv/tmp;
172 
173  p_->scale(beta);
174  p_->axpy(-1.0,*v_);
175  sMp = beta*(sMp+alpha*pnorm2);
176  pnorm2 = gv + beta*beta*pnorm2;
177  }
178  if (iflag > 0) {
179  pRed_ += sigma*(gv-0.5*sigma*kappa);
180  }
181 
182  if (iter == maxit_) {
183  iflag = 1;
184  }
185  if (iflag != 1) {
186  iter++;
187  }
188 
189  snorm = s.norm();
191  }
192 
193 #if 0
194  void truncatedCG_proj( Vector<Real> &s, Real &snorm, Real &del, int &iflag, int &iter, const Vector<Real> &x,
195  const Vector<Real> &grad, const Real &gnorm, ProjectedObjective<Real> &pObj ) {
196  Real tol = std::sqrt(ROL_EPSILON);
197 
198  const Real gtol = std::min(tol1_,tol2_*gnorm);
199 
200  // Compute Cauchy Point
201  Real scnorm = 0.0;
202  Teuchos::RCP<Vector<Real> > sc = x.clone();
203  cauchypoint(*sc,scnorm,del,iflag,iter,x,grad,gnorm,pObj);
204  Teuchos::RCP<Vector<Real> > xc = x.clone();
205  xc->set(x);
206  xc->plus(*sc);
207 
208  // Old and New Step Vectors
209  s.set(*sc);
210  snorm = s.norm();
211  Real snorm2 = snorm*snorm;
212  Teuchos::RCP<Vector<Real> > s1 = x.clone();
213  s1->zero();
214  Real s1norm2 = 0.0;
215 
216  // Gradient Vector
217  Teuchos::RCP<Vector<Real> > g = x.clone();
218  g->set(grad);
219  Teuchos::RCP<Vector<Real> > Hs = x.clone();
220  pObj.reducedHessVec(*Hs,s,*xc,x,tol);
221  g->plus(*Hs);
222  Real normg = g->norm();
223 
224  // Preconditioned Gradient Vector
225  Teuchos::RCP<Vector<Real> > v = x.clone();
226  pObj.reducedPrecond(*v,*g,*xc,x,tol);
227 
228  // Basis Vector
229  Teuchos::RCP<Vector<Real> > p = x.clone();
230  p->set(*v);
231  p->scale(-1.0);
232  Real pnorm2 = v->dot(*g);
233 
234  // Hessian Times Basis Vector
235  Teuchos::RCP<Vector<Real> > Hp = x.clone();
236 
237  iter = 0;
238  iflag = 0;
239  Real kappa = 0.0;
240  Real beta = 0.0;
241  Real sigma = 0.0;
242  Real alpha = 0.0;
243  Real tmp = 0.0;
244  Real gv = v->dot(*g);
245  Real sMp = 0.0;
246  pRed_ = 0.0;
247 
248  for (iter = 0; iter < maxit_; iter++) {
249  pObj.reducedHessVec(*Hp,*p,*xc,x,tol);
250 
251  kappa = p->dot(*Hp);
252  if (kappa <= 0) {
253  sigma = (-sMp+sqrt(sMp*sMp+pnorm2*(del*del-snorm2)))/pnorm2;
254  s.axpy(sigma,*p);
255  iflag = 2;
256  break;
257  }
258 
259  alpha = gv/kappa;
260  s1->set(s);
261  s1->axpy(alpha,*p);
262  s1norm2 = snorm2 + 2.0*alpha*sMp + alpha*alpha*pnorm2;
263 
264  if (s1norm2 >= del*del) {
265  sigma = (-sMp+sqrt(sMp*sMp+pnorm2*(del*del-snorm2)))/pnorm2;
266  s.axpy(sigma,*p);
267  iflag = 3;
268  break;
269  }
270 
271  pRed_ += 0.5*alpha*gv;
272 
273  s.set(*s1);
274  snorm2 = s1norm2;
275 
276  g->axpy(alpha,*Hp);
277  normg = g->norm();
278  if (normg < gtol) {
279  break;
280  }
281 
282  pObj.reducedPrecond(*v,*g,*xc,x,tol);
283  tmp = gv;
284  gv = v->dot(*g);
285  beta = gv/tmp;
286 
287  p->scale(beta);
288  p->axpy(-1.0,*v);
289  sMp = beta*(sMp+alpha*pnorm2);
290  pnorm2 = gv + beta*beta*pnorm2;
291  }
292  if (iflag > 0) {
293  pRed_ += sigma*(gv-0.5*sigma*kappa);
294  }
295 
296  if (iter == maxit_) {
297  iflag = 1;
298  }
299  if (iflag != 1) {
300  iter++;
301  }
302 
303  snorm = s.norm();
304  }
305 #endif
306 };
307 
308 }
309 
310 #endif
void initialize(const Vector< Real > &x, const Vector< Real > &s, const Vector< Real > &g)
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:141
virtual void initialize(const Vector< Real > &x, const Vector< Real > &s, const Vector< Real > &g)
void reducedHessVec(Vector< Real > &Hv, const Vector< Real > &v, const Vector< Real > &p, const Vector< Real > &d, const Vector< Real > &x, Real &tol)
Apply the reduced Hessian to a vector, v. The reduced Hessian first removes elements of v correspondi...
Contains definitions of custom data types in ROL.
Teuchos::RCP< Vector< Real > > p_
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
Provides interface for and implements trust-region subproblem solvers.
void pruneActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x)
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:155
Contains definitions for helper functions in ROL.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
Teuchos::RCP< Vector< Real > > v_
void setPredictedReduction(const Real pRed)
Teuchos::RCP< Vector< Real > > Hp_
Teuchos::RCP< Vector< Real > > g_
Provides interface for truncated CG trust-region subproblem solver.
void run(Vector< Real > &s, Real &snorm, Real &del, int &iflag, int &iter, const Vector< Real > &x, const Vector< Real > &grad, const Real &gnorm, ProjectedObjective< Real > &pObj)
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:194
virtual Real norm() const =0
Returns where .
void reducedPrecond(Vector< Real > &Mv, const Vector< Real > &v, const Vector< Real > &p, const Vector< Real > &d, const Vector< Real > &x, Real &tol)
Apply the reduced preconditioner to a vector, v. The reduced preconditioner first removes elements of...
Teuchos::RCP< Vector< Real > > s_
TruncatedCG(Teuchos::ParameterList &parlist)
static const double ROL_EPSILON
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:115