From 4856536a29606810f0f8ce9cf05efcfee31e48eb Mon Sep 17 00:00:00 2001 From: Karol Piotrowski Date: Tue, 26 May 2020 21:34:44 +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 | 294 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 294 insertions(+) create mode 100644 gmo.cpp diff --git a/gmo.cpp b/gmo.cpp new file mode 100644 index 0000000..c644d1e --- /dev/null +++ b/gmo.cpp @@ -0,0 +1,294 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +string generateValue() { + + char trash[100]; + string x = itoa(rand() % 1000,trash,10); + if (x.size() == 2) { + x = "0" + x; + } + else if (x.size() == 1) { + x = "00" + x; + } + + return x; + +} + + +string generateVegetable() { + + string taste = generateValue(); + string colour = generateValue(); + string size = generateValue(); + + return taste + colour + size; + +} + + +void generatePopulation(string * population,int length) { + + int i; + for(i=0;i&i, const pair&j) +{ + return i.first > j.first; +} + + +void ranking(string * population,string * parents, int populationSize, int parentsNumber) { + + int i; + pair fitnessTable[populationSize]; + for(i=0;i fitnessTable[populationSize]; + for(i=0;i=0;j--) { + if(not exists(parentsNumber,taken,j)) { + sum += fitnessTable[j].first; + fitnessTable[j].first = sum; + } + + } + roulette = rand() % fitnessTable[0].first; + j = 0; + while(exists(parentsNumber,taken,j)) { + j += 1; + } + while(roulette > fitnessTable[j].first and j= nextGenSize) { + break; + } + else { + nextGen[counter] = parents[i]; + counter +=1; + } + } + while(counter < nextGenSize) { + 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; + } + } + } + } + } +} + + +string * genetic_algorithm(string * population, int populationSize, int parentsNumber) { + + int iteration,i; + for(iteration=0;iteration<5;iteration++) { + string * parents = new string[parentsNumber]; + selection(population,parents,populationSize,parentsNumber); + + string * nextGen = new string[populationSize]; + crossover(parents,nextGen,parentsNumber,populationSize); + + delete[] population; + + population = new string[populationSize]; + for(i=0;i