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. --- src/operatorbasic.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'src/operatorbasic.cpp') 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; } -- cgit v1.2.3