ROL
ROL_Step.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_STEP_H
45 #define ROL_STEP_H
46 
47 #include "ROL_Vector.hpp"
48 #include "ROL_Objective.hpp"
49 #include "ROL_BoundConstraint.hpp"
50 #include "ROL_Constraint.hpp"
52 #include "ROL_Types.hpp"
53 
54 #include "ROL_ParameterList.hpp"
55 
62 namespace ROL {
63 
64 // We need a forward declaration here, because some steps are algorithms.
65 template<class Real>
66 class Algorithm;
67 
68 template <class Real>
69 class Step {
70 private:
71  ROL::Ptr<StepState<Real> > state_;
72 
73 protected:
74  ROL::Ptr<StepState<Real> > getState(void) {
75  return state_;
76  }
77 
78 public:
79 
80  virtual ~Step() {}
81 
82  Step(void) {
83  state_ = ROL::makePtr<StepState<Real>>();
84  }
85 
86 
89  virtual void initialize( Vector<Real> &x, const Vector<Real> &g,
91  AlgorithmState<Real> &algo_state ) {
92  initialize(x,x,g,obj,con,algo_state);
93  }
94 
97  virtual void initialize( Vector<Real> &x, const Vector<Real> &s, const Vector<Real> &g,
99  AlgorithmState<Real> &algo_state ) {
100  Real tol = std::sqrt(ROL_EPSILON<Real>()), one(1), zero(0);
101  // Initialize state descent direction and gradient storage
102  state_->descentVec = s.clone();
103  state_->gradientVec = g.clone();
104  state_->searchSize = zero;
105  // Project x onto constraint set
106  if ( con.isActivated() ) {
107  con.project(x);
108  }
109  // Update objective function, get value, and get gradient
110  obj.update(x,true,algo_state.iter);
111  algo_state.value = obj.value(x,tol);
112  algo_state.nfval++;
113  obj.gradient(*(state_->gradientVec),x,tol);
114  algo_state.ngrad++;
115  if ( con.isActivated() ) {
116  ROL::Ptr<Vector<Real> > xnew = x.clone();
117  xnew->set(x);
118  xnew->axpy(-one,(Step<Real>::state_->gradientVec)->dual());
119  con.project(*xnew);
120  xnew->axpy(-one,x);
121  algo_state.gnorm = xnew->norm();
122  }
123  else {
124  algo_state.gnorm = (state_->gradientVec)->norm();
125  }
126  }
127 
130  virtual void initialize( Vector<Real> &x, const Vector<Real> &g, Vector<Real> &l, const Vector<Real> &c,
132  AlgorithmState<Real> &algo_state ) {
133  }
134 
137  virtual void initialize( Vector<Real> &x, const Vector<Real> &g, Vector<Real> &l, const Vector<Real> &c,
139  AlgorithmState<Real> &algo_state ) {
140  }
141 
144  virtual void compute( Vector<Real> &s, const Vector<Real> &x,
146  AlgorithmState<Real> &algo_state ) {
147  throw Exception::NotImplemented(">>> ROL::Step::compute(s,x,obj,bnd,algo_state) is not implemented!");
148  }
149 
152  virtual void update( Vector<Real> &x, const Vector<Real> &s,
154  AlgorithmState<Real> &algo_state ) {
155  throw Exception::NotImplemented(">>> ROL::Step::update(x,s,obj,bnd,algo_state) is not implemented!");
156  }
157 
160  virtual void compute( Vector<Real> &s, const Vector<Real> &x, const Vector<Real> &l,
162  AlgorithmState<Real> &algo_state ) {
163  throw Exception::NotImplemented(">>> ROL::Step::compute(s,x,l,obj,con,algo_state) is not implemented!");
164  }
165 
168  virtual void update( Vector<Real> &x, Vector<Real> &l, const Vector<Real> &s,
170  AlgorithmState<Real> &algo_state ) {
171  throw Exception::NotImplemented(">>> ROL::Step::update(x,s,l,obj,bnd,con,algo_state) is not implemented!");
172  }
173 
176  virtual void compute( Vector<Real> &s, const Vector<Real> &x, const Vector<Real> &l,
179  AlgorithmState<Real> &algo_state ) {
180  throw Exception::NotImplemented(">>> ROL::Step::compute(s,x,l,obj,bnd,con,algo_state) is not implemented!");
181  }
182 
185  virtual void update( Vector<Real> &x, Vector<Real> &l, const Vector<Real> &s,
188  AlgorithmState<Real> &algo_state ) {
189  throw Exception::NotImplemented(">>> ROL::Step::update(x,s,l,obj,bnd,con,algo_state) is not implemented!");
190  }
191 
192 
193 
194  // Methods using an Optimization problem
195 
197 
198 
199 
200  ROL::Ptr<Objective<Real> > obj = opt.getObjective();
201  ROL::Ptr<Vector<Real> > x = opt.getSolutionVector();
202  ROL::Ptr<BoundConstraint<Real> > bnd = opt.getBoundConstraint();
203  ROL::Ptr<Constraint<Real> > con = opt.getConstraint();
204  ROL::Ptr<Vector<Real> > l = opt.getMultiplierVector();
205 
206  if( con == ROL::nullPtr ) { // has no equality constraint
207  if( bnd == ROL::nullPtr ) { // has no bound constraint or inactive
208  bnd = ROL::makePtr<BoundConstraint<Real>>();
209  bnd->deactivate();
210  }
211  initialize(*x, x->dual(), *obj, *bnd, algo_state);
212  }
213  else { // has equality constraint
214 
215  if( bnd == ROL::nullPtr ) {
216  initialize(*x,x->dual(),*l,l->dual(),*obj,*con,algo_state );
217  }
218  initialize(*x,x->dual(),*l,l->dual(),*obj,*con,*bnd,algo_state);
219  }
220  }
221 
223 
224 
225  ROL::Ptr<Objective<Real> > obj = opt.getObjective();
226  ROL::Ptr<Vector<Real> > x = opt.getSolutionVector();
227  ROL::Ptr<BoundConstraint<Real> > bnd = opt.getBoundConstraint();
228  ROL::Ptr<Constraint<Real> > con = opt.getConstraint();
229  ROL::Ptr<Vector<Real> > l = opt.getMultiplierVector();
230 
231  if( con == ROL::nullPtr ) { // has no equality constraint
232  if( bnd == ROL::nullPtr ) { // has no bound constraint
233  bnd = ROL::makePtr<BoundConstraint<Real>>();
234  bnd->deactivate();
235  }
236  compute(s,*x, *obj, *bnd, algo_state);
237  }
238  else { // has equality constraint
239  if( bnd == ROL::nullPtr ) {
240  compute(s,*x,*l,*obj,*con,algo_state);
241  }
242  compute(s,*x,*l,*obj,*con,*bnd,algo_state);
243  }
244 
245  }
246 
248 
249 
250  ROL::Ptr<Objective<Real> > obj = opt.getObjective();
251  ROL::Ptr<Vector<Real> > x = opt.getSolutionVector();
252  ROL::Ptr<BoundConstraint<Real> > bnd = opt.getBoundConstraint();
253  ROL::Ptr<Constraint<Real> > con = opt.getConstraint();
254  ROL::Ptr<Vector<Real> > l = opt.getMultiplierVector();
255 
256  if( con == ROL::nullPtr ) { // has no equality constraint
257  if( bnd == ROL::nullPtr ) { // has no bound constraint
258  bnd = ROL::makePtr<BoundConstraint<Real>>();
259  bnd->deactivate();
260  }
261  update(*x, s, *obj, *bnd, algo_state);
262  }
263  else { // has equality constraint
264  if( bnd == ROL::nullPtr ) {
265  update(*x,*l,s,*obj,*con,algo_state);
266  }
267  update(*x,*l,s,*obj,*con,*bnd,algo_state);
268  }
269 
270  }
271 
272 
275  virtual std::string printHeader( void ) const {
276  throw Exception::NotImplemented(">>> ROL::Step::printHeader() is not implemented!");
277  }
278 
281  virtual std::string printName( void ) const {
282  throw Exception::NotImplemented(">>> ROL::Step::printName() is not implemented!");
283  }
284 
287  virtual std::string print( AlgorithmState<Real> &algo_state, bool printHeader = false ) const {
288  throw Exception::NotImplemented(">>> ROL::Step::print(algo_state,printHeader) is not implemented!");
289  }
290 
293  const ROL::Ptr<const StepState<Real> > getStepState(void) const {
294  return state_;
295  }
296 
299  void reset(const Real searchSize = 1.0) {
300  state_->reset(searchSize);
301  }
302 
303  // struct StepState (scalars, vectors) map?
304 
305  // getState
306 
307  // setState
308 
309 }; // class Step
310 
311 } // namespace ROL
312 
313 #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:152
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:130
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:275
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:160
virtual ~Step()
Definition: ROL_Step.hpp:80
void compute(Vector< Real > &s, OptimizationProblem< Real > &opt, AlgorithmState< Real > &algo_state)
Definition: ROL_Step.hpp:222
bool isActivated(void) const
Check if bounds are on.
void initialize(OptimizationProblem< Real > &opt, AlgorithmState< Real > &algo_state)
Definition: ROL_Step.hpp:196
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
Provides the interface to compute optimization steps.
Definition: ROL_Step.hpp:69
Contains definitions of custom data types in ROL.
virtual Ptr< Objective< Real > > getObjective(void)
virtual Ptr< BoundConstraint< Real > > getBoundConstraint(void)
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:97
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
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:176
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:287
void update(OptimizationProblem< Real > &opt, const Vector< Real > &s, AlgorithmState< Real > &algo_state)
Definition: ROL_Step.hpp:247
State for algorithm class. Will be used for restarts.
Definition: ROL_Types.hpp:143
Step(void)
Definition: ROL_Step.hpp:82
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:168
void reset(const Real searchSize=1.0)
Get state for step object.
Definition: ROL_Step.hpp:299
ROL::Ptr< StepState< Real > > getState(void)
Definition: ROL_Step.hpp:74
ROL::Ptr< StepState< Real > > state_
Definition: ROL_Step.hpp:71
virtual Ptr< Vector< Real > > getSolutionVector(void)
virtual Ptr< Vector< Real > > getMultiplierVector(void)
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:144
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:137
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:185
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
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
Defines the general constraint operator interface.
virtual void project(Vector< Real > &x)
Project optimization variables onto the bounds.
virtual std::string printName(void) const
Print step name.
Definition: ROL_Step.hpp:281
virtual Ptr< Constraint< Real > > getConstraint(void)
const ROL::Ptr< const StepState< Real > > getStepState(void) const
Get state for step object.
Definition: ROL_Step.hpp:293