From 56988744209cd9d29ddb746c0be3665080bf844d Mon Sep 17 00:00:00 2001 From: Karol Piotrowski Date: Tue, 26 May 2020 12:49:35 +0000 Subject: [PATCH] =?UTF-8?q?Prze=C5=9Blij=20pliki=20do=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gmo.cpp | 142 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 126 insertions(+), 16 deletions(-) diff --git a/gmo.cpp b/gmo.cpp index 9cea8f4..773897e 100644 --- a/gmo.cpp +++ b/gmo.cpp @@ -6,12 +6,13 @@ #include #include #include +#include using namespace std; string generateValue() { char trash[100]; - string x = itoa(((rand() % 10) * 100 + (rand() % 10) *10 + (rand() % 10)),trash,10); + string x = itoa(rand() % 1000,trash,10); if (x.size() == 2) { x = "0" + x; } @@ -80,42 +81,151 @@ int fitness(string vegetable) { return (taste+colour+size)/3; } -//Selection jeszcze nie dokonczone -void selection(string * population,string * nextGen, int populationSize, int nextGenSize) { + +bool comparePair(const pair&i, const pair&j) +{ + return i.first > j.first; +} + + +void selection(string * population,string * parents, int populationSize, int parentsNumber) { int i; - int fitnessTable[populationSize]; - for(i=0;i fitnessTable[populationSize]; + for(i=0;i()); + sort(fitnessTable,fitnessTable+populationSize,comparePair); + cout << "fitnessTable:" << endl; for(i=0;i= nextGenSize) { + break; + } + else { + nextGen[counter] = parents[i]; + counter +=1; + } + } + + for(i=0;i= nextGenSize) { + break; + } + else { + for(j=i;j= nextGenSize) { + break; + } + else { + string couple[2]; + couple[0] = parents[i]; + couple[1] = parents[j]; + nextGen[counter] = cross(couple); + counter += 1; + } + } + } + } +} + int main() { + srand(time(0)); int i; - int size = 20; - string population[size]; + int populationSize = 50; + int parentsNumber = 5; + int maxPopulationSize = 1000; + string population[populationSize]; + int nextGenSize; - generatePopulation(population,size); + generatePopulation(population,populationSize); + + cout << "Wstepna populacja:" << endl; + for(i=0;i