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:

alt text

  • 2
    > Here, the code is somehow very crookedly inserted, and there are no special tags, there is a special magic button on which 10101 is drawn - insolor
  • services like [Pastie] (pastie.org/) can also be used to avoid cluttering up the issue with a large code listing - Specter
  • @ okwell5, To format a code, select it with the mouse and click on the button 101010 of the editor. - Nicolas Chabanovsky ♦
  • one
    > Services like Pastie can also be used to avoid cluttering up a question with a large code listing. They will not all go. Better make spoiler support. - gammaker

1 answer 1

The basics of working with graphics, see here.

You will need a circle drawing function. As a starting point, you can take this

 void drawCircle(float x, float y, float radius, int segments) { glBegin(GL_TRIANGLE_FAN); glVertex2f(x, y); for (int i = 0; i <= segments; ++i) { float t = 2*M_PI*i/segments glVertex2f(x + sin(t)*radius, y + cos(t)*radius); } glEnd(); } 

Drawing it in time or coordinates, depending on the value of the chromosomes, you get the dynamics of the development of populations.