-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.nlogo
1955 lines (1611 loc) · 51.1 KB
/
Model.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
breed [males male]
breed [females female]
males-own
[
; When dies a specific male? Value in years.
ageOfDeathYears
; Age in years, e.g. for plotting and statistics.
ageYears
; Age in days. Makes things easier due to the fact that one tick is a day.
ageDays
; Is a specific male able to breed?
isFertile
]
females-own
[
; When dies a specific female? Value in years.
ageOfDeathYears
; Age in years, e.g. for plotting and statistics.
ageYears
; Age in days. Makes things easier due to the fact that one tick is a day.
ageDays
; Is a specific female able to breed?
isFertile
; How many children can a specific female become?
maxChildren
; How many children had this female?
previousChildren
; When does the menopause begins?
startMenopauseYears
; Is an individual female pregnant?
isPregnant
; How long is a female pregnant?
pregnantSinceDays
]
globals
[
; The chip's capacity.
capacity
; How many females are on board? This number gets updates on each tick.
numberFemales
; How many females alive are infertile? Gets updated each tick.
numberFemalesInfertile
; How many males are on board? This number gets updated on each tick.
numberMales
; How many males alive are infertile? Gets updated each tick.
numberMalesInfertile
; How many accidents happens in the mission?
numberAccidents
; How many crew members died by accidents?
numberDeathsByAccidents
; How many crew members are died?
numberDeaths
; How many births?
numberBirths
; How many females are born?
numberBirthsFemales
; How many males are born?
numberBirthsMales
; The current year.
currentYear
; The crew's mean age.
meanAgeYears
; The mean age of females.
meanAgeFemalesYears
; The mean age of males.
meanAgeMalesYears
]
to setup
clear-all
; Configure the plots' pens to behave on a daily or monthly basis:
ifelse simulateMonthsInsteadOfDays
[
set-current-plot "Population over time (6300 years)"
set-current-plot-pen "females"
set-plot-pen-interval 30.42
set-current-plot-pen "males"
set-plot-pen-interval 30.42
set-current-plot-pen "total"
set-plot-pen-interval 30.42
set-current-plot "Population over time (last 365 days)"
set-current-plot-pen "females"
set-plot-pen-interval 30.42
set-current-plot-pen "males"
set-plot-pen-interval 30.42
set-current-plot-pen "total"
set-plot-pen-interval 30.42
]
[
set-current-plot "Population over time (6300 years)"
set-current-plot-pen "females"
set-plot-pen-interval 1
set-current-plot-pen "males"
set-plot-pen-interval 1
set-current-plot-pen "total"
set-plot-pen-interval 1
set-current-plot "Population over time (last 365 days)"
set-current-plot-pen "females"
set-plot-pen-interval 1
set-current-plot-pen "males"
set-plot-pen-interval 1
set-current-plot-pen "total"
set-plot-pen-interval 1
]
; The ship's capacity:
set capacity 500
; Load the ships's environment:
import-pcolors "Spaceship.png"
; Half the crew consists of males:
create-males initialCrewSize / 2
[
; Set the shape and color:
set shape "person"
set color blue ; males are blue
; Determine the date of death for each individual.
; The time of death is a normal distribution:
set ageOfDeathYears random-normal maxAgeMales ageStdDeviation
; Determine the initial age at time of mission's start:
set ageYears random-normal initialAgeMales initialAgeStdDeviation
; Calculate the corresponding age as days:
set ageDays ageYears * 365
; Initially, set all individuals to be fertile. We fix this in a moment.
set isFertile true
]
; Half the crew consists of females:
create-females initialCrewSize / 2
[
; Set the shape and color:
set shape "person"
set color red ; females are red
; Determine the date of death for each individual.
; The time of death is a normal distribution:
set ageOfDeathYears random-normal maxAgeFemals ageStdDeviation
; Determine the initial age at time of mission's start:
set ageYears random-normal initialAgeFemales initialAgeStdDeviation
; Calculate the corresponding age as days:
set ageDays ageYears * 365
; Initially, set all individuals to be fertile. We fix this in a moment.
set isFertile true
; Determine how many children a specific female can have.
; When the specific female is infertile, use 0. Otherwise, it is a normal distribution:
set maxChildren random-normal maxChildrenPerFemale maxChildrenStdDeviation
; Determine when the menopause begins:
set startMenopauseYears random-normal meanAgeMenopause ageMenopauseStdDeviation
; No female is pregnant at the time of mission's start:
set isPregnant false
set pregnantSinceDays 0
]
; Now, some initial crew members might got a negative age:
ask turtles with [ ageYears < 0 ]
[
set ageDays random 365
set ageYears ageDays / 365
]
; Distribute the crew across the spaceship:
ask turtles [ setxy random-xcor random-ycor ]
; Ensure the crew is not in space:
moveAwayFromSpace
; Count the number of females and males on board.
; This is necessary to this point in time, to
; calculate the fertile state of the crew:
countSex
; Get the initial crew's mean age:
determineMeanAges
; Now, we are fixing the crew's fertile state:
ask n-of (numberMales * (infertilityMales / 100)) males [ set isFertile false ]
ask n-of (numberFemales * (infertilityFemales / 100)) females [ set isFertile false ]
; Next, we can fix the number of possible children accordingly:
ask females [ set maxChildren ifelse-value isFertile [ maxChildren ] [ 0 ] ]
; Count the number of females and males, again.
; This time it is necessary, to show the correct statistics
; in the dashboard:
countSex
reset-ticks
output-print "Please wait until the mission's\nsimulation ends ..."
end
to go
; Check if and who died:
checkLife
; Determine if any accident occurs today:
checkAccident
; Count the number of females and males to consider
; the deaths:
countSex
; The mission might ended:
if missionEnds?
[
stop
]
; Doing males' actions:
actMales
; Doing females' actions:
actFemals
; Do mating:
mate
; Protect children and pregnant individuals:
protect
; Ensure the crew is not in space:
moveAwayFromSpace
; Determine the number of females and males, again.
; Due to births, the numbers might be increased:
countSex
; Calculate the mean age:
determineMeanAges
; Determine the current year:
determineYear
; Update the plotting:
updatePlots
; Next day or month (cf. simulateMonthsInsteadOfDays) ...
ifelse simulateMonthsInsteadOfDays
[
; Next month...
tick-advance 30.42
]
[
; Next day...
tick
]
end
to actMales
ask males
[
forward random 6
right random 90
]
end
to actFemals
ask females
[
forward random 6
right random 90
; Doing pregnancy management:
if isPregnant
[
ifelse simulateMonthsInsteadOfDays
[
set pregnantSinceDays pregnantSinceDays + 30.42
]
[
set pregnantSinceDays pregnantSinceDays + 1
]
]
]
; Determine how many births we have today:
let births count females with [ pregnantSinceDays > 270 ]
; End the pregnancy:
ask females with [ pregnantSinceDays > 270 ]
[
set pregnantSinceDays 0
set isPregnant false
set previousChildren previousChildren + 1
]
; For each birth...
repeat births
[
; Twin rate is approx. 1% per birth:
let isTwin random 100 > 98
; How many children does the female gets?
let numberChildren ifelse-value isTwin [ 2 ] [ 1 ]
; For each child:
repeat numberChildren
[
; 50/50 chance for sex:
let isMale random 100 > 50
; Birth:
ifelse not isMale
[
create-females 1
[
; Set the shape and color:
set shape "person"
set color red ; females are red
; Determine the date of death for each individual.
; The time of death is a normal distribution:
set ageOfDeathYears random-normal maxAgeFemals ageStdDeviation
; Determine the initial age at time of mission's start:
set ageYears 0
; Calculate the corresponding age as days:
set ageDays 1
; Is this individual fertile?
set isFertile random 100 < (100 - infertilityFemales)
; Determine how many children a specific female can have.
; When the specific female is infertile, use 0. Otherwise, it is a normal distribution:
set maxChildren random-normal maxChildrenPerFemale maxChildrenStdDeviation
; Determine when the menopause begins:
set startMenopauseYears random-normal meanAgeMenopause ageMenopauseStdDeviation
; Not yet pregnant:
set isPregnant false
set pregnantSinceDays 0
]
; Statistics:
set numberBirths numberBirths + 1
set numberBirthsFemales numberBirthsFemales + 1
]
[
create-males 1
[
; Set the shape and color:
set shape "person"
set color blue ; males are blue
; Determine the date of death for each individual.
; The time of death is a normal distribution:
set ageOfDeathYears random-normal maxAgeMales ageStdDeviation
; Determine the initial age at time of mission's start:
set ageYears 0
; Calculate the corresponding age as days:
set ageDays 1
; Is this individual fertile?
set isFertile random 100 < (100 - infertilityMales)
]
; Statistics:
set numberBirths numberBirths + 1
set numberBirthsMales numberBirthsMales + 1
]
]
]
; Move all newborn to the secure area:
ask turtles with [ ageDays = 1 ]
[
setxy 0 0
]
end
to checkAccident
; Determine if there is any accident. We choose a random number between 0 and 99.
; Any value below 99 means no accident.
let accident random 100
; Are we having an accident today?
if accident >= 99
[
set numberAccidents numberAccidents + 1
; Okay. Now we have to determine where in spaceship the accident occurs.
; As darker the color of the patches, as higher is the risk. The white
; areas are save. But from time to time, even there an accident happens.
;
; Value mappings:
; 0 - 70 = black area (pcolor 0)
; 71 - 90 = dark area (pcolor > 0 and pcolor < 2.9)
; 91 - 98 = gray area (pcolor > 2.9 and pcolor < 9)
; 99 = white area (pcolor > 9 and pcolor <= 9.9)
let affectedArea random 100
; The black area:
if affectedArea >= 0 and affectedArea <= 70
[
; Approx. 30% of these accidents are mortal:
let mortal random 100 >= 70
if mortal
[
; Determine how many crew members are killed:
let kills floor random-normal 1 1.1
if kills > 0
[
; Kill the affected members:
ask up-to-n-of kills turtles with [pcolor = 0] [ die ]
; Statistics:
set numberDeathsByAccidents numberDeathsByAccidents + kills
set numberDeaths numberDeaths + kills
]
]
]
; The dark area:
if affectedArea > 70 and affectedArea <= 90
[
; Approx. 15% of these accidents are mortal:
let mortal random 100 >= 85
if mortal
[
; Determine how many crew members are killed:
let kills floor random-normal 0.9 0.8
if kills > 0
[
; Kill the affected members:
ask up-to-n-of kills turtles with [pcolor = 0] [ die ]
; Statistics:
set numberDeathsByAccidents numberDeathsByAccidents + kills
set numberDeaths numberDeaths + kills
]
]
]
; The gray area:
if affectedArea > 90 and affectedArea <= 98
[
; Approx. 5% of these accidents are mortal:
let mortal random 100 >= 95
if mortal
[
; Determine how many crew members are killed:
let kills floor random-normal 0.8 0.8
if kills > 0
[
; Kill the affected members:
ask up-to-n-of kills turtles with [pcolor = 0] [ die ]
; Statistics:
set numberDeathsByAccidents numberDeathsByAccidents + kills
set numberDeaths numberDeaths + kills
]
]
]
; The white, save area:
if affectedArea > 98
[
; Approx. 1% of these accidents are mortal:
let mortal random 100 > 98
if mortal
[
; Determine how many crew members are killed:
let kills floor random-normal 0.5 0.5
if kills > 0
[
; Kill the affected members:
ask up-to-n-of kills turtles with [pcolor = 0] [ die ]
; Statistics:
set numberDeathsByAccidents numberDeathsByAccidents + kills
set numberDeaths numberDeaths + kills
]
]
]
]
end
to checkLife
ask males
[
ifelse ageYears > ageOfDeathYears
[
set numberDeaths numberDeaths + 1
die
]
[
ifelse simulateMonthsInsteadOfDays
[
set ageDays ageDays + 30.42
]
[
set ageDays ageDays + 1
]
set ageYears ageDays / 365
]
]
ask females
[
ifelse ageYears > ageOfDeathYears
[
set numberDeaths numberDeaths + 1
die
]
[
ifelse simulateMonthsInsteadOfDays
[
set ageDays ageDays + 30.42
]
[
set ageDays ageDays + 1
]
set ageYears ageDays / 365
]
]
end
to countSex
set numberMales count males
set numberMalesInfertile count males with [ isFertile = false ]
set numberFemales count females
set numberFemalesInfertile count females with [ isFertile = false ]
end
to determineMeanAges
if any? females
[
set meanAgeFemalesYears mean [ageYears] of females
]
if any? males
[
set meanAgeMalesYears mean [ageYears] of males
]
set meanAgeYears (meanAgeMalesYears + meanAgeFemalesYears) / 2
end
to determineYear
set currentYear ticks / 365
end
to mate
; The maximal number of males:
let maxAllowedMales count males with [ ageYears >= startAgePermittedMating and ageYears <= endAgePermittedMating ]
; Dynamic control?
if useDynamicPermittedMating
[
; Reached 90% of spaceship's capacity?
if count turtles + count females with [ isPregnant ] > capacity * 0.8
[
; Allow approx. 1/3 to be mate:
set maxAllowedMales floor maxAllowedMales / 3
]
; Reached 95% of spaceship's capacity?
if count turtles + count females with [ isPregnant ] > capacity * 0.9
[
; Restrict the males down to three:
set maxAllowedMales 3
]
if count turtles + count females with [ isPregnant ] > capacity * 0.95
[
; Restrict the males down to three:
set maxAllowedMales 0
]
]
; Start by selecting the allowed number of males in the correct age:
ask up-to-n-of maxAllowedMales males with [ ageYears >= startAgePermittedMating and ageYears <= endAgePermittedMating ]
[
; Choose a random partner:
let partner one-of females with [ not isPregnant and ageYears >= startAgePermittedMating and ageYears <= endAgePermittedMating and ageYears < startMenopauseYears]
; Both are fertile?
if partner != nobody and isFertile and [isFertile] of partner and [ previousChildren < maxChildren ] of partner
[
; Chances of pregnancy after intercourse: 75%
let getsPregnant random 100 > 75
if getsPregnant
[
ask partner
[
set isPregnant true
]
]
]
]
end
to-report missionEnds?
if (currentYear > 6300)
[
clear-output
output-print "The mission was successful:\nsurvivors reached the\ndistant planet."
report true
]
if (numberFemales + numberMales > capacity)
[
clear-output
output-print "Unfortunately, there was too\nmuch offspring, so that the\ncapacity of the spaceship was\nexceeded. The crew has starved,\ndied of thirst or suffocated."
report true
]
if (numberFemales = 0 and numberMales = 0)
[
clear-output
output-print "The mission failed because\nthe crew was extinct."
report true
]
report false
end
to moveAwayFromSpace
; Patch color >10 means red, which is the space around the ship.
; We move all crew members away into the ship:
while [any? turtles with [ pcolor > 10]]
[
ask turtles with [ pcolor > 10]
[
forward random 6
right random 90
]
]
end
to protect
; Protect children:
ask turtles with [ ageYears < 16 and pcolor < 9.8 ]
[
facexy 0 0
forward random 3
]
; Protect pregnant females:
ask females with [ isPregnant and pcolor < 9.8 ]
[
facexy 0 0
forward random 4
]
end
to updatePlots
; Set the range of the population's plot. We want to see the
; last year in more detail. Thus, we use this function to
; move the window of the plot along:
set-current-plot "Population over time (last 365 days)"
ifelse currentYear < 1.0
[
set-plot-x-range 0 365
]
[
set-plot-x-range ceiling ((currentYear * 365) - 365) ceiling (currentYear * 365)
]
update-plots
end
@#$#@#$#@
GRAPHICS-WINDOW
625
18
1397
415
-1
-1
11.76
1
10
1
1
1
0
0
0
1
-32
32
-16
16
1
1
1
ticks
30.0
SLIDER
9
31
224
64
initialCrewSize
initialCrewSize
2
250
100.0
2
1
people
HORIZONTAL
BUTTON
249
21
337
67
NIL
setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
249
68
337
117
NIL
go
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
MONITOR
367
107
497
152
# males
numberMales
17
1
11
MONITOR
498
107
617
152
# females
numberFemales
17
1
11
PLOT
626
420
1396
570
Population over time (6300 years)
Days
Number Peoples
0.0
2299500.0
0.0
500.0
false
true
"" ""
PENS
"females" 1.0 1 -5298144 true "" "plot numberFemales"
"males" 1.0 1 -13345367 true "" "plot numberMales"
"total" 1.0 0 -16777216 true "" "plot numberFemales + numberMales"
PLOT
625
577
1397
727
Population over time (last 365 days)
Days
Number Peoples
0.0
365.0
0.0
500.0
false
true
"" ""
PENS
"females" 1.0 1 -5298144 true "" "plot numberFemales"
"males" 1.0 1 -13345367 true "" "plot numberMales"
"total" 1.0 0 -16777216 true "" "plot numberFemales + numberMales"
MONITOR
366
20
618
85
Current Year
currentYear
1
1
16
SLIDER
8
247
225
280
maxAgeFemals
maxAgeFemals
0.5
120
85.0
0.1
1
years
HORIZONTAL
SLIDER
8
281
225
314
maxAgeMales
maxAgeMales
0.5
120
79.0
0.1
1
years
HORIZONTAL
SLIDER
8
315
225
348
ageStdDeviation
ageStdDeviation
0
25
15.0
1
1
years
HORIZONTAL
MONITOR
368
266
548
311
mean age crew
meanAgeYears
2
1
11
MONITOR
498
199
617
244
mean age femals
meanAgeFemalesYears
2
1
11
MONITOR
367
199
497
244
mean age males
meanAgeMalesYears
2
1
11
TEXTBOX
11
10
161
30
Initial parameters:
16
0.0
1
TEXTBOX
9
109
159
127
Crew's initial age:
11
0.0
1
SLIDER
8
127
223
160
initialAgeFemales
initialAgeFemales
1
80
20.0
1
1
years
HORIZONTAL
SLIDER
8
161
223
194
initialAgeMales
initialAgeMales
1
80