ROL
ROL_SecantStep.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_SECANTSTEP_H
45 #define ROL_SECANTSTEP_H
46 
47 #include "ROL_Types.hpp"
48 #include "ROL_Step.hpp"
49 #include "ROL_Secant.hpp"
50 
57 namespace ROL {
58 
59 template <class Real>
60 class SecantStep : public Step<Real> {
61 private:
62 
63  Teuchos::RCP<Secant<Real> > secant_;
65  Teuchos::RCP<Vector<Real> > gp_;
66  int verbosity_;
68 
69 public:
70 
72  using Step<Real>::compute;
73  using Step<Real>::update;
74 
84  SecantStep( Teuchos::ParameterList &parlist,
85  const Teuchos::RCP<Secant<Real> > &secant = Teuchos::null,
86  const bool computeObj = true )
87  : Step<Real>(), secant_(secant), esec_(SECANT_USERDEFINED),
88  gp_(Teuchos::null), verbosity_(0), computeObj_(computeObj) {
89  // Parse ParameterList
90  verbosity_ = parlist.sublist("General").get("Print Verbosity",0);
91  // Initialize secant object
92  if ( secant == Teuchos::null ) {
93  esec_ = StringToESecant(parlist.sublist("General").sublist("Secant").get("Type","Limited-Memory BFGS"));
94  secant_ = SecantFactory<Real>(parlist);
95  }
96  }
97 
98  void initialize( Vector<Real> &x, const Vector<Real> &s, const Vector<Real> &g,
100  AlgorithmState<Real> &algo_state ) {
101  Step<Real>::initialize(x,s,g,obj,con,algo_state);
102  gp_ = g.clone();
103  }
104 
105  void compute( Vector<Real> &s, const Vector<Real> &x,
107  AlgorithmState<Real> &algo_state ) {
108  Real one(1);
109  Teuchos::RCP<StepState<Real> > step_state = Step<Real>::getState();
110 
111  // Compute search direction
112  secant_->applyH(s,*(step_state->gradientVec));
113  s.scale(-one);
114  }
115 
117  AlgorithmState<Real> &algo_state ) {
118  Real tol = std::sqrt(ROL_EPSILON<Real>());
119  Teuchos::RCP<StepState<Real> > step_state = Step<Real>::getState();
120 
121  // Update iterate
122  algo_state.iter++;
123  x.plus(s);
124  (step_state->descentVec)->set(s);
125  algo_state.snorm = s.norm();
126 
127  // Compute new gradient
128  gp_->set(*(step_state->gradientVec));
129  obj.update(x,true,algo_state.iter);
130  if ( computeObj_ ) {
131  algo_state.value = obj.value(x,tol);
132  algo_state.nfval++;
133  }
134  obj.gradient(*(step_state->gradientVec),x,tol);
135  algo_state.ngrad++;
136 
137  // Update Secant Information
138  secant_->updateStorage(x,*(step_state->gradientVec),*gp_,s,algo_state.snorm,algo_state.iter+1);
139 
140  // Update algorithm state
141  (algo_state.iterateVec)->set(x);
142  algo_state.gnorm = (step_state->gradientVec)->norm();
143  }
144 
145  std::string printHeader( void ) const {
146  std::stringstream hist;
147 
148  if( verbosity_>0 ) {
149  hist << std::string(109,'-') << "\n";
151  hist << " status output definitions\n\n";
152  hist << " iter - Number of iterates (steps taken) \n";
153  hist << " value - Objective function value \n";
154  hist << " gnorm - Norm of the gradient\n";
155  hist << " snorm - Norm of the step (update to optimization vector)\n";
156  hist << " #fval - Cumulative number of times the objective function was evaluated\n";
157  hist << " #grad - Number of times the gradient was computed\n";
158  hist << std::string(109,'-') << "\n";
159  }
160 
161  hist << " ";
162  hist << std::setw(6) << std::left << "iter";
163  hist << std::setw(15) << std::left << "value";
164  hist << std::setw(15) << std::left << "gnorm";
165  hist << std::setw(15) << std::left << "snorm";
166  hist << std::setw(10) << std::left << "#fval";
167  hist << std::setw(10) << std::left << "#grad";
168  hist << "\n";
169  return hist.str();
170  }
171  std::string printName( void ) const {
172  std::stringstream hist;
173  hist << "\n" << EDescentToString(DESCENT_SECANT);
174  hist << " with " << ESecantToString(esec_) << "\n";
175  return hist.str();
176  }
177  std::string print( AlgorithmState<Real> &algo_state, bool print_header = false ) const {
178  std::stringstream hist;
179  hist << std::scientific << std::setprecision(6);
180  if ( algo_state.iter == 0 ) {
181  hist << printName();
182  }
183  if ( print_header ) {
184  hist << printHeader();
185  }
186  if ( algo_state.iter == 0 ) {
187  hist << " ";
188  hist << std::setw(6) << std::left << algo_state.iter;
189  hist << std::setw(15) << std::left << algo_state.value;
190  hist << std::setw(15) << std::left << algo_state.gnorm;
191  hist << "\n";
192  }
193  else {
194  hist << " ";
195  hist << std::setw(6) << std::left << algo_state.iter;
196  hist << std::setw(15) << std::left << algo_state.value;
197  hist << std::setw(15) << std::left << algo_state.gnorm;
198  hist << std::setw(15) << std::left << algo_state.snorm;
199  hist << std::setw(10) << std::left << algo_state.nfval;
200  hist << std::setw(10) << std::left << algo_state.ngrad;
201  hist << "\n";
202  }
203  return hist.str();
204  }
205 }; // class SecantStep
206 
207 } // namespace ROL
208 
209 #endif
std::string printHeader(void) const
Print iterate header.
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.
virtual void scale(const Real alpha)=0
Compute where .
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 .
Teuchos::RCP< Vector< Real > > gp_
Additional vector storage.
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
Provides the interface to compute optimization steps.
Definition: ROL_Step.hpp:69
Teuchos::RCP< StepState< Real > > getState(void)
Definition: ROL_Step.hpp:74
Contains definitions of custom data types in ROL.
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
ESecant StringToESecant(std::string s)
Definition: ROL_Types.hpp:477
std::string EDescentToString(EDescent tr)
Definition: ROL_Types.hpp:354
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
std::string print(AlgorithmState< Real > &algo_state, bool print_header=false) const
Print iterate status.
SecantStep(Teuchos::ParameterList &parlist, const Teuchos::RCP< Secant< Real > > &secant=Teuchos::null, const bool computeObj=true)
Constructor.
State for algorithm class. Will be used for restarts.
Definition: ROL_Types.hpp:91
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
ESecant
Enumeration of secant update algorithms.
Definition: ROL_Types.hpp:420
Provides interface for and implements limited-memory secant operators.
Definition: ROL_Secant.hpp:70
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.
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:89
Teuchos::RCP< Vector< Real > > iterateVec
Definition: ROL_Types.hpp:105
virtual Real norm() const =0
Returns where .
Teuchos::RCP< Secant< Real > > secant_
Secant object (used for quasi-Newton)
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
std::string ESecantToString(ESecant tr)
Definition: ROL_Types.hpp:429
std::string printName(void) const
Print step name.
Provides the interface to compute optimization steps with a secant method.