/
bacterias.cpp
891 lines (692 loc) · 26.1 KB
/
bacterias.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
// Bacterial coexistence simulation
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <iostream>
#include <fstream>
#include <vector>
#include "Bacterium.h"
#include "Strain.h"
#include "FoodSource.h"
// =====================================================================
// PROGRAM PARAMETERS -- values to be read from file
// General parameters
int grid_size; // Size of the grid (along each dimension)
int num_cycles; // Number of cycles to simulate
double growth_multiplier; // Global growth multiplier
bool all_die; // If false, only bacteria that are not at a food source die spontaneously (starvation?)
double global_death_rate; // Death probability per iteration
// Output parameters
bool report_to_screen; // Report simulation progress to screen?
char* log_fname; // Filename for logging
char* output_fname; // Filename for data output
int output_interval; // Number of iterations between data outputs
// Strains parameters
int num_strains; // Number of strains
char* strains_fname; // Filename for strains data (including name, growth_chance, sender degree and receiver degree)
// Antagonism
bool enable_antagonism; // Simulate for antangonistic interactions?
char* antagonism_table_fname; // If simulating antagonism, provide the antangonsm table filename
// Growth model
bool use_real_growth; // Indicates whether measured growth rates should be used (if false, will compute growth rates from sender degree)
double growth_intercept; // Growth rate for a strain with sender degree = 0
double growth_slope; // Growth rate reduction per additional sender degree
// Food sources
int food_source_rations; // Number of rations per new food source
int food_source_radius; // Radius of food sources
double food_source_spawn_rate; // Chance of new food source appearing per iteration
// Starting conditions
int start_population_per_strain; // Size of starting population, per strain (same for all strains)
int start_num_food_sources; // Starting number of food source
// =====================================================================
// GLOBAL VARIABLES
// Current cycle number
int cycle;
// Sequential labels for all bacteria that ever lived
int bac_counter;
// Seed of the RNG
unsigned int global_seed;
// Individual strain population size
int* strain_counts;
// List of Strains
Strain** strains;
// List of Bacteria
std::vector<Bacterium*> bacteria_list;
// List of Food Sources
std::vector<FoodSource*> food_list;
// Antagonism matrix
// antagonism[i][j] is true if strain i antagonizes strain j
bool** antagonisms;
// Filenames
char* params_fname;
// Log and data output files
FILE* log_file;
FILE* output_file;
// Global debug flag
const bool global_debug = false;
// =====================================================================
// Populates the strains array by reading the corresponding datafile
void create_strains() {
std::ifstream infile;
std::string str_buf, name;
double growth;
int snd_deg, rcv_deg, i;
fprintf(log_file, ">> Loading strains from \"%s\" ...\n", strains_fname);
infile.open(strains_fname, std::ifstream::in);
if (!infile) {
fprintf(log_file, "ERROR: could not open file\n");
exit(1);
}
if (enable_antagonism) {
fprintf(log_file, "Ignoring sender/receiver degrees (will compute them from the antagonisms table)\n");
}
getline(infile, str_buf); // skip header
i = 0;
while(!infile.eof() && i < num_strains) {
infile >> str_buf >> name >> growth >> rcv_deg >> snd_deg;
if (enable_antagonism) {
strains[i] = new Strain(i, name, growth, global_death_rate, 0, 0);
} else {
strains[i] = new Strain(i, name, growth, global_death_rate, snd_deg, rcv_deg);
}
// printf("%s %f %i %i\n", strains[i]->name.c_str(), strains[i]->growth_rate, strains[i]->receiver_degree, strains[i]->sender_degree);
i++;
}
if (i != num_strains) {
fprintf(log_file, "ERROR: expected %i strains, but found only %i!\n", num_strains, i);
exit(1);
}
fprintf(log_file, "%d strains loaded\n", i);
infile.close();
}
// =====================================================================
// Populates the antangonism matrix by reading the corresponding datafile
void load_antagonisms() {
std::ifstream infile;
double value;
int i, j;
fprintf(log_file, ">> Reading antagonism matrix from \"%s\" ...\n", antagonism_table_fname);
infile.open(antagonism_table_fname, std::ifstream::in);
if (!infile) {
fprintf(log_file, "Error: could not open file\n");
exit(1);
}
i = 0;
while(!infile.eof() && i < num_strains) {
for (j = 0; j < num_strains; j++) {
infile >> value;
if (value > 1) antagonisms[i][j] = true;
else antagonisms[i][j] = false;
}
i++;
}
// Compute sender degrees
fprintf(log_file, "Computing sender degrees ...\n");
int deg;
for (i = 0; i < num_strains; i++) {
deg = 0;
for (j = 0; j < num_strains; j++) {
if (antagonisms[i][j]) deg++;
}
strains[i]->sender_degree = deg;
}
// Compute receiver degrees
fprintf(log_file, "Computing receiver degrees ...\n");
for (i = 0; i < num_strains; i++) {
deg = 0;
for (j = 0; j < num_strains; j++) {
if (antagonisms[j][i]) deg++;
}
strains[i]->receiver_degree = deg;
}
infile.close();
}
// =====================================================================
// Compute the growth rates from the linear growth formula
void compute_growth_rates() {
int i;
for (i = 0; i < num_strains; i++) {
strains[i]->growth_rate = strains[i]->sender_degree*growth_slope + growth_intercept;
if (strains[i]->growth_rate < 0) {
fprintf(log_file, "ERROR: negative growth rate for strain %i (%s)\n", strains[i]->ID, strains[i]->name.c_str());
}
}
}
// =====================================================================
// Adds a new bacterium of strain at (x,y)
void add_bacterium(Strain* strain, int x, int y) {
bacteria_list.push_back(new Bacterium(bac_counter, strain, x, y));
bac_counter++;
}
// Removes the bacterium residing at 'idx' in the bacteria_list,
// freeing its memory use
void remove_bacterium(unsigned int idx) {
char strbuf[128];
if (global_debug) {
bacteria_list[idx]->repr(strbuf);
printf("Removing bacterium idx %i - %s\n", idx, strbuf);
}
delete bacteria_list[idx];
bacteria_list[idx] = bacteria_list.back();
bacteria_list.pop_back();
}
// =====================================================================
// Adds a new food source at (x,y)
void add_food_source(int x, int y, double radius, int rations) {
food_list.push_back(new FoodSource(x, y, radius, rations));
}
// Removes the food source residing at 'idx' in the food_list
void remove_food_source(unsigned int idx) {
if (global_debug) {
printf("Removing food source idx %i - position (%i,%i)\n", idx, food_list[idx]->x, food_list[idx]->y);
}
delete food_list[idx];
food_list[idx] = food_list.back();
food_list.pop_back();
// food_list.erase(food_list.begin()+idx);
}
// =====================================================================
// Creates the starting bacteria and populates the list
void create_starting_bacteria() {
int s, i;
Strain* strain;
for (s = 0; s < num_strains; s++) {
strain = strains[s];
for (i = 1; i <= start_population_per_strain; i++) {
add_bacterium(strain, randint(1,grid_size), randint(1,grid_size));
}
}
}
// =====================================================================
// Creates the starting food sources
void create_starting_food() {
for (int i = 0; i < start_num_food_sources; i++) {
add_food_source(randint(1, grid_size), randint(1, grid_size), food_source_radius, food_source_rations);
}
}
// Spawns a food source by rolling against the spawn rate
void spawn_food_source() {
if (randreal() <= food_source_spawn_rate) {
add_food_source(randint(1, grid_size), randint(1, grid_size), food_source_radius, food_source_rations);
if (global_debug) {
printf("Spawned food source at %i,%i with %i rations\n", food_list.back()->x, food_list.back()->y, food_list.back()->rations);
}
}
}
// =====================================================================
// Applies the grid topology to the coordinate pair (x,y)
void apply_topology(int& x, int& y) {
// Toroidal topology
if (x < 1) x = grid_size;
else if (x > grid_size) x = 1;
if (y < 1) y = grid_size;
else if (y > grid_size) y = 1;
}
// =====================================================================
// Move bacteria not at a food source
void move_bacteria() {
Bacterium* bac;
unsigned int i, counter = 0;
for (i = 0; i < bacteria_list.size(); i++) {
bac = bacteria_list[i];
if (!bac->isAtFoodSource) {
bac->move();
apply_topology(bac->x, bac->y);
counter++;
}
}
}
// =====================================================================
// Kill bacteria according to their death rate (only flags them as dead)
void kill_bacteria() {
Bacterium* bac;
unsigned int i;
for (i = 0; i < bacteria_list.size(); i++) {
bac = bacteria_list[i];
if (bac->isAlive && (all_die || !bac->isAtFoodSource)) {
if (randreal() <= bac->strain->death_rate) {
bac->isAlive = false;
if (global_debug) printf("Bacteria #%i died\n", bac->ID);
}
}
}
}
// =====================================================================
// Clean up empty food sources (by removing them from the food sources list)
void cleanup_empty_food_sources() {
unsigned int idx, i, size;
idx = 0;
size = food_list.size();
for (i = 0; i < size; i++) {
if (food_list[idx]->rations == 0) {
remove_food_source(idx);
} else {
idx++;
}
}
}
// Clean up dead bacteria (by removing them from the bacteria list)
void cleanup_dead_bacteria() {
unsigned int idx, i, size;
idx = 0;
size = bacteria_list.size();
for (i = 0; i < size; i++) {
if (!bacteria_list[idx]->isAlive) {
remove_bacterium(idx);
} else {
idx++;
}
}
}
// =====================================================================
// Check whether bacteria are at a food source, flagging them and adding
// them to the food source's list
void flag_bacteria_at_food_sources() {
Bacterium* bac;
FoodSource* fs;
unsigned int i, j, counter;
for (i = 0; i < food_list.size(); i++) {
food_list[i]->clearBacteriaHere();
}
counter = 0;
for (i = 0; i < bacteria_list.size(); i++) {
bac = bacteria_list[i];
bac->isAtFoodSource = false;
for (j = 0; j < food_list.size(); j++) {
fs = food_list[j];
if (fs->contains(bac->x, bac->y)) {
fs->bacteriaHere.push_back(bac);
if (!bac->isAtFoodSource) {
bac->isAtFoodSource = true;
counter++;
}
if (global_debug) printf("Found bacterium #%i at food source %i (%i,%i)\n", bac->ID, j, fs->x, fs->y);
}
}
}
}
// =====================================================================
// Implements interactions that occur at food sources
// Specifically:
// - bacteria antagonize each other (if enabled)
// - if not enough food for remaining bacteria, a lottery is made
// - lucky bacteria eat and perhaps reproduce
void food_sources_interactions() {
FoodSource* fs = NULL;
Bacterium* bac = NULL;
Bacterium* bac2 = NULL;
unsigned int fs_idx, idx2, size, num_bacs, i, j, num_will_eat;
char strbuf[256];
const int REWARD_AUTOMATIC = 0;
const int REWARD_CHANCE = 1;
// Local parameters
const bool debug = global_debug;
const int winner_reward = REWARD_CHANCE;
// Loop over all food sources
for (fs_idx = 0; fs_idx < food_list.size(); fs_idx++) {
fs = food_list[fs_idx];
// Bacteria count at this food source
num_bacs = (int)fs->bacteriaHere.size();
if (debug) printf("\nSource %i at %i,%i with %i rations has %i bacteria\n", fs_idx, fs->x, fs->y, fs->rations, num_bacs);
if (num_bacs == 0) continue;
if (debug) {
for (i = 0; i < num_bacs; i++) {
fs->bacteriaHere[i]->repr(strbuf);
printf("%i - %s\n", i, strbuf);
}
}
// Antagonisms
if (enable_antagonism) {
// For each bacteria, check all other bacteria at this food
// source. If it antagonizes any, it kills it and either
// reproduces automatically or gets a "free" chance to do so
for (i = 0; i < num_bacs; i++) {
bac = fs->bacteriaHere[i];
if (bac->isAlive) {
for (j = 0; j < num_bacs; j++) {
if (i == j) continue;
bac2 = fs->bacteriaHere[j];
if (bac2->isAlive) {
// Check if bac antagonizes bac2
if (antagonisms[bac->strain->ID][bac2->strain->ID]) {
// Finish him!
bac2->isAlive = false;
// Spoils of combat!
if (winner_reward == REWARD_AUTOMATIC) {
// Simplest: automatically reproduce
add_bacterium(bac->strain, bac->x, bac->y);
} else if (winner_reward == REWARD_CHANCE) {
// Better: give the bacterium a "free" chance to reproduce
if (randreal() <= bac->strain->growth_rate) {
add_bacterium(bac->strain, bac->x, bac->y);
}
} else {
printf("ERROR: unrecognized winner reward: %i\n", winner_reward);
exit(1);
}
}
}
}
}
}
// Cleanup the carnage at this food source
// (doesn't eliminate the bacteria from the global list yet)
idx2 = 0;
size = fs->bacteriaHere.size();
for (i = 0; i < size; i++) {
if (!fs->bacteriaHere[idx2]->isAlive) {
fs->bacteriaHere[idx2] = fs->bacteriaHere.back();
fs->bacteriaHere.pop_back();
} else {
idx2++;
}
}
num_bacs = (int)fs->bacteriaHere.size();
}
// Check available rations at this food source
if (fs->rations < num_bacs) {
// If not enough food for everyone, randomly select who will eat
num_will_eat = fs->rations;
// Partial Fisher-Yates shuffle
for (i = 0; i < num_will_eat; i++) {
j = randint(i,num_bacs-1);
if (debug) {
printf("i=%i <-> j=%i ", i, j); fflush(stdout);
printf("| %i\n", fs->bacteriaHere[j]->strain->ID);
}
if (j != i) {
bac = fs->bacteriaHere[j];
fs->bacteriaHere[j] = fs->bacteriaHere[i];
fs->bacteriaHere[i] = bac;
}
}
} else {
// If enough food, everyone gets to eat
num_will_eat = num_bacs;
}
if (debug) printf("%i bacteria will eat\n", num_will_eat);
// Now feed the bacteria, reducing the food source size, and let
// the fed bacteria have a chance to reproduce
if (debug) printf("Bacteria that reproduced:");
for (i = 0; i < num_will_eat; i++) {
fs->rations--;
bac = fs->bacteriaHere[i];
if (randreal() <= bac->strain->growth_rate) {
if (debug) printf(" #%i", bac->ID);
add_bacterium(bac->strain, bac->x, bac->y);
}
}
if (debug) printf("\n");
}
}
// =====================================================================
// Counts the number of bacteria of each strain
void compute_strain_counts() {
int i;
for (i = 0; i < num_strains; i++) {
strain_counts[i] = 0;
}
for (i = 0; i < (int)bacteria_list.size(); i++) {
strain_counts[bacteria_list[i]->strain->ID] += 1;
}
}
// =====================================================================
// Sets the seed of the RNG
void set_seed(unsigned int seed) {
srand(seed);
global_seed = seed;
}
// Resets the seed of the RNG (to a new random seed)
void reset_seed() {
srand(time(NULL));
set_seed(rand());
}
// =====================================================================
// Does dynamic allocation of data arrays
void do_allocations() {
int i;
strain_counts = new int[num_strains];
strains = new Strain*[num_strains];
antagonisms = new bool*[num_strains];
for (i = 0; i < num_strains; i++) {
antagonisms[i] = new bool[num_strains];
}
}
// Deallocates dynamic memory
void deallocate() {
int i;
delete strain_counts;
delete strains;
for (i = 0; i < num_strains; i++) {
delete antagonisms[i];
}
delete antagonisms;
delete log_fname;
delete output_fname;
delete strains_fname;
delete antagonism_table_fname;
}
// =====================================================================
// Returns the next data-holding line, skipping comments and blank lines
std::string get_next_line(std::ifstream& file, const char* param_name) {
char firstchar;
std::string line;
unsigned int i;
while (std::getline(file, line)) {
// std::cout << "--" << line << std::endl;
if (line.length() == 0) continue;
firstchar = '\0';
for (i = 0; i < line.length(); i++) {
if (line[i] != ' ') {
firstchar = line[i];
break;
}
}
if (firstchar == '\0' or firstchar == '#') continue;
else return line;
}
printf("Error! Reached end of file while looking for '%s'\n", param_name);
exit(EXIT_FAILURE);
}
// Load parameters from the given filename
void load_parameters(const char* params_fname) {
std::ifstream params_file(params_fname);
std::string line;
line = get_next_line(params_file, "grid_size");
grid_size = atoi(line.c_str());
line = get_next_line(params_file, "num_cycles");
num_cycles = atoi(line.c_str());
line = get_next_line(params_file, "growth_multiplier");
growth_multiplier = atof(line.c_str());
line = get_next_line(params_file, "global_death_rate");
global_death_rate = atof(line.c_str());
line = get_next_line(params_file, "all_die");
all_die = (atoi(line.c_str()) == 1);
line = get_next_line(params_file, "report_to_screen");
report_to_screen = (atoi(line.c_str()) == 1);
line = get_next_line(params_file, "log_fname");
log_fname = new char[strlen(line.c_str())];
strcpy(log_fname, line.c_str());
line = get_next_line(params_file, "output_fname");
output_fname = new char[strlen(line.c_str())];
strcpy(output_fname, line.c_str());
line = get_next_line(params_file, "output_interval");
output_interval = atoi(line.c_str());
line = get_next_line(params_file, "num_strains");
num_strains = atoi(line.c_str());
line = get_next_line(params_file, "strains_fname");
strains_fname = new char[strlen(line.c_str())];
strcpy(strains_fname, line.c_str());
line = get_next_line(params_file, "enable_antagonism");
enable_antagonism = (atoi(line.c_str()) == 1);
line = get_next_line(params_file, "antagonism_table_fname");
antagonism_table_fname = new char[strlen(line.c_str())];
strcpy(antagonism_table_fname, line.c_str());
line = get_next_line(params_file, "use_real_growth");
use_real_growth = (atoi(line.c_str()) == 1);
line = get_next_line(params_file, "growth_intercept");
growth_intercept = atof(line.c_str());
line = get_next_line(params_file, "growth_slope");
growth_slope = atof(line.c_str());
line = get_next_line(params_file, "food_source_rations");
food_source_rations = atoi(line.c_str());
line = get_next_line(params_file, "food_source_radius");
food_source_radius = atoi(line.c_str());
line = get_next_line(params_file, "food_source_spawn_rate");
food_source_spawn_rate = atof(line.c_str());
line = get_next_line(params_file, "start_population_per_strain");
start_population_per_strain = atoi(line.c_str());
line = get_next_line(params_file, "start_num_food_sources");
start_num_food_sources = atoi(line.c_str());
}
// =====================================================================
// Writes the current state of the simulation to the data ouput file
void data_output() {
int i;
fprintf(output_file, "%i %i %i", cycle, (int)bacteria_list.size(), (int)food_list.size());
for (i = 0; i < num_strains; i++) {
fprintf(output_file, " %i", strain_counts[i]);
}
fprintf(output_file, "\n");
}
// =====================================================================
// Logs a message to the logfile, echoing to screen if requested
void log(const char* message, bool echo_stdout) {
fputs(message, log_file);
if (echo_stdout) fputs(message, stdout);
}
// Wrapper for the above, echoing to stdout if report_to_screen is true
void log(const char* message) {
log(message, report_to_screen);
}
// #####################################################################
// #####################################################################
int main (int argc, char** argv) {
char strbuf[256];
time_t raw_time;
// Load parameters from the specified file
if (argc != 2) {
printf("Error! Must provide a parameters filename as first argument\n");
exit(EXIT_FAILURE);
} else {
raw_time = time(NULL);
printf(">> %s", ctime(&raw_time));
}
params_fname = new char[strlen(argv[1])];
strcpy(params_fname, argv[1]);
printf(">> Reading parameters file %s ...\n", params_fname);
load_parameters(params_fname);
const int report_interval = (num_cycles > 100 ? num_cycles : 100) / 100;
// Open log and data files
log_file = fopen(log_fname,"w");
output_file = fopen(output_fname,"w");
printf(">> Opened log file %s\n", log_fname);
raw_time = time(NULL);
sprintf(strbuf, ">> %s", ctime(&raw_time)); log(strbuf, false);
sprintf(strbuf, ">> Read parameters file %s ...\n", params_fname); log(strbuf, false);
// Report parameters
sprintf(strbuf, "grid_size = %i\n", grid_size); log(strbuf);
sprintf(strbuf, "num_cycles = %i\n", num_cycles); log(strbuf);
sprintf(strbuf, "growth_multiplier = %f\n", growth_multiplier); log(strbuf);
sprintf(strbuf, "global_death_rate = %f\n", global_death_rate); log(strbuf);
sprintf(strbuf, "all_die = %s\n", all_die ? "true" : "false"); log(strbuf);
sprintf(strbuf, "report_to_screen = %s\n", report_to_screen ? "true" : "false"); log(strbuf);
sprintf(strbuf, "log_fname = %s\n", log_fname); log(strbuf);
sprintf(strbuf, "output_fname = %s\n", output_fname); log(strbuf);
sprintf(strbuf, "output_interval = %i\n", output_interval); log(strbuf);
sprintf(strbuf, "num_strains = %i\n", num_strains); log(strbuf);
sprintf(strbuf, "strains_fname = %s\n", strains_fname); log(strbuf);
sprintf(strbuf, "enable_antagonism = %s\n", enable_antagonism ? "true" : "false"); log(strbuf);
sprintf(strbuf, "antagonism_table_fname = %s\n", antagonism_table_fname); log(strbuf);
sprintf(strbuf, "use_real_growth = %s\n", use_real_growth ? "true" : "false"); log(strbuf);
sprintf(strbuf, "growth_intercept = %f\n", growth_intercept); log(strbuf);
sprintf(strbuf, "growth_slope = %f\n", growth_slope); log(strbuf);
sprintf(strbuf, "food_source_rations = %i\n", food_source_rations); log(strbuf);
sprintf(strbuf, "food_source_radius = %i\n", food_source_radius); log(strbuf);
sprintf(strbuf, "food_source_spawn_rate = %f\n", food_source_spawn_rate); log(strbuf);
sprintf(strbuf, "start_population_per_strain = %i\n", start_population_per_strain); log(strbuf);
sprintf(strbuf, "start_num_food_sources = %i\n", start_num_food_sources); log(strbuf);
log(">> Parameters loaded successfully\n");
// Allocate data arrays
do_allocations();
log(">> Data arrays allocated\n");
// Set RNG seed
reset_seed();
sprintf(strbuf, ">> RNG seed: %u\n", global_seed); log(strbuf);
// Create strains using data from file
log(">> Loading strains ...\n");
create_strains();
// Load antagonism matrix (if applicable)
if (enable_antagonism) {
log(">> Antagonisms ENABLED\n");
load_antagonisms();
} else {
log(">> Antagonisms DISABLED\n");
}
// Compute growth_rates if not using real ones
if (use_real_growth) {
log(">> Using measured growth rates\n");
} else {
log(">> Computing growth rates from sender degrees ...\n");
compute_growth_rates();
}
// Report strains
log(">> Loaded strains:\n");
log("(name | sender degree | receiver degree | growth rate)\n");
int max_length = 0;
int max_size = 0;
for (int i = 0; i < num_strains; i++) {
if (strains[i]->name.length() > max_length)
max_length = strains[i]->name.length();
if (strains[i]->sender_degree > max_size)
max_size = strains[i]->sender_degree;
if (strains[i]->receiver_degree > max_size)
max_size = strains[i]->receiver_degree;
}
max_size = int(log10(max_size))+1;
for (int i = 0; i < num_strains; i++) {
sprintf(strbuf, "%-*s | %*i | %*i | %.6f\n", max_length, strains[i]->name.c_str(), max_size, strains[i]->sender_degree, max_size, strains[i]->receiver_degree, strains[i]->growth_rate); log(strbuf);
}
// Create starting bacteria
bac_counter = 0;
log(">> Creating bacteria ...\n");
create_starting_bacteria();
sprintf(strbuf, "Population: %i\n", (int)bacteria_list.size()); log(strbuf);
compute_strain_counts();
// Create food sources
log(">> Creating food sources ...\n");
create_starting_food();
sprintf(strbuf, "Food sources: %i\n", (int)food_list.size()); log(strbuf);
flag_bacteria_at_food_sources();
// Ready!
log(">> READY. Starting simulation ...\n", true);
if (!report_to_screen) {
printf("(no further output written to screen)\n");
}
// Main loop over cycles
for (cycle = 1; cycle <= num_cycles; cycle++) {
move_bacteria();
flag_bacteria_at_food_sources();
food_sources_interactions();
cleanup_empty_food_sources();
kill_bacteria();
cleanup_dead_bacteria();
spawn_food_source();
compute_strain_counts();
sprintf(strbuf, "Cycle %i, %i bacteria, %i sources\n", cycle, (int)bacteria_list.size(), (int)food_list.size());
log(strbuf, false);
if (report_to_screen) {
if (cycle % report_interval == 0) {
printf(">> Cycle %i, %i bacteria, %i sources\n", cycle, (int)bacteria_list.size(), (int)food_list.size());
}
}
if (cycle % output_interval == 0) {
data_output();
}
}
raw_time = time(NULL);
sprintf(strbuf, ">> %s", ctime(&raw_time)); log(strbuf, true);
deallocate();
}