forked from cppnuts-yt/Optimization-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDna.cpp
54 lines (52 loc) · 1.56 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
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
class Dna{
public:
string gens;
double fitness;
Dna(){gens="";fitness = 0;}
Dna(string str):gens{str}{}
Dna(int len):gens(len,'a'){
for(int i=0; i<len; i++){
gens[i] = getNewChar();
}
}
char getNewChar(){
int r = rand()%(122-63+1)+63;
if(r == 63) r=32;
if(r == 64) r=46;
return (char)r;
}
void calFitness(Dna& target){
float score=0;
for(int i=0; i<target.gens.length(); i++){
if(gens[i] == target.gens[i])
score++;
}
fitness = score/target.gens.length();
// cout << fitness << endl;
}
Dna crossover(Dna& partner){
Dna child(partner.gens.length());
int mid = floor(rand()%partner.gens.length());
for(int i=0; i<partner.gens.length(); i++){
if(i>mid)
child.gens[i] = gens[i];
else
child.gens[i] = partner.gens[i];
}
//cout << "created Child " << child.gens << endl;
return child;
}
void mutate(float mutationRate){
for(int i=0; i<gens.length(); i++){
float r = floor(rand()%100+1)/100;
if(mutationRate>r){
gens[i] = getNewChar();
}
}
cout << "gens: " << gens << endl;
}
};