ROL
ROL_Step.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_STEP_H
11 #define ROL_STEP_H
12 
13 #include "ROL_Vector.hpp"
14 #include "ROL_Objective.hpp"
15 #include "ROL_BoundConstraint.hpp"
16 #include "ROL_Constraint.hpp"
17 #include "ROL_Types.hpp"
18 
19 #include "ROL_ParameterList.hpp"
20 
27 namespace ROL {
28 
29 // We need a forward declaration here, because some steps are algorithms.
30 template<class Real>
31 class Algorithm;
32 
33 template <class Real>
34 class Step {
35 private:
36  ROL::Ptr<StepState<Real> > state_;
37 
38 protected:
39  ROL::Ptr<StepState<Real> > getState(void) {
40  return state_;
41  }
42 
43 public:
44 
45  virtual ~Step() {}
46 
47  Step(void) {
48  state_ = ROL::makePtr<StepState<Real>>();
49  }
50 
51 
54  virtual void initialize( Vector<Real> &x, const Vector<Real> &g,
56  AlgorithmState<Real> &algo_state ) {
57  initialize(x,x,g,obj,con,algo_state);
58  }
59 
62  virtual void initialize( Vector<Real> &x, const Vector<Real> &s, const Vector<Real> &g,
64  AlgorithmState<Real> &algo_state ) {
65  Real tol = std::sqrt(ROL_EPSILON<Real>()), one(1), zero(0);
66  // Initialize state descent direction and gradient storage
67  state_->descentVec = s.clone();
68  state_->gradientVec = g.clone();
69  state_->searchSize = zero;
70  // Project x onto constraint set
71  if ( con.isActivated() ) {
72  con.project(x);
73  }
74  // Update objective function, get value, and get gradient
75  obj.update(x,true,algo_state.iter);
76  algo_state.value = obj.value(x,tol);
77  algo_state.nfval++;
78  obj.gradient(*(state_->gradientVec),x,tol);
79  algo_state.ngrad++;
80  if ( con.isActivated() ) {
81  ROL::Ptr<Vector<Real> > xnew = x.clone();
82  xnew->set(x);
83  xnew->axpy(-one,(Step<Real>::state_->gradientVec)->dual());
84  con.project(*xnew);
85  xnew->axpy(-one,x);
86  algo_state.gnorm = xnew->norm();
87  }
88  else {
89  algo_state.gnorm = (state_->gradientVec)->norm();
90  }
91  }
92 
95  virtual void initialize( Vector<Real> &x, const Vector<Real> &g, Vector<Real> &l, const Vector<Real> &c,
97  AlgorithmState<Real> &algo_state ) {
98  }
99 
102  virtual void initialize( Vector<Real> &x, const Vector<Real> &g, Vector<Real> &l, const Vector<Real> &c,
104  AlgorithmState<Real> &algo_state ) {
105  }
106 
109  virtual void compute( Vector<Real> &s, const Vector<Real> &x,
111  AlgorithmState<Real> &algo_state ) {
112  throw Exception::NotImplemented(">>> ROL::Step::compute(s,x,obj,bnd,algo_state) is not implemented!");
113  }
114 
117  virtual void update( Vector<Real> &x, const Vector<Real> &s,
119  AlgorithmState<Real> &algo_state ) {
120  throw Exception::NotImplemented(">>> ROL::Step::update(x,s,obj,bnd,algo_state) is not implemented!");
121  }
122 
125  virtual void compute( Vector<Real> &s, const Vector<Real> &x, const Vector<Real> &l,
127  AlgorithmState<Real> &algo_state ) {
128  throw Exception::NotImplemented(">>> ROL::Step::compute(s,x,l,obj,con,algo_state) is not implemented!");
129  }
130 
133  virtual void update( Vector<Real> &x, Vector<Real> &l, const Vector<Real> &s,
135  AlgorithmState<Real> &algo_state ) {
136  throw Exception::NotImplemented(">>> ROL::Step::update(x,s,l,obj,bnd,con,algo_state) is not implemented!");
137  }
138 
141  virtual void compute( Vector<Real> &s, const Vector<Real> &x, const Vector<Real> &l,
144  AlgorithmState<Real> &algo_state ) {
145  throw Exception::NotImplemented(">>> ROL::Step::compute(s,x,l,obj,bnd,con,algo_state) is not implemented!");
146  }
147 
150  virtual void update( Vector<Real> &x, Vector<Real> &l, const Vector<Real> &s,
153  AlgorithmState<Real> &algo_state ) {
154  throw Exception::NotImplemented(">>> ROL::Step::update(x,s,l,obj,bnd,con,algo_state) is not implemented!");
155  }
156 
159  virtual std::string printHeader( void ) const {
160  throw Exception::NotImplemented(">>> ROL::Step::printHeader() is not implemented!");
161  }
162 
165  virtual std::string printName( void ) const {
166  throw Exception::NotImplemented(">>> ROL::Step::printName() is not implemented!");
167  }
168 
171  virtual std::string print( AlgorithmState<Real> &algo_state, bool printHeader = false ) const {
172  throw Exception::NotImplemented(">>> ROL::Step::print(algo_state,printHeader) is not implemented!");
173  }
174 
177  const ROL::Ptr<const StepState<Real> > getStepState(void) const {
178  return state_;
179  }
180 
183  void reset(const Real searchSize = 1.0) {
184  state_->reset(searchSize);
185  }
186 
187  // struct StepState (scalars, vectors) map?
188 
189  // getState
190 
191  // setState
192 
193 }; // class Step
194 
195 } // namespace ROL
196 
197 #endif
Provides the interface to evaluate objective functions.
virtual void update(Vector< Real > &x, const Vector< Real > &s, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Update step, if successful.
Definition: ROL_Step.hpp:117
virtual void initialize(Vector< Real > &x, const Vector< Real > &g, Vector< Real > &l, const Vector< Real > &c, Objective< Real > &obj, Constraint< Real > &con, AlgorithmState< Real > &algo_state)
Initialize step with equality constraint.
Definition: ROL_Step.hpp:95
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual std::string printHeader(void) const
Print iterate header.
Definition: ROL_Step.hpp:159
virtual void compute(Vector< Real > &s, const Vector< Real > &x, const Vector< Real > &l, Objective< Real > &obj, Constraint< Real > &con, AlgorithmState< Real > &algo_state)
Compute step (equality constraints).
Definition: ROL_Step.hpp:125
virtual ~Step()
Definition: ROL_Step.hpp:45
bool isActivated(void) const
Check if bounds are on.
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
Provides the interface to compute optimization steps.
Definition: ROL_Step.hpp:34
Contains definitions of custom data types in ROL.
virtual 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.
Definition: ROL_Step.hpp:62
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
virtual void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
virtual void compute(Vector< Real > &s, const Vector< Real > &x, const Vector< Real > &l, Objective< Real > &obj, Constraint< Real > &con, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step (equality constraints).
Definition: ROL_Step.hpp:141
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
virtual std::string print(AlgorithmState< Real > &algo_state, bool printHeader=false) const
Print iterate status.
Definition: ROL_Step.hpp:171
State for algorithm class. Will be used for restarts.
Definition: ROL_Types.hpp:109
Step(void)
Definition: ROL_Step.hpp:47
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
virtual void update(Vector< Real > &x, Vector< Real > &l, const Vector< Real > &s, Objective< Real > &obj, Constraint< Real > &con, AlgorithmState< Real > &algo_state)
Update step, if successful (equality constraints).
Definition: ROL_Step.hpp:133
void reset(const Real searchSize=1.0)
Get state for step object.
Definition: ROL_Step.hpp:183
ROL::Ptr< StepState< Real > > getState(void)
Definition: ROL_Step.hpp:39
ROL::Ptr< StepState< Real > > state_
Definition: ROL_Step.hpp:36
virtual void project(Vector< Real > &x)
Project optimization variables onto the bounds.
Provides the interface to apply upper and lower bound constraints.
virtual void compute(Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Compute step.
Definition: ROL_Step.hpp:109
virtual void initialize(Vector< Real > &x, const Vector< Real > &g, Vector< Real > &l, const Vector< Real > &c, Objective< Real > &obj, Constraint< Real > &con, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Initialize step with equality constraint.
Definition: ROL_Step.hpp:102
virtual void update(Vector< Real > &x, Vector< Real > &l, const Vector< Real > &s, Objective< Real > &obj, Constraint< Real > &con, BoundConstraint< Real > &bnd, AlgorithmState< Real > &algo_state)
Update step, if successful (equality constraints).
Definition: ROL_Step.hpp:150
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
Defines the general constraint operator interface.
virtual std::string printName(void) const
Print step name.
Definition: ROL_Step.hpp:165
const ROL::Ptr< const StepState< Real > > getStepState(void) const
Get state for step object.
Definition: ROL_Step.hpp:177