ROL
ROL_ProjectedSecantStep.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_PROJECTEDSECANTSTEP_H
11 #define ROL_PROJECTEDSECANTSTEP_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 ProjectedSecantStep : public Step<Real> {
27 private:
28 
29  ROL::Ptr<Secant<Real> > secant_;
31  ROL::Ptr<Vector<Real> > d_;
32  ROL::Ptr<Vector<Real> > gp_;
33  int verbosity_;
34  const bool computeObj_;
36 
37 public:
38 
40  using Step<Real>::compute;
41  using Step<Real>::update;
42 
51  ProjectedSecantStep( ROL::ParameterList &parlist,
52  const ROL::Ptr<Secant<Real> > &secant = ROL::nullPtr,
53  const bool computeObj = true )
54  : Step<Real>(), secant_(secant), d_(ROL::nullPtr), gp_(ROL::nullPtr),
55  verbosity_(0), computeObj_(computeObj), useProjectedGrad_(false) {
56  // Parse ParameterList
57  ROL::ParameterList& Glist = parlist.sublist("General");
58  useProjectedGrad_ = Glist.get("Projected Gradient Criticality Measure", false);
59  verbosity_ = parlist.sublist("General").get("Print Verbosity",0);
60  // Initialize secant object
61  if ( secant == ROL::nullPtr ) {
62  std::string secantType = parlist.sublist("General").sublist("Secant").get("Type","Limited-Memory BFGS");
63  esec_ = StringToESecant(secantType);
64  secant_ = SecantFactory<Real>(parlist);
65  }
66  }
67 
68  void initialize( Vector<Real> &x, const Vector<Real> &s, const Vector<Real> &g,
70  AlgorithmState<Real> &algo_state ) {
71  Step<Real>::initialize(x,s,g,obj,bnd,algo_state);
72  d_ = s.clone();
73  gp_ = g.clone();
74  }
75 
76  void compute( Vector<Real> &s, const Vector<Real> &x,
78  AlgorithmState<Real> &algo_state ) {
79  ROL::Ptr<StepState<Real> > step_state = Step<Real>::getState();
80  Real one(1);
81 
82  // Compute projected secant step
83  // ---> Apply inactive-inactive block of inverse secant to gradient
84  gp_->set(*(step_state->gradientVec));
85  bnd.pruneActive(*gp_,*(step_state->gradientVec),x,algo_state.gnorm);
86  secant_->applyH(s,*gp_);
87  bnd.pruneActive(s,*(step_state->gradientVec),x,algo_state.gnorm);
88  // ---> Add in active gradient components
89  gp_->set(*(step_state->gradientVec));
90  bnd.pruneInactive(*gp_,*(step_state->gradientVec),x,algo_state.gnorm);
91  s.plus(gp_->dual());
92  s.scale(-one);
93  }
94 
95  void update( Vector<Real> &x, const Vector<Real> &s,
97  AlgorithmState<Real> &algo_state ) {
98  Real tol = std::sqrt(ROL_EPSILON<Real>()), one(1);
99  ROL::Ptr<StepState<Real> > step_state = Step<Real>::getState();
100 
101  // Update iterate and store previous step
102  algo_state.iter++;
103  d_->set(x);
104  x.plus(s);
105  bnd.project(x);
106  (step_state->descentVec)->set(x);
107  (step_state->descentVec)->axpy(-one,*d_);
108  algo_state.snorm = s.norm();
109 
110  // Compute new gradient
111  gp_->set(*(step_state->gradientVec));
112  obj.update(x,true,algo_state.iter);
113  if ( computeObj_ ) {
114  algo_state.value = obj.value(x,tol);
115  algo_state.nfval++;
116  }
117  obj.gradient(*(step_state->gradientVec),x,tol);
118  algo_state.ngrad++;
119 
120  // Update Secant Information
121  secant_->updateStorage(x,*(step_state->gradientVec),*gp_,s,algo_state.snorm,algo_state.iter+1);
122 
123  // Update algorithm state
124  (algo_state.iterateVec)->set(x);
125  if ( useProjectedGrad_ ) {
126  gp_->set(*(step_state->gradientVec));
127  bnd.computeProjectedGradient( *gp_, x );
128  algo_state.gnorm = gp_->norm();
129  }
130  else {
131  d_->set(x);
132  d_->axpy(-one,(step_state->gradientVec)->dual());
133  bnd.project(*d_);
134  d_->axpy(-one,x);
135  algo_state.gnorm = d_->norm();
136  }
137  }
138 
139  std::string printHeader( void ) const {
140  std::stringstream hist;
141 
142  if( verbosity_>0 ) {
143  hist << std::string(109,'-') << "\n";
145  hist << " status output definitions\n\n";
146  hist << " iter - Number of iterates (steps taken) \n";
147  hist << " value - Objective function value \n";
148  hist << " gnorm - Norm of the gradient\n";
149  hist << " snorm - Norm of the step (update to optimization vector)\n";
150  hist << " #fval - Cumulative number of times the objective function was evaluated\n";
151  hist << " #grad - Number of times the gradient was computed\n";
152  hist << std::string(109,'-') << "\n";
153  }
154 
155  hist << " ";
156  hist << std::setw(6) << std::left << "iter";
157  hist << std::setw(15) << std::left << "value";
158  hist << std::setw(15) << std::left << "gnorm";
159  hist << std::setw(15) << std::left << "snorm";
160  hist << std::setw(10) << std::left << "#fval";
161  hist << std::setw(10) << std::left << "#grad";
162  hist << "\n";
163  return hist.str();
164  }
165  std::string printName( void ) const {
166  std::stringstream hist;
167  hist << "\n" << EDescentToString(DESCENT_SECANT);
168  hist << " with " << ESecantToString(esec_) << "\n";
169  return hist.str();
170  }
171  std::string print( AlgorithmState<Real> &algo_state, bool print_header = false ) const {
172  std::stringstream hist;
173  hist << std::scientific << std::setprecision(6);
174  if ( algo_state.iter == 0 ) {
175  hist << printName();
176  }
177  if ( print_header ) {
178  hist << printHeader();
179  }
180  if ( algo_state.iter == 0 ) {
181  hist << " ";
182  hist << std::setw(6) << std::left << algo_state.iter;
183  hist << std::setw(15) << std::left << algo_state.value;
184  hist << std::setw(15) << std::left << algo_state.gnorm;
185  hist << "\n";
186  }
187  else {
188  hist << " ";
189  hist << std::setw(6) << std::left << algo_state.iter;
190  hist << std::setw(15) << std::left << algo_state.value;
191  hist << std::setw(15) << std::left << algo_state.gnorm;
192  hist << std::setw(15) << std::left << algo_state.snorm;
193  hist << std::setw(10) << std::left << algo_state.nfval;
194  hist << std::setw(10) << std::left << algo_state.ngrad;
195  hist << "\n";
196  }
197  return hist.str();
198  }
199 }; // class ProjectedSecantStep
200 
201 } // namespace ROL
202 
203 #endif
Provides the interface to evaluate objective functions.
virtual void scale(const Real alpha)=0
Compute where .
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
ROL::Ptr< Vector< Real > > d_
Additional vector storage.
virtual void plus(const Vector &x)=0
Compute , where .
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.
ROL::Ptr< Vector< Real > > gp_
Additional vector storage.
ESecant StringToESecant(std::string s)
Definition: ROL_Types.hpp:513
std::string EDescentToString(EDescent tr)
Definition: ROL_Types.hpp:390
void update(Vector< Real > &x, const Vector< Real > &s, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Update step, if successful.
ProjectedSecantStep(ROL::ParameterList &parlist, const ROL::Ptr< Secant< Real > > &secant=ROL::nullPtr, const bool computeObj=true)
Constructor.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Provides the interface to compute optimization steps with projected secant method using line search...
virtual void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
void compute(Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step.
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.
ROL::Ptr< Secant< Real > > secant_
Secant object (used for quasi-Newton)
ESecant
Enumeration of secant update algorithms.
Definition: ROL_Types.hpp:456
ROL::Ptr< StepState< Real > > getState(void)
Definition: ROL_Step.hpp:39
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.
bool useProjectedGrad_
Whether or not to use to the projected gradient criticality measure.
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.
virtual void project(Vector< Real > &x)
Project optimization variables onto the bounds.
void pruneInactive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0))
Set variables to zero if they correspond to the -inactive set.
void pruneActive(Vector< Real > &v, const Vector< Real > &x, Real eps=Real(0))
Set variables to zero if they correspond to the -active set.
Provides the interface to apply upper and lower bound constraints.
void computeProjectedGradient(Vector< Real > &g, const Vector< Real > &x)
Compute projected gradient.
std::string printHeader(void) const
Print iterate header.
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
virtual Real norm() const =0
Returns where .
std::string ESecantToString(ESecant tr)
Definition: ROL_Types.hpp:465
std::string print(AlgorithmState< Real > &algo_state, bool print_header=false) const
Print iterate status.