26 #ifndef TOON_DOWNHILL_SIMPLEX_H 27 #define TOON_DOWNHILL_SIMPLEX_H 28 #include <TooN/TooN.h> 29 #include <TooN/helpers.h> 104 static const int Vertices = (N==-1?-1:N+1);
118 :simplex(c.size()+1, c.size()),values(c.size()+1)
126 epsilon =
sqrt(numeric_limits<Precision>::epsilon());
138 template<
class Function>
void restart(
const Function& func,
const Vector<N>& c, Precision spread)
140 for(
int i=0; i < simplex.num_rows(); i++)
143 for(
int i=0; i < simplex.num_cols(); i++)
144 simplex[i][i] += spread;
146 for(
int i=0; i < values.size(); i++)
147 values[i] = func(simplex[i]);
170 template<
class Function>
void restart(
const Function& func, Precision spread)
190 return std::min_element(&values[0], &values[0] + values.size()) - &values[0];
196 return std::max_element(&values[0], &values[0] + values.size()) - &values[0];
209 Precision second_worst_val=-HUGE_VAL, bestval = HUGE_VAL, worst_val = values[worst];
211 Vector<N> x0 = Zeros(simplex.num_cols());
214 for(
int i=0; i < simplex.num_rows(); i++)
216 if(values[i] < bestval)
224 if(values[i] > second_worst_val)
225 second_worst_val = values[i];
231 x0 *= 1.0 / simplex.num_cols();
236 Precision fr = func(xr);
242 Precision fe = func(xe);
261 if(fr < second_worst_val)
275 Precision fc = func(xc);
288 for(
int i=0; i < simplex.num_rows(); i++)
291 simplex[i] = simplex[best] +
sigma * (simplex[i] - simplex[best]);
292 values[i] = func(simplex[i]);
299 template<
class Function>
bool iterate(
const Function& func)
This is an implementation of the Downhill Simplex (Nelder & Mead, 1965) algorithm.
Definition: downhill_simplex.h:102
DownhillSimplex(const Function &func, const Vector< N > &c, Precision spread=1)
Initialize the DownhillSimplex class.
Definition: downhill_simplex.h:117
Precision norm(const Vector< Size, Precision, Base > &v)
Compute the norm of v.
Definition: helpers.h:97
void restart(const Function &func, Precision spread)
This function resets the simplex around the best current point.
Definition: downhill_simplex.h:170
bool iterate(const Function &func)
Perform one iteration of the downhill Simplex algorithm, and return the result of not DownhillSimplex...
Definition: downhill_simplex.h:299
Precision zero_epsilon
Additive term in tolerance to prevent excessive iterations if . Known as ZEPS in numerical recipies...
Definition: downhill_simplex.h:310
Pretty generic SFINAE introspection generator.
Definition: vec_test.cc:21
int get_worst() const
Get the index of the worst vertex.
Definition: downhill_simplex.h:194
void restart(const Function &func, const Vector< N > &c, Precision spread)
This function sets up the simplex around, with one point at c and the remaining points are made by mo...
Definition: downhill_simplex.h:138
int get_best() const
Get the index of the best vertex.
Definition: downhill_simplex.h:188
Precision alpha
Reflected size. Defaults to 1.
Definition: downhill_simplex.h:305
Precision gamma
Contraction ratio. Defaults to .5.
Definition: downhill_simplex.h:307
Precision epsilon
Tolerance used to determine if the optimization is complete. Defaults to square root of machine preci...
Definition: downhill_simplex.h:309
Precision sigma
Shrink ratio. Defaults to .5.
Definition: downhill_simplex.h:308
Matrix< R, C, P > sqrt(const Matrix< R, C, P, B > &m)
computes a matrix square root of a matrix m by the product form of the Denman and Beavers iteration a...
Definition: helpers.h:350
const Simplex & get_simplex() const
Return the simplex.
Definition: downhill_simplex.h:176
bool finished()
Check to see it iteration should stop.
Definition: downhill_simplex.h:155
Precision rho
Expansion ratio. Defaults to 2.
Definition: downhill_simplex.h:306
const Values & get_values() const
Return the score at the vertices.
Definition: downhill_simplex.h:182
void find_next_point(const Function &func)
Perform one iteration of the downhill Simplex algorithm.
Definition: downhill_simplex.h:201