ROL
ROL_StatusTest.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_STATUSTEST_H
11 #define ROL_STATUSTEST_H
12 
13 #include "ROL_Types.hpp"
14 #include "ROL_ParameterList.hpp"
15 
21 namespace ROL {
22 
23 template <class Real>
24 class StatusTest {
25 private:
26 
27  Real gtol_;
28  Real stol_;
29  int max_iter_;
30  bool use_rel_;
31 
32 public:
33 
34  virtual ~StatusTest() {}
35 
36  StatusTest( ROL::ParameterList &parlist ) {
37  Real em6(1e-6);
38  gtol_ = parlist.sublist("Status Test").get("Gradient Tolerance", em6);
39  stol_ = parlist.sublist("Status Test").get("Step Tolerance", em6*gtol_);
40  max_iter_ = parlist.sublist("Status Test").get("Iteration Limit", 100);
41  use_rel_ = parlist.sublist("Status Test").get("Use Relative Tolerances", false);
42  }
43 
44  StatusTest( Real gtol = 1.e-6, Real stol = 1.e-12, int max_iter = 100, bool use_rel = false ) :
45  gtol_(gtol), stol_(stol), max_iter_(max_iter), use_rel_(use_rel) {}
46 
49  virtual bool check( AlgorithmState<Real> &state ) {
50  if (state.iter==0 && use_rel_) {
51  gtol_ *= state.gnorm;
52  stol_ *= state.gnorm;
53  }
54  if ( (state.gnorm > gtol_) &&
55  (state.snorm > stol_) &&
56  (state.iter < max_iter_) ) {
57  return true;
58  }
59  else {
60  state.statusFlag = (state.gnorm <= gtol_ ? EXITSTATUS_CONVERGED
61  : state.snorm <= stol_ ? EXITSTATUS_STEPTOL
62  : state.iter >= max_iter_ ? EXITSTATUS_MAXITER
63  : std::isnan(state.gnorm)||std::isnan(state.snorm) ? EXITSTATUS_NAN
64  : EXITSTATUS_LAST);
65  return false;
66  }
67  }
68 
69 }; // class StatusTest
70 
71 } // namespace ROL
72 
73 #endif
EExitStatus statusFlag
Definition: ROL_Types.hpp:126
StatusTest(ROL::ParameterList &parlist)
Contains definitions of custom data types in ROL.
virtual bool check(AlgorithmState< Real > &state)
Check algorithm status.
StatusTest(Real gtol=1.e-6, Real stol=1.e-12, int max_iter=100, bool use_rel=false)
State for algorithm class. Will be used for restarts.
Definition: ROL_Types.hpp:109
virtual ~StatusTest()
Provides an interface to check status of optimization algorithms.