~swaits/differential-evolution

cab0760a995e9c06260a9f900755e477cc8d1ffd — swaits 19 years ago 6e2d4bf
added example code
added better default range
fixed BIGDOUBLE to be closer to max double (used for default best fitness)
3 files changed, 49 insertions(+), 2 deletions(-)

M de.h
M de.inl
A example.cpp
M de.h => de.h +1 -0
@@ 50,6 50,7 @@ namespace DE

		static const double DEFAULTSCALE;
		static const double DEFAULTCROSSOVER;
		static const double DEFAULTRANGE;
		static const double BIGDOUBLE;



M de.inl => de.inl +5 -2
@@ 17,7 17,10 @@ template <unsigned int DIM, unsigned int POP>
const double DE::Engine<DIM,POP>::DEFAULTCROSSOVER = 1.0;

template <unsigned int DIM, unsigned int POP>
const double DE::Engine<DIM,POP>::BIGDOUBLE = 1.0e30;
const double DE::Engine<DIM,POP>::DEFAULTRANGE= 1000000.0;

template <unsigned int DIM, unsigned int POP>
const double DE::Engine<DIM,POP>::BIGDOUBLE = 9.0e300; // close to max double

template <unsigned int DIM, unsigned int POP>
inline DE::Engine<DIM,POP>::Engine()


@@ 27,7 30,7 @@ inline DE::Engine<DIM,POP>::Engine()

	for ( unsigned int i=0;i<DIM;i++ )
	{
		this->minimum[i] = -BIGDOUBLE;
		this->minimum[i] = -DEFAULTRANGE;
		this->maximum[i] = -(this->minimum[i]);
	}


A example.cpp => example.cpp +43 -0
@@ 0,0 1,43 @@
#include <cstdio>

#include "de.h"

// just a helper function used in calculating the polynomails
double Polynomial(double x, double a, double b, double c, double d)
{
	return a*(x*x*x) + b*(x*x) + c*x + d;
}

// the function we're searching for
double TargetFunction(double input)
{
	return Polynomial(input,17.0,321.0,-2.0,324.0);
}

// our DE implementation
class PolySearch: public DE::Engine<4>
{
	public:
		double CalculateError(const double testcase[4], bool& stop)
		{
			double error = 0.0;
			for ( int i=-100;i<=100;i++ )
			{
				double test   = Polynomial((double)i,testcase[0],testcase[1],testcase[2],testcase[3]);
				double actual = TargetFunction((double)i);
				error += (test-actual)*(test-actual);
			}
			if ( error < 0.00000000000001 )
			{
				stop = true;
			}
		}
};

// main program
int main()
{
	PolySearch de;
	de.Solve();
	return 0;
}