TooN: DownhillSimplex< N, Precision > Class Template Reference

TooN 2.0.0-beta8

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Hierarchy
- Class Members

Public Member Functions | Public Attributes
DownhillSimplex< N, Precision > Class Template Reference
Function optimization

This is an implementation of the Downhill Simplex (Nelder & Mead, 1965) algorithm. More...

`#include <downhill_simplex.h>`

List of all members.

## Public Member Functions
template<class Function >
| DownhillSimplex (const Function &func, const Vector< N > &c, Precision spread=1)
template<class Function >
void | restart (const Function &func, const Vector< N > &c, Precision spread)
bool | finished ()
template<class Function >
void | restart (const Function &func, Precision spread)
const Simplex & | get_simplex () const
const Values & | get_values () const
int | get_best () const
int | get_worst () const
template<class Function >
void | find_next_point (const Function &func)
template<class Function >
bool | iterate (const Function &func)
## Public Attributes
Precision | alpha
Precision | rho
Precision | gamma
Precision | sigma
Precision | epsilon
Precision | zero_epsilon

## Detailed Description

### template<int N = -1, typename Precision = double>
class TooN::DownhillSimplex< N, Precision >

This is an implementation of the Downhill Simplex (Nelder & Mead, 1965) algorithm.

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:

- Find the worst (largest) point, .
- Find the centroid of the remaining points, .
- Let
- Compute a reflected point,
- If is better than the best point
  - Expand the simplex by extending the reflection to
  - Replace with the best point of , and .
- Else, if is between the best and second-worst point
  - Replace with .
- Else, if is better than
  - Contract the simplex by computing
  - If
    - Replace with .
- If has not been replaced, then shrink the simplex by a factor of around the best point.

This implementation uses:

Example usage:

    #include <TooN/optimization/downhill_simplex.h>
    using namespace std;
    using namespace TooN;

    double sq(double x)
    {
        return x*x;
    }

    double Rosenbrock(const Vector<2>& v)
    {
            return sq(1 - v[0]) + 100 * sq(v[1] - sq(v[0]));
    }

    int main()
    {
            Vector<2> starting_point = makeVector( -1, 1);

            DownhillSimplex<2> dh_fixed(Rosenbrock, starting_point, 1);

            while(dh_fixed.iterate(Rosenbrock))
            {
                cout << dh.get_values()[dh.get_best()] << endl;
            }

            cout << dh_fixed.get_simplex()[dh_fixed.get_best()] << endl;
    }

Parameters:

  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.

## Constructor & Destructor Documentation

DownhillSimplex | ( | const Function & | func,
|  | const Vector< N > & | c,
|  | Precision | 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.

Parameters:

  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.

References DownhillSimplex< N, Precision >::alpha, DownhillSimplex< N, Precision >::epsilon, DownhillSimplex< N, Precision >::gamma, DownhillSimplex< N, Precision >::restart(), DownhillSimplex< N, Precision >::rho, DownhillSimplex< N, Precision >::sigma, TooN::sqrt(), and DownhillSimplex< N, Precision >::zero_epsilon.

## Member Function Documentation

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 moving by spread along each axis aligned unit vector.

Parameters:

  func | Functor to minimize.
  c | c corner point of the simplex
  spread | spread simplex size

References Matrix< Rows, Cols, Precision, Layout >::num_cols(), Matrix< Rows, Cols, Precision, Layout >::num_rows(), and Vector< Size, Precision, Base >::size().

Referenced by DownhillSimplex< N, Precision >::DownhillSimplex(), and DownhillSimplex< N, Precision >::restart().

bool finished | ( |  | ) |

Check to see it iteration should stop.

You probably do not want to use this function. See iterate() instead. This function updates nothing. The termination criterion is that the simplex span (distancve between the best and worst vertices) is small compared to the scale or small overall.

References DownhillSimplex< N, Precision >::epsilon, DownhillSimplex< N, Precision >::get_best(), DownhillSimplex< N, Precision >::get_worst(), TooN::norm(), and DownhillSimplex< N, Precision >::zero_epsilon.

Referenced by DownhillSimplex< N, Precision >::iterate().

void restart | ( | const Function & | func,
|  | Precision | spread
| ) |  |

This function resets the simplex around the best current point.

Parameters:

  func | Functor to minimize.
  spread | simplex size

References DownhillSimplex< N, Precision >::get_best(), and DownhillSimplex< N, Precision >::restart().

void find_next_point | ( | const Function & | func | ) |

Perform one iteration of the downhill Simplex algorithm.

Parameters:

  func | Functor to minimize

References DownhillSimplex< N, Precision >::alpha, DownhillSimplex< N, Precision >::gamma, DownhillSimplex< N, Precision >::get_worst(), Matrix< Rows, Cols, Precision, Layout >::num_cols(), Matrix< Rows, Cols, Precision, Layout >::num_rows(), DownhillSimplex< N, Precision >::rho, DownhillSimplex< N, Precision >::sigma, and TooN::Zeros.

Referenced by DownhillSimplex< N, Precision >::iterate().

bool iterate | ( | const Function & | func | ) |

Perform one iteration of the downhill Simplex algorithm, and return the result of not DownhillSimplex::finished.

Parameters:

  func | Functor to minimize

References DownhillSimplex< N, Precision >::find_next_point(), and DownhillSimplex< N, Precision >::finished().