ROL
ROL_NewtonStep.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_NEWTONSTEP_H
45 #define ROL_NEWTONSTEP_H
46 
47 #include "ROL_Types.hpp"
48 #include "ROL_Step.hpp"
49 
56 namespace ROL {
57 
58 template <class Real>
59 class NewtonStep : public Step<Real> {
60 private:
61 
63  const bool computeObj_;
64 
65 public:
66 
68  using Step<Real>::compute;
69  using Step<Real>::update;
70 
78  NewtonStep( ROL::ParameterList &parlist, const bool computeObj = true )
79  : Step<Real>(), verbosity_(0), computeObj_(computeObj) {
80  // Parse ParameterList
81  verbosity_ = parlist.sublist("General").get("Print Verbosity",0);
82  }
83 
84  void compute( Vector<Real> &s, const Vector<Real> &x,
86  AlgorithmState<Real> &algo_state ) {
87  ROL::Ptr<StepState<Real> > step_state = Step<Real>::getState();
88  Real tol = std::sqrt(ROL_EPSILON<Real>()), one(1);
89 
90  // Compute unconstrained step
91  obj.invHessVec(s,*(step_state->gradientVec),x,tol);
92  s.scale(-one);
93  }
94 
96  AlgorithmState<Real> &algo_state ) {
97  Real tol = std::sqrt(ROL_EPSILON<Real>());
98  ROL::Ptr<StepState<Real> > step_state = Step<Real>::getState();
99 
100  // Update iterate
101  algo_state.iter++;
102  x.plus(s);
103  (step_state->descentVec)->set(s);
104  algo_state.snorm = s.norm();
105 
106  // Compute new gradient
107  obj.update(x,true,algo_state.iter);
108  if ( computeObj_ ) {
109  algo_state.value = obj.value(x,tol);
110  algo_state.nfval++;
111  }
112  obj.gradient(*(step_state->gradientVec),x,tol);
113  algo_state.ngrad++;
114 
115  // Update algorithm state
116  (algo_state.iterateVec)->set(x);
117  algo_state.gnorm = (step_state->gradientVec)->norm();
118  }
119 
120  std::string printHeader( void ) const {
121  std::stringstream hist;
122 
123  if( verbosity_>0 ) {
124  hist << std::string(109,'-') << "\n";
126  hist << " status output definitions\n\n";
127  hist << " iter - Number of iterates (steps taken) \n";
128  hist << " value - Objective function value \n";
129  hist << " gnorm - Norm of the gradient\n";
130  hist << " snorm - Norm of the step (update to optimization vector)\n";
131  hist << " #fval - Cumulative number of times the objective function was evaluated\n";
132  hist << " #grad - Number of times the gradient was computed\n";
133  hist << std::string(109,'-') << "\n";
134  }
135 
136  hist << " ";
137  hist << std::setw(6) << std::left << "iter";
138  hist << std::setw(15) << std::left << "value";
139  hist << std::setw(15) << std::left << "gnorm";
140  hist << std::setw(15) << std::left << "snorm";
141  hist << std::setw(10) << std::left << "#fval";
142  hist << std::setw(10) << std::left << "#grad";
143  hist << "\n";
144  return hist.str();
145  }
146  std::string printName( void ) const {
147  std::stringstream hist;
148  hist << "\n" << EDescentToString(DESCENT_NEWTON) << "\n";
149  return hist.str();
150  }
151  std::string print( AlgorithmState<Real> &algo_state, bool print_header = false ) const {
152  std::stringstream hist;
153  hist << std::scientific << std::setprecision(6);
154  if ( algo_state.iter == 0 ) {
155  hist << printName();
156  }
157  if ( print_header ) {
158  hist << printHeader();
159  }
160  if ( algo_state.iter == 0 ) {
161  hist << " ";
162  hist << std::setw(6) << std::left << algo_state.iter;
163  hist << std::setw(15) << std::left << algo_state.value;
164  hist << std::setw(15) << std::left << algo_state.gnorm;
165  hist << "\n";
166  }
167  else {
168  hist << " ";
169  hist << std::setw(6) << std::left << algo_state.iter;
170  hist << std::setw(15) << std::left << algo_state.value;
171  hist << std::setw(15) << std::left << algo_state.gnorm;
172  hist << std::setw(15) << std::left << algo_state.snorm;
173  hist << std::setw(10) << std::left << algo_state.nfval;
174  hist << std::setw(10) << std::left << algo_state.ngrad;
175  hist << "\n";
176  }
177  return hist.str();
178  }
179 }; // class Step
180 
181 } // namespace ROL
182 
183 #endif
Provides the interface to evaluate objective functions.
NewtonStep(ROL::ParameterList &parlist, const bool computeObj=true)
Constructor.
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:69
Contains definitions of custom data types in ROL.
std::string EDescentToString(EDescent tr)
Definition: ROL_Types.hpp:418
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
std::string printName(void) const
Print step name.
Provides the interface to compute optimization steps with Newton&#39;s method globalized using line searc...
State for algorithm class. Will be used for restarts.
Definition: ROL_Types.hpp:143
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
ROL::Ptr< StepState< Real > > getState(void)
Definition: ROL_Step.hpp:74
void compute(Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step.
ROL::Ptr< Vector< Real > > iterateVec
Definition: ROL_Types.hpp:157
virtual void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
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 Real norm() const =0
Returns where .
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
std::string print(AlgorithmState< Real > &algo_state, bool print_header=false) const
Print iterate status.
const bool computeObj_
std::string printHeader(void) const
Print iterate header.