-
Notifications
You must be signed in to change notification settings - Fork 0
/
PondTrade_step13_output statistics.nlogo
2552 lines (2181 loc) · 52.6 KB
/
PondTrade_step13_output statistics.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; GNU GENERAL PUBLIC LICENSE ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The PondTrade model
;; Copyright (C) 2018 Andreas Angourakis (andros.spica@gmail.com)
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;
;;; BREEDS ;;;;;;
;;;;;;;;;;;;;;;;;
breed [ settlements settlement ]
breed [ ships ship ]
;;;;;;;;;;;;;;;;;
;;; VARIABLES ;;;
;;;;;;;;;;;;;;;;;
globals
[
routes
;;; Output
patchesCount
coastalLandPatchesCount
shipCount
meanShipCargoValue
minShipCargoValue
maxShipCargoValue
minSettlementSize
maxSettlementSize
mainHub
meanTotalPathCostOfActiveRoutes
minTotalPathCostOfActiveRoutes
maxTotalPathCostOfActiveRoutes
meanRedTrait stdDevRedTrait modesRedTrait
meanGreenTrait stdDevGreenTrait modesGreenTrait
meanBlueTrait stdDevBlueTrait modesBlueTrait
meanLandTechTrait stdDevLandTechTrait modesLandTechTrait
meanPortTechTrait stdDevPortTechTrait modesPortTechTrait
meanSizeDecayTrait stdDevSizeDecayTrait modesSizeDecayTrait
meanStockDecayTrait stdDevStockDecayTrait modesStockDecayTrait
meanProductionTrait stdDevProductionTrait modesProductionTrait
meanFreqOverQualTrait stdDevFreqOverQualTrait modesFreqOverQualTrait
meanTransmissionTrait stdDevTransmissionTrait modesTransmissionTrait
meanMutationTrait stdDevMutationTrait modesMutationTrait
]
settlements-own
[
sizeLevel
currentNumberOfShips potentialNumberOfShips
stock
culturalVector
]
ships-own
[
isActivated
base route destination direction lastPosition
cargoValue
culturalSample
]
patches-own
[
isLand
pathCost
;;; path-finding related
parent-patch ; patch's predecessor
f ; the value of knowledge plus heuristic cost function f()
g ; the value of knowledge cost function g()
h ; the value of heuristic cost function h()
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; SETUP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to setup
clear-all
reset-ticks
; set the random seed so we can reproduce the same experiment
random-seed seed
set patchesCount count patches
create-map
create-coastal-settlements
set-routes
create-ships-per-settlement
update-output
update-display
update-plots
end
to create-map
let centralPatch patch (min-pxcor + (floor world-width / 2)) (min-pycor + (floor world-height / 2))
let halfSmallerDimension (world-width / 2)
if (world-width > world-height) [ set halfSmallerDimension (world-height / 2) ]
let minDistOfLandToCenter round ((pondSize / 100) * halfSmallerDimension)
ask patches
[
let coastThreshold minDistOfLandToCenter ; defaults to the basic value
;; add noise to coast line
; set general noise range depending on UI's coastalNoiseLevel and the size of world
let noiseRange (halfSmallerDimension * coastalNoiseLevel / 100)
; noiseType is specified with the chooser in the UI
if (noiseType = "uniform")
[
; adds a random amount from a uniform distribution with mean minDistOfLandToCenter
set noiseRange (random-float noiseRange) - (noiseRange / 2)
set coastThreshold minDistOfLandToCenter + noiseRange
]
if (noiseType = "normal")
[
; adds a random amount from a normal distribution with mean minDistOfLandToCenter
set coastThreshold random-normal minDistOfLandToCenter (halfSmallerDimension * coastalNoiseLevel / 100)
]
ifelse (distance centralPatch < coastThreshold)
[
set isLand false
]
[
set isLand true
]
]
smooth-coast-line
assign-path-cost
ask patches [ paint-terrain ]
end
to smooth-coast-line
; smooth coast line
repeat smoothIterations
[
ask patches
[
ifelse (isLand = false)
[
; water patch
; consider ratios instead of absolute numbers to avoid having isolated water bodies adjacent to the world limits (less than 8 neighbors)
if (count neighbors with [isLand = true] / count neighbors >= coastLineSmoothThreshold / 8)
[
; water patch has a certain number of land neighbors
set isLand true ; converted to land
]
]
[
; land patch
if (count neighbors with [isLand = false] / count neighbors >= coastLineSmoothThreshold / 8)
[
; land patch has a certain number of water neighbors
set isLand false ; converted to water
]
]
]
]
end
to assign-path-cost
ask patches
[
ifelse (isLand = false)
[ set pathCost 1 ] ; arbitrary unit for now
[ set pathCost relativePathCostInLand ] ; defined by parameter in relation to the cost of path in water (i.e., 1)
]
end
to paint-terrain ; ego = patch
ifelse (isLand = false)
[ set pcolor 106 ] ; blue for water
[ set pcolor 54 ] ; green for land
end
to create-coastal-settlements
; consider only coastal patches
let coastalPatches patches with [(isLand = true) and (any? neighbors with [isLand = false])]
repeat numberOfSettlements
[
; ask a random coastal patch without a settlement already
ask one-of coastalPatches with [not any? settlements-here]
[
sprout-settlements 1 ; creates one "turtle" of breed settlements
[
set sizeLevel 1 ; the size level is initiated at minimum (i.e., 1)
set stock 0
set culturalVector extract-rgb color ; 0#, 1# and 2#
; We add seven continuos cultural traits to the neutral RGB traits,
; representing their attitude and ability involving
; aspects we previously fixed as parameters and one variable:
; 3# relativePathCostInLand (normal distribution around global parameter)
set culturalVector lput (random-normal 0 landTechVariation) culturalVector
; 4# relativePathCostInPort (normal distribution around global parameter)
set culturalVector lput (random-normal 0 portTechVariation) culturalVector
; 5# settlementSizeDecayRate [0 - maxSettlementSizeDecayRate)
set culturalVector lput (random-float maxSettlementSizeDecayRate) culturalVector
; 6# stockDecayRate [0 - maxStockDecayRate)
set culturalVector lput (random-float maxStockDecayRate) culturalVector
; 7# produtionRate [0 - maxProductionRate)
set culturalVector lput (random-float maxProductionRate) culturalVector
; 8# frequencyOverQuality [0 - 1)
set culturalVector lput (random-float 1) culturalVector
; 9# traitTransmissionRate [0 - maxTraitTransmissionRate) *** now, it means specifically the 'openess' of a settlement towards other variants of a trait
set culturalVector lput (random-float maxTraitTransmissionRate) culturalVector
; 10# mutationVariation [0 - maxMutationVariation)
set culturalVector lput (random-float maxMutationVariation) culturalVector
set shape "circle 2"
]
; replace the land path cost with the port pathCost
set pathCost relativePathCostInPort
; exclude this patch from the pool of coastal patches
set coastalPatches other coastalPatches
]
]
end
to create-ships-per-settlement
ask settlements
[
let thisSettlement self ; to avoid the confusion of nested agent queries
set potentialNumberOfShips get-potential-number-of-ships
hatch-ships potentialNumberOfShips ; use the sizeLevel variable as the number of ships based in the settlement
[
setup-ship thisSettlement
]
set currentNumberOfShips get-current-number-of-ships
]
end
to setup-ship [ baseSettlement ]
set base baseSettlement
set isActivated true
; give meaningful display related to base
set shape "sailboat side" ; import this shape from the library (Tools > Shape editor > import from library)
set color [color] of base
set size 3
choose-destination
end
to set-routes
set routes [] ; initialize/reset the routes as an empty list
let settlementsWithoutRoutes settlements ; helper variable to keep track of which settlement already searched for routes
ask settlements
[
let thisSettlement self
ask other settlementsWithoutRoutes
[
let optimalRoute find-a-path ([patch-here] of thisSettlement) ([patch-here] of self) ; find the optimal route to this settlement
set routes lput optimalRoute routes ; add the optimal route to the end of the routes list
]
set settlementsWithoutRoutes other settlementsWithoutRoutes
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; CYCLE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to go
tick
if (ticks = 10000 or count turtles > 500) [ stop ]
update-ships
update-settlements
update-output
update-display
end
to update-ships
let activeShips ships with [isActivated]
let shipsInBase activeShips with [is-in-base]
let shipsInDestination activeShips with [is-in-destination]
; UPDATE LAST POSITION
ask activeShips
[
; update lastPosition if in a patch centre
if ((xcor = [pxcor] of patch-here) and (ycor = [pycor] of patch-here))
[
set lastPosition patch-here
]
]
; UNLOAD
ask (turtle-set shipsInBase shipsInDestination) with [cargoValue > 0]
[
; unload cargo (changes sizeLevel)
unload-cargo
]
; CHECK if the ship can be sustained when in the base
ask shipsInBase
[
if ([potentialNumberOfShips < currentNumberOfShips] of base)
[
; the current number of ships cannot be sustained
set isActivated false
; update currentNumberOfShips of base
ask base [ set currentNumberOfShips get-current-number-of-ships ]
]
]
set activeShips ships with [isActivated] ; update active ships
set shipsInBase shipsInBase with [isActivated] ; update ships in base
; LOAD
ask (turtle-set shipsInBase shipsInDestination)
[
; load cargo (changes stock)
load-cargo
]
; CHOOSE DESTINATION
ask shipsInBase with [cargoValue > 0]
[
; update the destination whenever in the base settlement and there is cargo to transport
choose-destination
]
; FIND DIRECTION in route
ask (turtle-set shipsInBase shipsInDestination)
[
find-direction
]
; MOVE towards the next position in the route
ask activeShips with [cargoValue > 0]
[
; move following the route when there is cargo to transport
move-to-destination
]
end
to choose-destination ; ego = ship
let thisShip self
; get routes connecting the base settlement
let routesFromBase get-routes-to-settlement [base] of thisShip
; order these routes by benefit/cost ratio
set routesFromBase sort-by [ [?1 ?2] -> benefit-cost-of-route ?1 thisShip > benefit-cost-of-route ?2 thisShip ] routesFromBase
; print the options available
; foreach routesFromBase
; [
; print "==============================================================="
; print "route between:"
; print [who] of get-origin-and-destination ?
; print "has the benefit-cost ratio of:"
; print benefit-cost-of-route ?
; ]
; print "-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x"
; select the one with higher benefit/cost ratio
set route first routesFromBase
; get the settlement of destination
set destination one-of (get-origin-and-destination route) with [who != [who] of ([base] of thisShip)]
end
to find-direction ; ego = ship
; find where in the route list is the ship
let currentPosition position lastPosition route
; set direction if in a settlement
ifelse (currentPosition = 0) ; in the first extreme of the route list
[
; move in the route list towards larger index numbers
set direction 1
]
[
if (currentPosition = (length route - 1)) ; in the last extreme of the route list
[
; move in the route list towards smaller index numbers
set direction -1
]
]
; else the ship is in route to either the base or the destination
end
to move-to-destination ; ego = ship
; find where in the route list is the ship
let currentPosition position lastPosition route
; move through the route following direction
let targetPatch item (currentPosition + direction) route
;move-to targetPatch ; constant travel time (1 patch per tick)
facexy ([pxcor] of targetPatch) ([pycor] of targetPatch)
forward min (
list
(1 / get-path-cost patch-here self) ; the maximum distance in a tick in the current patch
(distancexy ([pxcor] of targetPatch) ([pycor] of targetPatch)) ; the distance to the target patch
)
end
to-report is-in-base ; ego = ship
report (xcor = [xcor] of base) and (ycor = [ycor] of base) ; if the ship arrived at the centre of the base patch
end
to-report is-in-destination ; ego = ship
report (xcor = [xcor] of destination) and (ycor = [ycor] of destination) ; if the ship arrived at the centre of the destination patch
end
to unload-cargo ; ego = ship
let thisShip self
let settlementHere one-of settlements-here
; unload cargo
ask settlementHere [ add-trade-effect thisShip ]
end
to load-cargo ; ego = ship
let settlementHere one-of settlements-here
; load cargo
set cargoValue [stock] of settlementHere
ask settlementHere [ set stock 0 ] ; empty the settlement stock
set culturalSample [culturalVector] of settlementHere
end
to update-settlements
ask settlements
[
let thisSettlement self
; the sizeLevel of settlements decays with a constant rate, up to 1 (minimum)
set sizeLevel max (list 1 (sizeLevel * (1 - ((item 5 culturalVector) / 100)) ) )
; production in stock also decays with a constant rate
set stock stock * (1 - ((item 6 culturalVector) / 100))
; prodution is generated in proportion to sizeLevel, following a constant rate
set stock stock + sizeLevel * ((item 7 culturalVector) / 100)
; determine the current and potential number of ships
set currentNumberOfShips get-current-number-of-ships
set potentialNumberOfShips get-potential-number-of-ships
; conditions favors the creation of new ships
if (random-float 1 > currentNumberOfShips / potentialNumberOfShips )
[
; create a new ship or activate an old one
repeat 1
[
ifelse (any? ships with [not isActivated])
[
ask one-of ships with [not isActivated]
[
setup-ship thisSettlement
move-to thisSettlement
]
]
[
hatch-ships 1
[
setup-ship thisSettlement
]
]
]
set currentNumberOfShips get-current-number-of-ships ; update currentNumberOfShips
]
; add variation to the settlement traits (mutation)
mutate-traits
]
end
to add-trade-effect [ aShip ] ; ego = settlement
; cultural transmission ship to port
let newCulturalVector []
foreach culturalVector
[ ?1 ->
let otherSettlementTrait item (length newCulturalVector) [culturalSample] of aShip
let traitChange (otherSettlementTrait - ?1) * ((item 9 culturalVector) / 100)
set newCulturalVector lput (?1 + traitChange) newCulturalVector
]
; print (word "========== " self " ============")
; print (word "old vector: " culturalVector ", new vector: " newCulturalVector)
set culturalVector newCulturalVector
set sizeLevel sizeLevel + [cargoValue] of aShip
end
to mutate-traits
let mutationVariationToApply (item 10 culturalVector) / 100
;print "========================================"
;print culturalVector
; #1, #2 and #3
set culturalVector replace-item 0 culturalVector mutate-trait (item 0 culturalVector) 0 255 mutationVariationToApply
set culturalVector replace-item 1 culturalVector mutate-trait (item 1 culturalVector) 0 255 mutationVariationToApply
set culturalVector replace-item 2 culturalVector mutate-trait (item 2 culturalVector) 0 255 mutationVariationToApply
; #3 and #4 (relativePathCostInLand, relativePathCostInPort)
set culturalVector replace-item 3 culturalVector mutate-trait (item 3 culturalVector) (-1 * relativePathCostInLand + 1) 100 mutationVariationToApply ; arbitrary maximum
set culturalVector replace-item 4 culturalVector mutate-trait (item 4 culturalVector) (-1 * relativePathCostInPort + 1) 100 mutationVariationToApply ; arbitrary maximum
; #5, #6 and #6 (settlementSizeDecayRate, stockDecayRate, produtionRate)
set culturalVector replace-item 5 culturalVector mutate-trait (item 5 culturalVector) 0 maxSettlementSizeDecayRate mutationVariationToApply
set culturalVector replace-item 6 culturalVector mutate-trait (item 6 culturalVector) 0 maxStockDecayRate mutationVariationToApply
set culturalVector replace-item 7 culturalVector mutate-trait (item 7 culturalVector) 0 maxProductionRate mutationVariationToApply
; #8, #9 and #10 (frequencyOverQuality, traitTransmissionRate, mutationVariation)
set culturalVector replace-item 8 culturalVector mutate-trait (item 8 culturalVector) 0 1 mutationVariationToApply
set culturalVector replace-item 9 culturalVector mutate-trait (item 9 culturalVector) 0 maxTraitTransmissionRate mutationVariationToApply
set culturalVector replace-item 10 culturalVector mutate-trait (item 10 culturalVector) 0 maxMutationVariation mutationVariationToApply
;print culturalVector
end
to-report mutate-trait [ traitValue minValue maxValue mutationVar ]
report (max (list minValue min (list maxValue (traitValue + (random-normal 0 mutationVar) * (maxValue - minValue)))))
end
to-report get-potential-number-of-ships ; ego = settlement
report (
1 +
(sizeLevel - 1) * (item 8 culturalVector)
)
end
to-report get-current-number-of-ships ; ego = settlement
let thisSettlement self
report count ships with [isActivated and base = thisSettlement ]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; OUTPUT ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to update-output
set coastalLandPatchesCount count patches with [isLand = true and any? neighbors with [isLand = false]]
let activatedShips ships with [isActivated]
set shipCount count activatedShips
set meanShipCargoValue mean [cargoValue] of activatedShips
set minShipCargoValue min [cargoValue] of activatedShips
set maxShipCargoValue max [cargoValue] of activatedShips
set minSettlementSize min [sizeLevel] of settlements
set maxSettlementSize max [sizeLevel] of settlements
set mainHub max-one-of settlements [sizeLevel]
set meanTotalPathCostOfActiveRoutes mean [sum (map [ ?1 -> [pathCost] of ?1 ] route)] of activatedShips
set minTotalPathCostOfActiveRoutes min [sum (map [ ?1 -> [pathCost] of ?1 ] route)] of activatedShips
set maxTotalPathCostOfActiveRoutes max [sum (map [ ?1 -> [pathCost] of ?1 ] route)] of activatedShips
set meanRedTrait mean [item 0 culturalVector] of settlements
set stdDevRedTrait standard-deviation [item 0 culturalVector] of settlements
set modesRedTrait modes [ round (item 0 culturalVector) ] of settlements
set meanGreenTrait mean [item 1 culturalVector] of settlements
set stdDevGreenTrait standard-deviation [item 1 culturalVector] of settlements
set modesGreenTrait modes [ round (item 1 culturalVector) ] of settlements
set meanBlueTrait mean [item 2 culturalVector] of settlements
set stdDevBlueTrait standard-deviation [item 2 culturalVector] of settlements
set modesBlueTrait modes [ round (item 2 culturalVector) ] of settlements
set meanLandTechTrait mean [item 3 culturalVector] of settlements
set stdDevLandTechTrait standard-deviation [item 3 culturalVector] of settlements
set modesLandTechTrait modes [ round (relativePathCostInLand + item 3 culturalVector) ] of settlements
set meanPortTechTrait mean [item 4 culturalVector] of settlements
set stdDevPortTechTrait standard-deviation [item 4 culturalVector] of settlements
set modesPortTechTrait modes [ round (relativePathCostInPort + item 4 culturalVector) ] of settlements
set meanSizeDecayTrait mean [item 5 culturalVector] of settlements
set stdDevSizeDecayTrait standard-deviation [item 5 culturalVector] of settlements
set modesSizeDecayTrait modes [ round (item 5 culturalVector) ] of settlements
set meanStockDecayTrait mean [item 6 culturalVector] of settlements
set stdDevStockDecayTrait standard-deviation [item 6 culturalVector] of settlements
set modesStockDecayTrait modes [ round (item 6 culturalVector) ] of settlements
set meanProductionTrait mean [item 7 culturalVector] of settlements
set stdDevProductionTrait standard-deviation [item 7 culturalVector] of settlements
set modesProductionTrait modes [ round (item 7 culturalVector) ] of settlements
set meanFreqOverQualTrait mean [item 8 culturalVector] of settlements
set stdDevFreqOverQualTrait standard-deviation [item 8 culturalVector] of settlements
set modesFreqOverQualTrait modes [ round (item 8 culturalVector) ] of settlements
set meanTransmissionTrait mean [item 9 culturalVector] of settlements
set stdDevTransmissionTrait standard-deviation [item 9 culturalVector] of settlements
set modesTransmissionTrait modes [ round (item 9 culturalVector) ] of settlements
set meanMutationTrait mean [item 10 culturalVector] of settlements
set stdDevMutationTrait standard-deviation [item 10 culturalVector] of settlements
set modesMutationTrait modes [ round (item 10 culturalVector) ] of settlements
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; DISPLAY ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to update-display
paint-routes
paint-active-routes
ask settlements
[
set hidden? not showSettlements
; scale the size of settlements according to their dynamic free-scaled sizeLevel
set size 1 + (sizeLevel / maxSettlementSize) * 9
set color rgb (item 0 culturalVector) (item 1 culturalVector) (item 2 culturalVector)
]
ask ships
[
ifelse (isActivated)
[ set hidden? false ]
[ set hidden? true ]
]
end
to paint-routes
; resets route patches to the terrain color
foreach routes
[ ?1 ->
let aRoute ?1
foreach aRoute
[ ??1 ->
ask ??1 [ paint-terrain ]
]
]
; paint route patches in shades of red depending on route frequency
foreach routes
[ ?1 ->
let aRoute ?1
foreach aRoute
[ ??1 ->
ask ??1
[
if (showRoutes)
[
ifelse (pcolor < 11 or pcolor > 19) ; if its the first route crossing the patch
[
set pcolor 11
]
[
set pcolor min (list (pcolor + 1) (19)) ; sets a maximum at 19 (the brightest)
]
]
]
]
]
end
to paint-active-routes
ask ships
[
foreach route
[ ?1 ->
ask ?1
[
ifelse (showActiveRoutes)
[
set pcolor yellow
]
[
if (not showRoutes) ; if not displaying all routes
[
; resets to the patch terrain color
paint-terrain
]
]
]
]
]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Get and set routes (helper 'to-report' procedures) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to-report get-route [ settlement1 settlement2 ] ; accepts two settlements and returns a route
; get routes connecting settlement1
let routesFromSettlement1 filter
[ ?1 ->
([one-of settlements-here] of first ?1 = settlement1) or
([one-of settlements-here] of last ?1 = settlement1)
] routes
; get the route connecting settlement2 from the previous list
let routeFromSettlement1ToSettlement2 filter
[ ?1 ->
([one-of settlements-here] of first ?1 = settlement2) or
([one-of settlements-here] of last ?1 = settlement2)
] routesFromSettlement1
report first routeFromSettlement1ToSettlement2
end
to-report get-routes-to-settlement [ aSettlement ] ; accepts a settlement and return a list of routes
report filter
[ ?1 ->
([one-of settlements-here] of first ?1 = aSettlement) or
([one-of settlements-here] of last ?1 = aSettlement)
] routes
end
to-report get-origin-and-destination [ aRoute ] ; accepts a route and returns a turtle-set with two settlements
report (turtle-set ([ one-of settlements-here ] of first aRoute) ([one-of settlements-here ] of last aRoute))
end
to-report benefit-cost-of-route [ aRoute aShip ] ; accepts a route andpan returns a number (the benefit/cost ratio of the route)
let cost 0
foreach aRoute ; for every patch in the given route
[ ?1 ->
set cost cost + get-path-cost ?1 aShip
]
let originAndDestination get-origin-and-destination aRoute
let benefit 0
ask originAndDestination [ set benefit benefit + sizeLevel ] ; the benefit is the sum of the sizeLevel of the two settlements
report benefit / cost
end
to-report get-path-cost [ aPatch aShip ]
let pathCostOfPatch [pathCost] of aPatch
if ([isLand] of aPatch)
[
ifelse ([any? settlements-here] of aPatch)
[
; path cost in port apply
set pathCostOfPatch pathCostOfPatch + [(item 4 culturalVector)] of [base] of aShip
]
[
; path cost in land apply
set pathCostOfPatch pathCostOfPatch + [(item 3 culturalVector)] of [base] of aShip
]
]
report pathCostOfPatch
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; A* path finding algorithm ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; modified from Meghendra Singh's Astardemo1 model in NetLogo User Community Models
; http://ccl.northwestern.edu/netlogo/models/community/Astardemo1
; modified lines/fragments are marked with ";-------------------------------*"
; In this version, patches have different movement cost.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; the actual implementation of the A* path finding algorithm
; it takes the source and destination patches as inputs
; and reports the optimal path if one exists between them as output
to-report find-a-path [ source-patch destination-patch]
; initialize all variables to default values
let search-done? false
let search-path []
let current-patch 0
let open [] ;-------------------------------*
let closed [] ;-------------------------------*
;-------------------------------*
ask patches with [ f != 0 ]
[
set f 0
set h 0
set g 0
]
;-------------------------------*
; add source patch in the open list
set open lput source-patch open
; loop until we reach the destination or the open list becomes empty
while [ search-done? != true]
[
ifelse length open != 0
[
; sort the patches in open list in increasing order of their f() values
set open sort-by [ [?1 ?2] -> [f] of ?1 < [f] of ?2 ] open
; take the first patch in the open list
; as the current patch (which is currently being explored (n))
; and remove it from the open list
set current-patch item 0 open
set open remove-item 0 open
; add the current patch to the closed list
set closed lput current-patch closed
; explore the Von Neumann (left, right, top and bottom) neighbors of the current patch
ask current-patch
[
; if any of the neighbors is the destination stop the search process
ifelse any? neighbors4 with [ (pxcor = [ pxcor ] of destination-patch) and (pycor = [pycor] of destination-patch)] ;-------------------------------*
[
set search-done? true
]
[
; the neighbors should not already explored patches (part of the closed list)
ask neighbors4 with [ (not member? self closed) and (self != parent-patch) ] ;-------------------------------*
[
; the neighbors to be explored should also not be the source or
; destination patches or already a part of the open list (unexplored patches list)
if not member? self open and self != source-patch and self != destination-patch
[
;set pcolor 45 ;-------------------------------*
; add the eligible patch to the open list
set open lput self open
; update the path finding variables of the eligible patch
set parent-patch current-patch
set g [g] of parent-patch + pathCost ;-------------------------------*
set h distance destination-patch
set f (g + h)
]
]
]
; if self != source-patch ;-------------------------------*
; [
; set pcolor 35
; ]
]
]
[
; if a path is not found (search is incomplete) and the open list is exhausted
; display a user message and report an empty search path list.
user-message( "A path from the source to the destination does not exist." )
report []
]
]
; if a path is found (search completed) add the current patch
; (node adjacent to the destination) to the search path.
set search-path lput current-patch search-path
; trace the search path from the current patch
; all the way to the source patch using the parent patch
; variable which was set during the search for every patch that was explored
let temp first search-path
while [ temp != source-patch ]
[
; ask temp ;-------------------------------*
; [
; set pcolor 85
; ]
set search-path lput [parent-patch] of temp search-path
set temp [parent-patch] of temp
]
; add the destination patch to the front of the search path
set search-path fput destination-patch search-path
; reverse the search path so that it starts from a patch adjacent to the
; source patch and ends at the destination patch
set search-path reverse search-path
; report the search path
report search-path
end
@#$#@#$#@
GRAPHICS-WINDOW
292
16
666
391
-1
-1
6.0
1
10
1
1
1
0
0
0
1
-30
30
-30