Prześlij pliki do ''

This commit is contained in:
Karol Piotrowski 2020-05-24 20:32:46 +00:00
parent ff8be9c68e
commit 007a0c2661

123
gmo.cpp Normal file
View File

@ -0,0 +1,123 @@
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <string>
#include <math.h>
#include<bits/stdc++.h>
#include <algorithm>
using namespace std;
string generateValue() {
char trash[100];
string x = itoa(((rand() % 10) * 100 + (rand() % 10) *10 + (rand() % 10)),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<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;
}
//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;
}
}
int main() {
srand(time(0));
int i;
int size = 20;
string population[size];
generatePopulation(population,size);
string nextGen[5];
selection(population,nextGen,20,5);
return 0;
}