ROL
ROL_GradientStep.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_GRADIENTSTEP_H
11 #define ROL_GRADIENTSTEP_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 GradientStep : public Step<Real> {
27 private:
28 
29  int verbosity_;
30  const bool computeObj_;
31 
32 public:
33 
35  using Step<Real>::compute;
36  using Step<Real>::update;
37 
45  GradientStep( ROL::ParameterList &parlist, const bool computeObj = true )
46  : Step<Real>(), verbosity_(0), computeObj_(computeObj) {
47  // Parse ParameterList
48  verbosity_ = parlist.sublist("General").get("Print Verbosity",0);
49  }
50 
51  void compute( Vector<Real> &s, const Vector<Real> &x,
53  AlgorithmState<Real> &algo_state ) {
54  Real one(1);
55  ROL::Ptr<StepState<Real> > step_state = Step<Real>::getState();
56 
57  // Compute search direction
58  s.set((step_state->gradientVec)->dual());
59  s.scale(-one);
60  }
61 
63  AlgorithmState<Real> &algo_state ) {
64  Real tol = std::sqrt(ROL_EPSILON<Real>());
65  ROL::Ptr<StepState<Real> > step_state = Step<Real>::getState();
66 
67  // Update iterate and store step
68  algo_state.iter++;
69  x.plus(s);
70  (step_state->descentVec)->set(s);
71  algo_state.snorm = s.norm();
72 
73  // Compute new gradient
74  obj.update(x,true,algo_state.iter);
75  if ( computeObj_ ) {
76  algo_state.value = obj.value(x,tol);
77  algo_state.nfval++;
78  }
79  obj.gradient(*(step_state->gradientVec),x,tol);
80  algo_state.ngrad++;
81 
82  // Update algorithm state
83  (algo_state.iterateVec)->set(x);
84  algo_state.gnorm = (step_state->gradientVec)->norm();
85  }
86 
87  std::string printHeader( void ) const {
88  std::stringstream hist;
89 
90  if( verbosity_>0 ) {
91  hist << std::string(109,'-') << "\n";
93  hist << " status output definitions\n\n";
94  hist << " iter - Number of iterates (steps taken) \n";
95  hist << " value - Objective function value \n";
96  hist << " gnorm - Norm of the gradient\n";
97  hist << " snorm - Norm of the step (update to optimization vector)\n";
98  hist << " #fval - Cumulative number of times the objective function was evaluated\n";
99  hist << " #grad - Number of times the gradient was computed\n";
100  hist << std::string(109,'-') << "\n";
101  }
102 
103  hist << " ";
104  hist << std::setw(6) << std::left << "iter";
105  hist << std::setw(15) << std::left << "value";
106  hist << std::setw(15) << std::left << "gnorm";
107  hist << std::setw(15) << std::left << "snorm";
108  hist << std::setw(10) << std::left << "#fval";
109  hist << std::setw(10) << std::left << "#grad";
110  hist << "\n";
111  return hist.str();
112  }
113  std::string printName( void ) const {
114  std::stringstream hist;
115  hist << "\n" << EDescentToString(DESCENT_STEEPEST) << "\n";
116  return hist.str();
117  }
118  std::string print( AlgorithmState<Real> &algo_state, bool print_header = false ) const {
119  std::stringstream hist;
120  hist << std::scientific << std::setprecision(6);
121  if ( algo_state.iter == 0 ) {
122  hist << printName();
123  }
124  if ( print_header ) {
125  hist << printHeader();
126  }
127  if ( algo_state.iter == 0 ) {
128  hist << " ";
129  hist << std::setw(6) << std::left << algo_state.iter;
130  hist << std::setw(15) << std::left << algo_state.value;
131  hist << std::setw(15) << std::left << algo_state.gnorm;
132  hist << "\n";
133  }
134  else {
135  hist << " ";
136  hist << std::setw(6) << std::left << algo_state.iter;
137  hist << std::setw(15) << std::left << algo_state.value;
138  hist << std::setw(15) << std::left << algo_state.gnorm;
139  hist << std::setw(15) << std::left << algo_state.snorm;
140  hist << std::setw(10) << std::left << algo_state.nfval;
141  hist << std::setw(10) << std::left << algo_state.ngrad;
142  hist << "\n";
143  }
144  return hist.str();
145  }
146 }; // class GradientStep
147 
148 } // namespace ROL
149 #endif
Provides the interface to evaluate objective functions.
virtual void scale(const Real alpha)=0
Compute where .
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
std::string print(AlgorithmState< Real > &algo_state, bool print_header=false) const
Print iterate status.
Contains definitions of custom data types in ROL.
void compute(Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step.
std::string EDescentToString(EDescent tr)
Definition: ROL_Types.hpp:390
GradientStep(ROL::ParameterList &parlist, const bool computeObj=true)
Constructor.
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.
std::string printHeader(void) const
Print iterate header.
ROL::Ptr< StepState< Real > > getState(void)
Definition: ROL_Step.hpp:39
ROL::Ptr< Vector< Real > > iterateVec
Definition: ROL_Types.hpp:123
int verbosity_
Verbosity setting.
Provides the interface to apply upper and lower bound constraints.
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 set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:175
virtual Real norm() const =0
Returns where .
Provides the interface to compute optimization steps with the gradient descent method globalized usin...
std::string printName(void) const
Print step name.
const bool computeObj_
Allows line search to compute objective.