-
Notifications
You must be signed in to change notification settings - Fork 0
/
exo_ibm_v2.c
378 lines (313 loc) · 9.08 KB
/
exo_ibm_v2.c
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
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
/* function declaration */
void init_repro (int **tab_in, int pop_size);
void update_movement (int **tab_in, int pop_size, int maxDist, int S);
int update_death(int **tab_in, int pop_size, float pD);
int update_birth(int **tab_in, int pop_size, int pop_alive, float pB);
int** swap_tables (int **tab_in, int col_number, int pop_size, int pop_alive, int pop_new);
// void swap_ptrs (int **ptr1, int **ptr2);
/* main function */
int main(int argc, char const *argv[])
{
// define constants
// max time step
int maxTs = 10;
// World's size
int S = 40;
// initial pop
int popInit = 10;
// birth probability
float pB = 0.1;
// death probability
float pD = 0.1;
// maximal moving distance (could be a proportion of world's size)
int maxDist = 3;
// four columns: x, y, DoA, and reproduction
int col_number = 4;
// declare tracked variables
// population size tracker
int pop_size;
int pop_alive;
int pop_new;
// random generator seed
srand(time(NULL));
// build the initial pop table
// pointer to pointers
int **pop_table;
// allocate memory
// allocate first column
pop_table = malloc(popInit * sizeof(int *));
// allocate other columns to each row
for(int row = 0; row < popInit; ++row)
{
*(pop_table + row) = malloc(col_number * sizeof(int));
}
// initialize pop_tab
for (int row = 0; row < popInit; ++row)
{
// random values for the first two columns
// generate 2 random numbers between 0 and S included
int r1 = rand() % (S+1);
int r2 = rand() % (S+1);
// assign it
*(*(pop_table + row) + 0) = r1;
*(*(pop_table + row) + 1) = r2;
// every ind is alive at first
*(*(pop_table + row) + 2) = 1;
// no one has reproduced yet
*(*(pop_table + row) + 3) = 0;
}
// initialize pop_size
pop_size = popInit;
// debug OK
printf("pop init \nx\ty\tDoA\tRepro \n");
for(int row = 0; row < popInit; row++)
{
for(int col = 0; col < col_number; ++col)
{
printf("%d\t", *(*(pop_table + row) + col) );
}
printf("\n");
}
printf("pop size is %d \n", pop_size);
// for/while loop on time steps
for (int t = 0; t < maxTs; ++t)
{
/* update successively pop_table with the functions */
// initialize reproduction
init_repro (pop_table, pop_size);
// individuals move
update_movement(pop_table, pop_size, maxDist, S);
// // debug OK
// printf("after movement \nx\ty\tDoA\tRepro\n");
// for(int row = 0; row < pop_size; row++)
// {
// for(int col = 0; col < col_number; ++col)
// {
// printf("%d\t", *(*(pop_table + row) + col) );
// }
// printf("\n");
// }
// printf("pop size is %d \n", pop_size);
// die
pop_alive = update_death(pop_table, pop_size, pD);
// // debug OK
// printf("after selection \nx\ty\tDoA\tRepro\n");
// for(int row = 0; row < pop_size; row++)
// {
// for(int col = 0; col < col_number; ++col)
// {
// printf("%d\t", *(*(pop_table + row) + col) );
// }
// printf("\n");
// }
// printf("pop size is %d \n", pop_size);
// printf("pop alive is %d \n", pop_alive);
// and survivors reproduce
pop_new = update_birth(pop_table, pop_size, pop_alive, pB);
// // debug OK
// printf("after birth \nx\ty\tDoA\tRepro\n");
// for(int row = 0; row < pop_size; row++)
// {
// for(int col = 0; col < col_number; ++col)
// {
// printf("%d\t", *(*(pop_table + row) + col) );
// }
// printf("\n");
// }
// printf("pop size is %d \n", pop_size);
// printf("pop alive is %d \n", pop_alive);
// printf("pop new is %d \n", pop_new);
// change the content of pop_table
pop_table = swap_tables(pop_table, col_number, pop_size, pop_alive, pop_new);
// update popsize
pop_size = pop_new;
// debug
printf("pop at the end of timestep #%d \nx\ty\tDoA\tRepro\n", t+1);
for(int row = 0; row < pop_size; row++)
{
for(int col = 0; col < col_number; ++col)
{
printf("%d\t", *(*(pop_table + row) + col) );
}
printf("\n");
}
printf("pop size is %d \n", pop_size);
}
// free memory
for(int row = 0; row < pop_size; ++row)
{
free(*(pop_table + row));
}
free(pop_table);
return 0;
}
/* movement function */
void update_movement (int **tab_in, int pop_size, int maxDist, int S)
{
// browse individuals
for (int row = 0; row < pop_size; ++row)
{
// generate two random integer in [-maxDist ; maxDist]
int r1 = rand() % (2 * maxDist) - maxDist;
int r2 = rand() % (2 * maxDist) - maxDist;
// update positions
*(*(tab_in + row) + 0) += r1;
*(*(tab_in + row) + 1) += r2;
// check for world boundaries (reflective)
if (*(*(tab_in + row) + 0) > S)
{ *(*(tab_in + row) + 0) = S - ((*(*(tab_in + row) + 0)) - S);}
if (*(*(tab_in + row) + 0) < 0)
{ *(*(tab_in + row) + 0) *= -1;}
if (*(*(tab_in + row) + 1) > S)
{ *(*(tab_in + row) + 1) = S - ((*(*(tab_in + row) + 1)) - S);}
if (*(*(tab_in + row) + 1) < 0)
{ *(*(tab_in + row) + 1) *= -1;}
}
}
/* death function */
// takes a table in and updates it
int update_death(int **tab_in, int pop_size, float pD)
{
// local variables
// count of deaths
int death_count = 0;
// new pop_size
int res = 0;
// while/for loop on individuals in pop_table
for (int row = 0; row < pop_size; ++row)
{
// test for death
// generate a random number between 0 and 1
float r = rand() / (double)(RAND_MAX);
// test
if (r < pD)
{
*(*(tab_in + row) + 2) = 0;
death_count += 1;
}
}
// update table size
res = pop_size - death_count;
return res;
}
/* birth function */
int update_birth(int **tab_in, int pop_size, int pop_alive, float pB)
{
// local variables
// count of births
int birth_count = 0;
// new pop_size
int res;
// while/for loop on individuals in pop_table
for (int row = 0; row < pop_size; ++row)
{
// only living individuals reproduce
if (*(*(tab_in + row) + 2) == 1)
{
// test for birth
// generate a random number between 0 and 1
float r = rand()/(double)(RAND_MAX);
// test
if (r < pB)
{
*(*(tab_in + row) + 3) = 1;
birth_count += 1;
}
}
}
// update pop_size
res = pop_alive + birth_count;
return res;
}
/* function updating pop table */
int** swap_tables (int **tab_in, int col_number, int pop_size, int pop_alive, int pop_new)
{
// initialize returning pointer to pointers
int **tab_out = NULL;
// allocate mem to it
tab_out = malloc(pop_new * sizeof(int *));
for(int row = 0; row < pop_new; ++row)
{
*(tab_out + row) = malloc(col_number * sizeof(int));
}
// // debug OK
// printf("fresh tab_out \nx\ty\tDoA\tRepro\n");
// for(int row = 0; row < pop_new; row++)
// {
// for(int col = 0; col < col_number; ++col)
// {
// printf("%d\t", *(*(tab_out + row) + col) );
// }
// printf("\n");
// }
// fill it with the right info
// track the living and copy their values to the new table
// trackers of the survivors and new born
int i = 0;
int j = 0;
for (int row = 0; row < pop_size; ++row)
{
// browse the living and store them in the temporary table
if ((*(*(tab_in + row) + 2)) == 1 && i < pop_alive) //
{
// copy the survivor values to the temp table
for (int col = 0; col < col_number; ++col)
{
*(*(tab_out + i) + col) = *(*(tab_in + row) + col);
}
// *(*(tab_out + i) + 1) = *(*(tab_in + row) + 1);
// *(*(tab_out + i) + 2) = *(*(tab_in + row) + 2);
// // reset repro to 0
// *(*(tab_out + i) + 3) = 0;
// if individual reproduced add its offspring to tab_out at its parent's coordinates
if ((*(*(tab_in + row) + 3)) == 1) // && j > (pop_new - pop_alive)
{
// copy the parents coordinates to the temp table
*(*(tab_out + (pop_alive + j)) + 0) = *(*(tab_in + row) + 0);
*(*(tab_out + (pop_alive + j)) + 1) = *(*(tab_in + row) + 1);
// offspring are alive and did not reproduced yet
*(*(tab_out + (pop_alive + j)) + 2) = 1;
*(*(tab_out + (pop_alive + j)) + 3) = 0;
// increment j
j += 1;
}
// increment i
i += 1;
}
}
// // debug OK
// printf("tab_out \nx\ty\tDoA\tRepro\n");
// for(int row = 0; row < pop_new; row++)
// {
// for(int col = 0; col < col_number; ++col)
// {
// printf("%d\t", *(*(tab_out + row) + col) );
// }
// printf("\n");
// }
// free tab_in
for(int row = 0; row < pop_size; ++row)
{
free(*(tab_in + row));
}
free(tab_in);
return tab_out;
}
// function to reinitialise reproduction tracker at the begining of every iteration
void init_repro (int **tab_in, int pop_size)
{
// browse lines and set the fourth column value to 0
for(int row = 0; row < pop_size; ++row)
{
*(*(tab_in + row) + 3) = 0;
}
}
// void swap_ptrs (int **ptr1, int **ptr2)
// {
// int temp = **ptr1;
// **ptr1 = **ptr2;
// **ptr2 = temp;
// }