ROL
ROL_NonlinearCGStep.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_NONLINEARCGSTEP_H
45 #define ROL_NONLINEARCGSTEP_H
46 
47 #include "ROL_Types.hpp"
48 #include "ROL_Step.hpp"
49 #include "ROL_NonlinearCG.hpp"
50 
57 namespace ROL {
58 
59 template <class Real>
60 class NonlinearCGStep : public Step<Real> {
61 private:
62 
63  ROL::Ptr<NonlinearCG<Real> > nlcg_;
65  int verbosity_;
66  const bool computeObj_;
67 
68  std::string ncgName_;
69 
70 public:
71 
73  using Step<Real>::compute;
74  using Step<Real>::update;
75 
85  NonlinearCGStep( ROL::ParameterList &parlist,
86  const ROL::Ptr<NonlinearCG<Real> > &nlcg = ROL::nullPtr,
87  const bool computeObj = true )
88  : Step<Real>(), nlcg_(nlcg), enlcg_(NONLINEARCG_USERDEFINED),
89  verbosity_(0), computeObj_(computeObj) {
90  // Parse ParameterList
91  verbosity_ = parlist.sublist("General").get("Print Verbosity",0);
92  // Initialize secant object
93  ROL::ParameterList& Llist = parlist.sublist("Step").sublist("Line Search");
94  if ( nlcg == ROL::nullPtr ) {
95  ncgName_ = Llist.sublist("Descent Method").get("Nonlinear CG Type","Oren-Luenberger");
96  enlcg_
98  nlcg_ = ROL::makePtr<NonlinearCG<Real>>(enlcg_);
99  }
100  else {
101  ncgName_ = Llist.sublist("Descent Method").get("User Defined Nonlinear CG Name",
102  "Unspecified User Define Nonlinear CG Method");
103  }
104  }
105 
106  void compute( Vector<Real> &s, const Vector<Real> &x,
108  AlgorithmState<Real> &algo_state ) {
109  ROL::Ptr<StepState<Real> > step_state = Step<Real>::getState();
110  Real one(1);
111 
112  // Compute search direction
113  nlcg_->run(s,*(step_state->gradientVec),x,obj);
114  s.scale(-one);
115  }
116 
118  AlgorithmState<Real> &algo_state ) {
119  Real tol = std::sqrt(ROL_EPSILON<Real>());
120  ROL::Ptr<StepState<Real> > step_state = Step<Real>::getState();
121 
122  // Update iterate
123  algo_state.iter++;
124  x.plus(s);
125  (step_state->descentVec)->set(s);
126  algo_state.snorm = s.norm();
127 
128  // Compute new gradient
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 algorithm state
138  (algo_state.iterateVec)->set(x);
139  algo_state.gnorm = (step_state->gradientVec)->norm();
140  }
141 
142  std::string printHeader( void ) const {
143  std::stringstream hist;
144 
145  if( verbosity_>0 ) {
146  hist << std::string(109,'-') << "\n";
148  hist << " status output definitions\n\n";
149  hist << " iter - Number of iterates (steps taken) \n";
150  hist << " value - Objective function value \n";
151  hist << " gnorm - Norm of the gradient\n";
152  hist << " snorm - Norm of the step (update to optimization vector)\n";
153  hist << " #fval - Cumulative number of times the objective function was evaluated\n";
154  hist << " #grad - Number of times the gradient was computed\n";
155  hist << std::string(109,'-') << "\n";
156  }
157 
158  hist << " ";
159  hist << std::setw(6) << std::left << "iter";
160  hist << std::setw(15) << std::left << "value";
161  hist << std::setw(15) << std::left << "gnorm";
162  hist << std::setw(15) << std::left << "snorm";
163  hist << std::setw(10) << std::left << "#fval";
164  hist << std::setw(10) << std::left << "#grad";
165  hist << "\n";
166  return hist.str();
167  }
168  std::string printName( void ) const {
169  std::stringstream hist;
170  hist << "\n" << ncgName_ << " "
172  return hist.str();
173  }
174  std::string print( AlgorithmState<Real> &algo_state, bool print_header = false ) const {
175  std::stringstream hist;
176  hist << std::scientific << std::setprecision(6);
177  if ( algo_state.iter == 0 ) {
178  hist << printName();
179  }
180  if ( print_header ) {
181  hist << printHeader();
182  }
183  if ( algo_state.iter == 0 ) {
184  hist << " ";
185  hist << std::setw(6) << std::left << algo_state.iter;
186  hist << std::setw(15) << std::left << algo_state.value;
187  hist << std::setw(15) << std::left << algo_state.gnorm;
188  hist << "\n";
189  }
190  else {
191  hist << " ";
192  hist << std::setw(6) << std::left << algo_state.iter;
193  hist << std::setw(15) << std::left << algo_state.value;
194  hist << std::setw(15) << std::left << algo_state.gnorm;
195  hist << std::setw(15) << std::left << algo_state.snorm;
196  hist << std::setw(10) << std::left << algo_state.nfval;
197  hist << std::setw(10) << std::left << algo_state.ngrad;
198  hist << "\n";
199  }
200  return hist.str();
201  }
202 }; // class NonlinearCGStep
203 
204 } // namespace ROL
205 
206 #endif
Provides the interface to evaluate objective functions.
int verbosity_
Verbosity setting.
virtual void scale(const Real alpha)=0
Compute where .
void compute(Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step.
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:68
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 print(AlgorithmState< Real > &algo_state, bool print_header=false) const
Print iterate status.
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.
ENonlinearCG
Enumeration of nonlinear CG algorithms.
Definition: ROL_Types.hpp:564
ROL::Ptr< NonlinearCG< Real > > nlcg_
NonlinearCG object (used for quasi-Newton)
ROL::Ptr< StepState< Real > > getState(void)
Definition: ROL_Step.hpp:73
ROL::Ptr< Vector< Real > > iterateVec
Definition: ROL_Types.hpp:157
ENonlinearCG StringToENonlinearCG(std::string s)
Definition: ROL_Types.hpp:636
std::string printHeader(void) const
Print iterate header.
std::string printName(void) const
Print step name.
Provides the interface to apply upper and lower bound constraints.
NonlinearCGStep(ROL::ParameterList &parlist, const ROL::Ptr< NonlinearCG< Real > > &nlcg=ROL::nullPtr, const bool computeObj=true)
Constructor.
Provides the interface to compute optimization steps with nonlinear CG.
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.