TooN::ConjugateGradient< Size, Precision > Struct Template Reference
[Function optimization]

This class provides a nonlinear conjugate-gradient optimizer. More...

#include <conjugate_gradient.h>

List of all members.

Public Member Functions

template<class Func, class Deriv>
 ConjugateGradient (const Vector< Size > &start, const Func &func, const Deriv &deriv)
template<class Func>
void find_next_point (const Func &func)
bool finished ()
template<class Deriv>
void update_vectors_PR (const Deriv &deriv)
template<class Func, class Deriv>
bool iterate (const Func &func, const Deriv &deriv)

Public Attributes

const int size
Vector< Size > g
Vector< Size > h
Vector< Size > old_g
Vector< Size > old_h
Vector< Size > x
Vector< Size > old_x
Precision y
Precision old_y
Precision tolerance
Precision epsilon
int max_iterations
Precision bracket_initial_lambda
Precision linesearch_tolerance
Precision linesearch_epsilon
int linesearch_max_iterations
int iterations


Detailed Description

template<int Size, class Precision = double>
struct TooN::ConjugateGradient< Size, Precision >

This class provides a nonlinear conjugate-gradient optimizer.

The following code snippet will perform an optimization on the Rosenbrock Bananna function in two dimensions:

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

Vector<2> RosenbrockDerivatives(const Vector<2>& v)
{
    double x = v[0];
    double y = v[1];

    Vector<2> ret;
    ret[0] = -2+2*x-400*(y-sq(x))*x;
    ret[1] = 200*y-200*sq(x);

    return ret;
}

int main()
{
    ConjugateGradient<2> cg(makeVector(0,0), Rosenbrock, RosenbrockDerivatives);

    while(!cg.iterate(Rosenbrock, RosenbrockDerivatives))
        cout << "y_" << iteration << " = " << cg.y << endl;

    cout << "Optimal value: " << cg.y << endl;
}

The chances are that you will want to read the documentation for ConjugateGradient::ConjugateGradient and ConjugateGradient::iterate.

Linesearch is currently performed using golden-section search and conjugate vector updates are performed using the Polak-Ribiere equations. There many tunable parameters, and the internals are readily accessible, so alternative termination conditions etc can easily be substituted. However, ususally these will not be necessary.


Constructor & Destructor Documentation

template<int Size, class Precision = double>
template<class Func, class Deriv>
TooN::ConjugateGradient< Size, Precision >::ConjugateGradient ( const Vector< Size > &  start,
const Func &  func,
const Deriv &  deriv 
)

Initialize the ConjugateGradient class with sensible values.

Parameters:
start Starting point, x
func Function f to compute $f(x)$
deriv Function to compute $\nabla f(x)$


Member Function Documentation

template<int Size, class Precision = double>
template<class Func>
void TooN::ConjugateGradient< Size, Precision >::find_next_point ( const Func &  func  ) 

Perform a linesearch from the current point (x) along the current conjugate vector (h).

The linesearch does not make use of derivatives. You probably do not want to use this function. See iterate() instead. This function updates:

template<int Size, class Precision = double>
bool TooN::ConjugateGradient< Size, Precision >::finished (  ) 

Check to see it iteration should stop.

You probably do not want to use this function. See iterate() instead. This function updates nothing.

template<int Size, class Precision = double>
template<class Deriv>
void TooN::ConjugateGradient< Size, Precision >::update_vectors_PR ( const Deriv &  deriv  ) 

After an iteration, update the gradient and conjugate using the Polak-Ribiere equations.

/

Parameters:
deriv Functor to compute derivatives at the specified point. This function updates:
  • g
  • old_g
  • h
  • old_h

template<int Size, class Precision = double>
template<class Func, class Deriv>
bool TooN::ConjugateGradient< Size, Precision >::iterate ( const Func &  func,
const Deriv &  deriv 
)

Use this function to iterate over the optimization.

Note that after iterate returns false, g, h, old_g and old_h will not have been updated. This function updates:


Member Data Documentation

template<int Size, class Precision = double>
const int TooN::ConjugateGradient< Size, Precision >::size

Dimensionality of the space.

template<int Size, class Precision = double>
Vector<Size> TooN::ConjugateGradient< Size, Precision >::g

Gradient vector used by the next call to iterate().

template<int Size, class Precision = double>
Vector<Size> TooN::ConjugateGradient< Size, Precision >::h

Conjugate vector to be searched along in the next call to iterate().

template<int Size, class Precision = double>
Vector<Size> TooN::ConjugateGradient< Size, Precision >::old_g

Gradient vector used to compute $h$ in the last call to iterate().

template<int Size, class Precision = double>
Vector<Size> TooN::ConjugateGradient< Size, Precision >::old_h

Conjugate vector searched along in the last call to iterate().

template<int Size, class Precision = double>
Vector<Size> TooN::ConjugateGradient< Size, Precision >::x

Current position (best known point).

template<int Size, class Precision = double>
Vector<Size> TooN::ConjugateGradient< Size, Precision >::old_x

Previous best known point (not set at construction).

template<int Size, class Precision = double>
Precision TooN::ConjugateGradient< Size, Precision >::y

Function at $x$.

template<int Size, class Precision = double>
Precision TooN::ConjugateGradient< Size, Precision >::old_y

Function at old_x.

template<int Size, class Precision = double>
Precision TooN::ConjugateGradient< Size, Precision >::tolerance

Tolerance used to determine if the optimization is complete. Defaults to square root of machine precision.

template<int Size, class Precision = double>
Precision TooN::ConjugateGradient< Size, Precision >::epsilon

Additive term in tolerance to prevent excessive iterations if $x_\mathrm{optimal} = 0$. Known as ZEPS in numerical recipies. Defaults to 1e-20.

template<int Size, class Precision = double>
int TooN::ConjugateGradient< Size, Precision >::max_iterations

Maximum number of iterations. Defaults to size$*100$.

template<int Size, class Precision = double>
Precision TooN::ConjugateGradient< Size, Precision >::bracket_initial_lambda

Initial stepsize used in bracketing the minimum for the line search. Defaults to 1.

template<int Size, class Precision = double>
Precision TooN::ConjugateGradient< Size, Precision >::linesearch_tolerance

Tolerance used to determine if the linesearch is complete. Defaults to square root of machine precision.

template<int Size, class Precision = double>
Precision TooN::ConjugateGradient< Size, Precision >::linesearch_epsilon

Additive term in tolerance to prevent excessive iterations if $x_\mathrm{optimal} = 0$. Known as ZEPS in numerical recipies. Defaults to 1e-20.

template<int Size, class Precision = double>
int TooN::ConjugateGradient< Size, Precision >::linesearch_max_iterations

Maximum number of iterations in the linesearch. Defaults to 100.

template<int Size, class Precision = double>
int TooN::ConjugateGradient< Size, Precision >::iterations

Number of iterations performed.


Generated on Thu May 7 20:28:42 2009 for TooN by  doxygen 1.5.3