ROL
ROL_SecantStep.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 
10 #ifndef ROL_SECANTSTEP_H
11 #define ROL_SECANTSTEP_H
12 
13 #include "ROL_Types.hpp"
14 #include "ROL_Step.hpp"
15 #include "ROL_Secant.hpp"
16 
23 namespace ROL {
24 
25 template <class Real>
26 class SecantStep : public Step<Real> {
27 private:
28 
29  ROL::Ptr<Secant<Real> > secant_;
31  ROL::Ptr<Vector<Real> > gp_;
32  int verbosity_;
34 
35  std::string secantName_;
36 
37 public:
38 
40  using Step<Real>::compute;
41  using Step<Real>::update;
42 
52  SecantStep( ROL::ParameterList &parlist,
53  const ROL::Ptr<Secant<Real> > &secant = ROL::nullPtr,
54  const bool computeObj = true )
55  : Step<Real>(), secant_(secant), esec_(SECANT_USERDEFINED),
56  gp_(ROL::nullPtr), verbosity_(0), computeObj_(computeObj) {
57  // Parse ParameterList
58  verbosity_ = parlist.sublist("General").get("Print Verbosity",0);
59  // Initialize secant object
60  if ( secant == ROL::nullPtr ) {
61  secantName_ = parlist.sublist("General").sublist("Secant").get("Type","Limited-Memory BFGS");
63  secant_ = SecantFactory<Real>(parlist);
64  }
65  else {
66  secantName_ = parlist.sublist("General").sublist("Secant").get("User Defined Secant Name",
67  "Unspecified User Defined Secant Method");
68  }
69  }
70 
71  void initialize( Vector<Real> &x, const Vector<Real> &s, const Vector<Real> &g,
73  AlgorithmState<Real> &algo_state ) {
74  Step<Real>::initialize(x,s,g,obj,con,algo_state);
75  gp_ = g.clone();
76  }
77 
78  void compute( Vector<Real> &s, const Vector<Real> &x,
80  AlgorithmState<Real> &algo_state ) {
81  Real one(1);
82  ROL::Ptr<StepState<Real> > step_state = Step<Real>::getState();
83 
84  // Compute search direction
85  secant_->applyH(s,*(step_state->gradientVec));
86  s.scale(-one);
87  }
88 
90  AlgorithmState<Real> &algo_state ) {
91  Real tol = std::sqrt(ROL_EPSILON<Real>());
92  ROL::Ptr<StepState<Real> > step_state = Step<Real>::getState();
93 
94  // Update iterate
95  algo_state.iter++;
96  x.plus(s);
97  (step_state->descentVec)->set(s);
98  algo_state.snorm = s.norm();
99 
100  // Compute new gradient
101  gp_->set(*(step_state->gradientVec));
102  obj.update(x,true,algo_state.iter);
103  if ( computeObj_ ) {
104  algo_state.value = obj.value(x,tol);
105  algo_state.nfval++;
106  }
107  obj.gradient(*(step_state->gradientVec),x,tol);
108  algo_state.ngrad++;
109 
110  // Update Secant Information
111  secant_->updateStorage(x,*(step_state->gradientVec),*gp_,s,algo_state.snorm,algo_state.iter+1);
112 
113  // Update algorithm state
114  (algo_state.iterateVec)->set(x);
115  algo_state.gnorm = (step_state->gradientVec)->norm();
116  }
117 
118  std::string printHeader( void ) const {
119  std::stringstream hist;
120 
121  if( verbosity_>0 ) {
122  hist << std::string(109,'-') << "\n";
124  hist << " status output definitions\n\n";
125  hist << " iter - Number of iterates (steps taken) \n";
126  hist << " value - Objective function value \n";
127  hist << " gnorm - Norm of the gradient\n";
128  hist << " snorm - Norm of the step (update to optimization vector)\n";
129  hist << " #fval - Cumulative number of times the objective function was evaluated\n";
130  hist << " #grad - Number of times the gradient was computed\n";
131  hist << std::string(109,'-') << "\n";
132  }
133 
134  hist << " ";
135  hist << std::setw(6) << std::left << "iter";
136  hist << std::setw(15) << std::left << "value";
137  hist << std::setw(15) << std::left << "gnorm";
138  hist << std::setw(15) << std::left << "snorm";
139  hist << std::setw(10) << std::left << "#fval";
140  hist << std::setw(10) << std::left << "#grad";
141  hist << "\n";
142  return hist.str();
143  }
144  std::string printName( void ) const {
145  std::stringstream hist;
146  hist << "\n" << EDescentToString(DESCENT_SECANT);
147  hist << " with " << secantName_ << "\n";
148  return hist.str();
149  }
150  std::string print( AlgorithmState<Real> &algo_state, bool print_header = false ) const {
151  std::stringstream hist;
152  hist << std::scientific << std::setprecision(6);
153  if ( algo_state.iter == 0 ) {
154  hist << printName();
155  }
156  if ( print_header ) {
157  hist << printHeader();
158  }
159  if ( algo_state.iter == 0 ) {
160  hist << " ";
161  hist << std::setw(6) << std::left << algo_state.iter;
162  hist << std::setw(15) << std::left << algo_state.value;
163  hist << std::setw(15) << std::left << algo_state.gnorm;
164  hist << "\n";
165  }
166  else {
167  hist << " ";
168  hist << std::setw(6) << std::left << algo_state.iter;
169  hist << std::setw(15) << std::left << algo_state.value;
170  hist << std::setw(15) << std::left << algo_state.gnorm;
171  hist << std::setw(15) << std::left << algo_state.snorm;
172  hist << std::setw(10) << std::left << algo_state.nfval;
173  hist << std::setw(10) << std::left << algo_state.ngrad;
174  hist << "\n";
175  }
176  return hist.str();
177  }
178 }; // class SecantStep
179 
180 } // namespace ROL
181 
182 #endif
Provides the interface to evaluate objective functions.
void initialize(Vector< Real > &x, const Vector< Real > &s, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &con, AlgorithmState< Real > &algo_state)
Initialize step with bound constraint.
std::string secantName_
virtual void scale(const Real alpha)=0
Compute where .
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
void update(Vector< Real > &x, const Vector< Real > &s, Objective< Real > &obj, BoundConstraint< Real > &con, AlgorithmState< Real > &algo_state)
Update step, if successful.
virtual void plus(const Vector &x)=0
Compute , where .
SecantStep(ROL::ParameterList &parlist, const ROL::Ptr< Secant< Real > > &secant=ROL::nullPtr, const bool computeObj=true)
Constructor.
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
Provides the interface to compute optimization steps.
Definition: ROL_Step.hpp:34
Contains definitions of custom data types in ROL.
ESecant StringToESecant(std::string s)
Definition: ROL_Types.hpp:509
std::string EDescentToString(EDescent tr)
Definition: ROL_Types.hpp:386
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
virtual void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
State for algorithm class. Will be used for restarts.
Definition: ROL_Types.hpp:109
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
ESecant
Enumeration of secant update algorithms.
Definition: ROL_Types.hpp:452
std::string printHeader(void) const
Print iterate header.
ROL::Ptr< StepState< Real > > getState(void)
Definition: ROL_Step.hpp:39
Provides interface for and implements limited-memory secant operators.
Definition: ROL_Secant.hpp:45
ROL::Ptr< Vector< Real > > iterateVec
Definition: ROL_Types.hpp:123
std::string printName(void) const
Print step name.
void compute(Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step.
Provides the interface to apply upper and lower bound constraints.
int verbosity_
Verbosity setting.
ROL::Ptr< Secant< Real > > secant_
Secant object (used for quasi-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:54
std::string print(AlgorithmState< Real > &algo_state, bool print_header=false) const
Print iterate status.
virtual Real norm() const =0
Returns where .
ROL::Ptr< Vector< Real > > gp_
Additional vector storage.
Provides the interface to compute optimization steps with a secant method.