#include <downhill_simplex.h>
Public Member Functions | |
template<class Function> | |
DownhillSimplex (const Function &func, const Vec &c, double spread=1) | |
const Simplex & | get_simplex () const |
const Values & | get_values () const |
int | get_best () const |
int | get_worst () const |
template<class Function> | |
void | iterate (const Function &func) |
This particular instance will minimize a given function.
The function maintains points for a $N$ dimensional function,
At each iteration, the following algorithm is performed:
This implementation uses:
Example usage:
#include <utility> #include <TooN/optimization/downhill_simplex.h> using namespace std; using namespace TooN; template<class C> double length(const C& v) { return sqrt(v*v); } template<class C> double simplex_size(const C& s) { return abs(length(s.get_simplex()[s.get_best()] - s.get_simplex()[s.get_worst()]) / length( s.get_simplex()[s.get_best()])); } double Rosenbrock(Vector<2>& v) { return sq(1 - v[0]) + 100 * sq(v[1] - sq(v[0])); } int main() { Vector<2> starting_point = (make_Vector, -1, 1); DownhillSimplex<2> dh_fixed(RosenbrockF, starting_point, 1); while(simplex_size(dh_fixed) > 0.0000001) dh_fixed.iterate(RosenbrockF); cout << dh_fixed.get_simplex()[dh_fixed.get_best()] << endl; }
N | The dimension of the function to optimize. As usual, the default value of N (-1) indicates that the class is sized at run-time. |
TooN::DownhillSimplex< N >::DownhillSimplex | ( | const Function & | func, | |
const Vec & | c, | |||
double | spread = 1 | |||
) |
Initialize the DownhillSimplex class.
The simplex is automatically generated. One point is at c, the remaining points are made by moving c by spread along each axis aligned unit vector.
func | Functor to minimize. | |
c | Origin of the initial simplex. The dimension of this vector is used to determine the dimension of the run-time sized version. | |
spread | Size of the initial simplex. |
const Simplex& TooN::DownhillSimplex< N >::get_simplex | ( | ) | const |
Return the simplex.
const Values& TooN::DownhillSimplex< N >::get_values | ( | ) | const |
Return the score at the vertices.
int TooN::DownhillSimplex< N >::get_best | ( | ) | const |
Get the index of the best vertex.
int TooN::DownhillSimplex< N >::get_worst | ( | ) | const |
Get the index of the worst vertex.
void TooN::DownhillSimplex< N >::iterate | ( | const Function & | func | ) |
Perform one iteration of the downhill Simplex algorithm.
func | Functor to minimize |