ROL
ROL_NewtonStep.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_NEWTONSTEP_H
11#define ROL_NEWTONSTEP_H
12
13#include "ROL_Types.hpp"
14#include "ROL_Step.hpp"
15
21
22namespace ROL {
23
24template <class Real>
25class NewtonStep : public Step<Real> {
26private:
27
29 const bool computeObj_;
30
31public:
32
33 using Step<Real>::initialize;
34 using Step<Real>::compute;
35 using Step<Real>::update;
36
44 NewtonStep( ROL::ParameterList &parlist, const bool computeObj = true )
45 : Step<Real>(), verbosity_(0), computeObj_(computeObj) {
46 // Parse ParameterList
47 verbosity_ = parlist.sublist("General").get("Print Verbosity",0);
48 }
49
50 void compute( Vector<Real> &s, const Vector<Real> &x,
52 AlgorithmState<Real> &algo_state ) {
53 ROL::Ptr<StepState<Real> > step_state = Step<Real>::getState();
54 Real tol = std::sqrt(ROL_EPSILON<Real>()), one(1);
55
56 // Compute unconstrained step
57 obj.invHessVec(s,*(step_state->gradientVec),x,tol);
58 s.scale(-one);
59 }
60
62 AlgorithmState<Real> &algo_state ) {
63 Real tol = std::sqrt(ROL_EPSILON<Real>());
64 ROL::Ptr<StepState<Real> > step_state = Step<Real>::getState();
65
66 // Update iterate
67 algo_state.iter++;
68 x.plus(s);
69 (step_state->descentVec)->set(s);
70 algo_state.snorm = s.norm();
71
72 // Compute new gradient
73 obj.update(x,true,algo_state.iter);
74 if ( computeObj_ ) {
75 algo_state.value = obj.value(x,tol);
76 algo_state.nfval++;
77 }
78 obj.gradient(*(step_state->gradientVec),x,tol);
79 algo_state.ngrad++;
80
81 // Update algorithm state
82 (algo_state.iterateVec)->set(x);
83 algo_state.gnorm = (step_state->gradientVec)->norm();
84 }
85
86 std::string printHeader( void ) const {
87 std::stringstream hist;
88
89 if( verbosity_>0 ) {
90 hist << std::string(109,'-') << "\n";
92 hist << " status output definitions\n\n";
93 hist << " iter - Number of iterates (steps taken) \n";
94 hist << " value - Objective function value \n";
95 hist << " gnorm - Norm of the gradient\n";
96 hist << " snorm - Norm of the step (update to optimization vector)\n";
97 hist << " #fval - Cumulative number of times the objective function was evaluated\n";
98 hist << " #grad - Number of times the gradient was computed\n";
99 hist << std::string(109,'-') << "\n";
100 }
101
102 hist << " ";
103 hist << std::setw(6) << std::left << "iter";
104 hist << std::setw(15) << std::left << "value";
105 hist << std::setw(15) << std::left << "gnorm";
106 hist << std::setw(15) << std::left << "snorm";
107 hist << std::setw(10) << std::left << "#fval";
108 hist << std::setw(10) << std::left << "#grad";
109 hist << "\n";
110 return hist.str();
111 }
112 std::string printName( void ) const {
113 std::stringstream hist;
114 hist << "\n" << EDescentToString(DESCENT_NEWTON) << "\n";
115 return hist.str();
116 }
117 std::string print( AlgorithmState<Real> &algo_state, bool print_header = false ) const {
118 std::stringstream hist;
119 hist << std::scientific << std::setprecision(6);
120 if ( algo_state.iter == 0 ) {
121 hist << printName();
122 }
123 if ( print_header ) {
124 hist << printHeader();
125 }
126 if ( algo_state.iter == 0 ) {
127 hist << " ";
128 hist << std::setw(6) << std::left << algo_state.iter;
129 hist << std::setw(15) << std::left << algo_state.value;
130 hist << std::setw(15) << std::left << algo_state.gnorm;
131 hist << "\n";
132 }
133 else {
134 hist << " ";
135 hist << std::setw(6) << std::left << algo_state.iter;
136 hist << std::setw(15) << std::left << algo_state.value;
137 hist << std::setw(15) << std::left << algo_state.gnorm;
138 hist << std::setw(15) << std::left << algo_state.snorm;
139 hist << std::setw(10) << std::left << algo_state.nfval;
140 hist << std::setw(10) << std::left << algo_state.ngrad;
141 hist << "\n";
142 }
143 return hist.str();
144 }
145}; // class Step
146
147} // namespace ROL
148
149#endif
Contains definitions of custom data types in ROL.
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.
const bool computeObj_
void compute(Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step.
NewtonStep(ROL::ParameterList &parlist, const bool computeObj=true)
Constructor.
std::string printHeader(void) const
Print iterate header.
std::string print(AlgorithmState< Real > &algo_state, bool print_header=false) const
Print iterate status.
std::string printName(void) const
Print step name.
Provides the interface to evaluate objective functions.
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
virtual void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
virtual void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
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
ROL::Ptr< StepState< Real > > getState(void)
Definition ROL_Step.hpp:39
Step(void)
Definition ROL_Step.hpp:47
Defines the linear algebra or vector space interface.
virtual Real norm() const =0
Returns where .
virtual void scale(const Real alpha)=0
Compute where .
virtual void plus(const Vector &x)=0
Compute , where .
Real ROL_EPSILON(void)
Platform-dependent machine epsilon.
Definition ROL_Types.hpp:57
@ DESCENT_NEWTON
std::string EDescentToString(EDescent tr)
State for algorithm class. Will be used for restarts.
ROL::Ptr< Vector< Real > > iterateVec