2020-05-24 22:32:46 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
|
|
#include <ctime>
|
|
|
|
#include <string>
|
|
|
|
#include <math.h>
|
|
|
|
#include<bits/stdc++.h>
|
|
|
|
#include <algorithm>
|
2020-05-26 14:49:35 +02:00
|
|
|
#include <utility>
|
2020-05-24 22:32:46 +02:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
string generateValue() {
|
|
|
|
|
|
|
|
char trash[100];
|
2020-05-26 14:49:35 +02:00
|
|
|
string x = itoa(rand() % 1000,trash,10);
|
2020-05-24 22:32:46 +02:00
|
|
|
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<length;i++) {
|
|
|
|
population[i] = generateVegetable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int power(int x, int y) {
|
|
|
|
|
|
|
|
if (y == 0) return 1;
|
|
|
|
if (y == 1) return x;
|
|
|
|
|
|
|
|
int temp = power(x, y/2);
|
|
|
|
if (y%2 == 0) return temp * temp;
|
|
|
|
else return x * temp * temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int stringToInt(string str,int size) {
|
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
int i;
|
|
|
|
reverse(str.begin(),str.end());
|
|
|
|
|
|
|
|
for(i=0;i<size;i++) {
|
|
|
|
x += (str[i] - '0') * power(10,i);
|
|
|
|
}
|
|
|
|
|
|
|
|
reverse(str.begin(),str.end());
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int fitness(string vegetable) {
|
|
|
|
|
|
|
|
int taste = stringToInt(vegetable.substr(0,3),3);
|
|
|
|
int colour = stringToInt(vegetable.substr(3,3),3);
|
|
|
|
int size = stringToInt(vegetable.substr(6,3),3);
|
|
|
|
|
|
|
|
return (taste+colour+size)/3;
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:49:35 +02:00
|
|
|
|
|
|
|
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) {
|
2020-05-24 22:32:46 +02:00
|
|
|
|
|
|
|
int i;
|
2020-05-26 14:49:35 +02:00
|
|
|
pair <int,string> fitnessTable[populationSize];
|
|
|
|
for(i=0;i<populationSize;i++) {
|
|
|
|
fitnessTable[i] = make_pair(fitness(population[i]),population[i]);
|
2020-05-24 22:32:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 14:49:35 +02:00
|
|
|
sort(fitnessTable,fitnessTable+populationSize,comparePair);
|
2020-05-24 22:32:46 +02:00
|
|
|
|
2020-05-26 14:49:35 +02:00
|
|
|
cout << "fitnessTable:" << endl;
|
2020-05-24 22:32:46 +02:00
|
|
|
for(i=0;i<populationSize;i++) {
|
2020-05-26 14:49:35 +02:00
|
|
|
cout << fitnessTable[i].first << " " << fitnessTable[i].second << endl;
|
2020-05-24 22:32:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 14:49:35 +02:00
|
|
|
for(i=0;i<parentsNumber;i++) {
|
|
|
|
parents[i] = fitnessTable[i].second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int newton(int n,int k) {
|
2020-05-24 22:32:46 +02:00
|
|
|
|
2020-05-26 14:49:35 +02:00
|
|
|
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]) {
|
2020-05-24 22:32:46 +02:00
|
|
|
|
2020-05-26 14:49:35 +02:00
|
|
|
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;
|
2020-05-24 22:32:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-26 14:49:35 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:32:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
2020-05-26 14:49:35 +02:00
|
|
|
|
2020-05-24 22:32:46 +02:00
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
int i;
|
2020-05-26 14:49:35 +02:00
|
|
|
int populationSize = 50;
|
|
|
|
int parentsNumber = 5;
|
|
|
|
int maxPopulationSize = 1000;
|
|
|
|
string population[populationSize];
|
|
|
|
int nextGenSize;
|
|
|
|
|
|
|
|
generatePopulation(population,populationSize);
|
2020-05-24 22:32:46 +02:00
|
|
|
|
2020-05-26 14:49:35 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-05-24 22:32:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|