-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8697301
commit 28d245d
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(){ | ||
char r = (char)rand()%(122-63+1)+63; | ||
if(r == 63) r=' '; | ||
if(r == 64) r='.'; | ||
return 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; | ||
// 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(int mutationRate){ | ||
for(int i=0; i<gens.length(); i++){ | ||
int r = floor(rand()%100+1); | ||
cout << "r" << r << "mutationRate" << mutationRate << endl; | ||
if(mutationRate>r) | ||
gens[i] = getNewChar(); | ||
} | ||
//cout << "gens: " << gens << endl; | ||
} | ||
}; |