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"
56 #include "ROL_ValidParameters.hpp"
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 
116 // Uncomment to test for parameter inconsistencies
117 // Teuchos::RCP<const Teuchos::ParameterList> validParlist = getValidROLParameters();
118 // parlist.validateParametersAndSetDefaults(*validParlist);
119 
120  EStep els = StringToEStep(stepname);
121  TEUCHOS_TEST_FOR_EXCEPTION( !(isValidStep(els)),
122  std::invalid_argument,
123  "Invalid step name in algorithm constructor!");
125  StatusTestFactory<Real> statusTestFactory;
126  step_ = stepFactory.getStep(stepname,parlist);
127  status_ = statusTestFactory.getStatusTest(stepname,parlist);
128  state_ = Teuchos::rcp(new AlgorithmState<Real>);
129  printHeader_ = printHeader;
130  }
131 
135  virtual std::vector<std::string> run( Vector<Real> &x,
136  Objective<Real> &obj,
137  bool print = false,
138  std::ostream &outStream = std::cout,
139  bool printVectors = false,
140  std::ostream &vectorStream = std::cout ) {
142  con.deactivate();
143  return run(x,x.dual(),obj,con,print,outStream,printVectors,vectorStream);
144  }
145 
150  virtual std::vector<std::string> run( Vector<Real> &x,
151  const Vector<Real> &g,
152  Objective<Real> &obj,
153  bool print = false,
154  std::ostream &outStream = std::cout,
155  bool printVectors = false,
156  std::ostream &vectorStream = std::cout ) {
158  con.deactivate();
159  return run(x,g,obj,con,print,outStream,printVectors,vectorStream);
160  }
161 
165  virtual std::vector<std::string> run( Vector<Real> &x,
166  Objective<Real> &obj,
168  bool print = false,
169  std::ostream &outStream = std::cout,
170  bool printVectors = false,
171  std::ostream &vectorStream = std::cout ) {
172  return run(x,x.dual(),obj,con,print,outStream,printVectors,vectorStream);
173  }
174 
179  virtual std::vector<std::string> run( Vector<Real> &x,
180  const Vector<Real> &g,
181  Objective<Real> &obj,
183  bool print = false,
184  std::ostream &outStream = std::cout,
185  bool printVectors = false,
186  std::ostream &vectorStream = std::cout ) {
187  if(printVectors) {
188  x.print(vectorStream);
189  }
190 
191  std::vector<std::string> output;
192 
193  // Initialize Current Iterate Container
194  if ( state_->iterateVec == Teuchos::null ) {
195  state_->iterateVec = x.clone();
196  }
197  state_->iterateVec->set(x);
198 
199  // Initialize Step Container
200  Teuchos::RCP<Vector<Real> > s = x.clone();
201 
202  // Initialize Step
203  step_->initialize(x, g, obj, con, *state_);
204  output.push_back(step_->print(*state_,true));
205  if ( print ) {
206  outStream << step_->print(*state_,true);
207  }
208 
209  // Initialize Minimum Value and Vector
210  if ( state_->minIterVec == Teuchos::null ) {
211  state_->minIterVec = x.clone();
212  }
213  state_->minIterVec->set(x);
214  state_->minIter = state_->iter;
215  state_->minValue = state_->value;
216 
217  // Run Algorithm
218  while (status_->check(*state_)) {
219  step_->compute(*s, x, obj, con, *state_);
220  step_->update(x, *s, obj, con, *state_);
221 
222  if( printVectors ) {
223  x.print(vectorStream);
224  }
225 
226  // Store Minimal Value and Vector
227  if ( state_->minValue > state_->value ) {
228  state_->minIterVec->set(*(state_->iterateVec));
229  state_->minValue = state_->value;
230  state_->minIter = state_->iter;
231  }
232  // Update Output
233  output.push_back(step_->print(*state_,printHeader_));
234  if ( print ) {
235  outStream << step_->print(*state_,printHeader_);
236  }
237  }
238  return output;
239  }
240 
241 
245  virtual std::vector<std::string> run( Vector<Real> &x,
246  Vector<Real> &l,
247  Objective<Real> &obj,
249  bool print = false,
250  std::ostream &outStream = std::cout,
251  bool printVectors = false,
252  std::ostream &vectorStream = std::cout ) {
253 
254  return run(x, x.dual(), l, l.dual(), obj, con, print, outStream, printVectors, vectorStream);
255 
256  }
257 
258 
263  virtual std::vector<std::string> run( Vector<Real> &x,
264  const Vector<Real> &g,
265  Vector<Real> &l,
266  const Vector<Real> &c,
267  Objective<Real> &obj,
269  bool print = false,
270  std::ostream &outStream = std::cout,
271  bool printVectors = false,
272  std::ostream &vectorStream = std::cout ) {
273  if( printVectors ) {
274  x.print(vectorStream);
275  }
276 
277  std::vector<std::string> output;
278 
279  // Initialize Current Iterate Container
280  if ( state_->iterateVec == Teuchos::null ) {
281  state_->iterateVec = x.clone();
282  }
283  state_->iterateVec->set(x);
284 
285  // Initialize Current Lagrange Multiplier Container
286  if ( state_->lagmultVec == Teuchos::null ) {
287  state_->lagmultVec = l.clone();
288  }
289  state_->lagmultVec->set(l);
290 
291  // Initialize Step Container
292  Teuchos::RCP<Vector<Real> > s = x.clone();
293 
294  // Initialize Step
295  step_->initialize(x, g, l, c, obj, con, *state_);
296  output.push_back(step_->print(*state_,true));
297  if ( print ) {
298  outStream << step_->print(*state_,true);
299  }
300 
301  // Initialize Minimum Value and Vector
302  if ( state_->minIterVec == Teuchos::null ) {
303  state_->minIterVec = x.clone();
304  }
305  state_->minIterVec->set(x);
306  state_->minIter = state_->iter;
307  state_->minValue = state_->value;
308 
309  // Run Algorithm
310  while (status_->check(*state_)) {
311  step_->compute(*s, x, l, obj, con, *state_);
312  step_->update(x, l, *s, obj, con, *state_);
313 
314  if( printVectors ) {
315  x.print(vectorStream);
316  }
317 
318  output.push_back(step_->print(*state_,printHeader_));
319  if ( print ) {
320  outStream << step_->print(*state_,printHeader_);
321  }
322  }
323  return output;
324  }
325 
329  virtual std::vector<std::string> run( Vector<Real> &x,
330  Vector<Real> &l,
331  Objective<Real> &obj,
334  bool print = false,
335  std::ostream &outStream = std::cout,
336  bool printVectors = false,
337  std::ostream &vectorStream = std::cout) {
338  return run(x,x.dual(),l,l.dual(),obj,con,bnd,print,outStream,printVectors,vectorStream);
339  }
340 
345  virtual std::vector<std::string> run( Vector<Real> &x,
346  const Vector<Real> &g,
347  Vector<Real> &l,
348  const Vector<Real> &c,
349  Objective<Real> &obj,
352  bool print = false,
353  std::ostream &outStream = std::cout,
354  bool printVectors = false,
355  std::ostream &vectorStream = std::cout ) {
356  if(printVectors) {
357  x.print(vectorStream);
358  }
359 
360  std::vector<std::string> output;
361 
362  // Initialize Current Iterate Container
363  if ( state_->iterateVec == Teuchos::null ) {
364  state_->iterateVec = x.clone();
365  }
366  state_->iterateVec->set(x);
367 
368  // Initialize Current Lagrange Multiplier Container
369  if ( state_->lagmultVec == Teuchos::null ) {
370  state_->lagmultVec = l.clone();
371  }
372  state_->lagmultVec->set(l);
373 
374  // Initialize Step Container
375  Teuchos::RCP<Vector<Real> > s = x.clone();
376 
377  // Initialize Step
378  step_->initialize(x, g, l, c, obj, con, bnd, *state_);
379  output.push_back(step_->print(*state_,true));
380  if ( print ) {
381  outStream << step_->print(*state_,true);
382  }
383 
384  // Initialize Minimum Value and Vector
385  if ( state_->minIterVec == Teuchos::null ) {
386  state_->minIterVec = x.clone();
387  }
388  state_->minIterVec->set(x);
389  state_->minIter = state_->iter;
390  state_->minValue = state_->value;
391 
392  // Run Algorithm
393  while (status_->check(*state_)) {
394  step_->compute(*s, x, l, obj, con, bnd, *state_);
395  step_->update(x, l, *s, obj, con, bnd, *state_);
396  if( printVectors ) {
397  x.print(vectorStream);
398  }
399  output.push_back(step_->print(*state_,printHeader_));
400  if ( print ) {
401  outStream << step_->print(*state_,printHeader_);
402  }
403  }
404  return output;
405  }
406 
409  virtual std::vector<std::string> run( OptimizationProblem<Real> &opt,
410  bool print = false,
411  std::ostream &outStream = std::cout ) {
412  // Get components of optimization problem
413  Teuchos::RCP<Objective<Real> > obj = opt.getObjective();
414  Teuchos::RCP<Vector<Real> > x = opt.getSolutionVector();
415  Teuchos::RCP<BoundConstraint<Real> > bnd = opt.getBoundConstraint();
416  Teuchos::RCP<EqualityConstraint<Real> > con = opt.getEqualityConstraint();
417  Teuchos::RCP<Vector<Real> > l = opt.getMultiplierVector();
418 
419  // Call appropriate run function
420  if ( con == Teuchos::null ) {
421  if ( bnd == Teuchos::null ) {
422  return run(*x,*obj,print,outStream);
423  }
424  else {
425  return run(*x,*obj,*bnd,print,outStream);
426  }
427  }
428  else {
429  if ( bnd == Teuchos::null ) {
430  return run(*x,*l,*obj,*con,print,outStream);
431  }
432  else {
433  return run(*x,*l,*obj,*con,*bnd,print,outStream);
434  }
435  }
436  }
437 
438  std::string getIterHeader(void) {
439  return step_->printHeader();
440  }
441 
442  std::string getIterInfo(bool withHeader = false) {
443  return step_->print(*state_,withHeader);
444  }
445 
446  Teuchos::RCP<const AlgorithmState<Real> > getState(void) const {
447  return state_;
448  }
449 
450  void reset(void) {
451  state_ = Teuchos::rcp(new AlgorithmState<Real>);
452  }
453 
454 
455 
456 
457 
458 
459 }; // class Algorithm
460 
461 
462 } // namespace ROL
463 
464 #endif
Provides the interface to evaluate objective functions.
void reset(void)
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, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on equality constrained problems (Type-E). This general interface supports the use of d...
EStep StringToEStep(std::string s)
Definition: ROL_Types.hpp:307
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, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on equality constrained problems (Type-E). This is the primary Type-E interface...
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...
Provides the interface to compute optimization steps.
Definition: ROL_Step.hpp:69
Teuchos::RCP< Step< Real > > getStep(const std::string &type, Teuchos::ParameterList &parlist) const
Contains definitions of custom data types in ROL.
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
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:76
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This general interface supports the use of dual opt...
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
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:215
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, 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, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on equality and bound constrained problems (Type-EB). This general interface supports t...
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.
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &con, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on bound constrained problems (Type-B). This is the primary Type-B interface.
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:276
Teuchos::RCP< StatusTest< Real > > status_
virtual ~Algorithm()
Teuchos::RCP< EqualityConstraint< Real > > getEqualityConstraint(void)
Teuchos::RCP< Vector< Real > > getSolutionVector(void)
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, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on bound constrained problems (Type-B). This general interface supports the use of dual...
Teuchos::RCP< StatusTest< Real > > getStatusTest(const std::string step, Teuchos::ParameterList &parlist)
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, Vector< Real > &l, Objective< Real > &obj, EqualityConstraint< Real > &con, BoundConstraint< Real > &bnd, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on equality and bound constrained problems (Type-EB). This is the primary Type-EB inter...
void deactivate(void)
Turn off bounds.
EStep
Enumeration of step types.
Definition: ROL_Types.hpp:201
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.
virtual void print(std::ostream &outStream) const
Definition: ROL_Vector.hpp:234
std::string getIterHeader(void)