-
Notifications
You must be signed in to change notification settings - Fork 0
/
Host.cpp
519 lines (451 loc) · 15 KB
/
Host.cpp
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//****************************************************************************
//
//
//
//****************************************************************************
// ===========================================================================
// Libraries
// ===========================================================================
#include <math.h>
#include <string.h>
// ===========================================================================
// Project Files
// ===========================================================================
#include "Host.h"
#include "DefVal.h"
//############################################################################
// #
// Class Host #
// #
//############################################################################
// ===========================================================================
// Constructors
// ===========================================================================
Host::Host(void)
{
// Called as the default constructor. We want to avoid any unnecessary computations in this case
}
Host::Host(int useless_int)
{
nb_triangles_h = DefVal::N_TRIANGLES_HOST;
host_triangles = generateTriangles(0,nb_triangles_h);
nb_triangles_p = DefVal::N_TRIANGLES_PARASITE;
paras_triangles = generateTriangles(1,nb_triangles_p);
//printf("Host generation ...\n");
}
Host::Host(const Host &h2)
{
nb_triangles_h = DefVal::N_TRIANGLES_HOST;
nb_triangles_p = DefVal::N_TRIANGLES_PARASITE;
host_triangles = new Triangle [nb_triangles_h];
paras_triangles = new Triangle [nb_triangles_p];
for(int i = 0; i < nb_triangles_h; i++)
{
host_triangles[i].x = h2.host_triangles[i].x;
host_triangles[i].w = h2.host_triangles[i].w;
host_triangles[i].h = h2.host_triangles[i].h;
}
for(int i = 0; i < nb_triangles_p; i++)
{
paras_triangles[i].x = h2.paras_triangles[i].x;
paras_triangles[i].w = h2.paras_triangles[i].w;
paras_triangles[i].h = h2.paras_triangles[i].h;
}
}
// ===========================================================================
// Destructor
// ===========================================================================
Host::~Host(void)
{
// delete [] host_triangles;
// delete [] paras_triangles;
}
//============================================================================
// Methods definition
//============================================================================
// Takes 0: hosts or 1:parasites as parameter and returns a table of triangles with random but controlled attributes
Triangle* Host::generateTriangles(int whatAmI, int howMany)
{
int win_width = DefVal::PIC_WIDTH;
int wmin = 10;
int hmin = 10;
int wmax;
int hmax;
int n_triangles = howMany;
if (whatAmI==0){
wmax = int(DefVal::PIC_WIDTH*DefVal::HOST_WIDTH/100.0);
hmax = int(DefVal::PIC_HEIGHT*DefVal::HOST_HEIGHT/100.0);
} else if (whatAmI==1) {
wmax = int(DefVal::PIC_WIDTH*DefVal::PARASITE_WIDTH/100.0);
hmax = int(DefVal::PIC_HEIGHT*DefVal::PARASITE_HEIGHT/100.0);
}
Triangle* triangles;
triangles = new Triangle[n_triangles];
for (int i=0; i<n_triangles; i++)
{
int random_x = 10+(rand()%(int)(win_width-20+1)); // Position of triangle (random in range(10,win_width))
int random_w = wmin+(rand()%(int)(wmax-wmin+1)); // Random number btw wmin and wmax
int random_h = hmin+(rand()%(int)(hmax-hmin+1)); // Random number btw hmin and hmax
if (random_w%2 == 1)
random_w-=1;
triangles[i].x = random_x;
triangles[i].w = random_w;
triangles[i].h = random_h;
}
// Check boundaries
for (int i=0; i<n_triangles; i++)
{
if (triangles[i].x-0.5*triangles[i].w < 0) // Checks if triangle is too much on the left...
triangles[i].w = 2*triangles[i].x; // ...and corrects its width
if (triangles[i].x+0.5*triangles[i].w > win_width) // Checks if triangle is too much on the right
triangles[i].w = 2*(win_width-triangles[i].x);
}
return triangles;
}
unsigned int* Host::triangleProfile(Triangle* triangles, int size_triangles)
{
unsigned int* profile;
profile = new unsigned int[DefVal::PIC_WIDTH]; // An array of 500 values.
int y;
for (y=0; y < DefVal::PIC_WIDTH; y++) {
profile[y] = 0; // Initialize all values to 0
}
for (int i=0; i<size_triangles; i++)
{
//printf("%d %f %f %f\n", i, triangles[i].h, triangles[i].w, triangles[i].x);
int x = triangles[i].x;
int w = triangles[i].w;
int h = triangles[i].h;
if (w != 0){
for (int x1=x-w/2; x1<=x; x1++)
{
y = int(2*(h/w)*(x1-x+w/2));
if (y > profile[x1])
profile[x1] = y;
}
for (int x2=x+1; x2<=x+w/2; x2++)
{
y = int(-2*(h/w)*(x2-x-w/2));
if(x2 < DefVal::PIC_WIDTH)
{
if (y > profile[x2])
profile[x2] = y;
}
}
}
}
return profile;
}
// Generates matrix of 0s and 1s depending on weather we're below or above the profile
unsigned int** Host::matrixGenerator(Triangle* triangles, int size_triangles)
{
int win_width = DefVal::PIC_WIDTH;
int win_height = DefVal::PIC_HEIGHT;
unsigned int* prof = triangleProfile(triangles, size_triangles);
unsigned int** mat = new unsigned int*[win_width]; // Creation of lines
for (int x=0; x<win_width; x++)
mat[x] = new unsigned int[win_height]; // Creation of columns
for (int x=0; x<win_width; x++) // For each column of my matrix...
{
int temp_max = prof[x]; // max y to fill
for (int y=0; y<win_height; y++)
{
if (temp_max >= y)
mat[x][y] = 1;
else
mat[x][y] = 0;
}
}
return mat;
}
unsigned int ** Host::pix_to_mat(char * name_pic, int count) const
{
unsigned int w=DefVal::PIC_WIDTH;
unsigned int h=DefVal::PIC_HEIGHT;
unsigned char* pix=new unsigned char [3*w*h];
unsigned int** matH = new unsigned int*[w]; // Creation of lines
int x, y;
for (x=0; x<w; x++)
matH[x] = new unsigned int[h]; // Creation of columns
FILE * fi=fopen(name_pic, "rb");
fread(pix,sizeof(unsigned char), 3*w*h, fi);
for(x=0; x<w; x++)
{
for(y=0; y<h; y++)
{
if(pix[count]==255)
{
matH[x][h-y-1]=1;
}
else
{
matH[x][h-y-1]=0;
}
count++;
}
}
fclose(fi); // MARIANNE!
delete pix;
return matH;
}
double Host::getFitness(unsigned int** proE)
{
unsigned int** proH = matrixGenerator(host_triangles,nb_triangles_h); // host profile
unsigned int** proP = matrixGenerator(paras_triangles,nb_triangles_p); // parasite profile
float fitness = 1.0;
for (int x = 0; x < DefVal::PIC_WIDTH; ++x) // parse on x
{
for (int y = 1; y < DefVal::PIC_HEIGHT; ++y) // parse on y
{
if ( ((proH[x][y] == 1) && (proE[x][y] == 0)) || ((proH[x][y] == 0) && (proE[x][y] == 1)) ) // If a point of the profile is not in the enveloppe, or other way around
{
fitness += 1; // Not good
}
if ((proH[x][y] == 1) && (proP[x][y] ==1)) // If a point of the profile is infected by the parasite
{
fitness += 1; // Not good
}
}
}
delete [] proH;
delete [] proP;
return 1/fitness; // Inverse of what is not good
}
void Host::mutation(void)
{
}
void Host::save_picture(unsigned char* mat_pix, char * picture_name) //pix is an array 1D
{
//mat_pix is an array 1D which contains values for each canal (RGB,RGB,RGB....)
FILE *picture=fopen(picture_name, "wb");
unsigned int H=DefVal::PIC_HEIGHT;
unsigned int W=DefVal::PIC_WIDTH;
fprintf(picture, "P6\n %d %d\n %d\n",W,H,DefVal::MAX_VAL_PICTURE);
fwrite(mat_pix,sizeof(unsigned char),3*W*H, picture);
fclose(picture); // MARIANNE!
//delete [] mat_pix;
}
unsigned char * Host::convert_pixel (unsigned int** mat_host, unsigned int** mat_envt, unsigned int** mat_para)
{
unsigned int w=DefVal::PIC_WIDTH;
unsigned int h=DefVal::PIC_HEIGHT;
unsigned char * mat_pix= new unsigned char[3*w*h];
unsigned int x,y;
//Blue for envt, green for host, red for parasite
for(x=0; x<w; x++)
{
for(y=0; y<h; y++)
{
//Host
if(mat_host[x][y]==1)
{
// (h-1 - y)*w + x
mat_pix[3*(x+(h-y-1)*w)+1]=255;
}
else
{
mat_pix[3*(x+(h-y-1)*w)+1]=0;
}
//Environment
if(mat_envt[x][y]==1)
{
mat_pix[3*(x+(h-y-1)*w)+2]=255;
}
else
{
mat_pix[3*(x+(h-y-1)*w)+2]=0;
}
//Parasite
if(mat_para[x][y]==1)
{
mat_pix[3*(x+(h-y-1)*w)]=255;
}
else
{
mat_pix[3*(x+(h-y-1)*w)]=0;
}
}
}
return mat_pix;
}
void Host::format_and_save(unsigned int** mat_envt, int id)
{
// host matrix
unsigned int** mat_host= matrixGenerator(host_triangles, DefVal::N_TRIANGLES_HOST);
// parasite matrix
unsigned int ** mat_par= matrixGenerator(paras_triangles, DefVal::N_TRIANGLES_PARASITE);
unsigned char * pix= convert_pixel(mat_host, mat_envt, mat_par);
char name[100];
sprintf(name,"host_%d%s", id, DefVal::PIC_FORMAT.c_str());
save_picture(pix, name);
delete [] mat_host;
delete [] mat_par;
}
void Host::evolutionTriangles(void)
{
Triangle* t = host_triangles;
int n_triangles = DefVal::N_TRIANGLES_HOST;
Triangle* new_t;
new_t = new Triangle[n_triangles];
int win_width= DefVal::HOST_EVO_X;
int wmin=DefVal::HOST_EVO_W_MIN;
int wmax=DefVal::HOST_EVO_W_MAX;
int hmin=DefVal::HOST_EVO_H_MIN;
int hmax=DefVal::HOST_EVO_H_MAX;
int W= (DefVal::HOST_WIDTH*DefVal::WINDOW_WIDTH)/100;
int H=(DefVal::HOST_HEIGHT*DefVal::WINDOW_HEIGHT)/100;
for (int i=0; i<n_triangles; i++)
{
int random_x = rand()%(int)(win_width+1); // Position of triangle (random in range(0,win_width))
int random_w = wmin+(rand()%(int)(wmax-wmin+1)); // Random number btw wmin and wmax
int random_h = hmin+(rand()%(int)(hmax-hmin+1)); // Random number btw hmin and hmax
if (random_w%2 == 1)
random_w-=1;
int r1=rand()%(2);
if (r1==0) {new_t[i].x = t[i].x+random_x;}
else {new_t[i].x = t[i].x-random_x;}
int r2=rand()%(2);
if (r2==0) {new_t[i].w = t[i].w+random_w;}
else {new_t[i].w = t[i].w-random_w;}
int r3=rand()%(2);
if (r3==0) {new_t[i].h = t[i].h+random_h;}
else {new_t[i].h = t[i].h-random_h;}
//printf("%d %d %d\n", t[i].x, t[i].w, t[i].h);
}
// Check boundaries
for (int i=0; i<n_triangles; i++)
{
// Checks if triangle is too much on the left or rigth...
//if so, keep the old values of t
if ( (new_t[i].x - int(0.5*new_t[i].w) < 0) || (new_t[i].x+int(0.5*new_t[i].w) > DefVal::PIC_WIDTH) )
{
new_t[i].x = t[i].x;
new_t[i].w = t[i].w;
new_t[i].h= t[i].h;
}
// Checks if triangle is too large...
if (new_t[i].w>W | new_t[i].w<0)
new_t[i].w = t[i].w;
// Checks if triangle is too hight...
if (new_t[i].h>H | new_t[i].h<0)
new_t[i].h = t[i].h;
}
Triangle tri;
memcpy(host_triangles,new_t,n_triangles*(sizeof tri));
}
double* Host::compParaFitness(void)
{
unsigned int** hostProfile = matrixGenerator(host_triangles, DefVal::N_TRIANGLES_HOST);
double* res = new double [DefVal::N_TRIANGLES_HOST];
for(int i = 0; i < DefVal::N_TRIANGLES_PARASITE; i++)
{
// Number of pixels common to the host and the parasite
double count_parahost = 0.0;
// Number of pixels only in the parasite
double count_paraonly = 0.0;
unsigned int** onlyOneParaTriangle = matrixGenerator(¶s_triangles[i], 1);
for(int x = 0; x < DefVal::PIC_WIDTH; x ++)
{
for(int y = 1; y < DefVal::PIC_HEIGHT; y++)
{
if(onlyOneParaTriangle[x][y] == 1)
{
if (hostProfile[x][y] == 1)
{
count_parahost++;
}
else
{
count_paraonly++;
}
}
}
}
delete [] onlyOneParaTriangle;
// Condition to avoid NaN
if (count_parahost + count_paraonly != 0)
res[i] = count_parahost / (count_paraonly + count_parahost);
else
res[i] = 0;
}
delete [] hostProfile;
return res;
}
double* Host::paraReproductionRate(void)
{
double* res = new double [DefVal::N_TRIANGLES_HOST];
// percentage of parasite in host
double* p = compParaFitness();
for(int i = 0; i < DefVal::N_TRIANGLES_PARASITE; i++)
{
// Reproduction rate from the area in the host profile
double ra = DefVal::PARA_REP_IN*p[i] + (1-p[i])*DefVal::PARA_REP_OUT;
// Reproduction rate from the size of the area
double rb = sin( 3.141592653589793 * pow((paras_triangles[i].h * paras_triangles[i].w) / double(DefVal::PARASITE_WIDTH * DefVal::PARASITE_HEIGHT * DefVal::PIC_WIDTH * DefVal::PIC_HEIGHT), 3) );
res[i] = (ra + rb)/double(2);
}
return res;
}
void Host::evolutionPara(void)
{
Triangle* t = paras_triangles;
int n_triangles = DefVal::N_TRIANGLES_PARASITE;
Triangle* new_t;
new_t = new Triangle[n_triangles];
int win_width= DefVal::PARA_EVO_X;
int wmin=DefVal::PARA_EVO_W_MIN;
int wmax=DefVal::PARA_EVO_W_MAX;
int hmin=DefVal::PARA_EVO_H_MIN;
int hmax=DefVal::PARA_EVO_H_MAX;
int W= (DefVal::PARASITE_WIDTH*DefVal::WINDOW_WIDTH)/100;
int H=(DefVal::PARASITE_HEIGHT*DefVal::WINDOW_HEIGHT)/100;
double p;
double* fecondity = paraReproductionRate();
for (int i=0; i<n_triangles; i++)
{
p = (double) rand()/RAND_MAX;
// Is the parasite transmitted ?
if(p > fecondity[i])
{
// If not, we "remove" it
t[i].x = DefVal::WINDOW_WIDTH * ((double) rand()/RAND_MAX);
t[i].h = 0.0;
t[i].w = 0.0;
}
int random_x = rand()%(int)(win_width+1); // Position of triangle (random in range(0,win_width))
int random_w = wmin+(rand()%(int)(wmax-wmin+1)); // Random number btw wmin and wmax
int random_h = hmin+(rand()%(int)(hmax-hmin+1)); // Random number btw hmin and hmax
if (random_w%2 == 1)
random_w-=1;
int r1=rand()%(2);
if (r1==0) {new_t[i].x = t[i].x+random_x;}
else {new_t[i].x = t[i].x-random_x;}
int r2=rand()%(2);
if (r2==0) {new_t[i].w = t[i].w+random_w;}
else {new_t[i].w = t[i].w-random_w;}
int r3=rand()%(2);
if (r3==0) {new_t[i].h = t[i].h+random_h;}
else {new_t[i].h = t[i].h-random_h;}
//printf("%d %d %d\n", t[i].x, t[i].w, t[i].h);
}
// Check boundaries
for (int i=0; i<n_triangles; i++)
{
// Checks if triangle is too much on the left or rigth...
//if so, keep the old values of t
if ( (new_t[i].x - int(0.5*new_t[i].w) < 0) || (new_t[i].x+int(0.5*new_t[i].w) > DefVal::PIC_WIDTH) )
{
new_t[i].x = t[i].x;
new_t[i].w = t[i].w;
new_t[i].h= t[i].h;
}
// Checks if triangle is too large...
if (new_t[i].w>W | new_t[i].w<0)
new_t[i].w = t[i].w;
// Checks if triangle is too hight...
if (new_t[i].h>H | new_t[i].h<0)
new_t[i].h = t[i].h;
}
Triangle tri;
memcpy(paras_triangles,new_t,n_triangles*(sizeof tri));
}