diff options
author | Mike Buland <mike@xagasoft.com> | 2012-07-09 12:01:50 -0600 |
---|---|---|
committer | Mike Buland <mike@xagasoft.com> | 2012-07-09 12:01:50 -0600 |
commit | 1f7c135934b6604c5409d4b6f34861105c0a64cb (patch) | |
tree | cc421e2e8620b72e202f0eddf2cd5f1478d3bc06 /src/tests | |
parent | 40ee7ad5aeadeb9823e1cd6e1218a1999c608a65 (diff) | |
download | libgenetic-1f7c135934b6604c5409d4b6f34861105c0a64cb.tar.gz libgenetic-1f7c135934b6604c5409d4b6f34861105c0a64cb.tar.bz2 libgenetic-1f7c135934b6604c5409d4b6f34861105c0a64cb.tar.xz libgenetic-1f7c135934b6604c5409d4b6f34861105c0a64cb.zip |
It works well enough to solve polynomial maxima.
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/maxima/fitnessfunctioneq.cpp | 25 | ||||
-rw-r--r-- | src/tests/maxima/fitnessfunctioneq.h | 15 | ||||
-rw-r--r-- | src/tests/maxima/main.cpp | 39 |
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 | |||
4 | FitnessFunctionEq::FitnessFunctionEq() | ||
5 | { | ||
6 | } | ||
7 | |||
8 | FitnessFunctionEq::~FitnessFunctionEq() | ||
9 | { | ||
10 | } | ||
11 | |||
12 | double 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 | |||
6 | class FitnessFunctionEq : public Genetic::FitnessFunction | ||
7 | { | ||
8 | public: | ||
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> | ||
11 | using namespace Bu; | ||
12 | |||
13 | int 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 | |||