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"
54 #include "ROL_Constraint.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  ROL::Ptr<Step<Real> > step_;
75  ROL::Ptr<StatusTest<Real> > status_;
76  ROL::Ptr<AlgorithmState<Real> > state_;
77 
79 
80 public:
81 
82  virtual ~Algorithm() {}
83 
86  Algorithm( const ROL::Ptr<Step<Real> > & step,
87  const ROL::Ptr<StatusTest<Real> > & status,
88  bool printHeader = false ) {
89  step_ = step;
90  status_ = status;
91  state_ = ROL::makePtr<AlgorithmState<Real>>();
92  printHeader_ = printHeader;
93  }
94 
98  Algorithm( const ROL::Ptr<Step<Real> > & step,
99  const ROL::Ptr<StatusTest<Real> > & status,
100  const ROL::Ptr<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  ROL::ParameterList &parlist,
114  bool printHeader = false) {
115 
116 // Uncomment to test for parameter inconsistencies
117 // ROL::Ptr<const ROL::ParameterList> validParlist = getValidROLParameters();
118 // parlist.validateParametersAndSetDefaults(*validParlist);
119 
120  EStep els = StringToEStep(stepname);
121  ROL_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_ = ROL::makePtr<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  bnd.deactivate();
143  return run(x,x.dual(),obj,bnd,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  bnd.deactivate();
159  return run(x,g,obj,bnd,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,bnd,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 == ROL::nullPtr ) {
195  state_->iterateVec = x.clone();
196  }
197  state_->iterateVec->set(x);
198 
199  // Initialize Step Container
200  ROL::Ptr<Vector<Real> > s = x.clone();
201 
202  // Initialize Step
203  step_->initialize(x, g, obj, bnd, *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 == ROL::nullPtr ) {
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, bnd, *state_);
220  step_->update(x, *s, obj, bnd, *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  std::stringstream hist;
239  hist << "Optimization Terminated with Status: ";
240  hist << EExitStatusToString(state_->statusFlag);
241  hist << "\n";
242  output.push_back(hist.str());
243  if ( print ) {
244  outStream << hist.str();
245  }
246  return output;
247  }
248 
249 
253  virtual std::vector<std::string> run( Vector<Real> &x,
254  Vector<Real> &l,
255  Objective<Real> &obj,
256  Constraint<Real> &con,
257  bool print = false,
258  std::ostream &outStream = std::cout,
259  bool printVectors = false,
260  std::ostream &vectorStream = std::cout ) {
261 
262  return run(x, x.dual(), l, l.dual(), obj, con, print, outStream, printVectors, vectorStream);
263 
264  }
265 
266 
271  virtual std::vector<std::string> run( Vector<Real> &x,
272  const Vector<Real> &g,
273  Vector<Real> &l,
274  const Vector<Real> &c,
275  Objective<Real> &obj,
276  Constraint<Real> &con,
277  bool print = false,
278  std::ostream &outStream = std::cout,
279  bool printVectors = false,
280  std::ostream &vectorStream = std::cout ) {
281  if( printVectors ) {
282  x.print(vectorStream);
283  }
284 
285  std::vector<std::string> output;
286 
287  // Initialize Current Iterate Container
288  if ( state_->iterateVec == ROL::nullPtr ) {
289  state_->iterateVec = x.clone();
290  }
291  state_->iterateVec->set(x);
292 
293  // Initialize Current Lagrange Multiplier Container
294  if ( state_->lagmultVec == ROL::nullPtr ) {
295  state_->lagmultVec = l.clone();
296  }
297  state_->lagmultVec->set(l);
298 
299  // Initialize Step Container
300  ROL::Ptr<Vector<Real> > s = x.clone();
301 
302  // Initialize Step
303  step_->initialize(x, g, l, c, obj, con, *state_);
304  output.push_back(step_->print(*state_,true));
305  if ( print ) {
306  outStream << step_->print(*state_,true);
307  }
308 
309  // Initialize Minimum Value and Vector
310  if ( state_->minIterVec == ROL::nullPtr ) {
311  state_->minIterVec = x.clone();
312  }
313  state_->minIterVec->set(x);
314  state_->minIter = state_->iter;
315  state_->minValue = state_->value;
316 
317  // Run Algorithm
318  while (status_->check(*state_)) {
319  step_->compute(*s, x, l, obj, con, *state_);
320  step_->update(x, l, *s, obj, con, *state_);
321 
322  if( printVectors ) {
323  x.print(vectorStream);
324  }
325 
326  output.push_back(step_->print(*state_,printHeader_));
327  if ( print ) {
328  outStream << step_->print(*state_,printHeader_);
329  }
330  }
331  std::stringstream hist;
332  hist << "Optimization Terminated with Status: ";
333  hist << EExitStatusToString(state_->statusFlag);
334  hist << "\n";
335  output.push_back(hist.str());
336  if ( print ) {
337  outStream << hist.str();
338  }
339  return output;
340  }
341 
345  virtual std::vector<std::string> run( Vector<Real> &x,
346  Vector<Real> &l,
347  Objective<Real> &obj,
348  Constraint<Real> &con,
350  bool print = false,
351  std::ostream &outStream = std::cout,
352  bool printVectors = false,
353  std::ostream &vectorStream = std::cout) {
354  return run(x,x.dual(),l,l.dual(),obj,con,bnd,print,outStream,printVectors,vectorStream);
355  }
356 
361  virtual std::vector<std::string> run( Vector<Real> &x,
362  const Vector<Real> &g,
363  Vector<Real> &l,
364  const Vector<Real> &c,
365  Objective<Real> &obj,
366  Constraint<Real> &con,
368  bool print = false,
369  std::ostream &outStream = std::cout,
370  bool printVectors = false,
371  std::ostream &vectorStream = std::cout ) {
372  if(printVectors) {
373  x.print(vectorStream);
374  }
375 
376  std::vector<std::string> output;
377 
378  // Initialize Current Iterate Container
379  if ( state_->iterateVec == ROL::nullPtr ) {
380  state_->iterateVec = x.clone();
381  }
382  state_->iterateVec->set(x);
383 
384  // Initialize Current Lagrange Multiplier Container
385  if ( state_->lagmultVec == ROL::nullPtr ) {
386  state_->lagmultVec = l.clone();
387  }
388  state_->lagmultVec->set(l);
389 
390  // Initialize Step Container
391  ROL::Ptr<Vector<Real> > s = x.clone();
392 
393  // Initialize Step
394  step_->initialize(x, g, l, c, obj, con, bnd, *state_);
395  output.push_back(step_->print(*state_,true));
396  if ( print ) {
397  outStream << step_->print(*state_,true);
398  }
399 
400  // Initialize Minimum Value and Vector
401  if ( state_->minIterVec == ROL::nullPtr ) {
402  state_->minIterVec = x.clone();
403  }
404  state_->minIterVec->set(x);
405  state_->minIter = state_->iter;
406  state_->minValue = state_->value;
407 
408  // Run Algorithm
409  while (status_->check(*state_)) {
410  step_->compute(*s, x, l, obj, con, bnd, *state_);
411  step_->update(x, l, *s, obj, con, bnd, *state_);
412  if( printVectors ) {
413  x.print(vectorStream);
414  }
415  output.push_back(step_->print(*state_,printHeader_));
416  if ( print ) {
417  outStream << step_->print(*state_,printHeader_);
418  }
419  }
420  std::stringstream hist;
421  hist << "Optimization Terminated with Status: ";
422  hist << EExitStatusToString(state_->statusFlag);
423  hist << "\n";
424  output.push_back(hist.str());
425  if ( print ) {
426  outStream << hist.str();
427  }
428  return output;
429  }
430 
433  virtual std::vector<std::string> run( OptimizationProblem<Real> &opt,
434  bool print = false,
435  std::ostream &outStream = std::cout ) {
436  // Get components of optimization problem
437  ROL::Ptr<Objective<Real> > obj = opt.getObjective();
438  ROL::Ptr<Vector<Real> > x = opt.getSolutionVector();
439  ROL::Ptr<BoundConstraint<Real> > bnd = opt.getBoundConstraint();
440  ROL::Ptr<Constraint<Real> > con = opt.getConstraint();
441  ROL::Ptr<Vector<Real> > l = opt.getMultiplierVector();
442 
443  // Call appropriate run function
444  if ( con == ROL::nullPtr ) {
445  if ( bnd == ROL::nullPtr ) {
446  return run(*x,*obj,print,outStream);
447  }
448  else {
449  return run(*x,*obj,*bnd,print,outStream);
450  }
451  }
452  else {
453  if ( bnd == ROL::nullPtr ) {
454  return run(*x,*l,*obj,*con,print,outStream);
455  }
456  else {
457  return run(*x,*l,*obj,*con,*bnd,print,outStream);
458  }
459  }
460  }
461 
462  std::string getIterHeader(void) {
463  return step_->printHeader();
464  }
465 
466  std::string getIterInfo(bool withHeader = false) {
467  return step_->print(*state_,withHeader);
468  }
469 
470  ROL::Ptr<const AlgorithmState<Real> > getState(void) const {
471  return state_;
472  }
473 
474  void reset(void) {
475  state_->reset();
476  }
477 
478 
479 
480 
481 
482 
483 }; // class Algorithm
484 
485 
486 } // namespace ROL
487 
488 #endif
Provides the interface to evaluate objective functions.
void reset(void)
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:226
ROL::Ptr< Step< Real > > step_
ROL::Ptr< StatusTest< Real > > getStatusTest(const std::string step, ROL::ParameterList &parlist)
EStep StringToEStep(std::string s)
Definition: ROL_Types.hpp:389
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void print(std::ostream &outStream) const
Definition: ROL_Vector.hpp:249
void stepFactory(ROL::ParameterList &parlist, ROL::Ptr< 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, Vector< Real > &l, Objective< Real > &obj, Constraint< 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...
ROL::Ptr< Step< Real > > getStep(const std::string &type, ROL::ParameterList &parlist) const
Provides the interface to compute optimization steps.
Definition: ROL_Step.hpp:69
Contains definitions of custom data types in ROL.
ROL::Ptr< AlgorithmState< Real > > state_
virtual Ptr< Objective< Real > > getObjective(void)
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 Ptr< BoundConstraint< Real > > getBoundConstraint(void)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Algorithm(const std::string &stepname, ROL::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 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...
State for algorithm class. Will be used for restarts.
Definition: ROL_Types.hpp:143
virtual std::vector< std::string > run(OptimizationProblem< Real > &opt, bool print=false, std::ostream &outStream=std::cout)
Run algorithm using a ROL::OptimizationProblem.
std::string EExitStatusToString(EExitStatus tr)
Definition: ROL_Types.hpp:126
virtual Ptr< Vector< Real > > getSolutionVector(void)
Provides an interface to run optimization algorithms.
Algorithm(const ROL::Ptr< Step< Real > > &step, const ROL::Ptr< StatusTest< Real > > &status, const ROL::Ptr< 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 > &bnd, 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.
ROL::Ptr< const AlgorithmState< Real > > getState(void) const
Provides an interface to check status of optimization algorithms.
virtual Ptr< 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:357
virtual ~Algorithm()
virtual std::vector< std::string > run(Vector< Real > &x, Vector< Real > &l, Objective< Real > &obj, Constraint< 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...
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Vector< Real > &l, const Vector< Real > &c, Objective< Real > &obj, Constraint< 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...
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, Constraint< 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...
void deactivate(void)
Turn off bounds.
EStep
Enumeration of step types.
Definition: ROL_Types.hpp:274
Algorithm(const ROL::Ptr< Step< Real > > &step, const ROL::Ptr< StatusTest< Real > > &status, bool printHeader=false)
Constructor, given a step and a status test.
ROL::Ptr< StatusTest< Real > > status_
Defines the general constraint operator interface.
virtual Ptr< Constraint< Real > > getConstraint(void)
virtual std::vector< std::string > run(Vector< Real > &x, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &bnd, 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...
std::string getIterHeader(void)