Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnOptimizer.h
1 /******************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnOptimizer.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include "Eigen/Core"
16 
69  public:
70  static double EPSF;
71  static double EPSG;
72  static double EPSX;
73 
74  public:
75  unsigned _n;
76  double *_x;
77  double *_df;
78 
79  public:
81  drwnOptimizer();
83  drwnOptimizer(unsigned n);
85  drwnOptimizer(const drwnOptimizer& o);
86  virtual ~drwnOptimizer();
87 
90  void initialize(unsigned n, const double *x = NULL);
93  void initialize(const double *x = NULL);
94 
98  double solve(unsigned maxiter, bool bMonitor = false);
99 
101  virtual double objective(const double *x) const = 0;
103  virtual void gradient(const double *x, double *df) const = 0;
105  virtual double objectiveAndGradient(const double *x, double *df) const {
106  gradient(x, df); return objective(x);
107  }
108 
110  inline unsigned size() const { return _n; }
112  inline double operator[](unsigned i) const { return _x[i]; }
114  inline double& operator[](unsigned i) { return _x[i]; }
115 
117  virtual void monitor(unsigned iter, double objValue);
118 
119  protected:
121  enum drwnLBFGSResult {
122  DRWN_LBFGS_ERROR, DRWN_LBFGS_MAX_ITERS, DRWN_LBFGS_CONVERGED_F,
123  DRWN_LBFGS_CONVERGED_G, DRWN_LBFGS_CONVERGED_X
124  };
125 
126  drwnLBFGSResult lbfgsMinimize(int m, unsigned maxiter,
127  double epsg, double epsf, double epsx, bool bMonitor);
128  bool lbfgsSearch(double &f, const Eigen::VectorXd &s,
129  double& stp, Eigen::VectorXd& diag);
130  bool lbfgsStep(double& stx, double& fx, double& dx,
131  double& sty, double& fy, double& dy,
132  double& stp, const double& fp, const double& dp,
133  bool& brackt, const double& stmin, const double& stmax);
135 };
136 
137 
virtual double objectiveAndGradient(const double *x, double *df) const
returns value of objective function and populates gradient df at point x
Definition: drwnOptimizer.h:105
double & operator[](unsigned i)
returns a reference to the i-th component of the current solution
Definition: drwnOptimizer.h:114
void initialize(unsigned n, const double *x=NULL)
initialize an optimization problem of size n possibly with feasible starting point x (or zero) ...
Definition: drwnOptimizer.cpp:81
drwnOptimizer()
default constructor
Definition: drwnOptimizer.cpp:42
double solve(unsigned maxiter, bool bMonitor=false)
Solve the optimization problem for up to maxiter iterations to precision set by EPSF, EPSG, and EPSX static variables. Calls monitor function after each iteration if bMonitor is true.
Definition: drwnOptimizer.cpp:105
static double EPSG
deafult tolerance on gradient convergence
Definition: drwnOptimizer.h:71
Interface for solving large-scale unconstrained optimization problems using L-BFGS.
Definition: drwnOptimizer.h:68
virtual void gradient(const double *x, double *df) const =0
populates gradient of objective function at point x
double * _df
gradient at _x in
Definition: drwnOptimizer.h:77
double * _x
current feasible solution in
Definition: drwnOptimizer.h:76
unsigned _n
dimension of optimization problem (i.e., )
Definition: drwnOptimizer.h:75
static double EPSF
default tolerance on function convergence
Definition: drwnOptimizer.h:70
double operator[](unsigned i) const
returns the i-th component of the current solution
Definition: drwnOptimizer.h:112
unsigned size() const
dimension of optimization problem
Definition: drwnOptimizer.h:110
virtual double objective(const double *x) const =0
returns value of objective function at point x
virtual void monitor(unsigned iter, double objValue)
callback for each iteration during optimization (if bMonitor is true)
Definition: drwnOptimizer.cpp:136
static double EPSX
default tolerance on solution convergence
Definition: drwnOptimizer.h:72