-
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
82db9aa
commit a9f70d5
Showing
1 changed file
with
80 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,80 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include "dna.cpp" | ||
using namespace std; | ||
|
||
class Population{ | ||
private: | ||
vector<Dna> population; | ||
vector<Dna> matingPool; | ||
int generation; | ||
bool finished; | ||
Dna target; | ||
int mutatoinRate; | ||
float perfectScore = 1; | ||
public: | ||
Population(Dna target,int mutationRate):target{target},mutatoinRate{mutatoinRate},finished{false}{} | ||
|
||
void initializePopulation(int popMax){ | ||
int len = target.gens.length(); | ||
for(int j=0; j<popMax; j++){ | ||
population.push_back(Dna(len)); | ||
} | ||
cout << "population initialized with " << population.size() << endl; | ||
} | ||
void printPopulation() const{ | ||
for(auto elem: population){ | ||
cout << elem.gens << endl; | ||
} | ||
} | ||
void calFitness(){ | ||
for(auto& elem: population){ | ||
elem.calFitness(target); | ||
if(elem.fitness == target.fitness){ | ||
cout << "found it"; | ||
cout << elem.gens << endl; | ||
finished = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
float getMaxScore(){ | ||
int maxScore =0.0; | ||
for(auto& elem: population){ | ||
//cout << "elem.fitness " << elem.fitness << endl; | ||
if(maxScore < elem.fitness) | ||
maxScore = elem.fitness; | ||
} | ||
return maxScore; | ||
} | ||
|
||
void buildMatPool(){ | ||
int max = getMaxScore(); | ||
if(max<=0){ | ||
cout << "max calculation is zero"; | ||
max = 1; | ||
} | ||
cout << "maxScore " << max << endl; | ||
for(auto & elem: population){ | ||
float n = floor(elem.fitness/max); | ||
for(int i=0; i<n*100; i++){ | ||
matingPool.push_back(elem); | ||
} | ||
} | ||
} | ||
void generate(){ | ||
for(int i=0; i<population.size(); i++){ | ||
int a = floor(rand()%matingPool.size()); | ||
int b = floor(rand()%matingPool.size()); | ||
Dna partnerA = matingPool[a]; | ||
Dna partnerB = matingPool[b]; | ||
Dna child = partnerA.crossover(partnerB); | ||
child.mutate(mutatoinRate); | ||
population[i] = child; | ||
} | ||
} | ||
bool isFinished(){ | ||
return finished; | ||
} | ||
}; |