1 #include "optimization/brent.h" 
   17     template<
int Size, 
typename Precision, 
typename Func> 
struct LineSearch 
   29         :start(s),direction(d),f(func)
 
   36             return f(start + x * direction);
 
   54         Precision a, b, c, b_val, c_val;
 
   59         Precision lambda=initial_lambda;
 
   77             double last_good_lambda = lambda;
 
   87                 last_good_lambda = lambda;
 
  104                 double bad_lambda=lambda;
 
  110                     c = last_good_lambda + (bad_lambda - last_good_lambda)*l;
 
  135                 else if(lambda < zeps)
 
  231     : size(start.size()),
 
  232       g(size),h(size),minus_h(size),old_g(size),old_h(size),x(start),old_x(size)
 
  234         init(start, func(start), deriv(start));
 
  242     : size(start.size()),
 
  243       g(size),h(size),minus_h(size),old_g(size),old_h(size),x(start),old_x(size)
 
  245         init(start, func(start), deriv);
 
  255         using std::numeric_limits;
 
  267         tolerance = sqrt(numeric_limits<Precision>::epsilon());
 
  269         max_iterations = size * 100;
 
  271         bracket_initial_lambda = 1;
 
  273         linesearch_tolerance =  sqrt(numeric_limits<Precision>::epsilon());
 
  274         linesearch_epsilon = 1e-20;
 
  275         linesearch_max_iterations=100;
 
  277         bracket_epsilon=1e-20;
 
  304         double a = bracket[0][0];
 
  305         double b = bracket[1][0];
 
  306         double c = bracket[2][0];
 
  308         double a_val = bracket[0][1];
 
  309         double b_val = bracket[1][1];
 
  310         double c_val = bracket[2][1];
 
  317         if(a==0 && b== 0 && c == 0)
 
  332             assert(a < b && b < c);
 
  333             assert(a_val > b_val && b_val < c_val);
 
  338             assert(m[0] >= a && m[0] <= c);
 
  339             assert(m[1] <= b_val);
 
  352         return iterations > max_iterations || 2*
abs(y - old_y) <= tolerance * (
abs(y) + 
abs(old_y) + 
epsilon);
 
  371         Precision gamma = (g * g - old_g*
g)/(old_g * old_g);
 
  372         h = g + gamma * 
old_h;
 
  393     template<
class Func, 
class Deriv> 
bool iterate(
const Func& func, 
const Deriv& deriv)
 
const int size
Dimensionality of the space. 
Precision bracket_initial_lambda
Initial stepsize used in bracketing the minimum for the line search. Defaults to 1. 
int iterations
Number of iterations performed. 
Precision operator()(Precision x) const 
Vector< Size > g
Gradient vector used by the next call to iterate() 
bool iterate(const Func &func, const Deriv &deriv)
Everything lives inside this namespace. 
Precision old_y
Function at old_x. 
Vector< 2, Precision > brent_line_search(Precision a, Precision x, Precision b, Precision fx, const Functor &func, int maxiterations, Precision tolerance=sqrt(numeric_limits< Precision >::epsilon()), Precision epsilon=numeric_limits< Precision >::epsilon())
Precision tolerance
Tolerance used to determine if the optimization is complete. Defaults to square root of machine preci...
void update_vectors_PR(const Vector< Size > &grad)
void init(const Vector< Size > &start, const Precision &func, const Vector< Size > &deriv)
Precision linesearch_tolerance
Tolerance used to determine if the linesearch is complete. Defaults to square root of machine precisi...
Matrix< 3, 2, Precision > bracket_minimum_forward(Precision a_val, const Func &func, Precision initial_lambda, Precision zeps)
Vector< Size > old_g
Gradient vector used to compute $h$ in the last call to iterate() 
Vector< Size > x
Current position (best known point) 
Precision epsilon
Additive term in tolerance to prevent excessive iterations if . Known as ZEPS in numerical recipies...
Vector< Size > old_x
Previous best known point (not set at construction) 
void find_next_point(const Func &func)
ConjugateGradient(const Vector< Size > &start, const Func &func, const Vector< Size > &deriv)
Vector< Size > minus_h
negative of h as this is required to be passed into a function which uses references (so can't be tem...
Precision bracket_epsilon
Minimum size for initial minima bracketing. Below this, it is assumed that the system has converged...
Vector< 1 > makeVector(double x1)
Vector< Size > h
Conjugate vector to be searched along in the next call to iterate() 
ConjugateGradient(const Vector< Size > &start, const Func &func, const Deriv &deriv)
int max_iterations
Maximum number of iterations. Defaults to size . 
Precision linesearch_epsilon
Additive term in tolerance to prevent excessive iterations if . Known as ZEPS in numerical recipies...
int linesearch_max_iterations
Maximum number of iterations in the linesearch. Defaults to 100. 
Vector< Size > old_h
Conjugate vector searched along in the last call to iterate() 
const Vector< Size, Precision > & direction
 
LineSearch(const Vector< Size, Precision > &s, const Vector< Size, Precision > &d, const Func &func)
const Vector< Size, Precision > & start
 
static Operator< Internal::Zero > Zeros
bool isnan(const Vector< S, P, B > &v)