From 1f7c135934b6604c5409d4b6f34861105c0a64cb Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 9 Jul 2012 12:01:50 -0600 Subject: It works well enough to solve polynomial maxima. --- src/tests/maxima/fitnessfunctioneq.cpp | 25 ++++++++++++++++++++++ src/tests/maxima/fitnessfunctioneq.h | 15 +++++++++++++ src/tests/maxima/main.cpp | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 src/tests/maxima/fitnessfunctioneq.cpp create mode 100644 src/tests/maxima/fitnessfunctioneq.h create mode 100644 src/tests/maxima/main.cpp (limited to 'src/tests') 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 @@ +#include "fitnessfunctioneq.h" +#include "genetic/phenotypebinary.h" + +FitnessFunctionEq::FitnessFunctionEq() +{ +} + +FitnessFunctionEq::~FitnessFunctionEq() +{ +} + +double FitnessFunctionEq::operator()( Genetic::Phenotype *pTest ) +{ + Genetic::PhenotypeBinary *pbTest = + dynamic_cast( pTest ); + if( pbTest == NULL ) + return 0.0; + + uint32_t uRaw; + pbTest->extractBits( uRaw, 0, 32 ); + double x = ((double)uRaw / (double)(0xfffffffful))*5.0 - 2.5; + + return -1.8*(x*x*x*x) + 0.86*(x*x*x) + 4.0*(x*x); +} + 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 @@ +#ifndef GENETIC_FITNESS_FUNCTION_EQ_H +#define GENETIC_FITNESS_FUNCTION_EQ_H + +#include "genetic/fitnessfunction.h" + +class FitnessFunctionEq : public Genetic::FitnessFunction +{ +public: + FitnessFunctionEq(); + virtual ~FitnessFunctionEq(); + + virtual double operator()( Genetic::Phenotype *pTest ); +}; + +#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 @@ +#include "genetic/population.h" +#include "genetic/phenotypebinary.h" +#include "genetic/operatorbasic.h" +#include "genetic/explicitsimulation.h" +#include "fitnessfunctioneq.h" + +#include + +#include +#include +using namespace Bu; + +int main( int argc, char *argv[] ) +{ + Bu::Random::seed( time( NULL ) ); + sio << "Global maxima equation test" << sio.nl + << " - -1.8*x^4 + 0.86*x^3 + 4.0*x^2 == 3.53518 (approx)" << sio.nl + << sio.nl; + + Genetic::ExplicitSimulation ex( + new Genetic::OperatorBasic( + new Genetic::PhenotypeBinary( 32 ), + 0.01 + ), + new FitnessFunctionEq(), + 100, + .1, .05 + ); + + for( int j = 0; j < 100; j++ ) + { + ex.timestep(); + sio << ex.getMinFitness() << " - " << ex.getMaxFitness() << + sio.nl; + } + + return 0; +} + -- cgit v1.2.3