added example code
added better default range fixed BIGDOUBLE to be closer to max double (used for default best fitness)
This commit is contained in:
1 parent
6e2d4bf3ca
commit
cab0760a99
3 files changed
+49
-2
No files matched your search
@@ -50,6 +50,7 @@ namespace DE
|
||||
|
||||
static const double DEFAULTSCALE;
|
||||
static const double DEFAULTCROSSOVER;
|
||||
static const double DEFAULTRANGE;
|
||||
static const double BIGDOUBLE;
|
||||
|
||||
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
|
||||
|
||||
+43
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user