diff --git a/de.h b/de.h index a042500..93bdf8f 100644 --- a/de.h +++ b/de.h @@ -50,6 +50,7 @@ namespace DE static const double DEFAULTSCALE; static const double DEFAULTCROSSOVER; + static const double DEFAULTRANGE; static const double BIGDOUBLE; diff --git a/de.inl b/de.inl index d8a6627..9b48a32 100644 --- a/de.inl +++ b/de.inl @@ -17,7 +17,10 @@ template const double DE::Engine::DEFAULTCROSSOVER = 1.0; template -const double DE::Engine::BIGDOUBLE = 1.0e30; +const double DE::Engine::DEFAULTRANGE= 1000000.0; + +template +const double DE::Engine::BIGDOUBLE = 9.0e300; // close to max double template inline DE::Engine::Engine() @@ -27,7 +30,7 @@ inline DE::Engine::Engine() for ( unsigned int i=0;iminimum[i] = -BIGDOUBLE; + this->minimum[i] = -DEFAULTRANGE; this->maximum[i] = -(this->minimum[i]); } diff --git a/example.cpp b/example.cpp new file mode 100644 index 0000000..bb593f8 --- /dev/null +++ b/example.cpp @@ -0,0 +1,43 @@ +#include + +#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; +}