ROL
ROL_Bisection.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_BISECTION_H
11 #define ROL_BISECTION_H
12 
17 #include "ROL_LineSearch.hpp"
18 #include "ROL_BackTracking.hpp"
19 
20 namespace ROL {
21 
22 template<class Real>
23 class Bisection : public LineSearch<Real> {
24 private:
25  Real tol_;
26  ROL::Ptr<Vector<Real> > xnew_;
27  ROL::Ptr<LineSearch<Real> > btls_;
28 
29 public:
30 
31  virtual ~Bisection() {}
32 
33  // Constructor
34  Bisection( ROL::ParameterList &parlist ) : LineSearch<Real>(parlist) {
35  Real oem8(1.e-8);
36  tol_ = parlist.sublist("Step").sublist("Line Search").sublist("Line-Search Method").get("Bracketing Tolerance",oem8);
37  btls_ = ROL::makePtr<BackTracking<Real>>(parlist);
38  }
39 
40  void initialize( const Vector<Real> &x, const Vector<Real> &s, const Vector<Real> &g,
42  LineSearch<Real>::initialize(x,s,g,obj,con);
43  xnew_ = x.clone();
44  btls_->initialize(x,s,g,obj,con);
45  }
46 
47  void run( Real &alpha, Real &fval, int &ls_neval, int &ls_ngrad,
48  const Real &gs, const Vector<Real> &s, const Vector<Real> &x,
50  Real tol = std::sqrt(ROL_EPSILON<Real>()), half(0.5);
51  ls_neval = 0;
52  ls_ngrad = 0;
53  // Get initial line search parameter
54  alpha = LineSearch<Real>::getInitialAlpha(ls_neval,ls_ngrad,fval,gs,x,s,obj,con);
55 
56  // Compute value phi(0)
57  Real tl(0);
58  Real val_tl = fval;
59 
60  // Compute value phi(alpha)
61  Real tr = alpha;
63  obj.update(*xnew_);
64  Real val_tr = obj.value(*xnew_,tol);
65  ls_neval++;
66 
67  // Check if phi(alpha) < phi(0)
68  if ( val_tr < val_tl ) {
69  if ( LineSearch<Real>::status(LINESEARCH_BISECTION,ls_neval,ls_ngrad,tr,fval,gs,val_tr,x,s,obj,con) ) {
70  alpha = tr;
71  fval = val_tr;
72  return;
73  }
74  }
75 
76  // Get min( phi(0), phi(alpha) )
77  Real t(0);
78  Real val_t(0);
79  if ( val_tl < val_tr ) {
80  t = tl;
81  val_t = val_tl;
82  }
83  else {
84  t = tr;
85  val_t = val_tr;
86  }
87 
88  // Compute value phi(midpoint)
89  Real tc = half*(tl+tr);
91  Real val_tc = obj.value(*xnew_,tol);
92  ls_neval++;
93 
94  // Get min( phi(0), phi(alpha), phi(midpoint) )
95  if ( val_tc < val_t ) {
96  t = tc;
97  val_t = val_tc;
98  }
99 
100  Real t1(0), val_t1(0);
101  Real t2(0), val_t2(0);
102 
103  while ( !LineSearch<Real>::status(LINESEARCH_BISECTION,ls_neval,ls_ngrad,t,fval,gs,val_t,x,s,obj,con)
104  && std::abs(tr - tl) > tol_ ) {
105  t1 = half*(tl+tc);
107  obj.update(*xnew_);
108  val_t1 = obj.value(*xnew_,tol);
109  ls_neval++;
110 
111  t2 = half*(tr+tc);
113  obj.update(*xnew_);
114  val_t2 = obj.value(*xnew_,tol);
115  ls_neval++;
116 
117  if ( ( (val_tl <= val_tr) && (val_tl <= val_t1) && (val_tl <= val_t2) && (val_tl <= val_tc) )
118  || ( (val_t1 <= val_tr) && (val_t1 <= val_tl) && (val_t1 <= val_t2) && (val_t1 <= val_tc) ) ) {
119  if ( val_tl < val_t1 ) {
120  t = tl;
121  val_t = val_tl;
122  }
123  else {
124  t = t1;
125  val_t = val_t1;
126  }
127 
128  tr = tc;
129  val_tr = val_tc;
130  tc = t1;
131  val_tc = val_t1;
132  }
133  else if ( ( (val_tc <= val_tr) && (val_tc <= val_tl) && (val_tc <= val_t1) && (val_tc <= val_t2) ) ) {
134  t = tc;
135  val_t = val_tc;
136 
137  tl = t1;
138  val_tl = val_t1;
139  tr = t2;
140  val_tr = val_t2;
141  }
142  else if ( ( (val_t2 <= val_tr) && (val_t2 <= val_tl) && (val_t2 <= val_t1) && (val_t2 <= val_tc) )
143  || ( (val_tr <= val_tl) && (val_tr <= val_t1) && (val_tr <= val_t2) && (val_tr <= val_tc) ) ) {
144  if ( val_tr < val_t2 ) {
145  t = tr;
146  val_t = val_tr;
147  }
148  else {
149  t = t2;
150  val_t = val_t2;
151  }
152 
153  tl = tc;
154  val_tl = val_tc;
155  tc = t2;
156  val_tc = val_t2;
157  }
158  }
159 
160  fval = val_t;
161  alpha = t;
162 
163  if ( alpha < ROL_EPSILON<Real>() ) {
164  btls_->run(alpha,fval,ls_neval,ls_ngrad,gs,s,x,obj,con);
165  }
166  }
167 };
168 
169 }
170 
171 #endif
Provides the interface to evaluate objective functions.
void run(Real &alpha, Real &fval, int &ls_neval, int &ls_ngrad, const Real &gs, const Vector< Real > &s, const Vector< Real > &x, Objective< Real > &obj, BoundConstraint< Real > &con)
void updateIterate(Vector< Real > &xnew, const Vector< Real > &x, const Vector< Real > &s, Real alpha, BoundConstraint< Real > &con)
virtual Real getInitialAlpha(int &ls_neval, int &ls_ngrad, const Real fval, const Real gs, const Vector< Real > &x, const Vector< Real > &s, Objective< Real > &obj, BoundConstraint< Real > &con)
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
virtual void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
Provides interface for and implements line searches.
void initialize(const Vector< Real > &x, const Vector< Real > &s, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &con)
virtual ~Bisection()
ROL::Ptr< Vector< Real > > xnew_
Provides the interface to apply upper and lower bound constraints.
Bisection(ROL::ParameterList &parlist)
Implements a bisection line search.
ROL::Ptr< LineSearch< Real > > btls_
virtual void initialize(const Vector< Real > &x, const Vector< Real > &s, const Vector< Real > &g, Objective< Real > &obj, BoundConstraint< Real > &con)