-
Notifications
You must be signed in to change notification settings - Fork 0
/
fantasy-foodweb
996 lines (830 loc) · 50 KB
/
fantasy-foodweb
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
#!/usr/bin/env python
import random
import time
from listofsounds import forenamelist, surnamelist # this file should be in the same github repository.
#The intention is, given inputs, generate a set of animals and some skills
#generates a feeding matrix to see who eats what
#scores everything (rock paper scissors). allocates enenergy.
#adds a mutated creature, repeats.
#generic beat is shaped like:
#this_producer={
# 'name':Name,'type':Producer_type,'Species_energy':Species_energy
# 'compi':compi_type, 'predi':predi_type, 'defnd':defnd_type}
# note that fitness is computed on the fly, and used to assign energy
###########################################
## lookup Dictionarys of fun output text ##
Light_eater_compi = { 'SC': 'Quickly growing to full height', 'RK': 'Entangling climber', 'PA': 'Symbiotic Defender '}
Chem_eater_compi = { 'SC': 'Multiple food types', 'RK': 'Colonisation ', 'PA': 'Extremophile'}
Magic_eater_compi = { 'SC': 'Sprawling mass', 'RK': 'Magical Reservoirs in Roots/Stem/Spots', 'PA': 'Fire/Ice/Other Environmental discharge'}
Detri_eater_compi = { 'SC': 'Tendrils', 'RK': 'Mobile life stage - maggots, flies or similar', 'PA': 'Digestive ooze'}
Light_eater_defnd = { 'SC': 'Tall, high leaves', 'RK': 'Woody stems', 'PA': 'Sacrificial Fruits '}
Chem_eater_defnd = { 'SC': 'Withdrawal reflex', 'RK': 'Heavy metal concentrate', 'PA': 'Stone skin'}
Magic_eater_defnd = { 'SC': 'Floating/hiding/distracting', 'RK': 'Cursed Spines/thorns', 'PA': 'Regeneration'}
Detri_eater_defnd = { 'SC': 'Deep roots', 'RK': 'Brightly Poisonous', 'PA': 'Parasitic life stage'}
Herbi_eater_compi = { 'RK': 'Rapid growth', 'PA': 'Loud warning calls', 'SC': 'Dense and tough muscly body'}
Herbi_eater_predi = { 'RK': 'Long neck/good climber/burrower', 'PA': 'Specialised gut', 'SC': 'Grinding maw/teeth/beak'}
Herbi_eater_defnd = { 'RK': 'Horns/antlers/tusks/spikes', 'PA': 'Fast and nimble', 'SC': 'Keen senses'}
Herbi_eater_defbehav = { 'RK': 'Flee from danger', 'PA': 'Defend aggressively', 'SC': 'Sense and avoid'}
Meati_eater_compi = { 'PA': ' Climber/flyer/burrower', 'SC': 'Extra long grappling limbs', 'RK': 'Thick hide/shell '}
Meati_eater_predi = { 'PA': 'Crushing bite/pounce/squeeze', 'SC': 'stealthy camouflage', 'RK': 'Venomous strike'}
Meati_eater_defnd = { 'PA': 'seasonally nomadic', 'SC': 'defends territory / or den', 'RK': 'will form larger packs when hungry'}
Meati_eater_defbehav = { 'RK': 'flee from danger', 'PA': 'defend aggressively', 'SC': 'sense and avoid'}
Meati_eater_behav = { 'PA': 'an endurance chaser', 'SC': 'a killer charger', 'RK': 'a stealthy ambusher'}
dictofsize = {
'producer_1': 'finger long, grass',
'producer_2': 'hand size, potplant',
'producer_3': 'knee high, bush',
'producer_4': 'head height, large bush',
'producer_5': '2 men tall, fruit tree',
'producer_6': '4 men tall, large tree',
'producer_7': '6 men tall, mature tree',
'producer_8': 'giant, sequoia',
'producer_9': 'leviathan, small hill',
'beast_1': 'tiny, ant',
'beast_2': 'small, rat',
'beast_3': 'medium, fox',
'beast_4': 'human',
'beast_5': 'large, bear',
'beast_6': 'big, rhino',
'beast_7': 'huge, T-Rex',
'beast_8': 'giant, Diplodocus',
'beast_9': 'leviathan, small hill'
}
###########################################
## Organiser dictonaries for data passing##
dictofdicts = {
'Light_eater_compi': Light_eater_compi,
'Chem_eater_compi': Chem_eater_compi,
'Magic_eater_compi': Magic_eater_compi,
'Detri_eater_compi': Detri_eater_compi,
'Light_eater_defnd': Light_eater_defnd,
'Chem_eater_defnd': Chem_eater_defnd,
'Magic_eater_defnd': Magic_eater_defnd,
'Detri_eater_defnd': Detri_eater_defnd,
'Herbi_eater_compi': Herbi_eater_compi,
'Herbi_eater_predi': Herbi_eater_predi,
'Herbi_eater_defnd': Herbi_eater_defnd,
'Herbi_eater_defbehav': Herbi_eater_defbehav,
'Meati_eater_compi': Meati_eater_compi,
'Meati_eater_predi': Meati_eater_predi,
'Meati_eater_defnd': Meati_eater_defnd,
'Meati_eater_behav': Meati_eater_behav,
'Meati_eater_defbehav': Meati_eater_defbehav,
}
#used to define the blocks where creature codenames begin:
EntryCounters = {
'Light_counter': 10000,
'Chem_counter' : 20000,
'Magic_counter': 30000,
'Detri_counter': 40000,
'Herbivore_counter': 50000,
'Carnivore_counter': 60000
}
#######################################
#######################################
## Generate a new creature functions ##
###########VVVVVVVVVVV#################
##############VVVVV####################
#this function creates a random type of generic skill.
def Create_skill():
type_skill = ['SC', 'RK', 'PA']
temp_value = random.randint(0,2) #since lists start at 0
return(type_skill[temp_value])
def Create_size():
temp = random.randint(-1,9)
for x in range (0,4):
temp = temp + random.randint(-1,9) #gives nice normal distribution about 3.6
Size = int(abs(temp))+1 #makes sure size >0 and an nice integer
while Size > 9:
Size = Size-9
return(Size)
#Create a producer. Increment the correct counter.
def Create_Producer_type(EntryCounters):
total_energy = incoming_light+incoming_chem+incoming_magic+incoming_detritus
Temp_value = random.randint(1,total_energy)
if(Temp_value < incoming_light):
Producer_type = 'Light_eater'
EntryCounters['Light_counter'] = EntryCounters['Light_counter'] +1
Name = EntryCounters['Light_counter']
return(Producer_type, EntryCounters, Name)
else:
Temp_value = Temp_value-incoming_light
if(Temp_value < incoming_chem):
Producer_type = 'Chem_eater'
EntryCounters['Chem_counter'] = EntryCounters['Chem_counter'] +1
Name = EntryCounters['Chem_counter']
return(Producer_type, EntryCounters, Name)
else:
Temp_value = Temp_value-incoming_chem
if(Temp_value < incoming_magic):
Producer_type = 'Magic_eater'
EntryCounters['Magic_counter'] = EntryCounters['Magic_counter']+1
Name = EntryCounters['Magic_counter']
return(Producer_type, EntryCounters, Name)
else:
Producer_type ='Detri_eater'
EntryCounters['Detri_counter'] = EntryCounters['Detri_counter']+1
Name = EntryCounters['Detri_counter']
return(Producer_type, EntryCounters, Name)
def Create_Producer(EntryCounters, LocalSkillFreq):
#Create_Producer_type could be merged into this function, but meh
Producer_type, EntryCounters, Name= Create_Producer_type(EntryCounters)
if(random.randint(1,LocalSkillFreq)==1):
compi_type=Create_skill()
else: compi_type='--'
if(random.randint(1,LocalSkillFreq)==1):
defnd_type=Create_skill()
else: defnd_type='--'
Size = Create_size()
Species_energy = 100 #intilaises everyone the same starting energy.
Number = 10
this_producer={'name':Name,'type':Producer_type,'fitness':10,'Species_energy':Species_energy, 'Number':Number,'compi':compi_type, 'predi':'--', 'defnd':defnd_type, 'behav':' ', 'defbehav': ' ', 'Size':Size, 'Prey_size':' '}
return(this_producer,EntryCounters)
#create a new Herbivore
def Create_Herbivore(EntryCounters, LocalSkillFreq):
EntryCounters['Herbivore_counter'] +=1
Name=EntryCounters['Herbivore_counter']
Producer_type= 'Herbi_eater'
if(random.randint(1,LocalSkillFreq)==1):
compi_type=Create_skill()
else: compi_type='--'
if(random.randint(1,LocalSkillFreq)==1):
predi_type=Create_skill()
else: predi_type='--'
if(random.randint(1,LocalSkillFreq)==1):
defnd_type=Create_skill()
else: defnd_type='--'
if(random.randint(1,1)==1): # they all get a behav tag
defbehav_type=Create_skill()
else: defbehav_type='--'
Size = Create_size()
Species_energy = 100 #intilaises everyone the same starting energy.
Number = 10
this_beast={'name':Name,'type':Producer_type,'fitness':10,'Species_energy':Species_energy,'Number':Number,'compi':compi_type, 'predi':predi_type, 'defnd':defnd_type, 'behav':' ', 'defbehav':defbehav_type,'Size':Size, 'Prey_size':' '}
return(this_beast,EntryCounters)
#create a new Carnivore
def Create_Carnivore(EntryCounters, LocalSkillFreq):
EntryCounters['Carnivore_counter'] +=1
Name=EntryCounters['Carnivore_counter']
Producer_type= 'Meati_eater'
if(random.randint(1,LocalSkillFreq)==1):
compi_type=Create_skill()
else: compi_type='--'
if(random.randint(1,LocalSkillFreq)==1):
predi_type=Create_skill()
else: predi_type='--'
if(random.randint(1,LocalSkillFreq)==1):
defnd_type=Create_skill()
else: defnd_type='--'
if(random.randint(1,1)==1): #they all get a defbehav tag
defbehav_type=Create_skill()
else: defbehav_type='--' #defbav type defines how behaves when preyed upon themselves.
if(random.randint(1,LocalSkillFreq)==1):
behav_type=Create_skill()
else: behav_type='--' # behav type defines how behaves when hunting another creature
Size = Create_size()
Prey_size = Size + random.randint(-2,2)+ random.randint(-2,2)+ random.randint(-2,2)+ random.randint(-2,2)
if Prey_size > 9: Prey_size=Prey_size-8
if Prey_size < 1: Prey_size=Size
Species_energy = 100 #intilaises everyone the same starting energy.
Number=10
this_beast={'name':Name,'type':Producer_type,'fitness':10,'Species_energy':Species_energy,'Number':Number,'compi':compi_type, 'predi':predi_type, 'defnd':defnd_type, 'defbehav':defbehav_type, 'behav':behav_type, 'Size':Size, 'Prey_size':Prey_size}
return(this_beast,EntryCounters)
############################
############################
## Evaluation functions ##
#########VVVVVVVVVVV########
#############VVVV###########
def evaluate_carnivores(dictofcarnivore):
#calculate the total energy available at this trophic level
basic_share_of_energy = total_carnivore_energy/len(dictofcarnivore) #simple average
total_fitness = 0
#competition modifiers
for mainmonster in dictofcarnivore:
temp_fitness = 1 #assumes all equally fit
listofcodes=['RK','PA', 'SC','RK','PA', 'SC','RK']
#first run through all v all and assign energy based on size and target size alone
#do this outside the rock,paper scissor loop to conserve loops
size = dictofcarnivore[mainmonster]['Prey_size']
for tempmonster in dictofherbivore:
prey_size = dictofherbivore[tempmonster]['Size']
size_factor = evaluate_prey_size(size, prey_size) # measures how good the carnivore would be at catching the herbivore
temp_fitness = temp_fitness +size_factor*dictofherbivore[tempmonster]['Species_energy']
for tempmonster in dictofcarnivore:
prey_size = dictofcarnivore[tempmonster]['Size']
size_factor = evaluate_prey_size(size, prey_size)
temp_fitness = temp_fitness +size_factor*dictofcarnivore[tempmonster]['Species_energy']
for i in range (0,3):
first_key = listofcodes[i]
second_key = listofcodes[i+1]
third_key = listofcodes[i+2]
if dictofcarnivore[mainmonster]['compi']!='--': # only needs to run loops if there is a skill to compare
#START checking and scoring compi
if dictofcarnivore[mainmonster]['compi']==first_key:
for tempmonster in dictofcarnivore:
if dictofcarnivore[tempmonster]['compi']==second_key:
temp_fitness = temp_fitness - 0.1*dictofcarnivore[tempmonster]['Species_energy'] # being pushed out
elif dictofcarnivore[tempmonster]['compi']==third_key:
temp_fitness = temp_fitness + 0.1*dictofcarnivore[tempmonster]['Species_energy'] # mainmonster's compi beats this ones
else: temp_fitness = temp_fitness +0.01*dictofcarnivore[tempmonster]['Species_energy'] # against null skills
#END checking and scoring compi
#START checking and scoring predi -carnivores eating herbivores
#I am using the global variable for the different producers
#BECUASE this function only changes carnivores, not herbivores
if dictofcarnivore[mainmonster]['predi']!='--': # only needs to run loops if there is a skill to compare
if dictofcarnivore[mainmonster]['predi']==first_key:
for tempmonster in dictofherbivore:
size = dictofcarnivore[mainmonster]['Prey_size']
prey_size = dictofherbivore[tempmonster]['Size']
size_factor = evaluate_prey_size(size, prey_size) # measures how good the carnivore would be at catching the herbivore
if dictofherbivore[tempmonster]['defnd']==second_key:
temp_fitness = temp_fitness - 0.1*size_factor*dictofherbivore[tempmonster]['Species_energy'] # being pushed out
elif dictofherbivore[tempmonster]['defnd']==third_key:
temp_fitness = temp_fitness + 0.1*size_factor*dictofherbivore[tempmonster]['Species_energy'] # mainmonster's compi beats this ones
else: temp_fitness = temp_fitness +0.01*size_factor*dictofherbivore[tempmonster]['Species_energy'] #+1 against null skills
#END checking and scoring predi on herbivores
#START checking and scoring predi -carnivores eating carnivores (eg bears eating seals)
#I am using the global variable for the different producers
#BECUASE this function only changes carnivores, not herbivores
if dictofcarnivore[mainmonster]['predi']!='--': # only needs to run loops if there is a skill to compare
if dictofcarnivore[mainmonster]['predi']==first_key:
for tempmonster in dictofcarnivore:
size = dictofcarnivore[mainmonster]['Prey_size']
prey_size = dictofcarnivore[tempmonster]['Size']
size_factor = evaluate_prey_size(size, prey_size) # measures how good the carnivore would be at catching the other predator
if dictofcarnivore[tempmonster]!=dictofcarnivore[mainmonster]: #no fitness bonus from cannibilism!
if dictofcarnivore[tempmonster]['defnd']==second_key:
temp_fitness = temp_fitness - 0.1*size_factor*dictofcarnivore[tempmonster]['Species_energy'] # being pushed out
elif dictofcarnivore[tempmonster]['defnd']==third_key:
temp_fitness = temp_fitness + 0.1*size_factor*dictofcarnivore[tempmonster]['Species_energy'] # mainmonster's compi beats this ones
else: temp_fitness = temp_fitness +0.01*size_factor*dictofcarnivore[tempmonster]['Species_energy'] #+1 against null skills
#END checking and scoring predi on other preditors
#START checking and scoring behav on predator / herbivore interaction
if dictofcarnivore[mainmonster]['behav']!='--': # only needs to run loops if there is a skill to compare
if dictofcarnivore[mainmonster]['behav']==first_key:
for tempmonster in dictofherbivore:
size = dictofcarnivore[mainmonster]['Prey_size']
prey_size = dictofherbivore[tempmonster]['Size']
size_factor = evaluate_prey_size(size, prey_size) # measures how good the carnivore would be at catching the herbivore
if dictofherbivore[tempmonster]['defbehav']==second_key:
temp_fitness = temp_fitness - 0.1*size_factor*dictofherbivore[tempmonster]['Species_energy'] # being pushed out
elif dictofherbivore[tempmonster]['defbehav']==third_key:
temp_fitness = temp_fitness + 0.1*size_factor*dictofherbivore[tempmonster]['Species_energy'] # mainmonster's compi beats this ones
else: temp_fitness = temp_fitness +0.01*size_factor*dictofherbivore[tempmonster]['Species_energy'] #+1 against null skills
#END checking and scoring behav on predator/ herbivore interaction
# START checking behav when mainmonster is the predator and another predator the prey
if dictofcarnivore[mainmonster]['behav']!='--': # only needs to run loops if there is a skill to compare
if dictofcarnivore[mainmonster]['behav']==first_key:
for tempmonster in dictofcarnivore:
size = dictofcarnivore[mainmonster]['Prey_size']
prey_size = dictofcarnivore[tempmonster]['Size']
size_factor = evaluate_prey_size(size, prey_size) # measures how good the carnivore would be at catching the other predator
if dictofcarnivore[tempmonster]!=dictofcarnivore[mainmonster]: #no fitness bonus from cannibilism!
if dictofcarnivore[tempmonster]['defbehav']==second_key:
temp_fitness = temp_fitness - 0.1*size_factor*dictofcarnivore[tempmonster]['Species_energy'] # being pushed out
elif dictofcarnivore[tempmonster]['defbehav']==third_key:
temp_fitness = temp_fitness + 0.1*size_factor*dictofcarnivore[tempmonster]['Species_energy'] # mainmonster's compi beats this ones
else: temp_fitness = temp_fitness +0.01*size_factor*dictofcarnivore[tempmonster]['Species_energy'] #+1 against null skills
#END checking mainmonster as predator on the tempmonster.
# START checking when the tempmonset is predator using its behav against the mainmonster's defbehav
if dictofcarnivore[mainmonster]['defbehav']!='--': # only needs to run loops if there is a skill to compare
if dictofcarnivore[mainmonster]['defbehav']==first_key:
for tempmonster in dictofcarnivore:
size = dictofcarnivore[mainmonster]['Prey_size']
prey_size = dictofcarnivore[tempmonster]['Size']
size_factor = evaluate_prey_size(size, prey_size) # measures how good the carnivore would be at catching the other predator
if dictofcarnivore[tempmonster]!=dictofcarnivore[mainmonster]: #no fitness bonus from cannibilism!
if dictofcarnivore[tempmonster]['behav']==second_key:
temp_fitness = temp_fitness + 0.1*size_factor*dictofcarnivore[tempmonster]['Species_energy'] # being pushed out
# CHANGE TO WETHER IT@S A PLUS OR A MINUS HERE!
elif dictofcarnivore[tempmonster]['behav']==third_key:
temp_fitness = temp_fitness - 0.1*size_factor*dictofcarnivore[tempmonster]['Species_energy'] # mainmonster's compi beats this ones
# CHANGE TO WETHER IT@S A PLUS OR A MINUS HERE!
else: temp_fitness = temp_fitness +0.01*size_factor*dictofcarnivore[tempmonster]['Species_energy'] #+1 against null skills
dictofcarnivore[mainmonster]['fitness']=temp_fitness #set this monster's fitness from compi
#END looping through for 'RK' for main monster
if temp_fitness < 0: temp_fitness=0
total_fitness=total_fitness+temp_fitness
dictofcarnivore[mainmonster]['fitness']=round(temp_fitness,2) #set this monster's fitness from compi
#END looping through all three for main monster. new monster!
#END checking and scoring defence against predators
if total_fitness == 0: total_fitness=1
print('Carnivores checked: total fitness', round(total_fitness,2))
for mainmonster in dictofcarnivore:
share_factor=dictofcarnivore[mainmonster]['fitness']/total_fitness
energy = round(total_carnivore_energy*share_factor,2)
size = dictofcarnivore[mainmonster]['Size']
packsize = evaluate_pack_size(size, dictofcarnivore[mainmonster]['Prey_size'])
dictofcarnivore[mainmonster]['Species_energy']=energy
dictofcarnivore[mainmonster]['Number'] = round(energy/(packsize*(size**3)),0)
if dictofcarnivore[mainmonster]['Number'] <1:
dictofcarnivore[mainmonster]['Species_energy']=0
print(dictofcarnivore[mainmonster]['name'], 'has died out')
def evaluate_prey_size(size, prey_size):
#cases - can eat normally down two or up 1 size.
if size-1<=prey_size<=size+1:
factor=1.0
return(factor)
elif size>prey_size:
factor =0.1*prey_size/size
return(factor)
elif size<prey_size:
factor = 0.1*size/prey_size
return(factor)
def evaluate_pack_size(size,preysize):
if size< preysize :
packsize = (preysize-size+1)**4
else: packsize=1
return(packsize)
def evaluate_herbivores(dictofherbivore):
#compi setting
total_fitness=0
#compitition modifiers
#start loop. for each monster with 'RK' in compi, add energy to 'RK_energy'
#repeat for 'SC' and 'PA'
for mainmonster in dictofherbivore:
temp_fitness = 1 #assumes all equally fit
listofcodes=['RK','PA', 'SC','RK','PA', 'SC','RK']
for i in range (0,3):
first_key=listofcodes[i]
second_key=listofcodes[i+1]
third_key=listofcodes[i+2]
if dictofherbivore[mainmonster]['compi']!='--': # only needs to run loops if there is a skill to compare
if dictofherbivore[mainmonster]['compi']==first_key:
for tempmonster in dictofherbivore:
if dictofherbivore[tempmonster]['compi']==second_key:
temp_fitness = temp_fitness - 0.1*dictofherbivore[tempmonster]['Species_energy'] # being pushed out
elif dictofherbivore[tempmonster]['compi']==third_key:
temp_fitness = temp_fitness + 0.1*dictofherbivore[tempmonster]['Species_energy'] # mainmonster's compi beats this ones
else: temp_fitness = temp_fitness +0.01*dictofherbivore[tempmonster]['Species_energy'] #+1 against null skills
#END checking cand scoring compi
#START checking and scoring predi -herbivores eating producers
#I am using the global variable for the different producers
#BECUASE this function only changes herbivores, not producers
if dictofherbivore[mainmonster]['predi']!='--': # only needs to run loops if there is a skill to compare
if dictofherbivore[mainmonster]['predi']==first_key:
for tempmonster in dictofproducer:
if dictofproducer[tempmonster]['defnd']==second_key:
temp_fitness = temp_fitness - 0.1*dictofproducer[tempmonster]['Species_energy'] # being pushed out
elif dictofproducer[tempmonster]['defnd']==third_key:
temp_fitness = temp_fitness + 0.1*dictofproducer[tempmonster]['Species_energy'] # mainmonster's compi beats this ones
else: temp_fitness = temp_fitness +0.01*dictofproducer[tempmonster]['Species_energy'] #+1 against null skills
#END checking and scoring predi on producers
#start checking and scoring defnd against carnivores
if dictofherbivore[mainmonster]['defnd']!='--': # only needs to run loops if there is a skill to compare
if dictofherbivore[mainmonster]['defnd']==first_key:
for tempmonster in dictofcarnivore:
if dictofcarnivore[tempmonster]['predi']==second_key:
temp_fitness = temp_fitness - 0.1*dictofcarnivore[tempmonster]['Species_energy'] # being pushed out
elif dictofcarnivore[tempmonster]['predi']==third_key:
temp_fitness = temp_fitness + 0.1*dictofcarnivore[tempmonster]['Species_energy'] # mainmonster's compi beats this ones
else: temp_fitness = temp_fitness +0.01*dictofcarnivore[tempmonster]['Species_energy'] #+1 against null skills
#END checking defnd against carnivores
#START checking defbehav against carnivores
if dictofherbivore[mainmonster]['defbehav']!='--': # only needs to run loops if there is a skill to compare
if dictofherbivore[mainmonster]['defbehav']==first_key:
for tempmonster in dictofcarnivore:
if dictofcarnivore[tempmonster]['behav']==second_key:
temp_fitness = temp_fitness - 0.1*dictofcarnivore[tempmonster]['Species_energy'] # being pushed out
elif dictofcarnivore[tempmonster]['behav']==third_key:
temp_fitness = temp_fitness + 0.1*dictofcarnivore[tempmonster]['Species_energy'] # mainmonster's compi beats this ones
else: temp_fitness = temp_fitness +0.01*dictofcarnivore[tempmonster]['Species_energy'] #+1 against null skills
dictofherbivore[mainmonster]['fitness']=temp_fitness #set this monster's fitness from compi
#END looping through for 'RK' for main monster
if temp_fitness < 0: temp_fitness=0
total_fitness=total_fitness+temp_fitness
dictofherbivore[mainmonster]['fitness']=round(temp_fitness,2) #set this monster's fitness from compi
#END looping through all three for main monster. new monster!
#END checking and scoring defence against predators
if total_fitness == 0: total_fitness=1
('Herbivores Checked: total fitness', total_fitness)
for mainmonster in dictofherbivore:
share_factor=dictofherbivore[mainmonster]['fitness']/total_fitness
energy = round(total_herbivore_energy*share_factor,2)
size = dictofherbivore[mainmonster]['Size']
dictofherbivore[mainmonster]['Species_energy']= energy
dictofherbivore[mainmonster]['Number']= round(energy/(size**3),0)
if dictofherbivore[mainmonster]['Number'] <1:
dictofherbivore[mainmonster]['Species_energy']=0
print(dictofherbivore[mainmonster]['name'], 'is dying out')
##################
def evaluate_producers(dictofproducer, total_herbivore_energy, total_carnivore_energy):
#total hrebivore energy since that global one has to be altered.
#EG if there is only one producer and it's a light_eater, the uncaptured chem_energy is lost
#basic_share_of_energy=total_producer_energy/len(dictofproducer) #simple average for inital value
# have to calcualte shares based on indivisual enenergy sources.
total_fitness=0
for mainmonster in dictofproducer:
temp_fitness = 1 #assumes all equally fit
listofcodes=['RK','PA', 'SC','RK','PA', 'SC','RK']
for i in range (0,3):
first_key = listofcodes[i]
second_key = listofcodes[i+1]
third_key = listofcodes[i+2]
if dictofproducer[mainmonster]['compi']!='--': # only needs to run loops if there is a skill to compare
if dictofproducer[mainmonster]['compi']==first_key: #
for tempmonster in dictofproducer:
if dictofproducer[tempmonster]['compi']==second_key:
temp_fitness = temp_fitness - 0.1*dictofproducer[tempmonster]['Species_energy'] # being pushed out
elif dictofproducer[tempmonster]['compi']==third_key:
temp_fitness = temp_fitness + 0.1*dictofproducer[tempmonster]['Species_energy'] # mainmonster's compi beats this ones
else: temp_fitness = temp_fitness +0.01*dictofproducer[tempmonster]['Species_energy'] #+1 against null skills
#END checking cand scoring compi
#start checking and scoring defnd against herbivores eating them
if dictofproducer[mainmonster]['defnd']!='--': # only needs to run loops if there is a skill to compare
if dictofproducer[mainmonster]['defnd']==first_key:
for tempmonster in dictofherbivore:
if dictofherbivore[tempmonster]['predi']==second_key:
temp_fitness = temp_fitness - 0.1*dictofherbivore[tempmonster]['Species_energy'] # being pushed out
elif dictofherbivore[tempmonster]['predi']==third_key:
temp_fitness = temp_fitness + 0.1*dictofherbivore[tempmonster]['Species_energy'] # mainmonster's compi beats this ones
else: temp_fitness = temp_fitness +0.01*dictofherbivore[tempmonster]['Species_energy'] #+1 against null skills
dictofproducer[mainmonster]['fitness']=temp_fitness #set this monser's fitness from compi
#END looping through for 'RK' for main monster
if temp_fitness < 0: temp_fitness=0
total_fitness=total_fitness+temp_fitness
dictofproducer[mainmonster]['fitness']=round(temp_fitness,2) #set this monster's fitness from compi
#END looping through all three for main monster. new monster!
#END checking and scoring defence against predators
if total_fitness == 0: total_fitness=1
print('total fitness', total_fitness)
light_fitness = 1 # seperate fitness totals needed for each source
chem_fitness = 1 # to stop light eaters feast on chem energy ect.
magic_fitness = 1
detri_fitness = 1
for mainmonster in dictofproducer:
#share_factor=dictofproducer[mainmonster]['fitness']/total_fitness
#dictofproducer[mainmonster]['Species_energy']=total_herbivore_energy*share_factor
if dictofproducer[mainmonster]['type']=='Light_eater':
light_fitness =light_fitness+dictofproducer[mainmonster]['fitness']
elif dictofproducer[mainmonster]['type']=='Chem_eater':
chem_fitness =chem_fitness+dictofproducer[mainmonster]['fitness']
elif dictofproducer[mainmonster]['type']=='Magic_eater':
magic_fitness =magic_fitness+dictofproducer[mainmonster]['fitness']
elif dictofproducer[mainmonster]['type']=='Detri_eater':
detri_fitness =detri_fitness+dictofproducer[mainmonster]['fitness']
#tota;s found, let's share out the energy
total_producer_energy =0
for mainmonster in dictofproducer:
if dictofproducer[mainmonster]['type']=='Light_eater':
share_factor=dictofproducer[mainmonster]['fitness']/light_fitness
dictofproducer[mainmonster]['Species_energy']=round(incoming_light*share_factor,2)
elif dictofproducer[mainmonster]['type']=='Chem_eater':
share_factor=dictofproducer[mainmonster]['fitness']/chem_fitness
dictofproducer[mainmonster]['Species_energy']=round(incoming_chem*share_factor,2)
elif dictofproducer[mainmonster]['type']=='Magic_eater':
share_factor=dictofproducer[mainmonster]['fitness']/magic_fitness
dictofproducer[mainmonster]['Species_energy']=round(incoming_magic*share_factor,2)
elif dictofproducer[mainmonster]['type']=='Detri_eater':
share_factor=dictofproducer[mainmonster]['fitness']/detri_fitness
dictofproducer[mainmonster]['Species_energy']=round(incoming_detritus*share_factor,2)
energy = dictofproducer[mainmonster]['Species_energy']
size = dictofproducer[mainmonster]['Size']
dictofproducer[mainmonster]['Number'] = round(energy/(size**3),0)
if dictofproducer[mainmonster]['Number'] <1:
dictofproducer[mainmonster]['Species_energy']=0
print(dictofproducer[mainmonster]['name'], 'has died out')
total_producer_energy=total_producer_energy+dictofproducer[mainmonster]['Species_energy']
total_herbivore_energy = total_producer_energy/10
total_carnivore_energy = total_producer_energy/100
return(dictofproducer, total_herbivore_energy, total_carnivore_energy)
##############
def refill_foodweb(EntryCounters, dictofproducer, dictofherbivore, dictofcarnivore):
# can't mutate if all the beasts of that type mutally died out, so if they all gone add a new one
if len(dictofproducer)==0:
temp, EntryCounters=Create_Producer(EntryCounters,3)
tempname=temp['name']
dictofproducer[tempname]=temp #adds producer to dictionary
if len(dictofherbivore)==0:
temp, EntryCounters=Create_Herbivore(EntryCounters,3)
tempname=temp['name']
dictofherbivore[tempname]=temp #adds producer to dictionary
if len(dictofcarnivore)==0:
temp, EntryCounters=Create_Carnivore(EntryCounters,3)
tempname=temp['name']
dictofcarnivore[tempname]=temp #adds producer to dictionary
#now mutate existing beasts
for i in range(len(dictofproducer), Number_producers,1):
EntryCounters, dictofproducer=add_mutant(EntryCounters, dictofproducer)
for i in range(len(dictofherbivore), Number_herbivores,1):
EntryCounters, dictofherbivore=add_mutant(EntryCounters, dictofherbivore)
for i in range(len(dictofcarnivore), Number_carnivores,1):
EntryCounters, dictofcarnivore=add_mutant(EntryCounters, dictofcarnivore)
return(EntryCounters, dictofproducer, dictofherbivore, dictofcarnivore)
#####################################
#####################################
#### darwinian functions #####
############VV########VV#############
############VV########VV#############
def prune_dictionary(dictoflivingthings):
# this removes any species who's energy has fallen to zero - ie gone extinct
dictoflivingthings2 = dictoflivingthings
for thing in dictoflivingthings2:
if dictoflivingthings2[thing]['Species_energy'] == 0:
dictoflivingthings=removekey(dictoflivingthings,thing)
return(dictoflivingthings)
def cull_weakest(dictoflivingthings):
#this removes any species with the least energy in that dict.
dolt2 =dictoflivingthings #shortened to dolt2 here to save typing
weakest={'Species_energy':10000000} #initalise with an arbitary weakest monster
dolt2['weakest']=weakest
for thing in dolt2:
if dolt2[thing]['Species_energy']<dolt2['weakest']['Species_energy']:
dolt2['weakest'] = dolt2[thing]
dictoflivingthings = removekey(dictoflivingthings, dolt2['weakest']['name'])
dictoflivingthings = removekey(dictoflivingthings, 'weakest')
return(dictoflivingthings)
#implement a naive bubble sort to find creature with lowest species energy and send to remove_key
def removekey(d, key):
#the fucntion that actually kills species and removes from dictionary. from Stackexchange
print(key, ' is now extinct')
r = dict(d)
del r[key]
return r
def add_mutant(EntryCounters, dictoflivingthings):
parentname=random.choice(list(dictoflivingthings.keys()))
print ("about to mutate", parentname)
child, EntryCounters = mutate_monster(EntryCounters, parentname)
childname=child['name']
dictoflivingthings[childname]=child #adds child to correct dictionary
return(EntryCounters, dictoflivingthings)
# Create a new subspecies
def mutate_monster(EntryCounters, parentname): #parent name is numerical
if 10000 <parentname<49999:
dictofthings=dictofproducer
child, EntryCounters =Create_Producer(EntryCounters, 1)
mutation_trait_limit =4
elif 50000 <parentname <59999:
dictofthings=dictofherbivore
child, EntryCounters =Create_Herbivore(EntryCounters,1)
mutation_trait_limit= 6
else:
dictofthings=dictofcarnivore
child, EntryCounters =Create_Carnivore(EntryCounters,1)
mutation_trait_limit=9
#create space for child in index
parent = dictofthings[parentname]
#change child to match parent, unless that is the mutation
# get two mutations, including reasonalbe chance of 1 actual change and one changed to something identical eg Rk to RK, 2 mutations on the same item (same affect as one) or 2 mutatations that cancel each other out (size +1, size -1)
# so in the case either of the mutation indexys is equal to that possible mutation, that stat is equal to the randomly created monster, not the parent
mutationindex = random.randint(1,mutation_trait_limit)
mutationindey = random.randint(1,mutation_trait_limit)
child['Size']=parent['Size']
child['Prey_size'] = parent['Prey_size']
if mutationindex==1 or mutationindey==1:
child['Size'] = child['Size'] + random.randint(0,1)*2-1 # gives -1 or +1
elif mutationindex==2 or mutationindey==2:
child['Size'] = child['Size'] + random.randint(0,1)*2-1 # gives -1 or +1
elif mutationindex!=3 and mutationindey!=3:
child['compi'] = parent['compi']
elif mutationindex!=4 and mutationindey!=4:
child['defnd'] = parent['defnd']
elif mutationindex!=5 and mutationindey!=5:
child['predi'] = parent['predi']
elif mutationindex!=6 and mutationindey!=6:
child['defbehav'] = parent['defbehav']
elif mutationindex==7 or mutationindey==7:
child['behav'] = parent['behav']
else: # for mutationindex = 8 or 9
child['Prey_size'] =child['Prey_size'] + random.randint(0,1)*2-1 # gives -1 or +1
child['Number'] = 1 #set small child starter population
child['Species_energy'] = parent['Species_energy']/parent['Number']
if int(child['Size']) <1:
child['Size']=1
if int(child['Size']) >9:
child['Size']=9
if mutation_trait_limit ==9: # becuase otherwise Prey_size = ' '
if int(child['Prey_size']) <1:
child['Prey_size']=1
if int(child['Prey_size']) >9:
child['Prey_size']=9
#print(mutationindex, mutationindey)
#print(parent)
return(child,EntryCounters)
#######################
#######################
# Output Functions #
#######VVVVVVVVVV######
##########VVVV#########
def print_roughly(livingthingname): # used for testing at command line more than anything
if 10000 <livingthingname<49999:
dictofthings=dictofproducer
elif 50000 <livingthingname <59999:
dictofthings=dictofherbivore
else: dictofthings=dictofcarnivore
livingthing=dictofthings[livingthingname]
print(livingthing['name'],' ',livingthing['Number'], livingthing['Size'], livingthing ['Prey_size'],
livingthing['compi'],livingthing['defnd'],livingthing['predi'], livingthing['behav'], livingthing['defbehav'],
livingthing['fitness'], livingthing['Species_energy'])
def print_all():
print()
print(" ~~~ ")
print()
for producer in dictofproducer:
print_nicely(producer)
print()
for beast in dictofherbivore:
print_nicely(beast)
print()
for beast in dictofcarnivore:
print_nicely(beast)
print()
print(" ~~~ ")
print()
def print_nicely(livingthingname):
if 10000 <livingthingname<49999:
dictofthings=dictofproducer
elif 50000 <livingthingname <59999:
dictofthings=dictofherbivore
else: dictofthings=dictofcarnivore
livingthing=dictofthings[livingthingname]
#name it
f=random.randint(0,len(forenamelist)-1)
s=random.randint(0,len(forenamelist)-1)
commonname=forenamelist[f]+surnamelist[s]
#Pull out info that needs refining from that creatures entry
Broad_type=livingthing['type']
Broad_compi=livingthing['compi']
Broad_predi=livingthing['predi']
Broad_defnd=livingthing['defnd']
Broad_defbehav = livingthing['defbehav']
Broad_behav = livingthing['behav']
Broad_size =livingthing['Size']
Broad_preysize = livingthing['Prey_size']
Number = int(livingthing['Number'])
#now, lets look up what the SC, RK PA codes mean for each case
# First line of output
# The Creature is noted for X, Y ,Z unsual features
if Number >1: # dosen't bother printing if the popualtion of that creature is zero
if(Broad_compi=='--'):
Det_compi=''
else:
smalldictkey=Broad_type+'_compi'
smalldict=dictofdicts[smalldictkey]
Det_compi=smalldict[Broad_compi]
if(Broad_predi=='--'):
Det_predi=''
else:
smalldictkey=Broad_type+'_predi'
smalldict=dictofdicts[smalldictkey]
Det_predi=smalldict[Broad_predi]
if(Broad_defnd=='--'):
Det_defnd=''
else:
smalldictkey=Broad_type+'_defnd'
smalldict=dictofdicts[smalldictkey]
Det_defnd=smalldict[Broad_defnd]
if(Det_compi+Det_predi+Det_defnd==''):
final_word='nothing' # produces the 'noted for nothing' text
else: final_word=''
print('The {0} {1} ({2}) is noted for {3}, {4}, {5}, {6}.'.format(Broad_type, commonname, livingthing['name'],Det_compi,Det_predi,Det_defnd,final_word))
##### second line of output
#there are this many in the area.
if(Broad_type !='Meati_eater'):
if(Broad_type == 'Herbi_eater'):
tempkey = 'beast_'+str(Broad_size)
det_size = dictofsize[tempkey]
print(' There are ', Number, 'of these ', det_size, ' sized browsers in the area')
else:
tempkey = 'producer_'+str(Broad_size)
det_size = dictofsize[tempkey]
print('There are ', Number, 'of these', det_size, ' sized growths in the area')
else:
tempkey1= 'beast_'+str(Broad_size)
tempkey2='beast_'+str(Broad_preysize)
det_size = dictofsize[tempkey1]
det_preysize = dictofsize[tempkey2]
print('It is ', det_size, 'sized with a preffered prey of ', det_preysize, 'size')
if int(Broad_size)<int(Broad_preysize):
packsize = evaluate_pack_size(Broad_size, Broad_preysize)
print('It rarely found in packs of less than ', packsize, 'individuals. There are ', int(Number), 'of these packs in the area.')
else: print('There are ', Number, ' of these typically lone predators in the area')
#### third optional line of output
if(Broad_defbehav in {'RK','PA','SC'}):
smalldictkey=Broad_type+'_defbehav'
smalldict=dictofdicts[smalldictkey]
Det_defbehav=smalldict[Broad_defbehav]
print('when threatened it will ', Det_defbehav)
#### fouth optional line of output
if(Broad_behav in {'RK','PA','SC'}):
smalldictkey=Broad_type+'_behav'
smalldict=dictofdicts[smalldictkey]
Det_behav=smalldict[Broad_behav]
print('when hunting it is ', Det_behav)
# print('it has a current fitness score of {0} leading to total species energy of {1}.'.format(livingthing['fitness'],livingthing['Species_energy']))
#print('It eats mostly {0}.'.format(livingthing[first_choice_food]))
######
## DND outputs
## Dangerous: anything with human size prey, anything that will defend aggresively, parasitic life stage, digestive ooze
## Feature: anything big, anything with loud wanring calls,
## Useful: anything with horns, tusks spikes, brightly poisonous,
###############################
# Input functions #
def startup_data():
Nprod = input('Please enter the target number of producers (default 5)')
Nprod = startup_data_cleanse(Nprod,5)
Nherb = input('Please enter the target number of herbivores (default 5)')
Nherb = startup_data_cleanse(Nherb,5)
Ncarn = input('Please enter the target number of carnivores (default 2)')
Ncarn = startup_data_cleanse(Ncarn,2)
print("now enter the energy entering the ecosystem. This effects type of producer only")
print("You will get about 6 fox size predators per 10k energy entering the system")
print("Example - Enchanted Forest: Light =50000, Magic = 30000, Chem=0, Detri=1000")
print("Example - Vents and Caves: Light=0, Magic =0, Chem =10000, Detri=10000")
print("Default gives 3500 to all four")
Nlight = input('light =')
Nlight = startup_data_cleanse(Nlight,0)
Nmagic = input('magical energy =')
Nmagic = startup_data_cleanse(Nmagic,0)
Nchem = input('chemical energy =')
Nchem = startup_data_cleanse(Nchem,0)
Ndetri = input('detritus energy =')
Ndetri = startup_data_cleanse(Ndetri,0)
if (Nlight+Nmagic+Nchem+Ndetri == 0):
Nlight = Nmagic = Nchem = Ndetri = 35000
return(Nlight,Nmagic,Nchem,Ndetri,
Nprod,Nherb, Ncarn)
def startup_data_cleanse(rawinput, defaultval):
try:
rawinput = int(rawinput)
if rawinput==0: rawinput = defaultval
except ValueError:
rawinput = defaultval
return(rawinput)
###########################################################
# Start main body
# the first digit of the three code is a 'type' marker
skill_freq = 5 # 1= certain, 5 means most have 1-2
#starter info. an inline input
incoming_light, incoming_chem, incoming_magic, incoming_detritus, Number_producers, Number_herbivores, Number_carnivores = startup_data()
start_time = time.time() #real time measure. other programs on computer will affect
#inital best guess. Reliable when lots of producers
#will be corrected later, once you know how much energy is caputred
total_producer_energy = incoming_light+incoming_magic+incoming_chem+incoming_detritus
total_herbivore_energy = total_producer_energy/10
total_carnivore_energy = total_producer_energy/100
dictofproducer = {} #initialise dictionary
dictofherbivore = {} #initialise dictionary
dictofcarnivore = {} #initialise dictionary
t1 = EntryCounters['Light_counter']-10000
t2 = EntryCounters['Chem_counter']-20000
t3 = EntryCounters['Magic_counter']-30000
t4 = EntryCounters['Detri_counter']-40000
# sum total t1+t2+t3+t4 gives current number of producers
CurrentNumberProducers=t1+t2+t3+t4
CurrentNumberHerbivores = EntryCounters['Herbivore_counter']-50000
CurrentNumberCarnivores = EntryCounters['Carnivore_counter']-60000
#Create a producer, randomly proportional to amount of energy available.
for i in range(CurrentNumberProducers, Number_producers,1):
temp, EntryCounters=Create_Producer(EntryCounters,5)
tempname=temp['name']
dictofproducer[tempname]=temp #adds producer to dictionary
#print(dictofproducer)
for i in range(CurrentNumberHerbivores, Number_herbivores,1):
temp, EntryCounters=Create_Herbivore(EntryCounters,5)
tempname=temp['name']
dictofherbivore[tempname]=temp #adds producer to dictionary
for i in range(CurrentNumberCarnivores, Number_carnivores,1):
temp, EntryCounters=Create_Carnivore(EntryCounters,5)
tempname=temp['name']
dictofcarnivore[tempname]=temp #adds producer to dictionary
dictofproducer=prune_dictionary(dictofproducer)
dictofherbivore=prune_dictionary(dictofherbivore)
dictofcarnivore=prune_dictionary(dictofcarnivore)
i=1
k=0
while i >0:
#prunes the zero's out. evalutes until stable, cull's weakest. refills's foodweb. evals, prunes and repeats
EntryCounters, dictofproducer, dictofherbivore, dictofcarnivore = refill_foodweb(EntryCounters, dictofproducer, dictofherbivore, dictofcarnivore)
for x in range(0,3):
dictofproducer, total_herbivore_energy, total_carnivore_energy = evaluate_producers(dictofproducer, total_herbivore_energy, total_carnivore_energy)
evaluate_herbivores(dictofherbivore)
evaluate_carnivores(dictofcarnivore)
dictofproducer=prune_dictionary(dictofproducer)
dictofherbivore=prune_dictionary(dictofherbivore)
dictofcarnivore=prune_dictionary(dictofcarnivore)
if len(dictofproducer)>1 and len(dictofproducer)> Number_producers-3:
dictofproducer = cull_weakest(dictofproducer)
if len(dictofherbivore)>1 and len(dictofherbivore)> Number_herbivores-3:
dictofherbivore = cull_weakest(dictofherbivore)
if len(dictofcarnivore)>1 and len(dictofcarnivore)> Number_carnivores-3:
dictofcarnivore = cull_weakest(dictofcarnivore)
EntryCounters, dictofproducer, dictofherbivore, dictofcarnivore = refill_foodweb(EntryCounters, dictofproducer, dictofherbivore, dictofcarnivore)
dictofproducer, total_herbivore_energy, total_carnivore_energy = evaluate_producers(dictofproducer, total_herbivore_energy, total_carnivore_energy)
evaluate_herbivores(dictofherbivore)
evaluate_carnivores(dictofcarnivore)
dictofproducer=prune_dictionary(dictofproducer)
dictofherbivore=prune_dictionary(dictofherbivore)
dictofcarnivore=prune_dictionary(dictofcarnivore)
i=Number_producers-len(dictofproducer) + Number_herbivores-len(dictofherbivore)+Number_carnivores-len(dictofcarnivore)
k=k+1
if k > 250: i=-10 #breaks loop
for x in range(0,3):
#final evaluation set to produce good foodweb.
dictofproducer, total_herbivore_energy, total_carnivore_energy = evaluate_producers(dictofproducer, total_herbivore_energy, total_carnivore_energy)
evaluate_herbivores(dictofherbivore)
evaluate_carnivores(dictofcarnivore)
print()
print("foodweb stabilised in ", k+2, "iterations")
print("--- %s seconds ---" % (time.time()-start_time))
print_all()