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 
57 
63 namespace ROL {
64 
65 template<class Real>
67 
68 template<class Real>
70 
71 template <class Real>
72 class Algorithm {
73 private:
74  Teuchos::RCP<Step<Real> > step_;
75  Teuchos::RCP<StatusTest<Real> > status_;
76  Teuchos::RCP<AlgorithmState<Real> > state_;
77 
79 
80 public:
81 
82  virtual ~Algorithm() {}
83 
86  Algorithm( const Teuchos::RCP<Step<Real> > & step,
87  const Teuchos::RCP<StatusTest<Real> > & status,
88  bool printHeader = false ) {
89  step_ = step;
90  status_ = status;
91  state_ = Teuchos::rcp(new AlgorithmState<Real>);
92  printHeader_ = printHeader;
93  }
94 
98  Algorithm( const Teuchos::RCP<Step<Real> > & step,
99  const Teuchos::RCP<StatusTest<Real> > & status,
100  const Teuchos::RCP<AlgorithmState<Real> > & state,
101  bool printHeader = false ) {
102  step_ = step;
103  status_ = status;
104  state_ = state;
105  printHeader_ = printHeader;
106  }
107 
112  Algorithm( const std::string &stepname,
113  Teuchos::ParameterList &parlist,
114  bool printHeader = false) {
115  EStep els = StringToEStep(stepname);
116  TEUCHOS_TEST_FOR_EXCEPTION( !(isValidStep(els)),
117  std::invalid_argument,
118  "Invalid step name in algorithm constructor!");
120  StatusTestFactory<Real> statusTestFactory;
121  step_ = stepFactory.getStep(stepname,parlist);
122  status_ = statusTestFactory.getStatusTest(stepname,parlist);
123  state_ = Teuchos::rcp(new AlgorithmState<Real>);
124  printHeader_ = printHeader;
125  }
126 
130  virtual std::vector<std::string> run( Vector<Real> &x,
131  Objective<Real> &obj,
132  bool print = false,
133  std::ostream &outStream = std::cout ) {
135  con.deactivate();
136  return run(x,x.dual(),obj,con,print,outStream);
137  }
138 
143  virtual std::vector<std::string> run( Vector<Real> &x,
144  const Vector<Real> &g,
145  Objective<Real> &obj,
146  bool print = false,
147  std::ostream &outStream = std::cout ) {
149  con.deactivate();
150  return run(x,g,obj,con,print,outStream);
151  }
152 
156  virtual std::vector<std::string> run( Vector<Real> &x,
157  Objective<Real> &obj,
159  bool print = false,
160  std::ostream &outStream = std::cout ) {
161  return run(x,x.dual(),obj,con,print,outStream);
162  }
163 
168  virtual std::vector<std::string> run( Vector<Real> &x,
169  const Vector<Real> &g,
170  Objective<Real> &obj,
172  bool print = false,
173  std::ostream &outStream = std::cout ) {
174  std::vector<std::string> output;
175 
176  // Initialize Current Iterate Container
177  if ( state_->iterateVec == Teuchos::null ) {
178  state_->iterateVec = x.clone();
179  }
180  state_->iterateVec->set(x);
181 
182  // Initialize Step Container
183  Teuchos::RCP<Vector<Real> > s = x.clone();
184 
185  // Initialize Step
186  step_->initialize(x, g, obj, con, *state_);
187  output.push_back(step_->print(*state_,true));
188  if ( print ) {
189  outStream << step_->print(*state_,true);
190  }
191 
192  // Initialize Minimum Value and Vector
193  if ( state_->minIterVec == Teuchos::null ) {
194  state_->minIterVec = x.clone();
195  }
196  state_->minIterVec->set(x);
197  state_->minIter = state_->iter;
198  state_->minValue = state_->value;
199 
200  // Run Algorithm
201  while (status_->check(*state_)) {
202  step_->compute(*s, x, obj, con, *state_);
203  step_->update(x, *s, obj, con, *state_);
204  // Store Minimal Value and Vector
205  if ( state_->minValue > state_->value ) {
206  state_->minIterVec->set(*(state_->iterateVec));
207  state_->minValue = state_->value;
208  state_->minIter = state_->iter;
209  }
210  // Update Output
211  output.push_back(step_->print(*state_,printHeader_));
212  if ( print ) {
213  outStream << step_->print(*state_,printHeader_);
214  }
215  }
216  return output;
217  }
218 
219 
223  virtual std::vector<std::string> run( Vector<Real> &x,
224  Vector<Real> &l,
225  Objective<Real> &obj,
227  bool print = false,
228  std::ostream &outStream = std::cout ) {
229 
230  return run(x, x.dual(), l, l.dual(), obj, con, print, outStream);
231 
232  }
233 
234 
239  virtual std::vector<std::string> run( Vector<Real> &x,
240  const Vector<Real> &g,
241  Vector<Real> &l,
242  const Vector<Real> &c,
243  Objective<Real> &obj,
245  bool print = false,
246  std::ostream &outStream = std::cout ) {
247  std::vector<std::string> output;
248 
249  // Initialize Current Iterate Container
250  if ( state_->iterateVec == Teuchos::null ) {
251  state_->iterateVec = x.clone();
252  }
253  state_->iterateVec->set(x);
254 
255  // Initialize Current Lagrange Multiplier Container
256  if ( state_->lagmultVec == Teuchos::null ) {
257  state_->lagmultVec = l.clone();
258  }
259  state_->lagmultVec->set(l);
260 
261  // Initialize Step Container
262  Teuchos::RCP<Vector<Real> > s = x.clone();
263 
264  // Initialize Step
265  step_->initialize(x, g, l, c, obj, con, *state_);
266  output.push_back(step_->print(*state_,true));
267  if ( print ) {
268  outStream << step_->print(*state_,true);
269  }
270 
271  // Initialize Minimum Value and Vector
272  if ( state_->minIterVec == Teuchos::null ) {
273  state_->minIterVec = x.clone();
274  }
275  state_->minIterVec->set(x);
276  state_->minIter = state_->iter;
277  state_->minValue = state_->value;
278 
279  // Run Algorithm
280  while (status_->check(*state_)) {
281  step_->compute(*s, x, l, obj, con, *state_);
282  step_->update(x, l, *s, obj, con, *state_);
283  output.push_back(step_->print(*state_,printHeader_));
284  if ( print ) {
285  outStream << step_->print(*state_,printHeader_);
286  }
287  }
288  return output;
289  }
290 
294  virtual std::vector<std::string> run( Vector<Real> &x,
295  Vector<Real> &l,
296  Objective<Real> &obj,
299  bool print = false,
300  std::ostream &outStream = std::cout ) {
301  return run(x,x.dual(),l,l.dual(),obj,con,bnd,print,outStream);
302  }
303 
308  virtual std::vector<std::string> run( Vector<Real> &x,
309  const Vector<Real> &g,
310  Vector<Real> &l,
311  const Vector<Real> &c,
312  Objective<Real> &obj,
315  bool print = false,
316  std::ostream &outStream = std::cout ) {
317  std::vector<std::string> output;
318 
319  // Initialize Current Iterate Container
320  if ( state_->iterateVec == Teuchos::null ) {
321  state_->iterateVec = x.clone();
322  }
323  state_->iterateVec->set(x);
324 
325  // Initialize Current Lagrange Multiplier Container
326  if ( state_->lagmultVec == Teuchos::null ) {
327  state_->lagmultVec = l.clone();
328  }
329  state_->lagmultVec->set(l);
330 
331  // Initialize Step Container
332  Teuchos::RCP<Vector<Real> > s = x.clone();
333 
334  // Initialize Step
335  step_->initialize(x, g, l, c, obj, con, bnd, *state_);
336  output.push_back(step_->print(*state_,true));
337  if ( print ) {
338  outStream << step_->print(*state_,true);
339  }
340 
341  // Initialize Minimum Value and Vector
342  if ( state_->minIterVec == Teuchos::null ) {
343  state_->minIterVec = x.clone();
344  }
345  state_->minIterVec->set(x);
346  state_->minIter = state_->iter;
347  state_->minValue = state_->value;
348 
349  // Run Algorithm
350  while (status_->check(*state_)) {
351  step_->compute(*s, x, l, obj, con, bnd, *state_);
352  step_->update(x, l, *s, obj, con, bnd, *state_);
353  output.push_back(step_->print(*state_,printHeader_));
354  if ( print ) {
355  outStream << step_->print(*state_,printHeader_);
356  }
357  }
358  return output;
359  }
360 
363  virtual std::vector<std::string> run( OptimizationProblem<Real> &opt,
364  bool print = false,
365  std::ostream &outStream = std::cout ) {
366  // Get components of optimization problem
367  Teuchos::RCP<Objective<Real> > obj = opt.getObjective();
368  Teuchos::RCP<Vector<Real> > x = opt.getSolutionVector();
369  Teuchos::RCP<BoundConstraint<Real> > bnd = opt.getBoundConstraint();
370  Teuchos::RCP<EqualityConstraint<Real> > con = opt.getEqualityConstraint();
371  Teuchos::RCP<Vector<Real> > l = opt.getMultiplierVector();
372  // Call appropriate run function
373  if ( con == Teuchos::null ) {
374  if ( bnd == Teuchos::null ) {
375  return run(*x,*obj,print,outStream);
376  }
377  else {
378  return run(*x,*obj,*bnd,print,outStream);
379  }
380  }
381  else {
382  if ( bnd == Teuchos::null ) {
383  return run(*x,*l,*obj,*con,print,outStream);
384  }
385  else {
386  return run(*x,*l,*obj,*con,*bnd,print,outStream);
387  }
388  }
389  }
390 
391  std::string getIterHeader(void) {
392  return step_->printHeader();
393  }
394 
395  std::string getIterInfo(bool withHeader = false) {
396  return step_->print(*state_,withHeader);
397  }
398 
399  Teuchos::RCP<const AlgorithmState<Real> > getState(void) const {
400  return state_;
401  }
402 
403  void reset(void) {
404  state_ = Teuchos::rcp(new AlgorithmState<Real>);
405  }
406 
407 }; // class Algorithm
408 
409 
410 } // namespace ROL
411 
412 #endif
Provides the interface to evaluate objective functions.
void reset(void)
EStep StringToEStep(std::string s)
Definition: ROL_Types.hpp:257
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:69
Contains definitions of custom data types in ROL.
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_
Teuchos::RCP< BoundConstraint< Real > > getBoundConstraint(void)
State for algorithm class. Will be used for restarts.
Definition: ROL_Types.hpp:91
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...
Teuchos::RCP< const AlgorithmState< Real > > getState(void) const
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
Defines the equality constraint operator interface.
virtual std::vector< std::string > run(OptimizationProblem< Real > &opt, bool print=false, std::ostream &outStream=std::cout)
Run algorithm using a ROL::OptimizationProblem.
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.
Teuchos::RCP< Vector< Real > > getMultiplierVector(void)
Provides the interface to apply upper and lower bound constraints.
int isValidStep(EStep ls)
Verifies validity of a TrustRegion enum.
Definition: ROL_Types.hpp:226
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< EqualityConstraint< Real > > getEqualityConstraint(void)
Teuchos::RCP< Vector< Real > > getSolutionVector(void)
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:192
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< Objective< Real > > getObjective(void)
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)