From aaa733d789612289396d69bb6cf3a0e5de1798a8 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 4 Aug 2012 16:59:49 -0600 Subject: Optomized the mating / mutation setup. It should work the same, but it selects the number of bits to mutate first, then selects the individual bits instead of testing a random number on each bit. For very large phenotypes this will be significantly faster while producing about the same result. --- default.bld | 4 ++-- src/operatorbasic.cpp | 19 +++++++++---------- src/tests/binary.cpp | 3 ++- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/default.bld b/default.bld index 65c8adb..741f16a 100644 --- a/default.bld +++ b/default.bld @@ -1,5 +1,5 @@ -CXXFLAGS += "-pg"; -LDFLAGS += "-pg"; +# CXXFLAGS += "-pg"; +# LDFLAGS += "-pg"; target "libgenetic.a" { diff --git a/src/operatorbasic.cpp b/src/operatorbasic.cpp index ce37f78..2ac2b0f 100644 --- a/src/operatorbasic.cpp +++ b/src/operatorbasic.cpp @@ -28,21 +28,20 @@ Genetic::Phenotype *Genetic::OperatorBasic::mate( const Genetic::PhenotypeList &lParents ) { Genetic::Phenotype *pChild = lParents.first()->makeEmptyOffspring(); - + + int iBits = lParents.first()->getSize(); // Right now, we assume both parents have the same amount of "DNA" - int iSplitPoint = ((uint32_t)Bu::Random::rand())%lParents.first()->getSize(); + int iSplitPoint = ((uint32_t)Bu::Random::rand())%iBits; pChild->copyFrom( *lParents.first(), 0, iSplitPoint ); - pChild->copyFrom( *lParents.last(), iSplitPoint, - lParents.last()->getSize()-iSplitPoint ); + pChild->copyFrom( *lParents.last(), iSplitPoint, iBits-iSplitPoint ); + + int iChange = iBits * (Bu::Random::randNorm() * fMutationRate * 1.25); - // Mutate the child some - for( int j = 0; j < pChild->getSize(); j++ ) + for( int j = 0; j < iChange; j++ ) { - //sio << Bu::Random::randNorm() << sio.nl; - if( Bu::Random::randNorm() <= fMutationRate ) - pChild->mutate( j, 1.0 ); + pChild->mutate( Bu::Random::rand( 0, iBits ), 1.0 ); } - + return pChild; } diff --git a/src/tests/binary.cpp b/src/tests/binary.cpp index 727feec..3e2cbc7 100644 --- a/src/tests/binary.cpp +++ b/src/tests/binary.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include using namespace Bu; @@ -13,7 +14,7 @@ int main( int argc, char *argv[] ) Bu::Random::setGenerator(); Bu::Random::seed( time( NULL ) ); - Genetic::OperatorBasic op( new Genetic::PhenotypeBinary( 422*1024 ), 0.0 ); + Genetic::OperatorBasic op( new Genetic::PhenotypeBinary( 3453120 ), 0.000125 ); Genetic::Phenotype *pPb1 = op.random(); Genetic::Phenotype *pPb2 = op.random(); Genetic::Phenotype *pPb3; -- cgit v1.2.3