summaryrefslogtreecommitdiff
path: root/src/tests/maxima
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/maxima')
-rw-r--r--src/tests/maxima/fitnessfunctioneq.cpp25
-rw-r--r--src/tests/maxima/fitnessfunctioneq.h15
-rw-r--r--src/tests/maxima/main.cpp39
3 files changed, 79 insertions, 0 deletions
diff --git a/src/tests/maxima/fitnessfunctioneq.cpp b/src/tests/maxima/fitnessfunctioneq.cpp
new file mode 100644
index 0000000..5694507
--- /dev/null
+++ b/src/tests/maxima/fitnessfunctioneq.cpp
@@ -0,0 +1,25 @@
1#include "fitnessfunctioneq.h"
2#include "genetic/phenotypebinary.h"
3
4FitnessFunctionEq::FitnessFunctionEq()
5{
6}
7
8FitnessFunctionEq::~FitnessFunctionEq()
9{
10}
11
12double FitnessFunctionEq::operator()( Genetic::Phenotype *pTest )
13{
14 Genetic::PhenotypeBinary *pbTest =
15 dynamic_cast<Genetic::PhenotypeBinary *>( pTest );
16 if( pbTest == NULL )
17 return 0.0;
18
19 uint32_t uRaw;
20 pbTest->extractBits( uRaw, 0, 32 );
21 double x = ((double)uRaw / (double)(0xfffffffful))*5.0 - 2.5;
22
23 return -1.8*(x*x*x*x) + 0.86*(x*x*x) + 4.0*(x*x);
24}
25
diff --git a/src/tests/maxima/fitnessfunctioneq.h b/src/tests/maxima/fitnessfunctioneq.h
new file mode 100644
index 0000000..7139532
--- /dev/null
+++ b/src/tests/maxima/fitnessfunctioneq.h
@@ -0,0 +1,15 @@
1#ifndef GENETIC_FITNESS_FUNCTION_EQ_H
2#define GENETIC_FITNESS_FUNCTION_EQ_H
3
4#include "genetic/fitnessfunction.h"
5
6class FitnessFunctionEq : public Genetic::FitnessFunction
7{
8public:
9 FitnessFunctionEq();
10 virtual ~FitnessFunctionEq();
11
12 virtual double operator()( Genetic::Phenotype *pTest );
13};
14
15#endif
diff --git a/src/tests/maxima/main.cpp b/src/tests/maxima/main.cpp
new file mode 100644
index 0000000..ab90e0b
--- /dev/null
+++ b/src/tests/maxima/main.cpp
@@ -0,0 +1,39 @@
1#include "genetic/population.h"
2#include "genetic/phenotypebinary.h"
3#include "genetic/operatorbasic.h"
4#include "genetic/explicitsimulation.h"
5#include "fitnessfunctioneq.h"
6
7#include <time.h>
8
9#include <bu/random.h>
10#include <bu/sio.h>
11using namespace Bu;
12
13int main( int argc, char *argv[] )
14{
15 Bu::Random::seed( time( NULL ) );
16 sio << "Global maxima equation test" << sio.nl
17 << " - -1.8*x^4 + 0.86*x^3 + 4.0*x^2 == 3.53518 (approx)" << sio.nl
18 << sio.nl;
19
20 Genetic::ExplicitSimulation ex(
21 new Genetic::OperatorBasic(
22 new Genetic::PhenotypeBinary( 32 ),
23 0.01
24 ),
25 new FitnessFunctionEq(),
26 100,
27 .1, .05
28 );
29
30 for( int j = 0; j < 100; j++ )
31 {
32 ex.timestep();
33 sio << ex.getMinFitness() << " - " << ex.getMaxFitness() <<
34 sio.nl;
35 }
36
37 return 0;
38}
39