-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
748 lines (684 loc) · 32 KB
/
Copy pathmain.cpp
File metadata and controls
748 lines (684 loc) · 32 KB
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
#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
//world width
#define W 40
//world height
#define H 40
//world enviroments
enum terrain {GROUND, POISON, FOOD, CORPSE};
//world and genome generation
enum generation {DEFAULT, RANDOM, USER};
//cardinal directions
enum direction {WEST, NORTH, EAST, SOUTH, SIT};
//Genome size
#define GENOME_SIZE 7
//Genome location names
enum genes {FITNESS, MOVEMENT, SENSES, BRAIN_POWER, VIOLENCE, REPRODUCTION, FOOD_TYPE};
//enums of gene types
enum movement {UNMOVING, MOVING};
enum sense {NO_SENSE, SMELL, SIGHT};
enum brain {NO_POWER, BASIC, NORMAL, COMPLEX};
enum aggression {PEACEFUL, DEFENSE, AGGRESSIVE};
enum reproduc {ASEXUAL, SEXUAL};
enum food {VEG, OMN, CAR};
// Initial population size for the algorithm
int popSize = 10;
//structure of an individual
//contains it's genome array, latitude and longitude, number, and if it has reproduced
struct individual {
int genome[GENOME_SIZE];
int lat = -1;
int lon = -1;
int creatureNum = -1;
bool repro = false;
};
//structure of a global location
//has varables for local creatures, how many creatures are present, and the terrain
struct location {
individual p1;
individual p2;
int occupants;
int terrainType;
};
//world "Map"
map<pair<int, int>, location> worldMap;
int rand_num(int start, int end);
bool repeat(string s, char ch);
void mutatedGene(int (&genome)[GENOME_SIZE]);
individual create_gnome(individual creature, int i);
int cal_fitness(int genome[GENOME_SIZE]);
individual asexualRep(individual oldCreature, double mutationChance);
individual sexualRep(individual parent1, struct individual parent2, double mutationChance);
bool findPartner(individual creature, individual (&partner));
void simUtil(double mutationChance, int geneGen, int generations);
void move(vector<individual> (&population));
void move(individual (&creature));
void repopulate(vector<individual> (&population), vector<individual>(&newPopulation), double mutationChance);
individual findClosestCreature(individual creature);
vector<individual> findAllSurrounding(individual creature);
void generateWorld(int genType);
//Function to move every individual in the world
void move(vector<individual> (&population)) {
int direction;
individual newInd;
for (int i = 0; i < population.size(); ++i) {
if (population[i].genome[MOVEMENT] != UNMOVING) {
direction = rand() % 4;
if ((direction == WEST && population[i].lon == 0) || (direction == EAST && population[i].lon == W - 1) ||
(direction == NORTH && population[i].lat == 0) || (direction == SOUTH && population[i].lat == H - 1)) {
bool validMove = false;
while (!validMove) {
direction = rand() % 4;
if ((direction == WEST && population[i].lon > 0) ||
(direction == EAST && population[i].lon < W - 1) ||
(direction == NORTH && population[i].lat > 0) ||
(direction == SOUTH && population[i].lat < H - 1)) {
validMove = true;
}
}
}
if (population[i].genome[BRAIN_POWER] == BASIC && population[i].genome[SENSES] != NO_SENSE) {
if (direction == WEST && worldMap[make_pair(population[i].lat, population[i].lon - 1)].terrainType == POISON) {
direction = EAST;
} else if (direction == EAST && worldMap[make_pair(population[i].lat, population[i].lon + 1)].terrainType == POISON) {
direction = WEST;
} else if (direction == NORTH &&
worldMap[make_pair((population[i].lat - 1), population[i].lon)].terrainType == POISON) {
direction = SOUTH;
} else if (direction == SOUTH &&
worldMap[make_pair((population[i].lat + 1), population[i].lon)].terrainType == POISON) {
direction = NORTH;
}
} else if ((population[i].genome[BRAIN_POWER] == NORMAL && population[i].genome[SENSES] != NO_SENSE) ||
(population[i].genome[BRAIN_POWER] == COMPLEX && population[i].genome[SENSES] == SMELL)) {
if (direction == WEST && worldMap[make_pair(population[i].lat, population[i].lon - 1)].terrainType == POISON) {
direction = EAST;
} else if (direction == EAST && worldMap[make_pair(population[i].lat, population[i].lon + 1)].terrainType == POISON) {
direction = WEST;
} else if (direction == NORTH &&
worldMap[make_pair(population[i].lat - 1, population[i].lon)].terrainType == POISON) {
direction = SOUTH;
} else if (direction == SOUTH &&
worldMap[make_pair(population[i].lat + 1, population[i].lon)].terrainType == POISON) {
direction = NORTH;
}
if (direction == WEST && worldMap[make_pair(population[i].lat, population[i].lon - 1)].occupants != 1) {
direction = SIT;
} else if (direction == EAST && worldMap[make_pair(population[i].lat, population[i].lon + 1)].occupants != 1) {
direction = SIT;
} else if (direction == NORTH && worldMap[make_pair(population[i].lat - 1, population[i].lon)].occupants != 1) {
direction = SIT;
} else if (direction == SOUTH && worldMap[make_pair(population[i].lat + 1, population[i].lon)].occupants != 1) {
direction = SIT;
}
} else if (population[i].genome[BRAIN_POWER] == COMPLEX && population[i].genome[SENSES] != NO_SENSE) {//complex processing logic
//finds the closest creature if there is one and moves accordingly
individual foundInd = findClosestCreature(population[i]);
if (population[i].genome[VIOLENCE] == 2) {
if (foundInd.lon != -1) {
if (foundInd.lat < foundInd.lon) {
if (foundInd.lon < population[i].lon) {
direction = WEST;
}else {
direction = EAST;
}
}else {
if (foundInd.lat < population[i].lat) {
direction = SOUTH;
}else {
direction = NORTH;
}
}
}
if (worldMap[make_pair(population[i].lat, population[i].lon - 1)].terrainType == POISON) {
direction = EAST;
} else if (worldMap[make_pair(population[i].lat, population[i].lon + 1)].terrainType == POISON) {
direction = WEST;
} else if (worldMap[make_pair((population[i].lat - 1), population[i].lon)].terrainType == POISON) {
direction = SOUTH;
} else if (worldMap[make_pair((population[i].lat + 1), population[i].lon)].terrainType == POISON) {
direction = NORTH;
}
if (worldMap[make_pair(population[i].lat, population[i].lon - 1)].terrainType == FOOD ||
worldMap[make_pair(population[i].lat, population[i].lon - 1)].terrainType == CORPSE) {
direction = WEST;
} else if (worldMap[make_pair(population[i].lat, population[i].lon + 1)].terrainType == FOOD ||
worldMap[make_pair(population[i].lat, population[i].lon - 1)].terrainType == CORPSE) {
direction = EAST;
} else if (worldMap[make_pair((population[i].lat - 1), population[i].lon)].terrainType == FOOD ||
worldMap[make_pair(population[i].lat, population[i].lon - 1)].terrainType == CORPSE) {
direction = NORTH;
} else if (worldMap[make_pair((population[i].lat + 1), population[i].lon)].terrainType == FOOD ||
worldMap[make_pair(population[i].lat, population[i].lon - 1)].terrainType == CORPSE) {
direction = SOUTH;
}
}else {
if (worldMap[make_pair(population[i].lat, population[i].lon - 1)].terrainType == POISON ||
worldMap[make_pair(population[i].lat, population[i].lon - 1)].terrainType == CORPSE) {
direction = EAST;
}
if (worldMap[make_pair(population[i].lat, population[i].lon + 1)].terrainType == POISON ||
worldMap[make_pair(population[i].lat, population[i].lon + 1)].terrainType == CORPSE) {
if (direction == EAST) {
direction = SIT;
} else {
direction = WEST;
}
}
if (worldMap[make_pair((population[i].lat - 1), population[i].lon)].terrainType == POISON ||
worldMap[make_pair(population[i].lat - 1, population[i].lon)].terrainType == CORPSE) {
direction = SOUTH;
}if (worldMap[make_pair((population[i].lat + 1), population[i].lon)].terrainType == POISON ||
worldMap[make_pair(population[i].lat + 1, population[i].lon)].terrainType == CORPSE) {
if (direction == SOUTH) {
direction = SIT;
} else {
direction = NORTH;
}
}
if (worldMap[make_pair(population[i].lat, population[i].lon)].terrainType == FOOD) {
direction = SIT;
} else if (worldMap[make_pair(population[i].lat, population[i].lon - 1)].terrainType == FOOD) {
direction = WEST;
} else if (worldMap[make_pair(population[i].lat, population[i].lon + 1)].terrainType == FOOD) {
direction = EAST;
} else if (worldMap[make_pair((population[i].lat - 1), population[i].lon)].terrainType == FOOD) {
direction = NORTH;
} else if (worldMap[make_pair((population[i].lat + 1), population[i].lon)].terrainType == FOOD) {
direction = SOUTH;
}
if (foundInd.creatureNum != -1) {
vector<individual> nearCreatures = findAllSurrounding(population[i]);
vector<individual> newLocationCreatures;
if (direction == WEST) {
population[i].lon--;
newLocationCreatures = findAllSurrounding(population[i]);
population[i].lon++;
} else if (direction == EAST) {
population[i].lon++;
newLocationCreatures = findAllSurrounding(population[i]);
population[i].lon--;
} else if (direction == NORTH) {
population[i].lat++;
newLocationCreatures = findAllSurrounding(population[i]);
population[i].lat--;
} else if (direction == SOUTH) {
population[i].lat--;
newLocationCreatures = findAllSurrounding(population[i]);
population[i].lat++;
}
if (nearCreatures.size() > newLocationCreatures.size()) {
direction = SIT;
}
}
}
}
if (direction == WEST && worldMap[make_pair(population[i].lat, (population[i].lon - 1))].occupants < 2) {
worldMap[make_pair(population[i].lat, population[i].lon)].occupants--;
population[i].lon--;
worldMap[make_pair(population[i].lat, population[i].lon)].occupants++;
if (worldMap[make_pair(population[i].lat, population[i].lon++)].p1.creatureNum == population[i].creatureNum) {
worldMap[make_pair(population[i].lat, population[i].lon++)].p1 = newInd;
worldMap[make_pair(population[i].lat, population[i].lon)].p1 = population[i];
}else {
worldMap[make_pair(population[i].lat, population[i].lon++)].p2 = newInd;
worldMap[make_pair(population[i].lat, population[i].lon)].p2 = population[i];
}
} else if (direction == NORTH && worldMap[make_pair((population[i].lat-1), population[i].lon)].occupants < 2) {
worldMap[make_pair(population[i].lat, population[i].lon)].occupants--;
population[i].lat--;
worldMap[make_pair(population[i].lat, population[i].lon)].occupants++;
if (worldMap[make_pair(population[i].lat++, population[i].lon)].p1.creatureNum == population[i].creatureNum) {
worldMap[make_pair(population[i].lat++, population[i].lon)].p1 = newInd;
worldMap[make_pair(population[i].lat, population[i].lon)].p1 = population[i];
}else {
worldMap[make_pair(population[i].lat++, population[i].lon)].p2 = newInd;
worldMap[make_pair(population[i].lat, population[i].lon)].p2 = population[i];
}
} else if (direction == EAST && worldMap[make_pair((population[i].lat - 1), population[i].lon)].occupants < 2) {
worldMap[make_pair(population[i].lat, population[i].lon)].occupants--;
population[i].lon++;
worldMap[make_pair(population[i].lat, population[i].lon)].occupants++;
if (worldMap[make_pair(population[i].lat, population[i].lon--)].p1.creatureNum == population[i].creatureNum) {
worldMap[make_pair(population[i].lat, population[i].lon--)].p1 = newInd;
worldMap[make_pair(population[i].lat, population[i].lon)].p1 = population[i];
}else {
worldMap[make_pair(population[i].lat, population[i].lon--)].p2 = newInd;
worldMap[make_pair(population[i].lat, population[i].lon)].p2 = population[i];
}
} else if (direction == SOUTH && worldMap[make_pair((population[i].lat - 1), population[i].lon)].occupants < 2) {
worldMap[make_pair(population[i].lat, population[i].lon)].occupants--;
population[i].lat++;
worldMap[make_pair(population[i].lat, population[i].lon)].occupants++;
if (worldMap[make_pair(population[i].lat++, population[i].lon--)].p1.creatureNum ==
population[i].creatureNum) {
worldMap[make_pair(population[i].lat++, population[i].lon)].p1 = newInd;
worldMap[make_pair(population[i].lat, population[i].lon)].p1 = population[i];
} else {
worldMap[make_pair(population[i].lat++, population[i].lon)].p2 = newInd;
worldMap[make_pair(population[i].lat, population[i].lon)].p2 = population[i];
}
}
}
}
}
//move function for a single creature
//used when placing the new population in the world when parent space is full
void move(individual (&creature)) {
int direction = rand() % 4;
if (direction == WEST && worldMap[make_pair(creature.lat, creature.lon - 1)].terrainType == POISON) {
direction = EAST;
} else if (direction == EAST && worldMap[make_pair(creature.lat, creature.lon + 1)].terrainType == POISON) {
direction = WEST;
} else if (direction == NORTH && worldMap[make_pair(creature.lat - 1, creature.lon)].terrainType == POISON) {
direction = SOUTH;
} else if (direction == SOUTH && worldMap[make_pair(creature.lat + 1, creature.lon)].terrainType == POISON) {
direction = NORTH;
}
if (direction == WEST && worldMap[make_pair(creature.lat, creature.lon - 1)].occupants < 2) {
creature.lon--;
worldMap[make_pair(creature.lat, creature.lon)].occupants++;
if (worldMap[make_pair(creature.lat, creature.lon)].occupants == 1) {
worldMap[make_pair(creature.lat, creature.lon)].p1 = creature;
}else {
worldMap[make_pair(creature.lat, creature.lon)].p2 = creature;
}
} else if (direction == NORTH && worldMap[make_pair(creature.lat - 1, creature.lon)].occupants < 2) {
creature.lat--;
worldMap[make_pair(creature.lat, creature.lon)].occupants++;
if (worldMap[make_pair(creature.lat, creature.lon)].occupants == 1) {
worldMap[make_pair(creature.lat, creature.lon)].p1 = creature;
}else {
worldMap[make_pair(creature.lat, creature.lon)].p2 = creature;
}
} else if (direction == EAST && worldMap[make_pair(creature.lat - 1, creature.lon)].occupants < 2) {
creature.lon++;
worldMap[make_pair(creature.lat, creature.lon)].occupants++;
if (worldMap[make_pair(creature.lat, creature.lon)].occupants == 1) {
worldMap[make_pair(creature.lat, creature.lon)].p1 = creature;
}else {
worldMap[make_pair(creature.lat, creature.lon)].p2 = creature;
}
} else if (direction == SOUTH && worldMap[make_pair(creature.lat - 1, creature.lon)].occupants < 2) {
creature.lat++;
worldMap[make_pair(creature.lat, creature.lon)].occupants++;
if (worldMap[make_pair(creature.lat, creature.lon)].occupants == 1) {
worldMap[make_pair(creature.lat, creature.lon)].p1 = creature;
}else {
worldMap[make_pair(creature.lat, creature.lon)].p2 = creature;
}
}
}
// Function to find the closest creature to given
// returns a blank individual if closest is same tile or no creature in range
individual findClosestCreature(individual creature) {
individual foundCreature;
if (worldMap[make_pair(creature.lat, creature.lon)].occupants == 2) {
return foundCreature;
}
for (int x = -2; x < 3; x++) {
for (int y = -2; y < 3; y++) {
if ((creature.lat + x >= 0 && creature.lon + y >= 0) && (creature.lat + x < H && creature.lon + y < W)) {
if (worldMap[make_pair(creature.lat+x, creature.lon+y)].occupants != 0) {
foundCreature = worldMap[make_pair((creature.lat+x), (creature.lon+y))].p1;
return foundCreature;
}
}
}
}
return foundCreature;
}
//finds all of the creatures up to two tiles from given creature
vector<individual> findAllSurrounding(individual creature) {
vector<individual> foundCreatures;
for (int x = -2; x < 3; x++) {
for (int y = -2; y < 3; y++) {
if ((creature.lat + x >= 0 && creature.lon + y >= 0) && (creature.lat + x < H && creature.lon + y < W)) {
if (worldMap[make_pair(creature.lat+x, creature.lon+y)].occupants != 0) {
if (x == 0 && y == 0) {
if (worldMap[make_pair((creature.lat+x), (creature.lon+y))].p1.creatureNum != creature.creatureNum) {
foundCreatures.push_back(worldMap[make_pair((creature.lat+x), (creature.lon+y))].p1);
} else {
foundCreatures.push_back(worldMap[make_pair((creature.lat+x), (creature.lon+y))].p2);
}
}
if (worldMap[make_pair(creature.lat+x, creature.lon+y)].occupants == 1) {
foundCreatures.push_back(worldMap[make_pair((creature.lat + x), (creature.lon + y))].p1);
}else {
foundCreatures.push_back(worldMap[make_pair((creature.lat + x), (creature.lon + y))].p1);
foundCreatures.push_back(worldMap[make_pair((creature.lat + x), (creature.lon + y))].p2);
}
}
}
}
}
return foundCreatures;
}
// Function to return a random number
// from start and end
int rand_num(int start, int end)
{
int r = end - start;
int rnum = start + rand() % r;
return rnum;
}
// Function to return a mutated GNOME
// Mutated GNOME is a string
// with a random interchange
// of two genes to create variation in species
void mutatedGene(int (&genome)[GENOME_SIZE])
{
int r = rand_num(1, GENOME_SIZE);
if (r == MOVEMENT || r == REPRODUCTION) {
genome[r] = rand_num(0, 2);
} else if (r == SENSES || r == VIOLENCE || r == FOOD_TYPE) {
genome[r] = rand_num(0, 2);
} else if (r == BRAIN_POWER) {
genome[r] = rand_num(0, 3);
}
}
// Function to return a valid GENOME
// required to create the population
// TODO: Add user input of starting genome
individual create_gnome(struct individual individual, int i)
{
if (i == DEFAULT) {
for (int i = 0; i < GENOME_SIZE; i++) {
individual.genome[i] = 0;
}
}else if (i == RANDOM) {
for (int i = 0; i < GENOME_SIZE; i++) {
if (i == MOVEMENT || i == REPRODUCTION) {
individual.genome[i] = rand_num(0, 1);
} else if (i == SENSES || i == VIOLENCE || i == FOOD_TYPE) {
individual.genome[i] = rand_num(0, 2);
} else if (i == BRAIN_POWER) {
individual.genome[i] = rand_num(0, 3);
}
}
}else if (i == USER) {
}
return individual;
}
// Function to return the fitness value of a gnome.
// The fitness value is the path length
// of the path represented by the GNOME.
int cal_fitness(int genome[GENOME_SIZE])
{
genome[FITNESS] = 0;
for (int i = 1; i < GENOME_SIZE; i++) {
genome[FITNESS] += genome[i];
}
return genome[FITNESS];
}
//Asexual reproduction with only one creature
individual asexualRep(struct individual oldCreature, double mutationChance) {
struct individual newCreature = oldCreature;
mutatedGene(newCreature.genome);
newCreature.genome[FITNESS] = cal_fitness(newCreature.genome);
if (newCreature.genome[FITNESS] >= oldCreature.genome[FITNESS]) {
return newCreature;
} else {
if (rand() / RAND_MAX > mutationChance) {
return newCreature;
} else {
return oldCreature;
}
}
}
//Sexual reproduction between two creatures
individual sexualRep(individual parent1, individual parent2, double mutationChance) {
individual new_ind;
for (int i = 1; i < GENOME_SIZE; i++) {
int diff = abs(parent1.genome[i] - parent2.genome[i]);
if (diff == 0) {
new_ind.genome[i] = parent1.genome[i];
}else if (diff == 1) {
if (parent1.genome[i] > parent2.genome[i]) {
new_ind.genome[i] = parent1.genome[i];
} else {
new_ind.genome[i] = parent2.genome[i];
}
}else if (diff >= 2) {
if (parent1.genome[i] > parent2.genome[i]) {
new_ind.genome[i] = parent1.genome[i] - 1;
} else {
new_ind.genome[i] = parent2.genome[i] - 1;
}
}
}
individual mutated_ind = new_ind;
mutatedGene(mutated_ind.genome);
mutated_ind.genome[FITNESS] = cal_fitness(mutated_ind.genome);
if (mutated_ind.genome[FITNESS] >= new_ind.genome[FITNESS]) {
return mutated_ind;
} else {
if ((double)rand() / RAND_MAX > mutationChance) {
return mutated_ind;
} else {
return new_ind;
}
}
}
// Attempts to find a partner within 2 cells of an individual
bool findPartner(individual creature, individual (&partner)) {
bool found = false;
for (int x = -2; x < 3; x++) {
for (int y = -2; y < 3; y++) {
if ((creature.lat + x >= 0 && creature.lon + y >= 0) && (creature.lat + x < H && creature.lon + y < W)) {
if (worldMap[make_pair(creature.lat+x, creature.lon+y)].occupants != 0) {
partner = worldMap[make_pair(creature.lat+x, creature.lon+y)].p1;
found = true;
return found;
}
}
}
}
return found;
}
// repopulation function
void repopulate(vector<individual> (&population), vector<individual> (&newPopulation), double mutationChance) {
while (newPopulation.size() < popSize) {
if (population[0].repro) {
for (auto & i : population) {
i.repro = false;
}
}
for (int i = 0; i < population.size(); i++) {
individual &p1 = population[i];
if (!p1.repro) {
if (worldMap[make_pair(p1.lon, p1.lat)].occupants == 2) {
individual p2;
if (worldMap[make_pair(p1.lon, p1.lat)].p1.creatureNum == p1.creatureNum) {
p2 = worldMap[make_pair(p1.lon, p1.lat)].p2;
}else {
p2 = worldMap[make_pair(p1.lon, p1.lat)].p1;
}
if (p1.genome[REPRODUCTION] == ASEXUAL) {
newPopulation.push_back(asexualRep(p1, mutationChance));
if (worldMap[make_pair(p1.lon, p1.lat)].p1.creatureNum == p1.creatureNum) {
worldMap[make_pair(p1.lon, p1.lat)].p1.repro = true;
}else {
worldMap[make_pair(p1.lon, p1.lat)].p2.repro = true;
}
p1.repro = true;
}
if (!p1.repro && !p2.repro) {
newPopulation.push_back(sexualRep(p1, p2, mutationChance));
} else if (!p1.repro) {
bool found = findPartner(p1, p2);
if (found) {
newPopulation.push_back(sexualRep(p1, p2, mutationChance));
}
}
} else {
individual newP;
if (p1.genome[REPRODUCTION] == ASEXUAL) {
newP = asexualRep(p1, mutationChance);
newPopulation.push_back(newP);
} else {
individual p2;
bool found = findPartner(p1, p2);
if (found) {
newPopulation.push_back(sexualRep(p1, p2, mutationChance));
}
}
}
}
}
}
}
// Main Sim loop
void simUtil(double mutationChance, int geneGen, int generations)
{
// Generation Number
int gen = 1;
vector<individual> population;
// Populating the GENOME pool.
for (int i = 0; i < popSize; i++) {
individual creature;
creature = create_gnome(creature, geneGen);
creature.genome[FITNESS] = cal_fitness(creature.genome);
population.push_back(creature);
bool valid = false;
int lat = rand_num(0, H - 1);
int lon = rand_num(0, W - 1);
while (!valid) {
if (worldMap[make_pair(lat, lon)].occupants < 2) {
worldMap[make_pair(lat, lon)].occupants++;
population[i].lat = lat;
population[i].lon = lon;
if (worldMap[make_pair(lat, lon)].occupants == 1) {
worldMap[make_pair(lat, lon)].p1 = population[i];
} else {
worldMap[make_pair(lat, lon)].p2 = population[i];
}
valid = true;
} else {
lat = rand_num(0, H - 1);
lon = rand_num(0, W - 1);
}
}
}
cout << "\nInitial population: " << endl
<< "GENOME\n";
cout << "Movement Sense Brain Violence Repro Food Fitness\n";
for (int i = 0; i < popSize; i++) {
for (int j = 0; j < GENOME_SIZE; j++) {
cout << population[i].genome[j] << " ";
}
cout << population[i].genome[FITNESS] << endl;
}
cout << "\n";
bool found = false;
// Iteration to perform
// population crossing and gene mutation.
while (gen <= generations) {
move(population);
for (int i = 0; i < population.size(); i++) {
if (worldMap[make_pair(population[i].lat, population[i].lon)].terrainType == POISON) {
population.erase(population.begin() + i);
if (i != 0) {
i--;
}
}
if (worldMap[make_pair(population[i].lat, population[i].lon)].occupants == 2 && population[i].genome[VIOLENCE] == AGGRESSIVE) {
int x;
for (x = 0; x < population.size(); x++) {
if (x != i) {
if (population[i].lon == population[x].lon && population[i].lat == population[x].lat) {
break;
}
}
}
if (population[x].genome[VIOLENCE] >= 1) {
if ((double)rand() / RAND_MAX > 0.5) {
population.erase(population.begin() + x);
}else {
population.erase(population.begin() + i);
}
}
}
}
vector<struct individual> newPopulation;
repopulate(population, newPopulation, mutationChance);
population.clear();
for (const auto & i : newPopulation) {
population.push_back(i);
}
individual blankInd;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
worldMap[make_pair(i, j)].occupants = 0;
worldMap[make_pair(i, j)].p1 = blankInd;
worldMap[make_pair(i, j)].p2 = blankInd;
}
}
for (auto & i : population) {
if (worldMap[make_pair(i.lat, i.lon)].occupants < 2) {
worldMap[make_pair(i.lat, i.lon)].occupants++;
if (worldMap[make_pair(i.lat, i.lon)].occupants == 1) {
worldMap[make_pair(i.lat, i.lon)].p1 = i;
}else {
worldMap[make_pair(i.lat, i.lon)].p2 = i;
}
} else {
move(i);
}
}
cout << "Generation " << gen << " \n";
cout << "GNOME\n";
cout << "Movement Sense Brain Violence Repro Food Fitness\n";
for (auto & i : population) {
for (int j = 1; j < GENOME_SIZE; j++) {
cout << i.genome[j] << " ";
}
cout << i.genome[FITNESS] << endl;
}
gen++;
}
}
void generateWorld(int genType) {
if (genType == DEFAULT) {
for (int i = 0; i < W; ++i) {
for (int j = 0; j < H; ++j) {
worldMap[make_pair(i, j)].terrainType = GROUND;
}
}
}else if (genType == RANDOM) {
for (int i = 0; i < W; ++i) {
for (int j = 0; j < H; ++j) {
worldMap[make_pair(i, j)].terrainType = rand_num(GROUND, CORPSE);
}
}
}else if (genType == USER) {
}
}
int main()
{
int worldGen;
double mutationChance;
int geneGen;
int generations;
srand(time(0));
cout << "How would you like world generation:\n"
"0 - No obstacles\n"
"1 - Random obstacles\n";
cin >> worldGen;
generateWorld(worldGen);
cout << "Give a decimal for mutation chance (0.5):\n";
cin >> mutationChance;
cout << "How would you like starting gene generation:\n"
"0 - All base\n"
"1 - Random\n";
cin >> geneGen;
cout << "Give an integer number for the minimum population size:\n";
cin >> popSize;
cout << "How many generation do you want(give an integer number):\n";
cin >> generations;
simUtil(mutationChance, geneGen, generations);
}