#include "genetic/operatorbasic.h" #include "genetic/phenotype.h" #include #include using namespace Bu; Genetic::OperatorBasic::OperatorBasic( Phenotype *pProgenitor, float fMutationRate ) : pProgenitor( pProgenitor ), fMutationRate( fMutationRate ) { } Genetic::OperatorBasic::~OperatorBasic() { delete pProgenitor; } Genetic::Phenotype *Genetic::OperatorBasic::random() { Genetic::Phenotype *pNew = pProgenitor->makeEmptyOffspring(); pNew->randomize(); return pNew; } Genetic::Phenotype *Genetic::OperatorBasic::mate( const Genetic::PhenotypeList &lParents ) { Genetic::Phenotype *pChild = lParents.first()->makeEmptyOffspring(); // Right now, we assume both parents have the same amount of "DNA" int iSplitPoint = ((uint32_t)Bu::Random::rand())%lParents.first()->getSize(); pChild->copyFrom( *lParents.first(), 0, iSplitPoint ); pChild->copyFrom( *lParents.last(), iSplitPoint, lParents.last()->getSize()-iSplitPoint ); // Mutate the child some for( int j = 0; j < pChild->getSize(); j++ ) { //sio << Bu::Random::randNorm() << sio.nl; if( Bu::Random::randNorm() <= fMutationRate ) pChild->mutate( j, 1.0 ); } return pChild; }