Documentation


optimization_N_test.cpp

Example of multi-dimensional function optimization
#include <iostream>
#include <vector>
#include <animal/numrecipes/optimization/linmin.h>
#include <animal/numrecipes/optimization/powell.h>
#include <animal/numrecipes/optimization/dlinmin.h>
#include <animal/numrecipes/optimization/frprmn.h>

#ifdef USE_STL

double myFunction ( const std::vector<double>& x )
{
    return (x[0]-1)*(x[0]-1) + (x[1]-1)*(x[1]-1) + 1;
}

void myDeriv ( const std::vector<double>& x,  std::vector<double>& d )
{
    d[0] = 2*(x[0]-1);
    d[1] = 2*(x[1]-1);
}

class SomeClass
{
public:

    static double myFunction ( const std::vector<double>& x )
    {
        return (x[0]-1)*(x[0]-1) + (x[1]-1)*(x[1]-1) + 1;
    }

    static void myDeriv ( const std::vector<double>& x,  std::vector<double>& d )
    {
        d[0] = 2*(x[0]-1);
        d[1] = 2*(x[1]-1);
    }

    void optimize()
    {
        std::vector<double> fp(2); fp[0]=-4; fp[1]=7;
        unsigned int fiter;
        double fretf;
        nr::frprmn< nr::STL_Traits<> >(  fp, 2, 0.04, &fiter, &fretf, myFunction, myDeriv );
        cout<<"toto.frprmn returns: "<<endl
        <<"  p = "<< fp[0] <<", "<< fp[1] << endl
        <<"  fret = "<< fretf << endl
        <<"  in "<< fiter <<" iterations "<< endl;

    }

};

#else 

double myFunction ( double* x )
{
    return (x[0]-1)*(x[0]-1) + (x[1]-1)*(x[1]-1) + 1;
}

void myDeriv ( const double x[2], double d[2] )
{
    d[0] = 2*(x[0]-1);
    d[1] = 2*(x[1]-1);
}

class SomeClass
{
public:

    static double myFunction ( double* x )
    {
        return (x[0]-1)*(x[0]-1) + (x[1]-1)*(x[1]-1) + 1;
    }

    static void myDeriv ( const double x[2], double d[2] )
    {
        d[0] = 2*(x[0]-1);
        d[1] = 2*(x[1]-1);
    }

    void optimize()
    {
        double fp[2] = { -4, 7 }; double* fp1 = fp;
        unsigned int fiter;
        double fretf;
        nr::frprmn<>(  fp1, 2, 0.04, &fiter, &fretf, myFunction, myDeriv );
        std::cout<<"toto.frprmn returns: "<<std::endl
        <<"  p = "<< fp[0] <<", "<< fp[1] << std::endl
        <<"  fret = "<< fretf << std::endl
        <<"  in "<< fiter <<" iterations "<< std::endl;
    }

};
#endif




main()
{

#ifdef USE_STL

cout<<"Use STL "<< endl;


std::vector<double> p(2); p[0]=2; p[1]=4;
std::vector<double> xi(2); xi[0]=1; xi[1]=1;
double fret;

nr::linmin< nr::STL_Traits<> >( p, xi, 2, &fret, myFunction );
cout<<" linmin returns :"<< endl
<<"  p = "<< p[0] <<", "<< p[1] << endl
<<"  xi = "<< xi[0] <<", "<< xi[1] << endl
<<"  fret = "<< fret << endl; 

std::vector<double> pp(2); pp[0]=4; pp[1]=7;
unsigned int iter;
nr::powell< nr::STL_Traits<> >(  pp, 2, 0.04, &iter, &fret, myFunction );
cout<<" powell returns: "<<endl
<<"  p = "<< pp[0] <<", "<< pp[1] << endl
<<"  fret = "<< fret << endl
<<"  in "<< iter <<" iterations "<< endl;

std::vector<double> pd(2); pd[0]=2; pd[1]=4;
std::vector<double> xid(2); xid[0]=1; xid[1] = 1;
double fretd;

nr::dlinmin< nr::STL_Traits<> >( pd, xid, 2, &fretd, myFunction, myDeriv );
cout<<" dlinmin returns :"<< endl
<<"  p = "<< pd[0] <<", "<< pd[1] << endl
<<"  xi = "<< xid[0] <<", "<< xid[1] << endl
<<"  fret = "<< fretd << endl; 

std::vector<double> fp(2); fp[0]=-4; fp[1]=7;
unsigned int fiter;
double fretf;
nr::frprmn< nr::STL_Traits<> >(  fp, 2, 0.04, &fiter, &fretf, myFunction, myDeriv );
cout<<" frprmn returns: "<<endl
<<"  p = "<< fp[0] <<", "<< fp[1] << endl
<<"  fret = "<< fretf << endl
<<"  in "<< fiter <<" iterations "<< endl;

SomeClass toto;
toto.optimize();

#else 

std::cout<<"Use old style "<< std::endl;

double p[2] = { 2, 4 }; double* p1 = p;
double xi[2] = { 1, 1 }; double* xi1 = xi;
double fret;

nr::linmin<>( p1, xi1, 2, &fret, myFunction );
std::cout<<" linmin returns :"<< std::endl
<<"  p = "<< p[0] <<", "<< p[1] <<std:: endl
<<"  xi = "<< xi[0] <<", "<< xi[1] << std::endl
<<"  fret = "<< fret << std::endl; 

double pp[2] = { 4, 7 }; double* pp1 = pp;
unsigned int iter;
nr::powell<>(  pp1, 2, 0.04, &iter, &fret, myFunction );
std::cout<<" powell returns: "<<std::endl
<<"  p = "<< pp[0] <<", "<< pp[1] << std::endl
<<"  fret = "<< fret <<std::endl
<<"  in "<< iter <<" iterations "<< std::endl;


double pd[2] = { 2, 4 }; double* pd1 = pd;
double xid[2] = { 1, 1 }; double* xid1 = xid;
double fretd;

nr::dlinmin<>( pd1, xid1, 2, &fretd, myFunction, myDeriv );
std::cout<<" dlinmin returns :"<< std::endl
<<"  p = "<< pd[0] <<", "<< pd[1] << std::endl
<<"  xi = "<< xid[0] <<", "<< xid[1] << std::endl
<<"  fret = "<< fretd << std::endl; 

double fp[2] = { -4, 7 }; double* fp1 = fp;
unsigned int fiter;
double fretf;
nr::frprmn<>(  fp1, 2, 0.04, &fiter, &fretf, myFunction, myDeriv );
std::cout<<" frprmn returns: "<<std::endl
<<"  p = "<< fp[0] <<", "<< fp[1] << std::endl
<<"  fret = "<< fretf << std::endl
<<"  in "<< fiter <<" iterations "<< std::endl;

SomeClass toto;
toto.optimize();



#endif

}

Generated on Thu Dec 23 13:52:23 2004 by doxygen 1.3.6