-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRAPSim.nlogo
2409 lines (2068 loc) · 66.3 KB
/
TRAPSim.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
extensions [gis csv table nw ] ; GIS extension for NetLogo to import GIS shapefiles, csv extension to import csv files, table to create tables for variables, nw to create road networks from shapefiles
globals [
pollution-data
area
roads
streets
mean-pm10
back_pm10_sample
pm10-back pm10-road pm10-others
t_sajik t_jahamoon t_daesagwan t_daehak t_jongno
t_dongho t_ns2ho t_soparo t_saemunan t_ssm
v_sajik v_jahamoon v_daesagwan v_daehak v_jongno
v_dongho v_ns2ho v_soparo v_saemunan v_ssm
Jongno_p Jung_p
JongnoKerb_p Samil_p Sejong_p Pirum_p Yulgok_p
Drivers_p Walkers_p
d_nearroad e_nearroad
d_farroad e_farroad
]
breed [nodes node] ; Breeds allows the modeller to easily ask some sets of agents to run commands or otherwise take actions
breed [area-labels area-label]
breed [buildings building]
breed [s_entrances s_entrance]
breed [cars car]
breed [employees employee]
breed [drivers driver]
; XXX-own is the attributed tied to each entity group
links-own [ ; For instance, road-name, is-road?, max-spd, Daero?, and weight appear in the attribute table of each road segment
road-name
is-road?
max-spd
Daero?
weight
]
patches-own [
is-research-area?
is-endpoint?
intersection
mybuilding
building_info
dilution
countdown
dong-name
dong-code
road_buffer
pm10
pm10_indoor
centroid? ;;is it the centroid of a building?
id ;;if it is a centroid of a building, it has an ID that represents the building
b_entrance ;;nearest vertex on road. only for centroids.
]
nodes-own [
name
dong_code
endpoint?
line-start
line-end
auto?
green-light?
intersection?
b_entrance?
dist-original ;;distance from original point to here
]
s_entrances-own [
Line ;; Seoul Metro has lines from 1 to 9
]
buildings-own [
close-to-road?
;count-employees
]
cars-own [
speed
fueltype
tyre-wear
brake-wear
surface-wear
total-emission
origin ;;a vertex. where the vehicle begins the trip
destination ;; allocated destination
myoffice
goal ;;the b_entrance of the destination on the road
path-work ;; an agentset containing nodes to visit in the shortest path
path-home
nodes-remaining
current
to-node
myroad
current-link
district_name district_code
link-counter ;counter for distance along path
direction ;+1 work to work and -1 for to home
time-at-work ; how long to spend at work
random-car ; work like venice model, or go home to work and back
parked ; parked cars don't interfere with other traffic
owner
health
leave-home-hour
leave-home-mins
unwell_history
work-near-roads?
]
employees-own [
origin ;;a vertex. where the vehicle begins the trip
origin_patch
myhome
myoffice
goal ;;the b_entrance of the destination on the road
current
Heuristic
arrived?
time-at-work ; how long to spend at work
direction
arrive-tick
leave-home-hour
leave-home-mins
work-near-roads?
health
unwell_history
]
drivers-own [
commute_method
my_car
age
health
;work-near-roads?
]
;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;
to setup
ca ; clear everything
set-gis
add-labels
activate-links
set-signals
set-random-cars
set-resident-cars
set-resident-driver
set-incoming-traffic
set-subway-commuters
set-OD
set-path-node
set-path-link
set-road-ends
set-pm10
set-pm10-others
ask cars with [not random-car] [to-work-setup] ;; We setup the OD for resident vehicles
reset-ticks
end
;;---------------------------------------------------------
to go
;;---This section is a list of global variables that run in the Behaviorspace or on the HPC-----
set back_pm10_sample precision ([pm10] of patch 81 91) 2
set Jongno_p precision ([pm10] of patch 88 94) 2
set Jung_p precision ([pm10] of patch 38 68) 2
set JongnoKerb_p precision ([pm10] of patch 105 95) 2
set Samil_p precision ([pm10] of patch 77 77) 2 ;"Samil-daero"
set Sejong_p precision ([pm10] of patch 43 112) 2 ;"Sejong-daero"
set Pirum_p precision ([pm10] of patch 22 148) 2 ;"Pirun-daero"
set Yulgok_p precision ([pm10] of patch 124 117) 2 ;"Daehak-ro"
set Drivers_p precision ((count cars with [not random-car and health < 100] /
count cars with [not random-car]) * 100) 3
set Walkers_p precision ((count(employees with [health < 100]) / count employees) * 100) 3
set mean-pm10 precision mean [pm10] of patches with [is-research-area? = true] 2
;;---------------------------------------------------------------------
;;-- Stop condition--
if (export-raster = "no" and (ticks + 1) >= 127740) or (export-raster = "yes" and (ticks + 1) >= 1442) [stop]
;;--This section looks a bit messy but it makes cars to run differently on weekends.
ask cars [
let is-weekend? item 6 table:get pm10-road (ticks + 1)
let what-time? item 1 table:get pm10-road (ticks + 1)
let hours item 1 table:get pm10-back (ticks + 1)
let minutes item 4 table:get pm10-back (ticks + 1)
let weekday? item 5 table:get pm10-back (ticks + 1)
let travel-hours what-time? >= (8 + random 2) and what-time? < 22
ifelse (random-car)[move speed]
[ if is-weekend? = false and weekday? != "Mon" [travel speed]
if is-weekend? = false and weekday? = "Mon" and hours = 6 and minutes = 59 [to-work-setup set time-at-work 540 + random 61]
if is-weekend? = false and weekday? = "Mon" and hours >= 7 [travel speed]
if is-weekend? = false and weekday? = "Mon" and hours >= 7 and minutes = 5 and parked [park]
if (is-weekend? = true and awareness = "no" and travel-hours) [move speed] ;; move resident cars on weekends only when awareness off
if (is-weekend? = true and awareness = "yes" and travel-hours and health >= 100) [move speed] ;; take rest when awareness on
if (is-weekend? = true and awareness = "yes" and travel-hours and health < 100) [move-to origin to-work-setup] ;; take rest when awareness on
if (is-weekend? = true and what-time? >= 23) [move-to origin to-work-setup] ] ;; Flying cars may appear.
;; They are just heading home without using the road links.
]
speed-up
set-signal-colours
meet-traffic-lights
drive-out-of-cbd
add-cars
pollute
fadeout
kill-cars
add-employees
move-employees
move-drivers
health-loss
health-recovery
validation-plot
age-plot
tick
if(export-raster = "yes")[store-raster]
end
;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;
to set-gis
ask patches [ set pcolor white ] ;; set a white background
set area gis:load-dataset "GIS/Seoul_4daemoonArea.shp" ;; set shapefile
set roads gis:load-dataset "GIS/Seoul_4DaemoonLink.shp" ;; set road network
let world_envelope gis:load-dataset "GIS/Seoul_4DaemoonLink_buffer.shp" ;; set spatial extent
gis:set-world-envelope (gis:envelope-union-of gis:envelope-of world_envelope) ;; set spatial extent
;; Assign subdistrict name to each patch
ask patches gis:intersecting area [set is-research-area? true]
foreach gis:feature-list-of area [vector-feature ->
ask patches [if gis:intersects? vector-feature self
[ set dong-name gis:property-value vector-feature "adm_dr_nm_"
set dong-code gis:property-value vector-feature "DONG_CODE" ]
]]
;; Assign road info
ask patches gis:intersecting roads [set road_buffer true ]
ask patches with [road_buffer != true][ set road_buffer false ]
;; Assign Buildings
let building-layer gis:load-dataset "GIS/seoulCBD_Buildings.shp" ;; import buildings from a GIS file
gis:set-drawing-color 107 gis:fill building-layer 1.0 ;; colour buildings into blue
ask patches gis:intersecting building-layer [set mybuilding true ] ;; set the building areas as true
ask patches with [mybuilding != true][set mybuilding false] ;; set the non-building areas as false
;; Identify centroids and assign IDs to centroids
;; This loop is used as to assign a centroid location for each building so that the agents can set their destinations
foreach gis:feature-list-of building-layer [ feature ->
let centroid gis:location-of gis:centroid-of feature
if not empty? centroid [
create-buildings 1 [
set xcor item 0 centroid
set ycor item 1 centroid
set size 0
set centroid? true
set building_info self
set id gis:property-value feature "ID"
ifelse [road_buffer] of patch-here = true [set close-to-road? true][set close-to-road? false]
]]]
ask patches with [centroid? != true][set centroid? false set ID "not given" ]
;; Create subway entrances: "s_entrances"
let subway gis:load-dataset "GIS/Subway_SeoulCBD.shp"
foreach gis:feature-list-of subway [ vector-feature ->
foreach gis:vertex-lists-of vector-feature [ vertex ->
foreach vertex [ point ->
let location gis:location-of point
if not empty? location [
create-s_entrances 1 [
set xcor item 0 location
set ycor item 1 location
set shape "flag"
set size 3
set Line gis:property-value vector-feature "LineNo"
if Line = 1 [ set color blue ]
if Line = 2 [ set color lime ]
if Line = 3 [ set color orange ]
if Line = 4 [ set color sky ]
if Line = 5 [ set color magenta ]
]]]]]
ask s_entrances [
if count s_entrances-here > 1 [
ask other s_entrances-here [ die ] ]]
;; Create turtles representing the nodes. Create links to connect them
foreach gis:feature-list-of roads [ vector-feature ->
let first-vertex gis:property-value vector-feature "UP_FROM_NO"
let last-vertex gis:property-value vector-feature "UP_TO_NO"
foreach gis:vertex-lists-of vector-feature [ vertex ->
let previous-node nobody
foreach vertex [ point ->
let location gis:location-of point
if not empty? location
[ create-nodes 1 [
set xcor item 0 location
set ycor item 1 location
set size 0.05
set shape "circle"
set color one-of base-colors
set hidden? false
set line-start first-vertex
set line-end last-vertex
ifelse previous-node = nobody
[]
[create-link-with previous-node] ; create link to previous node
set previous-node self]
]
] ; end of foreach vertex
] ; end of foreach gis:vertex-lists-of vector-feature
] ; end of foreach gis:feature-list-of roads
;;delete duplicate vertices
;;(there may be more than one vertice on the same patch due to reducing size of the map).
;;therefore, this map is simplified from the original map.
ask nodes [
if count nodes-here > 1 [
ask other nodes-here [
ask myself [create-links-with other [link-neighbors] of myself]
die]]
]
;;find nearest node to become b_entrance
ask patches with [centroid? = true][
set b_entrance min-one-of nodes in-radius 200 [distance myself]
ask b_entrance [set b_entrance? true]]
ask patches with [centroid? = false][ask nodes [set b_entrance? false]]
end
;;--4daemoon represents four main gates built back in the previous Kingdom of Chosun.--;;
to draw-4daemoon
gis:set-drawing-color [ 229 255 204] gis:fill area 0 ;;RGB color
gis:set-drawing-color [ 64 64 64] gis:draw area 1
end
to draw-map
import-drawing "GIS/map.png"
end
to add-labels
foreach gis:feature-list-of area [vector-feature ->
let centroid gis:location-of gis:centroid-of vector-feature
if not empty? centroid
[ create-area-labels 1
[ set xcor item 0 centroid
set ycor item 1 centroid
set size 0
set label-color blue
set label gis:property-value vector-feature "adm_dr_nm_"
]]]
ask nodes [set dong_code [dong-code] of patch-here]
end
to activate-links
ask links [
set is-road? true
let way list [line-start] of end1 [line-end] of end2
let daero ["Jahamun-ro" "Sajik-ro" "Samil-daero" "Yulgok-ro" "Toegye-ro" "Saemunan-ro 3-gil" "Jangchungdan-ro"
"Taepyeong-ro" "Sejong-daero" "Jong-ro" "Eulji-ro" "Seosomun-ro" "Donhwamun-ro" "Sejong-daero 23-gil"]
foreach gis:feature-list-of roads [ vector-feature-sub ->
let mspeed gis:property-value vector-feature-sub "MAX_SPD"
let vector-start gis:property-value vector-feature-sub "UP_FROM_NO"
let vector-end gis:property-value vector-feature-sub "UP_TO_NO"
let start-end list vector-start vector-end
let end-start list vector-end vector-start
if way = start-end [set road-name gis:property-value vector-feature-sub "ROAD_NAME_"]
if road-name = one-of daero [set Daero? true]
if road-name = 0 or road-name = "" [set road-name [name] of end2 ]
set max-spd read-from-string mspeed
]
]
end
to set-signals
ask nodes [
ifelse count my-links > 2 ; We arbitrarily set the traffic lights where the link intersections are more than 2
[set size 3 ; There might be more in reality
set intersection? true
set auto? random 11 ; We set the timer
]
[set color grey]
]
ask nodes with [intersection? = true][ ; As the timer ticks down from 10 to 0
if auto? >= 5 [set color green set green-light? true] ; If the number is 5 or more then the traffic light is green
if auto? < 5 [set color red set green-light? false] ; If the number is less than 5 the light is red
set pcolor black + 5
ask neighbors [set pcolor black + 4]
ask patches with [pcolor = black + 4] [set intersection true]
]
end
;;- We set the road endpoints (or entry points) for the non-resident inbound vehicles.
to set-road-ends
let endpoints0 map node [5465 5463 5464 5451 5452 5798 5797 5685 5690 6745 6753 5653 5651 5652 6687 6679]
let endpoints filter is-node? endpoints0
foreach endpoints [ep ->
ask ep [set endpoint? true ask patch-here [set is-endpoint? true ]]
]
ask nodes with [endpoint? != true][set endpoint? false]
ask patches with [is-endpoint? = true][ask neighbors [set is-endpoint? true]]
ask patches with [is-endpoint? != true][set is-endpoint? false]
end
to set-random-cars
set-default-shape cars "car"
create-cars 50 [ ;; random-cars
set parked false
set random-car true
set size 2
set speed 0
set destination nobody
set origin one-of nodes move-to origin
]
ask n-of (int(.7 * count cars)) cars [set fueltype "Gasoline"] ; 70% of the vehicles in Seoul CBD owns gasoline (unleaded) cars
ask cars with [fueltype != "Gasoline"][set fueltype "Diesel" ] ; the next majority is Diesel
end
to set-path-node
ask cars [
; Randomly choose a target node to walk to
let target [goal] of self
;print target
if target != nobody [
; Remember the starting node
set current one-of nodes-here
; Define a path variable from the current node- take all but
; the first item (as first item is current node)
let path nobody
ask links [ set weight link-length ]
ask current [
set path but-first nw:turtles-on-weighted-path-to target weight
]
; Indicate the end node
ask last path [
set color [color] of self
set b_entrance? true
set size 0.5 ]
let path-work0 lput path path-work ;; assign all the nodes that leads to the destination node
set path-work item 0 path-work0
set path-work fput origin path-work
set to-node first path ;; or can code as ---> item 0 item 0 path-work
]
set nodes-remaining length [path-work] of self
set current-link link [who] of [current] of self [who] of [to-node] of self
face to-node
let points n-values [nodes-remaining] of self [x -> ([nodes-remaining] of self - 1) - x]
foreach points [ x ->
set path-home lput item x [path-work] of self path-home
]
set path-home lput origin path-home
]
end
to set-path-link
ask cars [
let vertex0 path-work
let vertex1 remove-item 0 path-work
let imsi0 []
foreach vertex0 [ x ->
let element [who] of x
set imsi0 lput element imsi0
]
let imsi00 bl imsi0
let imsi1 []
foreach vertex1 [ x ->
let element [who] of x
set imsi1 lput element imsi1
]
set myroad (map link imsi00 imsi1)
]
end
to set-pm10
; Import daily pollution
let p0 csv:from-file "GIS/jongno_pm10.csv"
let poll-value remove-item 0 p0 ;;remove headers in the csv file
let rep 0 ;; loop
set pm10-back table:make
set pm10-road table:make
foreach poll-value [poll ->
if item 1 poll = "Back" [
let counter item 0 poll ;; counter
let date/hour list (item 2 poll)(item 3 poll) ;; add date and place
let value lput item 4 poll date/hour
let pm10_ lput item 5 poll value
let minute lput item 6 poll pm10_
let wdays lput item 7 poll minute
let isweekend lput item 8 poll wdays
table:put pm10-back counter isweekend
]
if item 1 poll = "Road" [
let counter item 0 poll ;; counter
let date/hour list (item 2 poll)(item 3 poll) ;; add date and place
let value lput item 4 poll date/hour
let pm10_ lput item 5 poll value
let minute lput item 6 poll pm10_
let wdays lput item 7 poll minute
let isweekend lput item 8 poll wdays
table:put pm10-road counter isweekend
]
]
set rep rep + 1
ask patches with [is-research-area? = true and not road_buffer]
[set pm10 (item 2 table:get pm10-back 1) + random-float (item 3 table:get pm10-back 1) set pm10_indoor nobody]
ask patches with [is-research-area? = true and road_buffer = true]
[set pm10 (item 2 table:get pm10-road 1) + random-float (item 3 table:get pm10-road 1)]
end
to set-pm10-others
; Import daily pollution
let p0 csv:from-file "GIS/other_pm10.csv"
let poll-value remove-item 0 p0 ;;remove headers in the csv file
let rep 0 ;; loop
set pm10-others table:make
foreach poll-value [poll ->
if item 1 poll = "Mix" [
let counter item 0 poll ;; counter
let date/hour list (item 2 poll)(item 3 poll) ;; add date and place
let value lput item 4 poll date/hour
let #sd lput item 5 poll value
let minute lput item 6 poll #sd
let wdays lput item 7 poll minute
let isweekend lput item 8 poll wdays
table:put pm10-others counter isweekend
]]
ask patch max-pxcor max-pycor
[set pm10 (item 2 table:get pm10-others 1) + random-float (item 3 table:get pm10-others 1)]
end
to set-resident-cars
let vehicle csv:from-file "GIS/Seoul_Vehicle_sample.csv"
let rawheader item 0 vehicle
let carTT remove-item 0 rawheader
let carstat remove-item 0 vehicle
let districtCar table:make
let districtadminCode table:make
foreach carstat [ code ->
let gasdiesel list (item 2 code)(item 3 code)
let total lput item 4 code gasdiesel
table:put districtCar item 0 code total
table:put districtadminCode item 0 code item 1 code
]
foreach table:keys districtCar [ v ->
let carGroupID 0
foreach table:get districtCar v [ xx ->
create-cars xx [ ;; resident car ratio
set random-car false
set time-at-work 540 + random 61
setupCarGroup carGroupID
set size 2
set district_code v
set district_name table:get districtadminCode v
set shape "car"
set origin one-of nodes with [dong_code = [district_code] of myself]
set destination nobody
set health 300
set unwell_history false
set leave-home-hour 7
set leave-home-mins random 40
]
set carGroupID carGroupID + 1
]]
ask cars [ifelse origin != nobody [move-to origin]
[set origin one-of nodes with [dong_code = 1101054]
move-to origin
]
]
end
to setupCarGroup [Car_ID]
if Car_ID = 0 [set fueltype "Gasoline" set color green]
if Car_ID = 1 [set fueltype "Diesel" set color black + 1]
if Car_ID = 2 [set fueltype "LPG" set color blue + 2]
end
to set-OD ;; Decomposing matrix
let odcar csv:from-file "GIS/od_car.csv"
let rawheader item 0 odcar
let destinationNames remove-item 0 rawheader
let ODMat remove-item 0 odcar
let ODMatrix table:make
foreach ODMat [ origin-chart ->
let number remove-item 0 origin-chart
table:put ODMatrix item 0 origin-chart number
]
foreach table:keys ODMatrix [originName ->
let matrix-loop 0
let Num count cars with [district_code = originName]
let totalUsed 0
let number 0
foreach table:get ODMatrix originName [ percent ->
let newDestination item matrix-loop destinationNames
ifelse (newDestination != 1102060)
[set number precision (percent * Num) 0 set totalUsed totalUsed + number]
[set number Num - totalUsed]
let carsRemaining cars with [district_code = originName and destination = nobody]
ask n-of number carsRemaining [
set destination one-of patches with [dong-code = newDestination and centroid? = true and intersection != true]
set myoffice [building_info] of destination
set goal [b_entrance] of destination
ifelse [close-to-road?] of myoffice = true [ set work-near-roads? true ][ set work-near-roads? false ]
set path-work []
set path-home []
set myroad []
while [goal = origin] [
set destination one-of patches with [dong-code = newDestination and centroid? = true and intersection != true]
set goal [b_entrance] of destination
set myoffice [building_info] of destination
ifelse [close-to-road?] of myoffice = true [ set work-near-roads? true ][ set work-near-roads? false ]
]
]
set matrix-loop matrix-loop + 1
]
output-type totalused output-type " " output-type Num output-type " " output-print originName
]
;;;; Kill cars for the time being
ask cars with [destination = nobody][die]
type (word "Cars not able to find their destinations were killed for the time being" "\n"
"Flying cars might appear on Sat&Sun Nights: Simplifying the 'head home' procedure after a day trip")
;;;; Change vehicle's goal if loaded at endpoints
ask cars with [origin = [endpoint?] of nodes ][move-to one-of nodes in-radius 10 with [endpoint? = false] set endpoint? nodes-here]
ask cars [
if goal = [endpoint?] of nodes [ set goal one-of nodes with [endpoint? = false]
]]
end
to to-work-setup
;initialisation
set parked false
set to-node goal
set current-link item 0 myroad
ifelse ([end1] of current-link = origin) [set to-node [end2] of current-link]
[set to-node [end1] of current-link]
face to-node
set link-counter 0
set direction 1
end
to to-home-setup
;initialisation
set parked false
set to-node origin
set current-link last myroad
ifelse ([end1] of current-link = goal) [set to-node [end2] of current-link]
[set to-node [end1] of current-link]
face to-node
set link-counter (length myroad) - 1
set direction -1
end
to set-resident-driver
ask cars with [not random-car] [
let c self
hatch-drivers 1 [
set shape "person business"
set commute_method "Drive"
set my_car c
set age 25 + random 60
set health 300
;set work-near-roads? [work-near-roads?] of c
]
let my_driver item 0 [self] of drivers-on patch-here
if [my_car] of my_driver = c [set owner my_driver]]
;ask drivers with [work-near-roads? != true][set work-near-roads? false]
ask drivers [set hidden? true ]
end
to set-subway-commuters
create-employees no-of-employees [
set size 2
set shape "person business"
set arrived? false
set origin one-of s_entrances
set origin_patch [patch-here] of origin
set myhome (patch max-pxcor max-pycor)
set goal nobody
set Heuristic 0
set current 0
set direction 0
set arrive-tick 0
set leave-home-hour nobody
set leave-home-mins random 60
set health 300
]
ask n-of (count employees * .9) employees [
set leave-home-hour (6 + random 3)
]
ask employees
[
move-to patch max-pxcor max-pycor
if leave-home-hour = nobody [ set leave-home-hour (8 + random 4) ]
let p 0
ask origin [set p one-of buildings in-radius 20 with [ is-research-area? = true]]
set myoffice p
set goal [patch-here] of p
set work-near-roads? [close-to-road?] of myoffice = true
]
end
to set-incoming-traffic
let p0 csv:from-file "GIS/traffic_calibration.csv"
let traff-value remove-item 0 p0 ;;remove headers in the csv file
set t_sajik table:make
set t_jahamoon table:make
set t_daesagwan table:make
set t_daehak table:make
set t_jongno table:make
set t_dongho table:make
set t_ns2ho table:make
set t_soparo table:make
set t_saemunan table:make
set t_ssm table:make
foreach traff-value [traff ->
if item 1 traff = "A-02" [
let counter item 0 traff ;; counter
let date/hour list (item 2 traff)(item 3 traff) ;; add date and place
let #count lput item 4 traff date/hour
let weekday lput item 5 traff #count
table:put t_sajik counter weekday
]
if item 1 traff = "A-03" [
let counter item 0 traff ;; counter
let date/hour list (item 2 traff)(item 3 traff) ;; add date and place
let #count lput item 4 traff date/hour
let weekday lput item 5 traff #count
table:put t_jahamoon counter weekday
]
if item 1 traff = "A-04" [
let counter item 0 traff ;; counter
let date/hour list (item 2 traff)(item 3 traff) ;; add date and place
let #count lput item 4 traff date/hour
let weekday lput item 5 traff #count
table:put t_daesagwan counter weekday
]
if item 1 traff = "A-07" [
let counter item 0 traff ;; counter
let date/hour list (item 2 traff)(item 3 traff) ;; add date and place
let #count lput item 4 traff date/hour
let weekday lput item 5 traff #count
table:put t_daehak counter weekday
]
if item 1 traff = "A-08" [
let counter item 0 traff ;; counter
let date/hour list (item 2 traff)(item 3 traff) ;; add date and place
let #count lput item 4 traff date/hour
let weekday lput item 5 traff #count
table:put t_jongno counter weekday
]
if item 1 traff = "A-10" [
let counter item 0 traff ;; counter
let date/hour list (item 2 traff)(item 3 traff) ;; add date and place
let #count lput item 4 traff date/hour
let weekday lput item 5 traff #count
table:put t_dongho counter weekday
]
if item 1 traff = "A-21" [
let counter item 0 traff ;; counter
let date/hour list (item 2 traff)(item 3 traff) ;; add date and place
let #count lput item 4 traff date/hour
let weekday lput item 5 traff #count
table:put t_ns2ho counter weekday
]
if item 1 traff = "A-24" [
let counter item 0 traff ;; counter
let date/hour list (item 2 traff)(item 3 traff) ;; add date and place
let #count lput item 4 traff date/hour
let weekday lput item 5 traff #count
table:put t_soparo counter weekday
]
if item 1 traff = "A-14" [
let counter item 0 traff ;; counter
let date/hour list (item 2 traff)(item 3 traff) ;; add date and place
let #count lput item 4 traff date/hour
let weekday lput item 5 traff #count
table:put t_saemunan counter weekday
]
if item 1 traff = "A-16" [
let counter item 0 traff ;; counter
let date/hour list (item 2 traff)(item 3 traff) ;; add date and place
let #count lput item 4 traff date/hour
let weekday lput item 5 traff #count
table:put t_ssm counter weekday
]
]
extra-setting
end
to extra-setting
set v_sajik []
let k_sajik table:keys t_sajik
foreach k_sajik
[triggers -> repeat 60 [
let carcounts item 2 table:get t_sajik triggers
set v_sajik lput carcounts v_sajik ]]
set v_jahamoon []
let k_jahamoon table:keys t_jahamoon
foreach k_jahamoon
[triggers -> repeat 60 [
let carcounts item 2 table:get t_jahamoon triggers
set v_jahamoon lput carcounts v_jahamoon ]]
set v_daesagwan []
let k_daesagwan table:keys t_daesagwan
foreach k_daesagwan
[triggers -> repeat 60 [
let carcounts item 2 table:get t_daesagwan triggers
set v_daesagwan lput carcounts v_daesagwan ]]
set v_daehak []
let k_daehak table:keys t_daehak
foreach k_daehak
[triggers -> repeat 60 [
let carcounts item 2 table:get t_daehak triggers
set v_daehak lput carcounts v_daehak ]]
set v_jongno []
let k_jongno table:keys t_jongno
foreach k_jongno
[triggers -> repeat 60 [
let carcounts item 2 table:get t_jongno triggers
set v_jongno lput carcounts v_jongno ]]
set v_dongho []
let k_dongho table:keys t_dongho
foreach k_dongho
[triggers -> repeat 60 [
let carcounts item 2 table:get t_dongho triggers
set v_dongho lput carcounts v_dongho ]]
set v_ns2ho []
let k_ns2ho table:keys t_ns2ho
foreach k_ns2ho
[triggers -> repeat 60 [
let carcounts item 2 table:get t_ns2ho triggers
set v_ns2ho lput carcounts v_ns2ho ]]
set v_soparo []
let k_soparo table:keys t_soparo
foreach k_soparo
[triggers -> repeat 60 [
let carcounts item 2 table:get t_soparo triggers
set v_soparo lput carcounts v_soparo ]]
set v_saemunan []
let k_saemunan table:keys t_saemunan
foreach k_saemunan
[triggers -> repeat 60 [
let carcounts item 2 table:get t_saemunan triggers
set v_saemunan lput carcounts v_saemunan ]]
set v_ssm []
let k_ssm table:keys t_ssm
foreach k_ssm
[triggers -> repeat 60 [
let carcounts item 2 table:get t_ssm triggers
set v_ssm lput carcounts v_ssm ]]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;GO;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to move [dist] ;;
set current one-of nodes-here
let dxnode distance to-node
ifelse dxnode > dist [forward dist] [
let nextlinks [my-links] of to-node
ifelse count nextlinks = 1
[ set-next-car-link current-link to-node ]
[ set-next-car-link one-of nextlinks with [self != [current-link] of myself] to-node]
move dist - dxnode
set-emission
]
end
to set-next-car-link [way n]
set current-link way
move-to n
ifelse n = [end1] of way [set to-node [end2] of way] [set to-node [end1] of way]
face to-node
end
to travel [dist]
set current one-of nodes-here ;; if "current" isn't assigned, then the vehicles will fly everywhere..
let dxnode distance to-node
ifelse dxnode > dist
[forward dist]
[
move-to to-node
ifelse (direction = 1 and to-node != goal) or (direction = -1 and to-node != origin)
[
set link-counter link-counter + direction
set current-link item link-counter myroad
ifelse ([end1] of current-link = to-node) [set to-node [end2] of current-link]
[set to-node [end1] of current-link]
face to-node
travel dist - dxnode
set-emission
]
[ set speed 0
park
]
]
end