summaryrefslogtreecommitdiff
path: root/src/operatorbasic.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/operatorbasic.cpp')
-rw-r--r--src/operatorbasic.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/operatorbasic.cpp b/src/operatorbasic.cpp
new file mode 100644
index 0000000..ce37f78
--- /dev/null
+++ b/src/operatorbasic.cpp
@@ -0,0 +1,48 @@
1#include "genetic/operatorbasic.h"
2#include "genetic/phenotype.h"
3
4#include <bu/random.h>
5#include <bu/sio.h>
6using namespace Bu;
7
8Genetic::OperatorBasic::OperatorBasic( Phenotype *pProgenitor,
9 float fMutationRate ) :
10 pProgenitor( pProgenitor ),
11 fMutationRate( fMutationRate )
12{
13}
14
15Genetic::OperatorBasic::~OperatorBasic()
16{
17 delete pProgenitor;
18}
19
20Genetic::Phenotype *Genetic::OperatorBasic::random()
21{
22 Genetic::Phenotype *pNew = pProgenitor->makeEmptyOffspring();
23 pNew->randomize();
24 return pNew;
25}
26
27Genetic::Phenotype *Genetic::OperatorBasic::mate(
28 const Genetic::PhenotypeList &lParents )
29{
30 Genetic::Phenotype *pChild = lParents.first()->makeEmptyOffspring();
31
32 // Right now, we assume both parents have the same amount of "DNA"
33 int iSplitPoint = ((uint32_t)Bu::Random::rand())%lParents.first()->getSize();
34 pChild->copyFrom( *lParents.first(), 0, iSplitPoint );
35 pChild->copyFrom( *lParents.last(), iSplitPoint,
36 lParents.last()->getSize()-iSplitPoint );
37
38 // Mutate the child some
39 for( int j = 0; j < pChild->getSize(); j++ )
40 {
41 //sio << Bu::Random::randNorm() << sio.nl;
42 if( Bu::Random::randNorm() <= fMutationRate )
43 pChild->mutate( j, 1.0 );
44 }
45
46 return pChild;
47}
48