LineSearch.h
Go to the documentation of this file.
1 // Copyright (C) 2016 Yixuan Qiu <yixuan.qiu@cos.name>
2 // Under MIT license
3 
4 #ifndef LINE_SEARCH_H
5 #define LINE_SEARCH_H
6 
7 #include <Eigen/Core>
8 #include <stdexcept> // std::runtime_error
9 
10 
11 namespace LBFGSpp {
12 
13 
17 template <typename Scalar>
19 {
20 private:
22 
23 public:
40  template <typename Foo>
41  static void Backtracking(Foo& f, Scalar& fx, Vector& x, Vector& grad,
42  Scalar& step,
43  const Vector& drt, const Vector& xp,
44  const LBFGSParam<Scalar>& param)
45  {
46  // Decreasing and increasing factors
47  const Scalar dec = 0.5;
48  const Scalar inc = 2.1;
49 
50  // Check the value of step
51  if(step <= Scalar(0))
52  std::invalid_argument("'step' must be positive");
53 
54  // Save the function value at the current x
55  const Scalar fx_init = fx;
56  // Projection of gradient on the search direction
57  const Scalar dg_init = grad.dot(drt);
58  // Make sure d points to a descent direction
59  if(dg_init > 0)
60  std::logic_error("the moving direction increases the objective function value");
61 
62  const Scalar dg_test = param.ftol * dg_init;
63  Scalar width;
64 
65  int iter;
66  for(iter = 0; iter < param.max_linesearch; iter++)
67  {
68  // x_{k+1} = x_k + step * d_k
69  x.noalias() = xp + step * drt;
70  // Evaluate this candidate
71  fx = f(x, grad);
72 
73  if(fx > fx_init + step * dg_test)
74  {
75  width = dec;
76  } else {
77  // Armijo condition is met
79  break;
80 
81  const Scalar dg = grad.dot(drt);
82  if(dg < param.wolfe * dg_init)
83  {
84  width = inc;
85  } else {
86  // Regular Wolfe condition is met
88  break;
89 
90  if(dg > -param.wolfe * dg_init)
91  {
92  width = dec;
93  } else {
94  // Strong Wolfe condition is met
95  break;
96  }
97  }
98  }
99 
100  if(iter >= param.max_linesearch)
101  throw std::runtime_error("the line search routine reached the maximum number of iterations");
102 
103  if(step < param.min_step)
104  throw std::runtime_error("the line search step became smaller than the minimum value allowed");
105 
106  if(step > param.max_step)
107  throw std::runtime_error("the line search step became larger than the maximum value allowed");
108 
109  step *= width;
110  }
111  }
112 };
113 
114 
115 } // namespace LBFGSpp
116 
117 #endif // LINE_SEARCH_H
Scalar min_step
Definition: Param.h:135
Definition: LBFGS.h:11
static void Backtracking(Foo &f, Scalar &fx, Vector &x, Vector &grad, Scalar &step, const Vector &drt, const Vector &xp, const LBFGSParam< Scalar > &param)
Definition: LineSearch.h:41
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: LineSearch.h:21
Scalar max_step
Definition: Param.h:141


co_scan
Author(s):
autogenerated on Mon Feb 28 2022 23:00:43