-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchapter2modelV2.cpp
1532 lines (1207 loc) · 52.7 KB
/
chapter2modelV2.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
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream> // input and output from the console
#include <string> // manipulate char strings
#include <fstream> // handle files
#include <time.h> // get the time for random number generator
#include <stdlib.h> // random generator tools
#include <algorithm> // random shuffle
#include <vector> // needed for random shuffle
using namespace std; // not to have to write std:: in front of every call
/* ---------------------------- Global variables ---------------------------- */
/* give a name to the simulation */
// hard coded NOT GOOD
string simulationName = "test";
/* define animals and resources modelled */
// hard coded NOT GOOD
int resourceTypesNb = 2; // Number of resources available
int preyTypesNb = 2; // Number of preys modelled
int predatorTypesNb = 1; // Number of predators modelled
/* pointers to individual types arrays */
string *resourceTypes; // resource1..n
string *preyTypes; // prey1..n
string *predatorTypes; // types of predator simulated: predator1..n
/* pointers to initial population sizes arrays */
int *predatorsInitialDensities;
int *preysInitialDensities;
/* pointers to members' matching lists (arrays) + size variables */
int memberMatchingListsSize = resourceTypesNb + preyTypesNb + predatorTypesNb; // total number of types
string *memberTypes; //
int *typeTags; //
int *indexInLandscape; // column index of each type in the landscape table
/* pointer to diets' table: will indicate who eats who and who does not */
int **dietsTable;
/* world size */
int worldSize; // side of the squared lanscape in number of cells [0;+inf[
/* time variables */
int timeMax; // simulation time
/* ---------------------------- Global functions ---------------------------- */
int sumColumn(int **table, int maxRow, int columnIndex) // sum of row values in a column
{
int sum = 0;
int r = 0;
while (r < maxRow)
{
sum += table[r][columnIndex];
r++;
}
return sum;
}
double randomNumberGenerator(double min, double max) // generates a random number between min and max
{
double res;
res = min + (double)rand() * (max - min + 1) / (RAND_MAX - 1);
return res;
}
vector<int> shuffleOrder(int populationSize)
{
vector<int> popVector; // initialise a population vector
for (int i = 0; i < populationSize; ++i) // create a vector of indexes of size of current population
popVector.push_back(i);
random_shuffle(popVector.begin(), popVector.end()); // shuffle indexes
/* debug : OK
cout << "popVector contains:";
for (std::vector<int>::iterator it = popVector.begin(); it != popVector.end(); ++it)
cout << ' ' << *it;
cout << '\n';
*/
return popVector;
}
void assignTagsIndexes() // matches names, tags and column index in landscapeTablePtr table.
{
/* allocate memory */
memberTypes = new string[memberMatchingListsSize];
typeTags = new int[memberMatchingListsSize];
indexInLandscape = new int[memberMatchingListsSize];
/* assign values */
/* choose a tag system: I have chose this one, allowing for 99 types of resources, preys an predators */
int resourceTagStart = 101; // tag of the first resource on the list
int preyTagStart = 201; // tag of the first prey on the list
int predatorTagStart = 301; // tag of the first predator on the list
/* assign tags iteratively */
int r = 0; // initialise row counter
while (r < memberMatchingListsSize)
{
for (int res = 0; res < resourceTypesNb; res++)
{
memberTypes[r] = resourceTypes[res];
typeTags[r] = resourceTagStart + res;
indexInLandscape[r] = 3 + res; // before: cellCode - x - y
/* debug : OK
cout << "memberTypes[" << r << "] is " << memberTypes[r] << endl;
cout << "typeTags[" << r << "] is " << typeTags[r] << endl;
cout << "indexInLandscape[" << r << "] is " << indexInLandscape[r] << endl;
*/
r++;
}
for (int prey = 0; prey < preyTypesNb; prey++)
{
memberTypes[r] = preyTypes[prey];
typeTags[r] = preyTagStart + prey;
indexInLandscape[r] = 3 + resourceTypesNb + prey; // before: x - y - resource densities
/* debug : OK
cout << "memberTypes[" << r << "] is " << memberTypes[r] << endl;
cout << "typeTags[" << r << "] is " << typeTags[r] << endl;
cout << "indexInLandscape[" << r << "] is " << indexInLandscape[r] << endl;
*/
r++;
}
for (int pred = 0; pred < predatorTypesNb; pred++)
{
memberTypes[r] = predatorTypes[pred];
typeTags[r] = predatorTagStart + pred;
indexInLandscape[r] = 3 + resourceTypesNb + 2 * preyTypesNb + pred; // before: x - y - resource densities - prey densities - prey catches
/* debug : OK
cout << "memberTypes[" << r << "] is " << memberTypes[r] << endl;
cout << "typeTags[" << r << "] is " << typeTags[r] << endl;
cout << "indexInLandscape[" << r << "] is " << indexInLandscape[r] << endl;
*/
r++;
}
}
}
int getMemberIndexFromTag(int TypeTag)
{
int index = 0; // declare integer to be returned
int row = 0;
int rowMax = memberMatchingListsSize;
while (row < rowMax)
{
if (TypeTag == typeTags[row]) // if tag corresponds
{
index = row;
break; // exits the nested loops
}
row++;
}
return index; // returns the row index of the tag. It will be the same in all members matching lists.
}
void makeDietsTable() // this table allows for each member of the system to eat and be eaten by another
{
int dietsTableSize = memberMatchingListsSize;
// no need for headers when the memberMatchingListsIndex is known
/* allocate memory. Make sure it is freed at the end of the main function! */
dietsTable = new int *[dietsTableSize];
for (int row = 0; row < dietsTableSize; row++)
{
dietsTable[row] = new int[dietsTableSize];
}
/* assign lines and columns headers : not needed here but useful for debug
for (int row = 1; row < dietsTableSize; row++)
{
dietsTable[row][0] = typeTags[row - 1]; // -1 because we start row at 1 while we want typeTags[0]
}
for (int col = 1; col < dietsTableSize; col++)
{
dietsTable[0][col] = typeTags[col - 1];
}
*/
/* intialise all values to 0 */
for (int row = 0; row < dietsTableSize; row++)
{
for (int col = 0; col < dietsTableSize; col++)
dietsTable[row][col] = 0;
}
/* Set to 1 when one feeds on the other */
dietsTable[0][2] = 1; // prey1 feeds on resource1
dietsTable[1][3] = 1; // prey2 feeds on resource2
dietsTable[2][4] = 1; // predator1 feeds on prey1
dietsTable[3][4] = 1; // predator1 feeds on prey2
/* debug : OK
cout << "dietsTable" << endl;
for (int row = 0; row < dietsTableSize; row++)
{
for (int col = 0; col < dietsTableSize; col++)
{
cout << dietsTable[row][col];
if (col == (dietsTableSize - 1)) // last loop
cout << endl;
else
cout << " ";
}
}
*/
}
vector<int> getDietLandscapeIndexes(int MembersMatchingListsIndex) // get the diet of a particular member of the food chain from its typeTag.
{
vector<int> dietIndexes; // declare the vector to be returned
int rowMax = memberMatchingListsSize; // row number of dietTable
int typesInDiet = sumColumn(dietsTable, rowMax, MembersMatchingListsIndex);
/* loop over lines to fill vector with diet's members column index in landscape table */
for (int row = 0; row < rowMax; row++)
{
if (dietsTable[row][MembersMatchingListsIndex] == 1 && dietIndexes.size() <= typesInDiet) // control not to add to the array more than its size
{
dietIndexes.push_back(indexInLandscape[row]); // the row number of this match is enough to know its index in the landscape table!
}
}
/* debug : OK
cout << memberTypes[MembersMatchingListsIndex] << "'s diet's member(s) column index(es) in landscape table is(are) : ";
for (std::vector<int>::iterator it = dietIndexes.begin(); it != dietIndexes.end(); ++it)
cout << ' ' << *it;
cout << '\n';
*/
return dietIndexes;
}
int getCellCode(string *xyCoordinates, int *CellCodes, int LandscapeSize, int x, int y)
{
int cellCode = 0; // declare the integer to be returned
/* turn x y coordinates into string */
string XYcoord = to_string(x) + ";" + to_string(y);
/* iterate through xyCoordinates to find match */
int row = 0;
int rowMax = LandscapeSize * LandscapeSize; // WARNING only works if squared
while (row < rowMax)
{
if (XYcoord == xyCoordinates[row]) // if XY corresponds
{
cellCode = CellCodes[row]; // the row index is the same in all landscape matching lists
break;
}
row++;
}
return cellCode;
}
int transmissiveBoundaries(int coordinate, int LandscapeSize)
{
if (coordinate >= LandscapeSize)
{
coordinate -= LandscapeSize;
}
else if (coordinate < 0)
{
coordinate += LandscapeSize;
}
return coordinate;
}
/* ----------------------------- Object classes ----------------------------- */
/*
create a class:
- all the variables that define it
- all the functions that manipulate these variables
- make sure that the values can be used by other classes
*/
class landscape
{
/* list of landscape-specific variables */
private: // variables that should not be modified directly, nor accessed from the main function
int Size; // side of the squared lanscape in number of cells [0;+inf[
int MaxResource; // max amount of resources on a cell
int rowNb; // row number
int columnNb; // column number
int resColumnStart; // indexes for table building convinience
int preyColumnStart; //
int predatorColumnStart; //
fstream resultsTable; // file to write results in
fstream snapshotTable; // file to save all relevant landscape cell info
protected: // variables that should not be modified directly, nor accessed from the main function, but accessible to the other classes
int landscapeMatchingListsSize; //
public: // can be used / called in the main function
int **landscapeTablePtr; // pointer to the landscape table
string *XYcoordinates; // landscape matching lists
int *cellCode; //
landscape(int size, int maxResource) // constructor: function that creates objects of the class by assigning values to or initializing the variables
{
Size = size;
MaxResource = maxResource;
/* column index where every group of info starts in the table*/
resColumnStart = 3; // before: cellCode - x - y
preyColumnStart = resColumnStart + resourceTypesNb; // before: x - y - resource densities
predatorColumnStart = preyColumnStart + 2 * preyTypesNb; // before: x - y - resource densities - prey densities - prey catches
/* table size */
rowNb = Size * Size;
columnNb = resColumnStart + resourceTypesNb + 2 * preyTypesNb + predatorTypesNb;
/* create landscape matching lists */
landscapeMatchingListsSize = rowNb;
XYcoordinates = new string[landscapeMatchingListsSize];
cellCode = new int[landscapeMatchingListsSize];
/* create a dynamic 2D array
Super clear video: https://www.youtube.com/watch?v=mGl9LO-je3o&list=PL43pGnjiVwgSSRlwfahAuIqoJ8TfDIlHq&index=6&ab_channel=CodeBeauty */
landscapeTablePtr = new int *[rowNb]; // define the pointer to an array of int pointers for each row
for (int row = 0; row < rowNb; row++) // for each row, create a pointer to an array of size columnNb
{
landscapeTablePtr[row] = new int[columnNb];
}
/* fill the table with info
x/y cell coordinates */
int r = 0; // initialise row counter
int x = 0; // intialise x counter
int y = 0;
while (r < rowNb)
{
if (y > (Size - 1)) // we reach y = Size-1, reset y to 0 and increment x
{
y = 0;
x++;
}
landscapeTablePtr[r][0] = r;
landscapeTablePtr[r][1] = x;
landscapeTablePtr[r][2] = y;
/* match in xy coordinates to cellCode */
XYcoordinates[r] = to_string(x) + ";" + to_string(y);
cellCode[r] = r;
/* debug : OK
cout << "XYcoordinates[" << r << "] is " << XYcoordinates[r] << " cellCode[" << r << "] is " << cellCode[r] << endl;
*/
y++;
r++;
}
/* initialise resources to maximum */
r = 0;
while (r < rowNb)
{
for (int res = 0; res < resourceTypesNb; res++)
landscapeTablePtr[r][resColumnStart + res] = MaxResource;
r++;
}
/* initialise prey densities and catches to 0 */
r = 0;
while (r < rowNb)
{
for (int prey = 0; prey < (2 * preyTypesNb); prey++)
landscapeTablePtr[r][preyColumnStart + prey] = 0;
/* a specific number to check if the info is where expected: OK
landscapeTablePtr[r][preyColumnStart + prey] = 1 + prey; */
r++;
}
/* initialise predator densities to 0 */
r = 0;
while (r < rowNb)
{
for (int pred = 0; pred < predatorTypesNb; pred++)
landscapeTablePtr[r][predatorColumnStart + pred] = 0;
/* a specific number to check if the info is where expected: OK
landscapeTablePtr[r][predatorColumnStart + pred] = 11 + pred; */
r++;
}
}
~landscape() // free memory allocated to landscape table
{
/* free matching list memory */
delete[] XYcoordinates;
delete[] cellCode;
/* free landscape tabel memory */
for (int row = 0; row < rowNb; row++) // free memory allocated to each row
{
delete[] landscapeTablePtr[row];
}
delete[] landscapeTablePtr; // free the memory allocated to the array of pointer to each row
/* debug :OK
cout << "landscape destructor has been called successfully" << endl << endl;
*/
}
void resetLandscape() // function to reset resources to maximum and counts to 0
{
/* debug
cout << "reinitialising landscape ..." << endl
<< endl;
*/
int r = 0;
while (r < rowNb)
{
/* refill resources */
for (int res = 0; res < resourceTypesNb; res++)
landscapeTablePtr[r][resColumnStart + res] = MaxResource;
/* resetting counts to O */
for (int col = preyColumnStart; col < columnNb; col++)
landscapeTablePtr[r][col] = 0;
r++;
}
}
void getInfo() // function to cast out the landscape table and check if all good
{
/* cast out the column names */
cout << "cellCode"
<< " "
<< "xCell"
<< " "
<< "yCell"
<< " ";
for (int i = 0; i < resourceTypesNb; i++)
cout << resourceTypes[i] << " ";
for (int i = 0; i < preyTypesNb; i++)
cout << preyTypes[i]
<< " ";
for (int i = 0; i < preyTypesNb; i++)
cout << preyTypes[i] << "catches"
<< " ";
for (int i = 0; i < predatorTypesNb; i++)
cout << predatorTypes[i] << " ";
cout << endl;
/* iterate through lines and column to cast out the values */
int r = 0;
while (r < rowNb)
{
for (int column = 0; column < columnNb; column++)
cout << landscapeTablePtr[r][column] << " ";
cout << endl;
r++;
}
cout << endl;
}
void createResultsTable(string name) // creates a resultsTable
{
/* debug
cout << "creating " << name << endl
<< endl;
*/
/* write headers */
resultsTable.open(name, ios::out);
if (resultsTable.is_open())
{
resultsTable << "timeStep"
<< ",";
for (int i = 0; i < resourceTypesNb; i++)
resultsTable << resourceTypes[i] << "amount"
<< ",";
for (int i = 0; i < preyTypesNb; i++)
resultsTable << preyTypes[i] << "PopulationSize"
<< ",";
for (int i = 0; i < preyTypesNb; i++)
resultsTable << preyTypes[i] << "catches"
<< ",";
for (int i = 0; i < predatorTypesNb; i++)
{
resultsTable << predatorTypes[i] << "PopulationSize";
if (i == (predatorTypesNb - 1))
resultsTable << "\n";
else
resultsTable << ",";
}
/* close file when finished */
resultsTable.close();
}
}
void createSnapshotTable(string name) // creates a snapshot file
{
/* debug
cout << "creating " << name << endl
<< endl;
*/
/* write headers */
snapshotTable.open(name, ios::out);
if (snapshotTable.is_open())
{
snapshotTable << "timeStep"
<< ","
<< "cellCode"
<< ","
<< "xCell"
<< ","
<< "yCell"
<< ",";
for (int i = 0; i < resourceTypesNb; i++)
snapshotTable << resourceTypes[i] << "amount"
<< ",";
for (int i = 0; i < preyTypesNb; i++)
snapshotTable << preyTypes[i]
<< ",";
for (int i = 0; i < preyTypesNb; i++)
snapshotTable << preyTypes[i] << "catches"
<< ",";
for (int i = 0; i < predatorTypesNb; i++)
{
snapshotTable << predatorTypes[i];
if (i == (predatorTypesNb - 1))
snapshotTable << "\n";
else
snapshotTable << ",";
}
/* close file when finished */
snapshotTable.close();
}
}
void saveMeasures(string name, int ts) // write sum of results columns in the results file
{
/* debug
cout << "appending " << name << endl
<< endl;
*/
resultsTable.open(name, ios::app);
if (resultsTable.is_open())
{
resultsTable << ts
<< ",";
/* write the sum of the measure columns */
for (int i = 0; i < resourceTypesNb; i++)
resultsTable << sumColumn(landscapeTablePtr, rowNb, resColumnStart + i)
<< ",";
for (int i = 0; i < preyTypesNb; i++)
resultsTable << sumColumn(landscapeTablePtr, rowNb, preyColumnStart + i)
<< ",";
for (int i = 0; i < preyTypesNb; i++)
resultsTable << sumColumn(landscapeTablePtr, rowNb, preyColumnStart + preyTypesNb + i)
<< ",";
for (int i = 0; i < predatorTypesNb; i++)
{
resultsTable << sumColumn(landscapeTablePtr, rowNb, predatorColumnStart + i);
if (i == (predatorTypesNb - 1))
resultsTable << "\n";
else
resultsTable << ",";
}
/* close file when finished */
resultsTable.close();
}
}
void snapshot(string name, int ts) // write all info columns in the snapshot file
{
/* debug
cout << "appending " << name << endl
<< endl;
*/
snapshotTable.open(name, ios::app);
if (snapshotTable.is_open())
{
/* iterate through lines and column to cast out the values */
int r = 0;
while (r < rowNb)
{
snapshotTable << ts
<< ",";
for (int column = 0; column < columnNb; column++)
{
if (column != (columnNb - 1))
snapshotTable << landscapeTablePtr[r][column] << ",";
else
snapshotTable << landscapeTablePtr[r][column] << "\n";
}
snapshotTable << "\n";
r++;
}
/* close file when finished */
snapshotTable.close();
}
}
};
class animals
{
private:
/* useful variables for memory allocation */
int columnNb; // number of info stored in the table
/* population level constants that might have different values according to animal types */
int initialDensity;
int typeTag; // a integer tag for each animal type
int maxMove; // max number of cells an animal can move by in each direction
int reproductionCost; // resources needed to reproduce
int maxOffspring; // max number of offspring when passing reproduction trial
protected:
int landscapeSize;
int maintenanceCost; // resources needed to survive a time step
/* matching informations */
int membersMatchingListsIndex; //
vector<int> dietLandscapeIndexes; // array containing their column index in the landscape table
public:
/* population level variables */
int currentPopulationSize; // current population size of the animal
int **populationTablePtr; //
animals(int InitialDensity, int TypeTag, int MaxMove, int MaintenanceCost, int ReproductionCost, int LandscapeSize, int MaxOffspring, string *XYcoordinates, int *CellCodes) // initialise the constants shared by all animal types
{
initialDensity = InitialDensity;
typeTag = TypeTag;
maxMove = MaxMove;
reproductionCost = ReproductionCost;
maintenanceCost = MaintenanceCost;
maxOffspring = MaxOffspring;
landscapeSize = LandscapeSize;
if (maxMove >= landscapeSize)
cout << "Warning animal's moving range is larger than the landscape size, can mess cell coordinates when moving" << endl;
currentPopulationSize = initialDensity; // initialise current population size variable
membersMatchingListsIndex = getMemberIndexFromTag(typeTag);
dietLandscapeIndexes = getDietLandscapeIndexes(membersMatchingListsIndex);
/* create animals table */
columnNb = 7; // x - y - cellCode - resourceConsumed - deadOrAlive - offspring - age
populationTablePtr = new int *[currentPopulationSize]; // define the pointer to an array of int pointers for each row
for (int row = 0; row < currentPopulationSize; row++) // for each row, create a pointer to an integer array of size columnNb
{
populationTablePtr[row] = new int[columnNb];
}
/* add the animals with their info */
int r = 0;
while (r < initialDensity)
{
/* debug
cout << "float(landscapeSize) is " << float(landscapeSize) << endl;
cout << "random number between zero and landsape size without imposing data type is " << randomNumberGenerator(0, landscapeSize) << endl;
cout << "random number between zero and landsape size imposing float(size) is " << randomNumberGenerator(0, float(landscapeSize)) << endl;
cout << "the value that goes in the table is " << int(randomNumberGenerator(0, float(landscapeSize))) << endl;
*/
/* assign random coordinates between 0 and Size*/
populationTablePtr[r][0] = int(randomNumberGenerator(0, landscapeSize - 1));
populationTablePtr[r][1] = int(randomNumberGenerator(0, landscapeSize - 1));
/* update populationTablePtr with prey info - columns indexes hard coded NOT GOOD */
populationTablePtr[r][2] = getCellCode(XYcoordinates, CellCodes, landscapeSize, populationTablePtr[r][0], populationTablePtr[r][1]);
populationTablePtr[r][3] = 0; // initialise resource consumed
populationTablePtr[r][4] = 1; // the animal is alive
populationTablePtr[r][5] = 0; // the animal has no offspring yet
populationTablePtr[r][6] = 0; // initial population is of age 0
r++;
}
}
~animals() // free memory allocated to animals table
{
for (int row = 0; row < currentPopulationSize; row++) // free memory allocated to each row
{
delete[] populationTablePtr[row];
}
delete[] populationTablePtr; // free the memory allocated to the array of pointer to each row
/* debug : OK
cout << "An animal destructor has been called successfully" << endl << endl;
*/
}
void randomMove(string *XYcoordinates, int *CellCodes)
{
/* debug
cout << memberTypes[membersMatchingListsIndex] << " are moving at random" << endl
<< endl;
*/
/* iterate through individuals */
int ind = 0;
while (ind < currentPopulationSize)
{
/* update position with random number in moving range */
int xCell = populationTablePtr[ind][0];
int yCell = populationTablePtr[ind][1];
xCell += randomNumberGenerator(-1 * maxMove, maxMove);
yCell += randomNumberGenerator(-1 * maxMove, maxMove);
/* check boundaries */
populationTablePtr[ind][0] = transmissiveBoundaries(xCell, landscapeSize); // xCell
populationTablePtr[ind][1] = transmissiveBoundaries(yCell, landscapeSize); // yCell
populationTablePtr[ind][2] = getCellCode(XYcoordinates, CellCodes, landscapeSize, populationTablePtr[ind][0], populationTablePtr[ind][1]); // update cell code
ind++; // next individual
}
}
void survivalTrial()
{
/* debug
cout << memberTypes[membersMatchingListsIndex] << " survival trials" << endl
<< endl;
*/
/* iterate through individuals */
int ind = 0;
int zz = 0;
while (ind < currentPopulationSize)
{
if (populationTablePtr[ind][4] == 1) // if individual is alive
{
/* if resonsume < maintenance cost -> change deadOrAlive to 0 else remove maintenance cost from the individual's resource pool */
if (populationTablePtr[ind][3] < maintenanceCost)
{
populationTablePtr[ind][4] = 0;
zz += 1;
}
else
populationTablePtr[ind][3] -= maintenanceCost;
}
ind++; // next individual
}
/* debug
if (zz > 0)
cout << zz << " " << memberTypes[membersMatchingListsIndex] << " had not enough resources to survive" << endl
<< endl;
*/
}
void reproductionTrial()
{
/* debug
cout << memberTypes[membersMatchingListsIndex] << " reproduction trials" << endl
<< endl;
*/
/* iterate through individuals */
int ind = 0;
while (ind < currentPopulationSize)
{
if (populationTablePtr[ind][4] == 1 && populationTablePtr[ind][3] >= reproductionCost) // if individual is alive
{
populationTablePtr[ind][5] = randomNumberGenerator(0, maxOffspring); // even if it has the resource it might not reproduce (and thus not paying the cost)
if (populationTablePtr[ind][5] > 0)
populationTablePtr[ind][3] -= reproductionCost; // if at least one offspring subtract reproduction cost from resource pool
}
ind++; // next individual
}
}
void updatePopulationTable(bool debug) // updates current pop size, creates a new table with updated information, replaces popTable with the updated version, reset offspring to 0, deletes the older version
{
/* update current population size with deaths and offspring */
int oldPopulationSize = currentPopulationSize; // store current population size for later purpose // store previous population size
int deadInds = currentPopulationSize - sumColumn(populationTablePtr, currentPopulationSize, 4); // number of dead ind is pop size - sum of alive individuals
int newInds = sumColumn(populationTablePtr, currentPopulationSize, 5); // sum of all offspring produces
currentPopulationSize += newInds - deadInds; // update current population size/* debug */
/* make warnings if low pop */
if (float(currentPopulationSize) <= 0.1 * float(initialDensity))
cout << memberTypes[membersMatchingListsIndex] << "'s population is under 10 percent of its initial size" << endl
<< endl;
if (currentPopulationSize == 0)
cout << memberTypes[membersMatchingListsIndex] << "'s population has gone extinct" << endl
<< endl;
if (debug == true)
{
cout << memberTypes[membersMatchingListsIndex] << ": " << newInds << " offspring and " << deadInds << " deaths" << endl
<< endl;
}
// */
/* allocate memory to a new table of the right size */
int **newTablePtr = new int *[currentPopulationSize];
for (int row = 0; row < currentPopulationSize; row++)
{
newTablePtr[row] = new int[columnNb];
}
/* replicate all living individuals from the previous list in the new table and add offspring */
int newTabRow = 0; // initialise a row counter for the new table
for (int oldPopRow = 0; oldPopRow < oldPopulationSize; oldPopRow++) // iterate through previous pop table
{
int indDoAstatus = populationTablePtr[oldPopRow][4]; // get dead or alive status
int indOffspring = populationTablePtr[oldPopRow][5]; // get offspring number
int indAge = populationTablePtr[oldPopRow][6]; // get age
if (indDoAstatus == 1 && newTabRow < currentPopulationSize) // if individual is alive and we don't override the new table's dimensions
{
for (int col = 0; col < 5; col++)
newTablePtr[newTabRow][col] = populationTablePtr[oldPopRow][col]; // copy x y position cellCode resource pool and DoA status into new table
newTablePtr[newTabRow][5] = 0; // reset offspring to zero in the new table
newTablePtr[newTabRow][6] = indAge + 1; // individual is one time step older in the new table
newTabRow++; // increment new table row counter
}
/* add all the offspring at the cell of their parent */
if (indOffspring > 0 && newTabRow < currentPopulationSize)
{
for (int i = 0; i < indOffspring; i++) // for each offspring
{
for (int col = 0; col < 3; col++) // copy only position variables
newTablePtr[newTabRow][col] = populationTablePtr[oldPopRow][col];
newTablePtr[newTabRow][3] = 0; // new individual has not consumed any resource yet
newTablePtr[newTabRow][4] = 1; // new individual is alive
newTablePtr[newTabRow][5] = 0; // new individual has not reproduced yet
newTablePtr[newTabRow][6] = 0; // new individual is of age 0
newTabRow++; // increment new table row counter
}
}
}
/* older table not needed anymore, delete it */
for (int row = 0; row < oldPopulationSize; row++)
{
delete[] populationTablePtr[row];
}
delete[] populationTablePtr;
populationTablePtr = newTablePtr;
newTablePtr = NULL;
}
void measureDensity(int **LandscapeTable)
{
/* debug
cout << "measuring " << memberTypes[membersMatchingListsIndex] << " densities" << endl
<< endl;
*/
/* get animal's column index in landscapeTable */
int colIndex = indexInLandscape[membersMatchingListsIndex];
/* resetting densities to 0 */
for (int landRow = 0; landRow < (landscapeSize * landscapeSize); landRow++)
{
LandscapeTable[landRow][colIndex] = 0;
}
/* iterate through population table */
int ind = 0;
while (ind < currentPopulationSize)
{
/* increment landscape table at the right position */
int rowIndex = populationTablePtr[ind][2];
LandscapeTable[rowIndex][colIndex] += 1;
/* debug
cout << "incrementing cell [" << rowIndex << "] [" << colIndex << "]" << endl
<< endl;
*/
ind++; // next individual
}
}
void getInfo() // function to cast out the animalsTable and check if all good
{
/* cast out the column names */
cout << memberTypes[membersMatchingListsIndex] << "'s population table" << endl;
cout << "xCell"
<< " "
<< "yCell"
<< " "
<< "cellCode"
<< " "
<< "resourcesConsumed"
<< " "
<< "deadOrAlive"
<< " "
<< "offspring"
<< " "
<< "age"
<< endl;
/* iterate through lines and column to cast out the values */
int r = 0;
while (r < currentPopulationSize)
{
for (int column = 0; column < columnNb; column++)
cout << populationTablePtr[r][column] << " ";
cout << endl;
r++;
}
cout << endl;
}
};