This repository has been archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlexIsmodesThreeAgentPredationModel.nlogo
1543 lines (1358 loc) · 37.9 KB
/
AlexIsmodesThreeAgentPredationModel.nlogo
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
globals
[
max-humans ;
max-wolves
max-rabbits
rabbit-sensing-radius
wolf-sensing-radius
human-sensing-radius
]
; Humans, wolves, and rabbits are the three different breeds of turtles
breed [ humans human ]
breed [ wolves wolf ]
breed [ rabbits rabbit ]
turtles-own [ energy gender age birth-tick lifetime hmyclosest hmynext wmyclosest wmynext rmyclosest rmynext]
patches-own [ countdown ]
to setup
clear-all
; Check model-version switch
; if we're not modeling grass, then the sheep don't need to eat to survive
; otherwise the grass's state of growth and growing logic need to be set up
ifelse model-version = "sheep-wolves-grass" [
ask patches [
set pcolor one-of [ green brown ]
ifelse pcolor = green
[ set countdown grass-regrowth-time ]
[ set countdown random grass-regrowth-time ] ; initialize grass regrowth clocks randomly for brown patches
]
]
[
ask patches [ set pcolor green ]
]
create-rabbits initial-number-rabbits ; create the rabbits, then initialize their variables
[
set rmyclosest 0
set shape "rabbit"
set color white
set size 1.5
set label-color blue - 2
set lifetime rabbit-lifetime
set age 0
set rabbit-sensing-radius rabbit-mate-distance
set energy random (2 * rabbit-gain-from-food)
setxy random-xcor random-ycor
ifelse random 2 = 1
[ set gender "male" ]
[ set gender "female" ]
]
create-wolves initial-number-wolves ; create the wolves, then initialize their variables
[
set wmyclosest 0
set shape "wolf"
set color 3.5
set size 2
set lifetime 15
set age 0
set wolf-sensing-radius wolf-mate-distance
set energy random (2 * wolf-gain-from-food)
setxy random-xcor random-ycor
ifelse random 2 = 1
[ set gender "male" ]
[ set gender "female" ]
]
create-humans initial-number-humans ; create the humans, then initialize their variables
[
set hmyclosest 0
set shape "person"
set color black
set lifetime 70
set age 0
set human-sensing-radius human-mate-distance
set size 2.5 ; easier to see
set energy random (2 * human-gain-from-food)
setxy random-xcor random-ycor
ifelse random 2 = 1
[ set gender "male" ]
[ set gender "female" ]
]
display-labels
reset-ticks
end
to r-choose-neighbors
;if not any? rabbits and count humans > max-humans
if count rabbits >= 2
[
ask rabbits
[
set rmynext min-one-of other rabbits [distance myself] ;; choose my nearest neighbor based on distance
set rmyclosest distance rmynext
]
]
end
to w-choose-neighbors
;if not any? rabbits and count humans > max-humans
if count wolves >= 2
[
ask wolves
[
set wmynext min-one-of other wolves [distance myself] ;; choose my nearest neighbor based on distance
set wmyclosest distance wmynext
]
]
end
to h-choose-neighbors
;if not any? rabbits and count humans > max-humans
if count humans >= 2
[
ask humans
[
set hmynext min-one-of other humans [distance myself] ;; choose my nearest neighbor based on distance
set hmyclosest distance hmynext
]
]
end
to aging
set age age + 1
end
;testing!
to go
; stop the simulation of no humans, wolves or rabbits
if not any? turtles [ stop ]
; stop the model if there are no humans or no wolves or no rabbits and stop if the number of humans or wolves or rabbits gets too large
;if not any? rabbits and if not any? wolves
;[ user-message "The humans have inherited the earth" stop ]
if count rabbits = 0 and count wolves = 0 [ user-message "The humans have inherited the earth" stop ]
if count rabbits = 0 and count humans = 0 [ user-message "The wolves have inherited the earth" stop ]
if count wolves = 0 and count humans = 0 [ user-message "The rabbits have inherited the earth" stop ]
;if not any? rabbits and if not any? humans
;[ user-message "The wolves have inherited the earth" stop ]
;if not any? wolves and if not any? humans
;[ user-message "The rabbits have inherited the earth" stop ]
;if not any? wolves and count rabbits > max-rabbits [ user-message "The rabbits have inherited the earth" stop ]
r-choose-neighbors
w-choose-neighbors
h-choose-neighbors
ask rabbits [
move-rabbit
if model-version = "rabbits-wolves-humans" [
set energy energy - 1 ; deduct energy for rabbits only if running rabbits-wolves-humans model version
eat-grass ; rabbits eat grass only if running rabbits-wolves-humans model version
death ; rabbits die if out of energy
]
reproduce-rabbits ; sheep reproduce at random rate governed by slider
]
ask wolves [
move
set energy energy - 1 ; wolves lose energy as they move
w-eat-rabbits ; wolves eat a rabbit on their patch
death ; wolves die if out of energy
reproduce-wolves ; wolves reproduce at random rate governed by slider
]
ask humans [
move
set energy energy - 1 ; humans lose energy as they move
repeat 5 [ shoot-eat-rabbits ]
shoot-wolves ; wolves eat a sheep on their patch
death ; humans die if out of energy
reproduce-humans ; wolves reproduce at random rate governed by slider
]
if model-version = "sheep-wolves-grass" [ ask patches [ grow-grass ] ]
; set grass count patches with [pcolor = green]
display-labels
ask turtles [aging]
ask patches [ grow-grass ]
tick
end
to move ; turtle procedure
rt random 50
lt random 50
fd 1
end
to move-rabbit
let target one-of patches with [ pcolor = green ]
if is-patch? target [ move-to target ]
end
to eat-grass ; sheep procedure
; sheep eat grass, turn the patch brown
if pcolor = green [
set pcolor brown
set energy energy + rabbit-gain-from-food ; sheep gain energy by eating
]
end
to-report potential-mate-rabbit
; this is a reporter that gets called in the
; context of a rabbit looking for potential mates
let prospects other rabbits in-radius rabbit-sensing-radius ; rabbit-sensing-radius
; - if potential mate already has another mate
; - how recently it has reproduced ...
; - think about sex: have a turtles-own variable for sex that indicates
; male vs. female so mates must be opposite
; narrow down prospects to only include those of the opposite gender to the current rabbit (myself).
set prospects prospects with [ gender != [gender] of myself ] ; select only prospects with the opposite gender
; select for other attributes, such as not having a mate and whether it's reproduced recently ...
; once you've narrowed this down, we can report one of the remaining prospects:
report one-of prospects ; if there are no prospects, this will report nobody
end
to-report potential-mate-wolf
; this is a reporter that gets called in the
; context of a wolf looking for potential mates
let prospects other wolves in-radius wolf-sensing-radius ; wolf-sensing-radius
; - if potential mate already has another mate
; - how recently it has reproduced ...
; - think about sex: have a turtles-own variable for sex that indicates
; male vs. female so mates must be opposite
; narrow down prospects to only include those of the opposite gender to the current wolf (myself).
set prospects prospects with [ gender != [gender] of myself ] ; select only prospects with the opposite gender
; select for other attributes, such as not having a mate and whether it's reproduced recently ...
; once you've narrowed this down, we can report one of the remaining prospects:
report one-of prospects ; if there are no prospects, this will report nobody
end
to-report potential-mate-human
; this is a reporter that gets called in the
; context of a human looking for potential mates
let prospects other humans in-radius human-sensing-radius ; human-sensing-radius
; - if potential mate already has another mate
; - how recently it has reproduced ...
; - think about sex: have a turtles-own variable for sex that indicates
; male vs. female so mates must be opposite
; narrow down prospects to only include those of the opposite gender to the current wolf (myself).
set prospects prospects with [ gender != [gender] of myself ] ; select only prospects with the opposite gender
; select for other attributes, such as not having a mate and whether it's reproduced recently ...
; once you've narrowed this down, we can report one of the remaining prospects:
report one-of prospects ; if there are no prospects, this will report nobody
end
to reproduce-rabbits ; rabbit procedure
let mate potential-mate-rabbit
if mate != nobody and random-float 100 < rabbit-reproduce
[
move-to [patch-here] of mate
set energy energy * 0.75
ask mate [ set energy energy * 0.75 ]
hatch 1 [
set energy 0.25 * ([ energy ] of myself + [energy] of mate)
set age 0
ifelse random 2 = 1
[ set gender "male"]
[ set gender "female" ]
rt random-float 360
fd 1
]
]
end
to reproduce-wolves ; wolf procedure
let mate potential-mate-wolf
if mate != nobody and random-float 100 < wolf-reproduce
[
move-to [patch-here] of mate
set energy energy * 0.75
ask mate [ set energy energy * 0.75 ]
hatch 1 [
set age 0
set energy 0.25 * ([ energy ] of myself + [energy] of mate)
ifelse random 2 = 1
[ set gender "male"]
[ set gender "female" ]
rt random-float 360
fd 1
]
]
end
to reproduce-humans ; human procedure
let mate potential-mate-human
if mate != nobody and random-float 100 < human-reproduce
[
move-to [patch-here] of mate
set energy energy * 0.75
ask mate [ set energy energy * 0.75 ]
hatch 1 [
set age 0
set energy 0.25 * ([ energy ] of myself + [energy] of mate)
ifelse random 2 = 1
[ set gender "male"]
[ set gender "female" ]
rt random-float 360
fd 1
]
]
end
to w-eat-rabbits ; WOLF KILL/EAT RABBIT
let preyisrabbit one-of rabbits in-radius wolf-hunt-distance
if preyisrabbit != nobody [ ; did we get one? if so,
ask preyisrabbit [ die ] ; kill it, and...
set energy energy + wolf-gain-from-food ; get energy from eating
]
end
to w-eat-humans ; WOLF KILL/EAT HUMAN
let preyishuman one-of humans-here
if preyishuman != nobody [ ; did we get one? if so,
ask preyishuman [ die ] ; kill it, and...
set energy energy + wolf-gain-from-food ; get energy from eating
]
end
to-report potential-shot-rabbit ; HUMAN IDENTIFY POTENTIAL SHOT RABBIT
; this is a reporter that gets called in the
; context of a human shooting a nearby wolf
let prospects rabbits in-radius 10 ;human-sensing-radius
; this is just a place-holder and you should replace it with yur
; code for finding a potential mate
; You might consider whether the potential mate already has another
; mate or how recently it has reproduced ...
; also think about sex: have a turtles-own variable for sex that indicates
; male vs. female so mates must be opposite
report one-of prospects
end
to shoot-eat-rabbits ; HUMAN KILL/EAT RABBIT
let preyisrabbit potential-shot-rabbit
if preyisrabbit != nobody and shooting-accuracy > random-float 100 [
ask preyisrabbit [ die ] ; kill it, and...
set energy energy + human-gain-from-food ; get energy from eating
]
end
to-report potential-shot-wolf ; HUMAN IDENTIFY POTENTIAL SHOT WOLF
; this is a reporter that gets called in the
; context of a human shooting a nearby wolf
let prospects other wolves in-radius 5 ;human-sensing-radius
; this is just a place-holder and you should replace it with yur
; code for finding a potential mate
; You might consider whether the potential mate already has another
; mate or how recently it has reproduced ...
; also think about sex: have a turtles-own variable for sex that indicates
; male vs. female so mates must be opposite
report one-of prospects
end
to shoot-wolves ; HUMAN KILL WOLF
let preyiswolf potential-shot-wolf
if preyiswolf != nobody and shooting-accuracy > random-float 100 [
ask preyiswolf [die]
]
end
to death ; turtle procedure (i.e. both wolf nd sheep procedure)
; when energy dips below zero, die
if energy < 0 [ die ]
if random-float 1.0 < (0.75 * (age / lifetime) ^ 4)
[ die ]
end
to grow-grass ; patch procedure
; countdown on brown patches: if reach 0, grow some grass
if pcolor = brown [
ifelse countdown <= 0
[ set pcolor green
set countdown grass-regrowth-time ]
[ set countdown countdown - 1 ]
]
end
to-report grass
ifelse model-version = "rabbits-wolves-humans" [
report patches with [pcolor = green]
]
[ report 0 ]
end
to display-labels
ask turtles [ set label "" ]
if show-energy? [
ask wolves [ set label round energy ]
if model-version = "rabbits-wolves-humans" [ ask rabbits [ set label round energy ] ]
]
end
to-report survivors
ifelse any? humans
[
report "humans"
]
[
ifelse any? wolves
[ report "wolves" ]
[ report "rabbits" ]
]
end
; Modified Uri Wilensky's Wolf-Sheep Predation Model (1997)
; Modification called "Three-Agent Predation Model" by Alex Ismodes (2019)
@#$#@#$#@
GRAPHICS-WINDOW
685
10
1193
519
-1
-1
9.804
1
14
1
1
1
0
1
1
1
-25
25
-25
25
1
1
1
ticks
30.0
SLIDER
515
40
680
73
initial-number-humans
initial-number-humans
0
250
15.0
1
1
NIL
HORIZONTAL
SLIDER
175
80
330
113
rabbit-gain-from-food
rabbit-gain-from-food
0.0
50.0
17.0
1.0
1
NIL
HORIZONTAL
SLIDER
175
120
330
153
rabbit-reproduce
rabbit-reproduce
0
100
25.0
1.0
1
%
HORIZONTAL
SLIDER
340
40
505
73
initial-number-wolves
initial-number-wolves
0
250
25.0
1
1
NIL
HORIZONTAL
SLIDER
340
80
505
113
wolf-gain-from-food
wolf-gain-from-food
0.0
100.0
33.0
1.0
1
NIL
HORIZONTAL
SLIDER
340
120
505
153
wolf-reproduce
wolf-reproduce
0.0
100
11.0
1.0
1
%
HORIZONTAL
SLIDER
180
260
330
293
grass-regrowth-time
grass-regrowth-time
0
100
4.0
1
1
NIL
HORIZONTAL
BUTTON
5
170
85
203
setup
setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
85
170
170
203
go
go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
0
PLOT
5
525
320
695
Populations
time
pop.
0.0
100.0
0.0
100.0
true
true
"" ""
PENS
"rabbits / 100" 1.0 0 -1264960 true "" "plot count rabbits / 400"
"wolves" 1.0 0 -13791810 true "" "plot count wolves"
"grass / 4" 1.0 0 -10899396 true "" "if model-version = \"sheep-wolves-grass\" [ plot count grass / 4 ]"
"humans" 1.0 0 -2674135 true "" "plot count humans"
MONITOR
5
475
80
520
rabbits
count rabbits
3
1
11
MONITOR
85
475
160
520
wolves
count wolves
3
1
11
MONITOR
245
475
320
520
grass
count grass / 4
0
1
11
TEXTBOX
200
15
305
33
Rabbit settings
14
0.0
0
TEXTBOX
375
15
465
33
Wolf settings
14
0.0
0
SWITCH
5
250
170
283
show-energy?
show-energy?
1
1
-1000
CHOOSER
5
40
170
85
model-version
model-version
"sheep-wolves" "rabbits-wolves-humans"
1
SLIDER
175
40
330
73
initial-number-rabbits
initial-number-rabbits
0
250
73.0
1
1
NIL
HORIZONTAL
SLIDER
515
80
680
113
human-gain-from-food
human-gain-from-food
0
100
13.0
1
1
NIL
HORIZONTAL
SLIDER
515
120
680
153
human-reproduce
human-reproduce
0
100
7.0
1
1
%
HORIZONTAL
MONITOR
165
475
240
520
humans
count humans
3
1
11
TEXTBOX
540
15
650
33
Human settings
14
0.0
1
BUTTON
85
210
170
243
step
go
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
SLIDER
515
240
680
273
shooting-accuracy
shooting-accuracy
0
100
6.0
1
1
%
HORIZONTAL
PLOT
325
525
640
695
Avg Age
time
mean age
0.0
100.0
0.0
1.0
true
true
"" ""
PENS
"turtles" 1.0 0 -16777216 true "" "plot mean [ age ] of turtles"
"humans" 1.0 0 -2674135 true "" "plot mean [age] of humans"
"wolves" 1.0 0 -13791810 true "" "plot mean [age] of wolves"
"rabbits" 1.0 0 -1264960 true "" "plot mean [age] of rabbits"
PLOT
645
525
960
695
Avg Energy
tick
mean energy
0.0
10.0
0.0
10.0
true
true
"" ""
PENS
"rabbits" 1.0 0 -1264960 true "" "plot mean [ energy ] of rabbits"
"wolves" 1.0 0 -13791810 true "" "plot mean [ energy ] of wolves"
"humans" 1.0 0 -2674135 true "" "plot mean [ energy ] of humans"
"turtles" 1.0 0 -16777216 true "" "plot mean [ energy ] of turtles"
SLIDER
175
160
330
193
rabbit-lifetime
rabbit-lifetime
0
10
8.0
1
1
ticks
HORIZONTAL
SLIDER
340
160
505
193
wolf-lifetime
wolf-lifetime
0
30
10.0
1
1
ticks
HORIZONTAL
SLIDER
175
200
330
233
rabbit-mate-distance
rabbit-mate-distance
0
10
5.0
1
1
NIL
HORIZONTAL
SLIDER
340
200
505
233
wolf-mate-distance
wolf-mate-distance
0
10
10.0
1
1
NIL
HORIZONTAL
SLIDER
340
240
505
273
wolf-hunt-distance
wolf-hunt-distance
0
10
2.0
0.5
1
NIL
HORIZONTAL
BUTTON
5
90
170
123
Save the values
export-world user-new-file
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
5
130
170
163
Load values
import-world user-file
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
TEXTBOX
200
240
315
271
Grass settings\n
14
0.0
1
SLIDER
515
160
680
193
human-lifetime
human-lifetime
0
70
50.0
1
1
ticks
HORIZONTAL
SLIDER
515
200
680
233
human-mate-distance
human-mate-distance
0
15
2.0
1
1
NIL
HORIZONTAL
PLOT
5