-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdna.cpp
55 lines (42 loc) · 1.13 KB
/
dna.cpp
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
#include "dna.h"
Config config;
DNA::DNA() {
this->target = config.TARGET_PHRASE;
}
DNA::DNA(int genesLength) {
this->target = config.TARGET_PHRASE;
for(int i = 0; i < genesLength; i++) {
this->genes += (char) genRandom(32, 128);
}
}
DNA DNA::crossover(DNA parent1, DNA parent2) {
DNA child;
int midpt = int(genRandom(0, parent1.genes.length()));
for (int i=0; i < parent1.genes.length(); i++) {
if (i > midpt) {
child.genes += parent1.genes[i];
} else {
child.genes += parent2.genes[i];
}
}
std::cout<<"produced child with genes "<<child.getGenes()<<std::endl;
return child;
}
void DNA::mutate(float mutationRate) {
for (int i=0; i< this->genes.length(); i++) {
if (genRandom(0, 100) < int(mutationRate * 100)) {
this->genes[i] = (char) genRandom(32, 128);
std::cout<<"mutating to: "<<this->genes<<std::endl;
}
}
}
void DNA::fitness() {
int score = 0;
for (int i=0; i< this->genes.length(); i++) {
if (this->genes[i] == this->target[i]) {
score++;
}
}
this->fitness_value = float(score)/(this->target.length());
std::cout<<"Fitness value for "<< this->genes<<": "<<this->fitness_value<<std::endl;
}