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  Teuchos::RCP<NonlinearCG<Real> > nlcg_;
65  int verbosity_;
66  const bool computeObj_;
67 
68 public:
69 
71  using Step<Real>::compute;
72  using Step<Real>::update;
73 
83  NonlinearCGStep( Teuchos::ParameterList &parlist,
84  const Teuchos::RCP<NonlinearCG<Real> > &nlcg = Teuchos::null,
85  const bool computeObj = true )
86  : Step<Real>(), nlcg_(nlcg), enlcg_(NONLINEARCG_USERDEFINED),
87  verbosity_(0), computeObj_(computeObj) {
88  // Parse ParameterList
89  verbosity_ = parlist.sublist("General").get("Print Verbosity",0);
90  // Initialize secant object
91  Teuchos::ParameterList& Llist = parlist.sublist("Step").sublist("Line Search");
92  if ( nlcg == Teuchos::null ) {
93  enlcg_
94  = StringToENonlinearCG(Llist.sublist("Descent Method").get("Nonlinear CG Type","Oren-Luenberger"));
95  nlcg_ = Teuchos::rcp(new NonlinearCG<Real>(enlcg_));
96  }
97  }
98 
99  void compute( Vector<Real> &s, const Vector<Real> &x,
101  AlgorithmState<Real> &algo_state ) {
102  Teuchos::RCP<StepState<Real> > step_state = Step<Real>::getState();
103  Real one(1);
104 
105  // Compute search direction
106  nlcg_->run(s,*(step_state->gradientVec),x,obj);
107  s.scale(-one);
108  }
109 
111  AlgorithmState<Real> &algo_state ) {
112  Real tol = std::sqrt(ROL_EPSILON<Real>());
113  Teuchos::RCP<StepState<Real> > step_state = Step<Real>::getState();
114 
115  // Update iterate
116  algo_state.iter++;
117  x.plus(s);
118  (step_state->descentVec)->set(s);
119  algo_state.snorm = s.norm();
120 
121  // Compute new gradient
122  obj.update(x,true,algo_state.iter);
123  if ( computeObj_ ) {
124  algo_state.value = obj.value(x,tol);
125  algo_state.nfval++;
126  }
127  obj.gradient(*(step_state->gradientVec),x,tol);
128  algo_state.ngrad++;
129 
130  // Update algorithm state
131  (algo_state.iterateVec)->set(x);
132  algo_state.gnorm = (step_state->gradientVec)->norm();
133  }
134 
135  std::string printHeader( void ) const {
136  std::stringstream hist;
137 
138  if( verbosity_>0 ) {
139  hist << std::string(109,'-') << "\n";
141  hist << " status output definitions\n\n";
142  hist << " iter - Number of iterates (steps taken) \n";
143  hist << " value - Objective function value \n";
144  hist << " gnorm - Norm of the gradient\n";
145  hist << " snorm - Norm of the step (update to optimization vector)\n";
146  hist << " #fval - Cumulative number of times the objective function was evaluated\n";
147  hist << " #grad - Number of times the gradient was computed\n";
148  hist << std::string(109,'-') << "\n";
149  }
150 
151  hist << " ";
152  hist << std::setw(6) << std::left << "iter";
153  hist << std::setw(15) << std::left << "value";
154  hist << std::setw(15) << std::left << "gnorm";
155  hist << std::setw(15) << std::left << "snorm";
156  hist << std::setw(10) << std::left << "#fval";
157  hist << std::setw(10) << std::left << "#grad";
158  hist << "\n";
159  return hist.str();
160  }
161  std::string printName( void ) const {
162  std::stringstream hist;
163  hist << "\n" << ENonlinearCGToString(enlcg_) << " "
165  return hist.str();
166  }
167  std::string print( AlgorithmState<Real> &algo_state, bool print_header = false ) const {
168  std::stringstream hist;
169  hist << std::scientific << std::setprecision(6);
170  if ( algo_state.iter == 0 ) {
171  hist << printName();
172  }
173  if ( print_header ) {
174  hist << printHeader();
175  }
176  if ( algo_state.iter == 0 ) {
177  hist << " ";
178  hist << std::setw(6) << std::left << algo_state.iter;
179  hist << std::setw(15) << std::left << algo_state.value;
180  hist << std::setw(15) << std::left << algo_state.gnorm;
181  hist << "\n";
182  }
183  else {
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 << std::setw(15) << std::left << algo_state.snorm;
189  hist << std::setw(10) << std::left << algo_state.nfval;
190  hist << std::setw(10) << std::left << algo_state.ngrad;
191  hist << "\n";
192  }
193  return hist.str();
194  }
195 }; // class NonlinearCGStep
196 
197 } // namespace ROL
198 
199 #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.
std::string printName(void) const
Print step name.
virtual void plus(const Vector &x)=0
Compute , where .
NonlinearCGStep(Teuchos::ParameterList &parlist, const Teuchos::RCP< NonlinearCG< Real > > &nlcg=Teuchos::null, const bool computeObj=true)
Constructor.
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.
std::string print(AlgorithmState< Real > &algo_state, bool print_header=false) const
Print iterate status.
Teuchos::RCP< NonlinearCG< Real > > nlcg_
NonlinearCG object (used for quasi-Newton)
std::string EDescentToString(EDescent tr)
Definition: ROL_Types.hpp:354
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
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.
ENonlinearCG
Enumeration of nonlinear CG algorithms.
Definition: ROL_Types.hpp:569
std::string printHeader(void) const
Print iterate header.
std::string ENonlinearCGToString(ENonlinearCG tr)
Definition: ROL_Types.hpp:583
ENonlinearCG StringToENonlinearCG(std::string s)
Definition: ROL_Types.hpp:641
Provides the interface to apply upper and lower bound constraints.
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.
Teuchos::RCP< Vector< Real > > iterateVec
Definition: ROL_Types.hpp:105
virtual Real norm() const =0
Returns where .
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.