The image does not evolve...also, problem with a while loop.

well, i was making a program that would evolve a random set of pixels to look exactly the same as another, simple image (the image is in fact, a simple red happy face).

however, the random generated image does not evolve (the program just show random generated images, again and again, if it does at all)or (in the last advance i managed to make) just give the impresion of being evolving, and then just stops after a few generations, without showing any real advance.
But now, that i put a simple function wich porpouse is only to order and execute the rest of the functions (before, i would simply order the rest of the fuctions in the main function)the screen just goes black (the problem lies in the while loop, but i dont really know why it makes a problem).

here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
void putpixel(int x, int y, int color)
{
  unsigned int *ptr = (unsigned int*)screen->pixels;
  int lineoffset = y * (screen->pitch / 4);
  ptr[lineoffset + x] = color;
}

Uint32 get_pixel32( SDL_Surface *surface, int x, int y )
{
    //Convert the pixels to 32 bit
    Uint32 *pixels = (Uint32 *)surface->pixels;

    //Get the requested pixel
    return pixels[ ( y * surface->w ) + x ];
}

void put_pixel32( SDL_Surface *surface, int x, int y, Uint32 pixel )
{
    //Convert the pixels to 32 bit
    Uint32 *pixels = (Uint32 *)surface->pixels;
    
    //Set the pixel
    pixels[ ( y * surface->w ) + x ] = pixel;
}

//start organism arrays; each organism has 6400 genes (80x80)
unsigned int poblacion[POBLACION][80][80];
unsigned int modelorganism[80][80];
unsigned int nuevapoblacion[POBLACION][80][80];
int totalfitness;
int currentfitness[POBLACION];
int get_fittest();
void reproduccion();
void show_poporg();


void create_generation()
{
	//
	unsigned int allelos[3] = {get_pixel32( Alleles, 0, 0),get_pixel32( Alleles, 1, 0),get_pixel32( Alleles, 2, 0)};
	//
	for (int i = 0; i < POBLACION; i++)
	{
		for (int j = 0; j < 80; j++)
		{
			for (int h = 0; h < 80; h++)
			{
				poblacion[i][j][h] = allelos[rand()%3];
			}
		}
	}

	//
	for (int e = 0; e < 80; e++)
	{
		for(int t = 0; t < 80; t++)
		{
			modelorganism[e][t] = get_pixel32( Smiley, 0+e, 0+t );
		}
	}

}

int get_fittest()
{
	//variables (fitness)
	int fitnesstally;
	//compare every member of the poblation to the model
	for ( int h = 0; h < POBLACION; h++)
	{
		fitnesstally = 0;
		for (int i = 0; i < 80; i++)
		{
			for (int j = 0; j < 80; j++)
			{
				if ( poblacion[h][i][j] == modelorganism[i][j])
				{
					++fitnesstally;
				}
			}
		}

		currentfitness[h] = fitnesstally;
		totalfitness += currentfitness[h];
	}

	if (totalfitness == MAXFITNESS)
		{
			return 0;
		}

		else

		{
			return 1;
		}
}

int get_organism()
{
	int selecthresh;
	int parent;
	int running_total;

	selecthresh = rand()%(totalfitness+1);
	running_total=0;

	for(parent=0; parent < POBLACION; parent++)
	{
		running_total += currentfitness[parent];

		if (running_total >= selecthresh)
		{
			return parent;
		}
	}
	return 0;
}


void reproduccion()
{
	int threshold = 40;
	int parent1;
	int parent2;

	for (int h = 0; h < POBLACION; h++)
	{
		parent1 = get_organism();
		parent2 = get_organism();
		for (int i = 0; i < 80; i++)
		{
			for (int j = 0; j < 80; j++)
			{
				if ( i < threshold)
				{
					nuevapoblacion[h][i][j] = poblacion[parent1][i][j];
				}

				else
				{
					nuevapoblacion[h][i][j] = poblacion[parent2][i][j];
				}
			}
		}
	}

	for (int p=0; p<POBLACION; p++)
	{
		for (int t=0; t<80; t++)
		{
			for ( int e=0; e<80; e++)
			{
			poblacion[p][t][e] = nuevapoblacion[p][t][e];
			}
		}
	}
}



void set_model()
{
	//put the pixels
	for (int i=0; i<80; i++)
	{
		for (int j=0; j<80; j++)
		{
			put_pixel32(screen, 10+i,10+j, modelorganism[i][j]);
		}
	}
}

void show_poporg()
{
	for( int i = 0; i < 80; i++)
	{
		for( int j = 0; j < 80; j++ )
		{
			put_pixel32(screen, 10+i, 100+j, poblacion[1][i][j]);
		}
	}
}

void produce_EV()
{
	create_generation();
	int cont = 1;
	while (cont == 1 )
	{
	cont = get_fittest();
	reproduccion();
	show_poporg();
	}
}

Timer::Timer()
{
    //Initialize the variables
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;
}

void Timer::start()
{
    //Start the timer
    started = true;

    //Unpause the timer
    paused = false;

    //Get the current clock time
    startTicks = SDL_GetTicks();
}

void Timer::stop()
{
    //Stop the timer
    started = false;

    //Unpause the timer
    paused = false;
}

void Timer::pause()
{
    //If the timer is running and isn't already paused
    if( ( started == true ) && ( paused == false ) )
    {
        //Pause the timer
        paused = true;

        //Calculate the paused ticks
        pausedTicks = SDL_GetTicks() - startTicks;
    }
}

void Timer::unpause()
{
    //If the timer is paused
    if( paused == true )
    {
        //Unpause the timer
        paused = false;

        //Reset the starting ticks
        startTicks = SDL_GetTicks() - pausedTicks;

        //Reset the paused ticks
        pausedTicks = 0;
    }
}

int Timer::get_ticks()
{
    //If the timer is running
    if( started == true )
    {
        //If the timer is paused
        if( paused == true )
        {
            //Return the number of ticks when the timer was paused
            return pausedTicks;
        }
        else
        {
            //Return the current time minus the start time
            return SDL_GetTicks() - startTicks;
        }
    }

    //If the timer isn't running
    return 0;
}

bool Timer::is_started()
{
    return started;
}

bool Timer::is_paused()
{
    return paused;
}

int main( int argc, char* args[] )
{
    //Quit flag
    bool quit = false;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //Load the files
	    if( load_files() == false )
    {
        return 1;
    }

	Timer fps;

	//make the screen white
	SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );

    //While the user hasn't quit
    while( quit == false )
    {
		//Start the frame timer
        fps.start();

        //While there's event to handle
        while( SDL_PollEvent( &event ) )
        {
            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }
		//Apply the images to the screen
		set_model();
		show_organism();
		produce_EV();
		

		//Update the screen
		if( SDL_Flip( screen ) == -1 )
		{
			return 1;
		}

		//Cap the frame rate
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
    }

	clean_up();
    return 0;
}


im using SDL as mi graphic compiler by the way.
thanks beforehand
Last edited on
You're throwing us off with a bad title, I believe what you are looking for is someone to help you with Data Compression. Unfourtunatly that is a field I do not have enough experiance in myself to help you in.

I would suggest you change your title to reflect what you actually need help with, then more people may be able to help you.
i don´t actually know what you mean with data compression, but what i was actually looking for corrections on my evolutionary algorithm, however, if you could explain why did you thought data comrpesion was one of my problems? ; also im sorry if the title is misleading, but i don´t really know how to express my problem(s), i really thought it explained enough (even then i changed it, so it would be more understable)
Where is the definition of show_organism, called on line 328?
show_organism only shows a random generated image, and has no real porpouse on the evolution (i only needed to compare to "evolutive organism" to a random generated one)

however, here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void show_organism()
{
	unsigned int allelos[3] = {get_pixel32( Alleles, 0, 0),get_pixel32( Alleles, 1, 0),get_pixel32( Alleles, 2, 0)};
	//create the variables
	unsigned int genome[80][80];
	//define organism genome
	for (int i=0; i<80; i++)
	{
		for (int j=0; j<80; j++)
		{
			genome[i][j] = allelos[rand()%3];
		}
	}
	//put the pixels
	for (int i=0; i<80; i++)
	{
		for (int j=0; j<80; j++)
		{
			put_pixel32(screen, 100+i,10+j, genome[i][j]);
		}
	}

}
What is the value of MAXFITNESS? Is it 80*80*POBLACION? produce_EV will only return when the fitnesstally equals this value (every member of population matches model) and because you are double buffered, no screen updates will occur until you call SDL_Flip. This wants to be changed so produce_ev only does a single interation.

Your show_poporg only shows one member of the population.

I'm not convinced by your reproduction operator. By taking the first half of one parent and the second half of the other parent, with no mutation, I'm not convinced you are guaranteed to ever evolve to the model.

Yes, the value of MAXFITNESS is 80*80*POBLACION. And supposdly, the purpose of produce_EV is to continue the evolution process as long as totalfitness is not equal to MAXFITNESS. however, when i run the program, everything just turns blacks (if i eliminate the "while" part of produce_EV, this doesn't happens).

And exactly what could i do to change the SDL_Flip problem?

show_poporg only shows one member of the population, because i thought that it was unnecessary to do otherwise.

And yes, i understand that right now my evolutionary process, but i didnt want to put a mutation process until i could make a simple reproduction process work. The problem is, that i dont see (when i can see, and that is when i eliminate the while part of produce_EV) any kind of evolutionary process.
As I tried to explain, the problem is that your produce_ev will never return. Because of this SDL_Flip is never called, and nothing is ever drawn on the screen.

If you remove the while loop from produce_ev, then every time you call it, it will call create_generation every time.

You need to rewrite produce_ev so that create_generation is only called once in total, but then that each call to produce_ev only calls reproduccion once, and only if the we haven't evolved into the target.
i have modified produce_ev, so create_generation only gets called once.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int first = 0;
void produce_EV()
{
	int cont = 0;

	 if (first == 0)
	 {
		 create_generation();
		 first = 1;
	 }

	if (cont == 0 )
	{
	cont = get_fittest();
	reproduccion();
	show_poporg();
	}
}


Now, the image seems to evolve, but it just makes some generations (or it looks like it, the image at least changes) and then suddenly stops.
My best guess (and I'm totally guessing now) is that the population has converged. Like I said above, I don't think the reproduction operator is any good.
using a simple log function, i managed to see that for some reason, my get_fittest() function never returns 0,even when i leave "return 0" as it's only posibility (in wich case, the program will crash). i believe this to be the cause of the problems as it is the only clue i can get, even when i dont understand why the image would seem to evolve at first, without the get_fittest() function working.

However, i have no idea why get_fittest never returns that value.
And thank you for your previous replies.
get_fittest only returns 0 when every member of the population is the target. With your reproduction operator, I think this is highly unlikely to happen.

Your reproduction operator takes whole columns from the parents. What if the model has a column in it, that is not in the initial population. Can we ever reach the model?
I assume that it would still be possible to get to the model, even if the process were slower.

But that aside, i managed to see the actual problem, as why i get a black screen. with the log i managed to see that the image was actually evolving into the model, the problem is that, the program never gets to the "update the screen" and "cap the frame rate" part, because my evolution is infinitely looping. in theory, i should be able to see only the final result (asuming that my image would evolve to that point).

any idea of how to fix this, in a form that i could actually see the process of the evolution?
Topic archived. No new replies allowed.