vnl_amoeba.h
Go to the documentation of this file.
1 // This is core/vnl/algo/vnl_amoeba.h
2 #ifndef vnl_amoeba_h_
3 #define vnl_amoeba_h_
4 //:
5 // \file
6 // \brief Nelder-Meade downhill simplex.
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date 23 Oct 97
9 //
10 // \verbatim
11 // Modifications
12 // 971023 AWF Initial version
13 // dac (Manchester) 26/03/2001: tidied up documentation
14 // Tim Cootes 7-Jan-02: Added documentation and additional methods
15 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
16 // \endverbatim
17 
18 //-----------------------------------------------------------------------------
19 
20 #include <vnl/vnl_vector.h>
21 #include <vnl/algo/vnl_algo_export.h>
22 
23 class vnl_cost_function;
25 
26 //: Nelder-Meade downhill simplex.
27 // vnl_amoeba is an implementation of the Nelder-Meade downhill simplex
28 // algorithm. For most problems, it's a few times slower than
29 // vnl_levenberg_marquardt, but it can perform much better on noisy error
30 // functions.
31 //
32 // It works by creating a simplex (n+1 points in n-D space) which then
33 // crawls about the space searching for the solution.
34 //
35 // By default the set of (n+1) starting points are generated by applying
36 // a scaling (relative_diameter) to each element of the supplied starting
37 // vector, with a small offset used instead if the value is zero.
38 //
39 // Alternatively, if one uses minimize(x,dx), then the starting points
40 // are obtained by adding each dx[i] to the elements of x, one at a time.
41 // This is useful if you know roughly the scale of your space.
42 
43 class VNL_ALGO_EXPORT vnl_amoeba
44 {
45  public:
46  int verbose;
47  int maxiter;
48  double X_tolerance;
49  double F_tolerance;
50 
51  //: Define maximum number of iterations to use
52  void set_max_iterations(int n) { maxiter = n; }
53 
54  //: Define tolerance on elements of x
55  void set_x_tolerance(double tol) { X_tolerance = tol; }
56 
57  //: Define tolerance on function evaluation
58  void set_f_tolerance(double tol) { F_tolerance = tol; }
59 
60  //: Define scaling used to select starting vertices relative to initial x0.
61  // I.e. the i'th vertex has x[i] = x0[i]*(1+relative_diameter)
62  void set_relative_diameter(double r) { relative_diameter = r; }
63 
64  void set_zero_term_delta(double d) { zero_term_delta = d; }
65  //: Scaling used to select starting vertices relative to initial x0.
66  // I.e. the i'th vertex has x[i] = x0[i]*(1+relative_diameter)
69  //: Construct and supply function to be minimized
71 
72  //: Modify x to minimise function supplied in constructor
73  // Start simplex defined by scaling elements of x
74  void minimize(vnl_vector<double>& x);
75 
76  //: Perform optimisation.
77  // Start simplex defined by adding dx[i] to each x[i]
78  void minimize(vnl_vector<double>& x, const vnl_vector<double>& dx);
79 
80  double get_end_error() const { return end_error_; }
81 
82  //: Number of evaluations used in last call to minimize
83  int get_num_evaluations() const { return num_evaluations_; }
84 
85  public:
86  //: Modify x so as to minimise f(x)
87  static void minimize(vnl_cost_function& f, vnl_vector<double>& x);
88 
89  //: Modify x so as to minimise f(x)
90  // Start simplex defined by adding dx[i] to each x[i]
91  static void minimize(vnl_cost_function& f, vnl_vector<double>& x,
92  const vnl_vector<double>& dx);
93 
94  //: Modify x so as to minimise f(x)
95  // delta defines relative size of initial simplex
96  // ie the i'th vertex has xi[i] = x[i]*(1+delta)
97  static void minimize(vnl_cost_function& f, vnl_vector<double>& x,
98  double delta);
99 
100  //: Modify x so as to minimise f(x)
101  static void minimize(vnl_least_squares_function& f, vnl_vector<double>& x);
102 
103  static bool default_verbose;
104 
105  protected:
107  double end_error_;
109 };
110 
111 // Private struct needs to be declared in the header file
112 // in order to instantiate STL container of it elsewhere.
114 {
116  double fv;
117 
118  vnl_amoeba_SimplexCorner(int = 0);
120  static int compare(vnl_amoeba_SimplexCorner const& s1,
121  vnl_amoeba_SimplexCorner const& s2);
122 };
123 
124 #endif // vnl_amoeba_h_
An object that represents a function from R^n -> R.
void set_x_tolerance(double tol)
Define tolerance on elements of x.
Definition: vnl_amoeba.h:55
vnl_vector< double > v
Definition: vnl_amoeba.h:115
vnl_amoeba_SimplexCorner & operator=(const vnl_amoeba_SimplexCorner &that)
static int compare(vnl_amoeba_SimplexCorner const &s1, vnl_amoeba_SimplexCorner const &s2)
Definition: vnl_amoeba.cxx:79
Nelder-Meade downhill simplex.
Definition: vnl_amoeba.h:43
double F_tolerance
Definition: vnl_amoeba.h:49
double relative_diameter
Scaling used to select starting vertices relative to initial x0.
Definition: vnl_amoeba.h:67
void set_zero_term_delta(double d)
Definition: vnl_amoeba.h:64
double end_error_
Definition: vnl_amoeba.h:107
void set_max_iterations(int n)
Define maximum number of iterations to use.
Definition: vnl_amoeba.h:52
Abstract base for minimising functions.
double get_end_error() const
Definition: vnl_amoeba.h:80
double zero_term_delta
Definition: vnl_amoeba.h:68
void set_f_tolerance(double tol)
Define tolerance on function evaluation.
Definition: vnl_amoeba.h:58
void set_relative_diameter(double r)
Define scaling used to select starting vertices relative to initial x0.
Definition: vnl_amoeba.h:62
static bool default_verbose
Definition: vnl_amoeba.h:103
int verbose
Definition: vnl_amoeba.h:46
int maxiter
Definition: vnl_amoeba.h:47
vnl_cost_function * fptr
Definition: vnl_amoeba.h:106
int get_num_evaluations() const
Number of evaluations used in last call to minimize.
Definition: vnl_amoeba.h:83
int num_evaluations_
Definition: vnl_amoeba.h:108
double X_tolerance
Definition: vnl_amoeba.h:48