Prześlij pliki do ''

This commit is contained in:
Karol Piotrowski 2020-05-26 12:49:35 +00:00
parent 007a0c2661
commit 5698874420

156
gmo.cpp
View File

@ -6,12 +6,13 @@
#include <math.h>
#include<bits/stdc++.h>
#include <algorithm>
#include <utility>
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) {
int i;
int fitnessTable[populationSize];
for(i=0;i<populationSize;i++) {
fitnessTable[i] = fitness(population[i]);
}
sort(fitnessTable,fitnessTable+populationSize,greater<int>());
for(i=0;i<populationSize;i++) {
fitnessTable[i] = 1000 - fitnessTable[i];
cout << fitnessTable[i] << endl;
}
bool comparePair(const pair<int, string>&i, const pair<int, string>&j)
{
return i.first > j.first;
}
void selection(string * population,string * parents, int populationSize, int parentsNumber) {
int i;
pair <int,string> fitnessTable[populationSize];
for(i=0;i<populationSize;i++) {
fitnessTable[i] = make_pair(fitness(population[i]),population[i]);
}
sort(fitnessTable,fitnessTable+populationSize,comparePair);
cout << "fitnessTable:" << endl;
for(i=0;i<populationSize;i++) {
cout << fitnessTable[i].first << " " << fitnessTable[i].second << endl;
}
for(i=0;i<parentsNumber;i++) {
parents[i] = fitnessTable[i].second;
}
}
int newton(int n,int k) {
if (k==0 or n==k) return 1;
else return newton(n-1,k-1) + newton(n-1,k);
}
string mutate(string child) {
int d3 = rand() % 3;
string mutation = generateValue();
switch(d3) {
case 0:
child = mutation + child.substr(3,6);
break;
case 1:
child = child.substr(0,3) + mutation + child.substr(6,3);
break;
case 2:
child = child.substr(0,6) + mutation;
break;
}
}
string cross(string parent[2]) {
int i;
string child = "";
for(i=0;i<3;i++) {
child += parent[rand() % 2].substr(i*3,3);
}
int d1000 = rand() % 1000;
if (d1000 == 0) {
cout << "MUTACJA!";
child = mutate(child);
}
return child;
}
void crossover(string * parents,string * nextGen,int parentsNumber,int nextGenSize) {
int counter = 0;
int i,j;
for(i=0;i<parentsNumber;i++) {
if (counter >= nextGenSize) {
break;
}
else {
nextGen[counter] = parents[i];
counter +=1;
}
}
for(i=0;i<parentsNumber;i++) {
if (counter >= nextGenSize) {
break;
}
else {
for(j=i;j<parentsNumber;j++) {
if (counter >= 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<populationSize;i++) {
cout << population[i] << endl;
}
string parents[parentsNumber];
selection(population,parents,populationSize,parentsNumber);
cout << "Wybrane do rozrodu:" << endl;
for(i=0;i<parentsNumber;i++) {
cout << parents[i] << endl;
}
int ffa = newton(parentsNumber,2);
if (ffa + parentsNumber <= maxPopulationSize) nextGenSize = ffa + parentsNumber;
else nextGenSize = maxPopulationSize;
string nextGen[nextGenSize];
crossover(parents,nextGen,parentsNumber,nextGenSize);
cout << "Nowe pokolenie:" << endl;
for(i=0;i<nextGenSize;i++) {
cout << nextGen[i] << endl;
}
string nextGen[5];
selection(population,nextGen,20,5);