-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNA.cpp
More file actions
60 lines (50 loc) · 1.62 KB
/
DNA.cpp
File metadata and controls
60 lines (50 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "DNA.h"
//-------------------------------------------------------
DNA::DNA(){
mutationAmount = 1;
}
//-------------------------------------------------------
void DNA::setup(int _numOfGenes, int _geneSize)
{
numOfGenes = _numOfGenes;
geneSize = _geneSize;
// initialize genes
for (int i = 0; i < numOfGenes; i++) {
for (int j = 0; j < geneSize; j++) {
genes.push_back(ofRandom(1));
}
}
}
//------------------------------------------------------------------
// Crossover
DNA DNA::crossover(DNA partner) {
// initialize and setup a local, temporary DNA object
DNA child;
child.setup(numOfGenes, geneSize);
// pick a random number within the genesize
int midpoint = int(ofRandom(genes.size()));
// create genes
for (int i = 0; i < genes.size(); i++) {
// all the genes indexed above the midpoint are taken from parent one
// all genes indexed below or equal to the midpoint are taken from two parent
if (i > midpoint){
child.genes[i] = genes[i];
}else{
child.genes[i] = partner.genes[i];
}
}
// return the local temporary DNA object
return child;
}
//------------------------------------------------------------------
void DNA::mutate(float mutationRate) {
// for each gene
// test if a random number falls below the mutation rate
// if below, give the current gene a new random value
// within the mutation amount
for (int i = 0; i < genes.size(); i++) {
if (ofRandom(1) < mutationRate) {
genes[i] = ofRandom(mutationAmount);
}
}
}