Task: there is a goal (circle) to which, in a certain number of steps, circle individuals either achieve or do not reach. Achievement using a genetic algorithm. I wrote the algorithm myself, everything seems to be ready. But with visualization it is hard for me. It remains only to draw objects in a cycle of steps (like movement or at least new ones like in the picture). You should get something like this (or drawing by the second) (see picture).
By code: the x, y coordinates are H [i] .genes [0] and H [i] .genes 1 respectively
Here is the code:
// moveC.cpp: ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅Ρ ΡΠΎΡΠΊΡ Π²Ρ
ΠΎΠ΄Π° Π΄Π»Ρ ΠΊΠΎΠ½ΡΠΎΠ»ΡΠ½ΠΎΠ³ΠΎ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ. // #include "stdafx.h" #include <stdlib.h> #include<stdio.h> //#include <gl/glut.h" #include <math.h> #include<glut.h> GLint Width = 512, Height = 512; //-------------------------------- const int N = 3; const int n = 4; float L = 400; float h = 1; //-------------------------------- class hromos { public: float genes[n]; }; //-------------------------------- hromos *H; //-------------------------------- hromos mutation(hromos hr) { if (hr.genes[0] < L / 2) hr.genes[0] += hr.genes[2]; //x if (hr.genes[0] > L / 2) hr.genes[0] -= hr.genes[2]; //x hr.genes[1] += hr.genes[3]; //y hr.genes[3] += h; //hy return hr; } //-------------------------------- void crossover_and_mutation() { int g1, g2; float c; hromos *ocross; ocross = new hromos[N]; hromos *mas; mas = new hromos[3 * N]; //Π²ΡΠ±ΠΎΡ 2Ρ
ΠΎΡΠΎΠ±Π΅ΠΉ int io1, io2; //ΡΠ°Π·Π½ΡΠ΅ ΠΈΠ½Π΄Π΅ΠΊΡΡ for (int i = 0; i < N / 2; ++i) { do io1 = rand(); while (io1 >= N && io1 >= n); io2 = io1; while (io2 = io1 && io2 >= n) io2 = rand(); //ΠΊΠΎΠΏΠΈΡΡΠ΅ΠΌ Π²ΡΠ±ΡΠ°Π½Π½ΡΠ΅ ΠΎΡΠΎΠ±ΠΈ Π² ΠΎΡΠ΄Π΅Π»ΡΠ½ΡΠΉ ΠΌΠ°ΡΡΠΈΠ² Π΄Π»Ρ ΠΊΡΠΎΡΡΠΈΠ½Π³ΠΎΠ²Π΅ΡΠ° ocross[i] = H[io1]; ocross[i + 1] = H[io2]; //Π²ΡΠ±ΠΈΡΠ°Π΅ΠΌ 2 ΡΠ»ΡΡΠ°ΠΉΠ½ΡΡ
Π³Π΅Π½Π° Π΄Π»Ρ ΠΎΠ±ΠΌΠ΅Π½Π° do g1 = rand(); while (g1 >= N && g1 >= n); g2 = g1; while (g2 = g1 && g2 >= n) g2 = rand(); //ΠΎΠ±ΠΌΠ΅Π½ ΠΏΠ΅ΡΠ²ΡΠΌΠΈ Π³Π΅Π½Π°ΠΌΠΈ c = ocross[i].genes[g1]; ocross[i].genes[g1] = ocross[i + 1].genes[g1]; ocross[i + 1].genes[g1] = c; //ΠΎΠ±ΠΌΠ΅Π½ Π²ΡΠΎΡΡΠΌΠΈ Π³Π΅Π½Π°ΠΌΠΈ c = ocross[i].genes[g2]; ocross[i].genes[g2] = ocross[i + 1].genes[g2]; ocross[i + 1].genes[g2] = c; //ΠΌΡΡΠ°ΡΠΈΡ ΠΏΠΎΡΠΎΠΌΠΊΠΎΠ² ocross[i] = mutation(ocross[i]); ocross[i + 1] = mutation(ocross[i + 1]); } //ΠΏΠΎΠΌΠ΅ΡΠ°Π΅ΠΌ Π²ΡΠ΅Ρ
ΠΎΡΠΎΠ±Π΅ΠΉ(ΠΏΠΎΠΏ.+ΠΏΠΎΡΠΎΠΌΠΊΠΈ) Π² 1 ΠΌΠ°ΡΡΠΈΠ² Π΄Π»Ρ ΡΠΎΡΡΠΈΡΠΎΠ²ΠΊΠΈ for (int i = 0; i < N; ++i) mas[i] = H[i]; int k = 0; for (int i = N; i < (N + N); ++i) { mas[i] = ocross[k]; k++; } //ΡΠΎΡΡΠΈΡΡΠ΅ΠΌ ΠΏΠΎ Π»ΡΡΡΠΈΠΌ ΠΏΠΎΠΊΠ°Π·Π°ΡΠ΅Π»ΡΠΌ x,y ΠΏΠΎ ΡΠ±ΡΠ²Π°Π½ΠΈΡ hromos d; for (int i = 0; i < (N + N + N); ++i) for (int j = i; j < (N + N + N); ++j) { if ((mas[i].genes[0] + mas[i].genes[1]) < (mas[j].genes[0] + mas[j].genes[1])) { d = mas[i]; mas[i] = mas[j]; mas[j] = d; } } //ΠΈΠ· mas ΡΠΎΠ·Π΄Π°Π΅ΠΌ Π½ΠΎΠ²ΡΡ ΠΏΠΎΠΏΡΠ»ΡΡΠΈ ΠΈΠ· Π»ΡΡΡΠΈΡ
ΠΎΡΠΎΠ±Π΅ΠΉ for (int i = 0; i < N; ++i) H[i] = mas[i]; //ΡΠΈΡΡΠ΅ΠΌ ΠΎΡΠΎΠ±Π΅ΠΉ //Π²ΡΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π°Π΅ΠΌ ΠΏΠ°ΠΌΡΡΡ delete[]ocross; } //-------------------------------- int _tmain(int argc, char *argv[]) { int steps; printf("\nInput count of steps: "); scanf("%d", &steps); H = new hromos[N]; for (int i = 0; i < N; ++i) for (int j = 0; j < n; ++j) H[i].genes[j] = rand(); //Π½Π° ΠΊΠ°ΠΆΠ΄ΠΎΠΌ ΡΠ°Π³Π΅ Π΄ΠΎΠ»ΠΆΠ½Π° ΡΠΈΡΠΎΠ²Π°ΡΡΡΡ Π½ΠΎΠ²Π°Ρ ΠΏΠΎΠΏΡΠ»ΡΡΠΈΡ for (int step = 1; step < steps; ++step) { crossover_and_mutation(); //Π·Π΄Π΅ΡΡ Π½Π°Π΄ΠΎ Π½Π°ΡΠΈΡΠΎΠ²Π°ΡΡ ΠΏΠΎΠ»ΡΡΠΈΠ²ΡΡΡΡΡ Π½ΠΎΠ²ΡΡ ΠΏΠΎΠΏΡΠ»ΡΡΠΈΡ H[i] } }
Picture: