ROL
ROL_Algorithm.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_ALGORITHM_H
45 #define ROL_ALGORITHM_H
46 
47 #include "ROL_Types.hpp"
48 #include "ROL_Step.hpp"
49 #include "ROL_StepFactory.hpp"
50 #include "ROL_StatusTest.hpp"
52 #include "ROL_Objective.hpp"
53 #include "ROL_BoundConstraint.hpp"
55 
61 namespace ROL {
62 
63 template<class Real>
65 
66 template<class Real>
68 
69 template <class Real>
70 class Algorithm {
71 private:
72  Teuchos::RCP<Step<Real> > step_;
73  Teuchos::RCP<StatusTest<Real> > status_;
74  Teuchos::RCP<AlgorithmState<Real> > state_;
75 
77 
78 public:
79 
80  virtual ~Algorithm() {}
81 
84  Algorithm( const Teuchos::RCP<Step<Real> > & step,
85  const Teuchos::RCP<StatusTest<Real> > & status,
86  bool printHeader = false ) {
87  step_ = step;
88  status_ = status;
89  state_ = Teuchos::rcp(new AlgorithmState<Real>);
90  printHeader_ = printHeader;
91  }
92 
96  Algorithm( const Teuchos::RCP<Step<Real> > & step,
97  const Teuchos::RCP<StatusTest<Real> > & status,
98  const Teuchos::RCP<AlgorithmState<Real> > & state,
99  bool printHeader = false ) {
100  step_ = step;
101  status_ = status;
102  state_ = state;
103  printHeader_ = printHeader;
104  }
105 
110  Algorithm( const std::string &stepname,
111  Teuchos::ParameterList &parlist,
112  bool printHeader = false) {
113  EStep els = StringToEStep(stepname);
114  TEUCHOS_TEST_FOR_EXCEPTION( !(isValidStep(els)),
115  std::invalid_argument,
116  "Invalid step name in algorithm constructor!");
118  StatusTestFactory<Real> statusTestFactory;
119  step_ = stepFactory.getStep(stepname,parlist);
120  status_ = statusTestFactory.getStatusTest(stepname,parlist);
121  state_ = Teuchos::rcp(new AlgorithmState<Real>);
122  printHeader_ = printHeader;
123  }
124 
128  virtual std::vector<std::string> run( Vector<Real> &x,
129  Objective<Real> &obj,
130  bool print = false,
131  std::ostream &outStream = std::cout ) {
133  con.deactivate();
134  return run(x,x.dual(),obj,con,print,outStream);
135  }
136 
141  virtual std::vector<std::string> run( Vector<Real> &x,
142  const Vector<Real> &g,
143  Objective<Real> &obj,
144  bool print = false,
145  std::ostream &outStream = std::cout ) {
147  con.deactivate();
148  return run(x,g,obj,con,print,outStream);
149  }
150 
154  virtual std::vector<std::string> run( Vector<Real> &x,
155  Objective<Real> &obj,
157  bool print = false,
158  std::ostream &outStream = std::cout ) {
159  return run(x,x.dual(),obj,con,print,outStream);
160  }
161 
166  virtual std::vector<std::string> run( Vector<Real> &x,
167  const Vector<Real> &g,
168  Objective<Real> &obj,
170  bool print = false,
171  std::ostream &outStream = std::cout ) {
172  std::vector<std::string> output;
173 
174  // Initialize Current Iterate Container
175  if ( state_->iterateVec == Teuchos::null ) {
176  state_->iterateVec = x.clone();
177  }
178  state_->iterateVec->set(x);
179 
180  // Initialize Step Container
181  Teuchos::RCP<Vector<Real> > s = x.clone();
182 
183  // Initialize Step
184  step_->initialize(x, g, obj, con, *state_);
185  output.push_back(step_->print(*state_,true));
186  if ( print ) {
187  outStream << step_->print(*state_,true);
188  }
189 
190  // Initialize Minimum Value and Vector
191  if ( state_->minIterVec == Teuchos::null ) {
192  state_->minIterVec = x.clone();
193  }
194  state_->minIterVec->set(x);
195  state_->minIter = state_->iter;
196  state_->minValue = state_->value;
197 
198  // Run Algorithm
199  while (status_->check(*state_)) {
200  step_->compute(*s, x, obj, con, *state_);
201  step_->update(x, *s, obj, con, *state_);
202  // Store Minimal Value and Vector
203  if ( state_->minValue > state_->value ) {
204  state_->minIterVec->set(*(state_->iterateVec));
205  state_->minValue = state_->value;
206  state_->minIter = state_->iter;
207  }
208  // Update Output
209  output.push_back(step_->print(*state_,printHeader_));
210  if ( print ) {
211  outStream << step_->print(*state_,printHeader_);
212  }
213  }
214  return output;
215  }
216 
217 
221  virtual std::vector<std::string> run( Vector<Real> &x,
222  Vector<Real> &l,
223  Objective<Real> &obj,
225  bool print = false,
226  std::ostream &outStream = std::cout ) {
227 
228  return run(x, x.dual(), l, l.dual(), obj, con, print, outStream);
229 
230  }
231 
232 
237  virtual std::vector<std::string> run( Vector<Real> &x,
238  const Vector<Real> &g,
239  Vector<Real> &l,
240  const Vector<Real> &c,
241  Objective<Real> &obj,
243  bool print = false,
244  std::ostream &outStream = std::cout ) {
245  std::vector<std::string> output;
246 
247  // Initialize Current Iterate Container
248  if ( state_->iterateVec == Teuchos::null ) {
249  state_->iterateVec = x.clone();
250  }
251  state_->iterateVec->set(x);
252 
253  // Initialize Current Lagrange Multiplier Container
254  if ( state_->lagmultVec == Teuchos::null ) {
255  state_->lagmultVec = l.clone();
256  }
257  state_->lagmultVec->set(l);
258 
259  // Initialize Step Container
260  Teuchos::RCP<Vector<Real> > s = x.clone();
261 
262  // Initialize Step
263  step_->initialize(x, g, l, c, obj, con, *state_);
264  output.push_back(step_->print(*state_,true));
265  if ( print ) {
266  outStream << step_->print(*state_,true);
267  }
268 
269  // Initialize Minimum Value and Vector
270  if ( state_->minIterVec == Teuchos::null ) {
271  state_->minIterVec = x.clone();
272  }
273  state_->minIterVec->set(x);
274  state_->minIter = state_->iter;
275  state_->minValue = state_->value;
276 
277  // Run Algorithm
278  while (status_->check(*state_)) {
279  step_->compute(*s, x, l, obj, con, *state_);
280  step_->update(x, l, *s, obj, con, *state_);
281  output.push_back(step_->print(*state_,printHeader_));
282  if ( print ) {
283  outStream << step_->print(*state_,printHeader_);
284  }
285  }
286  return output;
287  }
288 
292  virtual std::vector<std::string> run( Vector<Real> &x,
293  Vector<Real> &l,
294  Objective<Real> &obj,
297  bool print = false,
298  std::ostream &outStream = std::cout ) {
299  return run(x,x.dual(),l,l.dual(),obj,con,bnd,print,outStream);
300  }
301 
306  virtual std::vector<std::string> run( Vector<Real> &x,
307  const Vector<Real> &g,
308  Vector<Real> &l,
309  const Vector<Real> &c,
310  Objective<Real> &obj,
313  bool print = false,
314  std::ostream &outStream = std::cout ) {
315  std::vector<std::string> output;
316 
317  // Initialize Current Iterate Container
318  if ( state_->iterateVec == Teuchos::null ) {
319  state_->iterateVec = x.clone();
320  }
321  state_->iterateVec->set(x);
322 
323  // Initialize Current Lagrange Multiplier Container
324  if ( state_->lagmultVec == Teuchos::null ) {
325  state_->lagmultVec = l.clone();
326  }
327  state_->lagmultVec->set(l);
328 
329  // Initialize Step Container
330  Teuchos::RCP<Vector<Real> > s = x.clone();
331 
332  // Initialize Step
333  step_->initialize(x, g, l, c, obj, con, bnd, *state_);
334  output.push_back(step_->print(*state_,true));
335  if ( print ) {
336  outStream << step_->print(*state_,true);
337  }
338 
339  // Initialize Minimum Value and Vector
340  if ( state_->minIterVec == Teuchos::null ) {
341  state_->minIterVec = x.clone();
342  }
343  state_->minIterVec->set(x);
344  state_->minIter = state_->iter;
345  state_->minValue = state_->value;
346 
347  // Run Algorithm
348  while (status_->check(*state_)) {
349  step_->compute(*s, x, l, obj, con, bnd, *state_);
350  step_->update(x, l, *s, obj, con, bnd, *state_);
351  output.push_back(step_->print(*state_,printHeader_));
352  if ( print ) {
353  outStream << step_->print(*state_,printHeader_);
354  }
355  }
356  return output;
357  }
358 
359  std::string getIterHeader(void) {
360  return step_->printHeader();
361  }
362 
363  std::string getIterInfo(bool withHeader = false) {
364  return step_->print(*state_,withHeader);
365  }
366 
367  Teuchos::RCP<const AlgorithmState<Real> > getState(void) const {
368  return state_;
369  }
370 
371 }; // class Algorithm
372 
373 
374 } // namespace ROL
375 
376 #endif
Provides the interface to evaluate objective functions.
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:213
EStep StringToEStep(std::string s)
Definition: ROL_Types.hpp:218
void stepFactory(Teuchos::ParameterList &parlist, Teuchos::RCP< ROL::Step< Real > > &step)
A minimalist step factory which specializes the Step Type depending on whether a Trust-Region or Line...
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &con, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on bound constrained problems (Type-B). This is the primary Type-B interface.
Provides the interface to compute optimization steps.
Definition: ROL_Step.hpp:67
Contains definitions of custom data types in ROL.
Teuchos::RCP< const AlgorithmState< Real > > getState(void) const
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Teuchos::RCP< AlgorithmState< Real > > state_
State for algorithm class. Will be used for restarts.
Definition: ROL_Types.hpp:77
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &con, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on bound constrained problems (Type-B). This general interface supports the use of dual...
Defines the equality constraint operator interface.
virtual std::vector< std::string > run(Vector< Real > &x, Vector< Real > &l, Objective< Real > &obj, EqualityConstraint< Real > &con, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on equality constrained problems (Type-E). This is the primary Type-E interface...
Provides an interface to run optimization algorithms.
Provides an interface to check status of optimization algorithms.
Algorithm(const Teuchos::RCP< Step< Real > > &step, const Teuchos::RCP< StatusTest< Real > > &status, const Teuchos::RCP< AlgorithmState< Real > > &state, bool printHeader=false)
Constructor, given a step, a status test, and a previously defined algorithm state.
Provides the interface to apply upper and lower bound constraints.
int isValidStep(EStep ls)
Verifies validity of a TrustRegion enum.
Definition: ROL_Types.hpp:188
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Vector< Real > &l, const Vector< Real > &c, Objective< Real > &obj, EqualityConstraint< Real > &con, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on equality constrained problems (Type-E). This general interface supports the use of d...
Teuchos::RCP< StatusTest< Real > > status_
virtual ~Algorithm()
Teuchos::RCP< StatusTest< Real > > getStatusTest(const std::string step, Teuchos::ParameterList &parlist)
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
Teuchos::RCP< Step< Real > > step_
Algorithm(const Teuchos::RCP< Step< Real > > &step, const Teuchos::RCP< StatusTest< Real > > &status, bool printHeader=false)
Constructor, given a step and a status test.
std::string getIterInfo(bool withHeader=false)
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Vector< Real > &l, const Vector< Real > &c, Objective< Real > &obj, EqualityConstraint< Real > &con, BoundConstraint< Real > &bnd, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on equality and bound constrained problems (Type-EB). This general interface supports t...
void deactivate(void)
Turn off bounds.
EStep
Enumeration of step types.
Definition: ROL_Types.hpp:156
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This general interface supports the use of dual opt...
virtual std::vector< std::string > run(Vector< Real > &x, Vector< Real > &l, Objective< Real > &obj, EqualityConstraint< Real > &con, BoundConstraint< Real > &bnd, bool print=false, std::ostream &outStream=std::cout)
Run algorithm on equality and bound constrained problems (Type-EB). This is the primary Type-EB inter...
Teuchos::RCP< Step< Real > > getStep(const std::string &type, Teuchos::ParameterList &parlist) const
Algorithm(const std::string &stepname, Teuchos::ParameterList &parlist, bool printHeader=false)
Constructor, given a string, for the step, and a parameter list of various options. The status test is determined based on the step string.
std::string getIterHeader(void)