ROL
ROL_NewtonKrylovStep.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_NEWTONKRYLOVSTEP_H
45 #define ROL_NEWTONKRYLOVSTEP_H
46 
47 #include "ROL_Types.hpp"
48 #include "ROL_Step.hpp"
49 
50 #include "ROL_Secant.hpp"
51 #include "ROL_Krylov.hpp"
52 #include "ROL_LinearOperator.hpp"
53 
54 #include <sstream>
55 #include <iomanip>
56 
63 namespace ROL {
64 
65 template <class Real>
66 class NewtonKrylovStep : public Step<Real> {
67 private:
68 
69  Teuchos::RCP<Secant<Real> > secant_;
70  Teuchos::RCP<Krylov<Real> > krylov_;
71 
74 
75  Teuchos::RCP<Vector<Real> > gp_;
76 
79  int verbosity_;
80  const bool computeObj_;
81 
83 
84  class HessianNK : public LinearOperator<Real> {
85  private:
86  const Teuchos::RCP<Objective<Real> > obj_;
87  const Teuchos::RCP<Vector<Real> > x_;
88  public:
89  HessianNK(const Teuchos::RCP<Objective<Real> > &obj,
90  const Teuchos::RCP<Vector<Real> > &x) : obj_(obj), x_(x) {}
91  void apply(Vector<Real> &Hv, const Vector<Real> &v, Real &tol) const {
92  obj_->hessVec(Hv,v,*x_,tol);
93  }
94  };
95 
96  class PrecondNK : public LinearOperator<Real> {
97  private:
98  const Teuchos::RCP<Objective<Real> > obj_;
99  const Teuchos::RCP<Vector<Real> > x_;
100  public:
101  PrecondNK(const Teuchos::RCP<Objective<Real> > &obj,
102  const Teuchos::RCP<Vector<Real> > &x) : obj_(obj), x_(x) {}
103  void apply(Vector<Real> &Hv, const Vector<Real> &v, Real &tol) const {
104  Hv.set(v.dual());
105  }
106  void applyInverse(Vector<Real> &Hv, const Vector<Real> &v, Real &tol) const {
107  obj_->precond(Hv,v,*x_,tol);
108  }
109  };
110 
111 public:
112 
114  using Step<Real>::compute;
115  using Step<Real>::update;
116 
124  NewtonKrylovStep( Teuchos::ParameterList &parlist, const bool computeObj = true )
125  : Step<Real>(), secant_(Teuchos::null), krylov_(Teuchos::null),
126  gp_(Teuchos::null), iterKrylov_(0), flagKrylov_(0),
127  verbosity_(0), computeObj_(computeObj), useSecantPrecond_(false) {
128  // Parse ParameterList
129  Teuchos::ParameterList& Glist = parlist.sublist("General");
130  useSecantPrecond_ = Glist.sublist("Secant").get("Use as Preconditioner", false);
131  verbosity_ = Glist.get("Print Verbosity",0);
132  // Initialize Krylov object
133  ekv_ = StringToEKrylov(Glist.sublist("Krylov").get("Type","Conjugate Gradients"));
134  krylov_ = KrylovFactory<Real>(parlist);
135  // Initialize secant object
136  esec_ = StringToESecant(Glist.sublist("Secant").get("Type","Limited-Memory BFGS"));
137  if ( useSecantPrecond_ ) {
138  secant_ = SecantFactory<Real>(parlist);
139  }
140  }
141 
152  NewtonKrylovStep(Teuchos::ParameterList &parlist,
153  const Teuchos::RCP<Krylov<Real> > &krylov,
154  const Teuchos::RCP<Secant<Real> > &secant,
155  const bool computeObj = true)
156  : Step<Real>(), secant_(secant), krylov_(krylov),
158  gp_(Teuchos::null), iterKrylov_(0), flagKrylov_(0),
159  verbosity_(0), computeObj_(computeObj), useSecantPrecond_(false) {
160  // Parse ParameterList
161  Teuchos::ParameterList& Glist = parlist.sublist("General");
162  useSecantPrecond_ = Glist.sublist("Secant").get("Use as Preconditioner", false);
163  verbosity_ = Glist.get("Print Verbosity",0);
164  // Initialize secant object
165  if ( useSecantPrecond_ && secant_ == Teuchos::null ) {
166  esec_ = StringToESecant(Glist.sublist("Secant").get("Type","Limited-Memory BFGS"));
167  secant_ = SecantFactory<Real>(parlist);
168  }
169  // Initialize Krylov object
170  if ( krylov_ == Teuchos::null ) {
171  ekv_ = StringToEKrylov(Glist.sublist("Krylov").get("Type","Conjugate Gradients"));
172  krylov_ = KrylovFactory<Real>(parlist);
173  }
174  }
175 
176  void initialize( Vector<Real> &x, const Vector<Real> &s, const Vector<Real> &g,
178  AlgorithmState<Real> &algo_state ) {
179  Step<Real>::initialize(x,s,g,obj,bnd,algo_state);
180  if ( useSecantPrecond_ ) {
181  gp_ = g.clone();
182  }
183  }
184 
185  void compute( Vector<Real> &s, const Vector<Real> &x,
187  AlgorithmState<Real> &algo_state ) {
188  Real one(1);
189  Teuchos::RCP<StepState<Real> > step_state = Step<Real>::getState();
190 
191  // Build Hessian and Preconditioner object
192  Teuchos::RCP<Objective<Real> > obj_ptr = Teuchos::rcpFromRef(obj);
193  Teuchos::RCP<LinearOperator<Real> > hessian
194  = Teuchos::rcp(new HessianNK(obj_ptr,algo_state.iterateVec));
195  Teuchos::RCP<LinearOperator<Real> > precond;
196  if ( useSecantPrecond_ ) {
197  precond = secant_;
198  }
199  else {
200  precond = Teuchos::rcp(new PrecondNK(obj_ptr,algo_state.iterateVec));
201  }
202 
203  // Run Krylov method
204  flagKrylov_ = 0;
205  krylov_->run(s,*hessian,*(step_state->gradientVec),*precond,iterKrylov_,flagKrylov_);
206 
207  // Check Krylov flags
208  if ( flagKrylov_ == 2 && iterKrylov_ <= 1 ) {
209  s.set((step_state->gradientVec)->dual());
210  }
211  s.scale(-one);
212  }
213 
214  void update( Vector<Real> &x, const Vector<Real> &s,
216  AlgorithmState<Real> &algo_state ) {
217  Real tol = std::sqrt(ROL_EPSILON<Real>());
218  Teuchos::RCP<StepState<Real> > step_state = Step<Real>::getState();
219 
220  // Update iterate
221  algo_state.iter++;
222  x.plus(s);
223  (step_state->descentVec)->set(s);
224  algo_state.snorm = s.norm();
225 
226  // Compute new gradient
227  if ( useSecantPrecond_ ) {
228  gp_->set(*(step_state->gradientVec));
229  }
230  obj.update(x,true,algo_state.iter);
231  if ( computeObj_ ) {
232  algo_state.value = obj.value(x,tol);
233  algo_state.nfval++;
234  }
235  obj.gradient(*(step_state->gradientVec),x,tol);
236  algo_state.ngrad++;
237 
238  // Update Secant Information
239  if ( useSecantPrecond_ ) {
240  secant_->updateStorage(x,*(step_state->gradientVec),*gp_,s,algo_state.snorm,algo_state.iter+1);
241  }
242 
243  // Update algorithm state
244  (algo_state.iterateVec)->set(x);
245  algo_state.gnorm = step_state->gradientVec->norm();
246  }
247 
248  std::string printHeader( void ) const {
249  std::stringstream hist;
250 
251  if( verbosity_>0 ) {
252  hist << std::string(109,'-') << "\n";
254  hist << " status output definitions\n\n";
255  hist << " iter - Number of iterates (steps taken) \n";
256  hist << " value - Objective function value \n";
257  hist << " gnorm - Norm of the gradient\n";
258  hist << " snorm - Norm of the step (update to optimization vector)\n";
259  hist << " #fval - Cumulative number of times the objective function was evaluated\n";
260  hist << " #grad - Number of times the gradient was computed\n";
261  hist << " iterCG - Number of Krylov iterations used to compute search direction\n";
262  hist << " flagCG - Krylov solver flag" << "\n";
263  hist << std::string(109,'-') << "\n";
264  }
265 
266  hist << " ";
267  hist << std::setw(6) << std::left << "iter";
268  hist << std::setw(15) << std::left << "value";
269  hist << std::setw(15) << std::left << "gnorm";
270  hist << std::setw(15) << std::left << "snorm";
271  hist << std::setw(10) << std::left << "#fval";
272  hist << std::setw(10) << std::left << "#grad";
273  hist << std::setw(10) << std::left << "iterCG";
274  hist << std::setw(10) << std::left << "flagCG";
275  hist << "\n";
276  return hist.str();
277  }
278  std::string printName( void ) const {
279  std::stringstream hist;
280  hist << "\n" << EDescentToString(DESCENT_NEWTONKRYLOV);
281  hist << " using " << EKrylovToString(ekv_);
282  if ( useSecantPrecond_ ) {
283  hist << " with " << ESecantToString(esec_) << " preconditioning";
284  }
285  hist << "\n";
286  return hist.str();
287  }
288  std::string print( AlgorithmState<Real> &algo_state, bool print_header = false ) const {
289  std::stringstream hist;
290  hist << std::scientific << std::setprecision(6);
291  if ( algo_state.iter == 0 ) {
292  hist << printName();
293  }
294  if ( print_header ) {
295  hist << printHeader();
296  }
297  if ( algo_state.iter == 0 ) {
298  hist << " ";
299  hist << std::setw(6) << std::left << algo_state.iter;
300  hist << std::setw(15) << std::left << algo_state.value;
301  hist << std::setw(15) << std::left << algo_state.gnorm;
302  hist << "\n";
303  }
304  else {
305  hist << " ";
306  hist << std::setw(6) << std::left << algo_state.iter;
307  hist << std::setw(15) << std::left << algo_state.value;
308  hist << std::setw(15) << std::left << algo_state.gnorm;
309  hist << std::setw(15) << std::left << algo_state.snorm;
310  hist << std::setw(10) << std::left << algo_state.nfval;
311  hist << std::setw(10) << std::left << algo_state.ngrad;
312  hist << std::setw(10) << std::left << iterKrylov_;
313  hist << std::setw(10) << std::left << flagKrylov_;
314  hist << "\n";
315  }
316  return hist.str();
317  }
318 }; // class NewtonKrylovStep
319 
320 } // namespace ROL
321 
322 #endif
const Teuchos::RCP< Vector< Real > > x_
Provides the interface to evaluate objective functions.
virtual void scale(const Real alpha)=0
Compute where .
Teuchos::RCP< Vector< Real > > gp_
virtual void plus(const Vector &x)=0
Compute , where .
void apply(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply linear operator.
void update(Vector< Real > &x, const Vector< Real > &s, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Update step, if successful.
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
Provides the interface to compute optimization steps.
Definition: ROL_Step.hpp:69
int verbosity_
Verbosity level.
const Teuchos::RCP< Objective< Real > > obj_
Teuchos::RCP< StepState< Real > > getState(void)
Definition: ROL_Step.hpp:74
Contains definitions of custom data types in ROL.
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
Teuchos::RCP< Krylov< Real > > krylov_
Krylov solver object (used for inexact Newton)
ESecant StringToESecant(std::string s)
Definition: ROL_Types.hpp:477
std::string EDescentToString(EDescent tr)
Definition: ROL_Types.hpp:354
PrecondNK(const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< Vector< Real > > &x)
int flagKrylov_
Termination flag for Krylov method (used for inexact Newton)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
EKrylov
Enumeration of Krylov methods.
Definition: ROL_Types.hpp:493
EKrylov StringToEKrylov(std::string s)
Definition: ROL_Types.hpp:546
State for algorithm class. Will be used for restarts.
Definition: ROL_Types.hpp:91
void initialize(Vector< Real > &x, const Vector< Real > &s, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Initialize step with bound constraint.
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:213
std::string printHeader(void) const
Print iterate header.
HessianNK(const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< Vector< Real > > &x)
ESecant
Enumeration of secant update algorithms.
Definition: ROL_Types.hpp:420
void compute(Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step.
void applyInverse(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply inverse of linear operator.
void apply(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply linear operator.
Provides interface for and implements limited-memory secant operators.
Definition: ROL_Secant.hpp:70
Provides definitions for Krylov solvers.
Definition: ROL_Krylov.hpp:57
Provides the interface to apply a linear operator.
Provides the interface to apply upper and lower bound constraints.
std::string print(AlgorithmState< Real > &algo_state, bool print_header=false) const
Print iterate status.
NewtonKrylovStep(Teuchos::ParameterList &parlist, const bool computeObj=true)
Constructor.
std::string EKrylovToString(EKrylov tr)
Definition: ROL_Types.hpp:501
Provides the interface to compute optimization steps with projected inexact Newton&#39;s method using lin...
int iterKrylov_
Number of Krylov iterations (used for inexact Newton)
virtual void initialize(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &con, AlgorithmState< Real > &algo_state)
Initialize step with bound constraint.
Definition: ROL_Step.hpp:89
Teuchos::RCP< Vector< Real > > iterateVec
Definition: ROL_Types.hpp:105
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:196
virtual Real norm() const =0
Returns where .
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
Teuchos::RCP< Secant< Real > > secant_
Secant object (used for quasi-Newton)
std::string printName(void) const
Print step name.
const Teuchos::RCP< Vector< Real > > x_
bool useSecantPrecond_
Whether or not a secant approximation is used for preconditioning inexact Newton. ...
std::string ESecantToString(ESecant tr)
Definition: ROL_Types.hpp:429
const Teuchos::RCP< Objective< Real > > obj_
NewtonKrylovStep(Teuchos::ParameterList &parlist, const Teuchos::RCP< Krylov< Real > > &krylov, const Teuchos::RCP< Secant< Real > > &secant, const bool computeObj=true)
Constructor.