This repository was archived by the owner on Apr 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraffic Grid.nlogo
More file actions
1640 lines (1418 loc) · 45 KB
/
Traffic Grid.nlogo
File metadata and controls
1640 lines (1418 loc) · 45 KB
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
[
counter ;; for ids
grid-x-inc ;; the amount of patches in between two roads in the x direction
grid-y-inc ;; the amount of patches in between two roads in the y direction
acceleration ;; the constant that controls how much a car speeds up or slows down by if
;; it is to accelerate or decelerate
phase ;; keeps track of the phase
num-cars-stopped ;; the number of cars that are stopped during a single pass thru the go procedure
current-light ;; the currently selected light
;; patch agentsets
intersections ;; agentset containing the patches that are intersections
roads ;; agentset containing the patches that are roads
objective-counter ;; number of objectives done
;; Things bellow are for A*
p-valids ;; Valid Patches for moving not wall)
Start ;; Starting patch
Final-Cost ;; The final cost of the path given by A*
]
breed [ cars car ]
cars-own
[
id
speed ;; the speed of the turtle
up-car? ;; true if the turtle moves downwards and false if it moves to the right
wait-time ;; the amount of time since the last time a turtle has moved
objective ;;
objectives
next-cross
i-go-first
persistence
conflicting-cars
conflicting-car
path
next
]
patches-own
[
intersection? ;; true if the patch is at the intersection of two roads
green-light-up? ;; true if the green light is above the intersection. otherwise, false.
;; false for a non-intersection patches.
my-row ;; the row of the intersection counting from the upper left corner of the
;; world. -1 for non-intersection patches.
my-column ;; the column of the intersection counting from the upper left corner of the
;; world. -1 for non-intersection patches.
my-phase ;; the phase for the intersection. -1 for non-intersection patches.
auto? ;; whether or not this intersection will switch automatically.
;; false for non-intersection patches.
;; Things bellow are for A*
father ;; Previous patch in this partial path
Cost-path ;; Stores the cost of the path to the current patch
visited? ;; has the path been visited previously? That is,
;; at least one path has been calculated going through this patch
active? ;; is the patch active? That is, we have reached it, but
;; we must consider it because its children have not been explored
]
;;;;;;;;;;;;;;;;;;;;;;
;; Setup Procedures ;;
;;;;;;;;;;;;;;;;;;;;;;
;; Initialize the display by giving the global and patch variables initial values.
;; Create num-cars of turtles if there are enough road patches for one turtle to
;; be created per road patch. Set up the plots.
to setup
clear-all
setup-globals
;; First we ask the patches to draw themselves and set up a few variables
setup-patches
make-current one-of intersections
label-current
set-default-shape turtles "car"
if (num-cars > count roads)
[
user-message (word "There are too many cars for the amount of "
"road. Either increase the amount of roads "
"by increasing the GRID-SIZE-X or "
"GRID-SIZE-Y sliders, or decrease the "
"number of cars by lowering the NUMBER slider.\n"
"The setup has stopped.")
stop
]
;; Now create the turtles and have each created turtle call the functions setup-cars and set-car-color
create-cars num-cars
[
setup-cars
set-initial-car-color
record-data
setObjective
setPath
]
;; give the turtles an initial speed
ask cars [ set-car-speed
if not (intersection?)
[set next-cross get-next-crossing
;ask next-cross [set pcolor black]
]
]
ask cars [send-message "update" self]
ask cars [set-conflicting-car]
ask cars[set-passing-first-variable]
reset-ticks
end
;; Initialize the global variables to appropriate values
to setup-globals
set counter 0
set objective-counter 0
set current-light nobody ;; just for now, since there are no lights yet
set phase 0
set num-cars-stopped 0
set grid-x-inc world-width / grid-size-x
set grid-y-inc world-height / grid-size-y
;; don't make acceleration 0.1 since we could get a rounding error and end up on a patch boundary
set acceleration 0.099
end
;; Make the patches have appropriate colors, set up the roads and intersections agentsets,
;; and initialize the traffic lights to one setting
to setup-patches
;; initialize the patch-owned variables and color the patches to a base-color
ask patches
[
set intersection? false
set auto? false
set green-light-up? true
set my-row -1
set my-column -1
set my-phase -1
set pcolor brown + 3
set father nobody
set Cost-path 0
set visited? false
set active? false
]
;; initialize the global variables that hold patch agentsets
set roads patches with
[(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) or
(floor((pycor + max-pycor) mod grid-y-inc) = 0)]
set intersections roads with
[(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) and
(floor((pycor + max-pycor) mod grid-y-inc) = 0)]
ask roads [ set pcolor white ]
setup-intersections
end
;; Give the intersections appropriate values for the intersection?, my-row, and my-column
;; patch variables. Make all the traffic lights start off so that the lights are red
;; horizontally and green vertically.
to setup-intersections
ask intersections
[
set intersection? true
set green-light-up? true
set my-phase 0
set auto? true
set my-row floor((pycor + max-pycor) / grid-y-inc)
set my-column floor((pxcor + max-pxcor) / grid-x-inc)
set-signal-colors
]
end
;; Initialize the turtle variables to appropriate values and place the turtle on an empty road patch.
to setup-cars ;; turtle procedure
set id counter
set counter (counter + 1)
set conflicting-cars 0
set persistence 0
set speed 0
set wait-time 0
set next 0
put-on-empty-road
ifelse intersection?
[
ifelse random 2 = 0
[ set up-car? true ]
[ set up-car? false ]
]
[
; if the turtle is on a vertical road (rather than a horizontal one)
ifelse (floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0)
[ set up-car? true ]
[ set up-car? false ]
]
ifelse up-car?
[ set heading 180 ]
[ set heading 90 ]
end
;; Find a road patch without any turtles on it and place the turtle there.
to put-on-empty-road ;; turtle procedure
move-to one-of roads with [(not any? turtles-on self) and (intersection? = false)]
end
;;;;;;;;;;;;;;;;;;;;;;;;
;; Runtime Procedures ;;
;;;;;;;;;;;;;;;;;;;;;;;;
;; Run the simulation
to go
update-current
;; have the intersections change their color
set-signals
set num-cars-stopped 0
;; set the turtles speed for this time thru the procedure, move them forward their speed,
;; record data for plotting, and set the color of the turtles to an appropriate color
;; based on their speed
ask cars [
if not (color = yellow)
[set color blue]
set-car-speed
nextPatch
record-data
set-car-color
if test-objective
[setObjective
set color yellow
set objective-counter objective-counter + 1
show objective-counter
setPath]
;;3 kinds of agents: blind agents plan before go, struggler agents replan when they have a lot of other cars around, while provident agents plan from time to time during its trip
let struggle (count turtles in-radius 2) > 3 and agent_commitment = "Struggler Agent" ;;in this case the agent must be in a struggle to try to replan
let time-to-replan ticks mod (perseverance + 1) = id mod (perseverance + 1) and agent_commitment = "Provident Agent" ;;in this case the agent replan from time to time
if struggle
[ ifelse persistence = 0
[ if not (color = yellow)
[set color red]
setPath
set persistence Perseverance]
[set persistence persistence - 1]
]
if time-to-replan
[if not (color = yellow)
[set color red]
setPath]
if not (intersection?) and not (get-next-crossing = next-cross)
[;ask next-cross [set pcolor white]
set next-cross get-next-crossing
set conflicting-cars 0
send-message "update" self
;ask next-cross [set pcolor black]
]
;;if next = 0 or [pxcor] of patch-here >= [pxcor] of next or [pycor] of patch-here <= [pycor] of next or ([pxcor] of patch-here = max-pxcor and [pxcor] of next = min-pxcor) or ([pycor] of patch-here = min-pycor and [pycor] of next = max-pycor)
]
;ask cars[
; if [pxcor] of next = [pxcor] of objective and [pycor] of next = [pycor] of objective and length path = 0
; [setPath
; set next 0]
;]
;;to tests number of initial conflicting cars
;show "Info:"
;ask cars [
;if conflicting-cars != 0
; [show count conflicting-cars]]
ask cars [set-conflicting-car]
ask cars [if not (intersection?)
[set-passing-first-variable]]
ask cars [adjust-velocity]
;; update the phase and the global clock
next-phase
tick
end
to choose-current
if mouse-down?
[
let x-mouse mouse-xcor
let y-mouse mouse-ycor
if [intersection?] of patch x-mouse y-mouse
[
update-current
unlabel-current
make-current patch x-mouse y-mouse
label-current
stop
]
]
end
;; Set up the current light and the interface to change it.
to make-current [light]
set current-light light
set current-phase [my-phase] of current-light
set current-auto? [auto?] of current-light
end
;; update the variables for the current light
to update-current
ask current-light [
set my-phase current-phase
set auto? current-auto?
]
end
;; label the current light
to label-current
ask current-light
[
ask patch-at -1 1
[
set plabel-color black
set plabel "current"
]
]
end
;; unlabel the current light (because we've chosen a new one)
to unlabel-current
ask current-light
[
ask patch-at -1 1
[
set plabel ""
]
]
end
;; have the traffic lights change color if phase equals each intersections' my-phase
to set-signals
ask intersections with [auto? and phase = floor ((my-phase * ticks-per-cycle) / 100)]
[
set green-light-up? (not green-light-up?)
set-signal-colors
]
end
;; This procedure checks the variable green-light-up? at each intersection and sets the
;; traffic lights to have the green light up or the green light to the left.
to set-signal-colors ;; intersection (patch) procedure
ifelse power?
[
ifelse green-light-up?
[
ask patch-at -1 0 [ set pcolor red ]
ask patch-at 0 1 [ set pcolor green ]
]
[
ask patch-at -1 0 [ set pcolor green ]
ask patch-at 0 1 [ set pcolor red ]
]
]
[
ask patch-at -1 0 [ set pcolor white ]
ask patch-at 0 1 [ set pcolor white ]
]
end
;; set the turtles' speed based on whether they are at a red traffic light or the speed of the
;; turtle (if any) on the patch in front of them
to set-car-speed ;; turtle procedure
ifelse pcolor = red
[ set speed 0 ]
[
ifelse up-car?
[ set-speed 0 -1 ]
[ set-speed 1 0 ]
]
end
;changes the turtle direction
to change-direction
if intersection?
[
set up-car? (not up-car?)
ifelse up-car?
[ move-to patch-here
set heading 180
]
[ move-to patch-here
set heading 90 ]
]
end
to nextPatch
ifelse not empty? path
[set next item 0 path
if intersection?
[
ifelse heading = 90
[if pxcor = [pxcor] of next
[change-direction]]
[if pycor = [pycor] of next
[change-direction]]
]
ifelse 18 = pxcor
[ifelse -18 = pycor
[if pxcor > [pxcor] of next or pycor < [pycor] of next [fd speed]]
[if pxcor > [pxcor] of next or pycor > [pycor] of next [fd speed]]]
[ifelse -18 = pycor
[if pxcor < [pxcor] of next or pycor < [pycor] of next [fd speed]]
[if pxcor < [pxcor] of next or pycor > [pycor] of next [fd speed]]]
set path but-first path]
[setPath]
end
;; set the speed variable of the car to an appropriate value (not exceeding the
;; speed limit) based on whether there are cars on the patch in front of the car
to set-speed [ delta-x delta-y ] ;; turtle procedure
;; get the turtles on the patch in front of the turtle
let turtles-ahead turtles-at delta-x delta-y
let turtles-more-ahead turtles-at delta-x delta-y
ifelse up-car?
[set turtles-more-ahead (turtle-set turtles-at delta-x (delta-y - 1) turtles-at delta-x (delta-y - 2))]
[set turtles-more-ahead (turtle-set turtles-at (delta-x + 1) delta-y turtles-at (delta-x + 2) delta-y)]
;; if there are turtles in front of the turtle, slow down
;; otherwise, speed up
ifelse any? turtles-ahead
[
ifelse any? (turtles-ahead with [ up-car? != [up-car?] of myself ])
[
set speed 0
]
[
set speed [speed] of one-of turtles-ahead
slow-down
]
]
[
ifelse any? turtles-more-ahead with [ [speed] of myself < speed]
[set speed [speed] of one-of turtles-more-ahead
slow-down]
[speed-up] ]
end
;; decrease the speed of the turtle
to slow-down ;; turtle procedure
set speed speed / 2
if speed <= 0 ;;if speed < 0
[ set speed 0 ]
end
;; increase the speed of the turtle
to speed-up ;; turtle procedure
ifelse speed > speed-limit
[ set speed speed-limit ]
[ set speed speed + acceleration ]
end
;; set the color of the turtle to a different color based on how fast the turtle is moving
to set-initial-car-color ;; turtle procedure
ifelse speed < (speed-limit / 2)
[ set color blue ]
[ set color cyan - 2 ]
end
;; set the color of the turtle to a different color based on how fast the turtle is moving
to set-car-color ;; turtle procedure
if (not (color = yellow) and not (color = red))
[ifelse speed < (speed-limit / 2)
[ set color blue ]
[ set color cyan - 2 ]
]
end
;; keep track of the number of stopped turtles and the amount of time a turtle has been stopped
;; if its speed is 0
to record-data ;; turtle procedure
ifelse speed = 0
[
set num-cars-stopped num-cars-stopped + 1
set wait-time wait-time + 1
]
[ set wait-time 0 ]
end
to change-current
ask current-light
[
set green-light-up? (not green-light-up?)
set-signal-colors
]
end
;; cycles phase to the next appropriate value
to next-phase
;; The phase cycles from 0 to ticks-per-cycle, then starts over.
set phase phase + 1
if phase mod ticks-per-cycle = 0
[ set phase 0 ]
end
to setObjective
set objective one-of roads
while [pxcor = [pxcor] of objective and pycor = [pycor] of objective]
[ set objective one-of roads ]
end
to setPath
set path A* patch-here objective roads
end
to-report test-objective
ifelse pxcor = [pxcor] of objective and pycor = [pycor] of objective
[report true]
[report false]
end
;Things below are for negotiation
to-report get-next-crossing
let c-x-cord pxcor
let c-y-cord pycor
ifelse up-car?
[let int-temp intersections with [pxcor = c-x-cord and pycor < c-y-cord]
ifelse count int-temp = 0
[report intersections with [pxcor = c-x-cord] with-max [pycor]]
[report int-temp with-max [pycor]]
]
[let int-temp intersections with [pycor = c-y-cord and pxcor > c-x-cord]
ifelse count int-temp = 0
[report intersections with [pycor = c-y-cord] with-min [pxcor]]
[report int-temp with-min [pxcor]]
]
end
;;; Send a message to all cars
;;; Sends the next crossing patch, the car turtle with all info
to send-message [msg car-info]
ask cars [new-message msg car-info false]
end
;;;
;;; Send a message to a specified car
;;; Sends the next crossing patch, the car turtle with all info
to send-message-to-car [id-car msg car-info]
ask cars [
if id-car = id
[new-message msg car-info true]]
end
;;;
;;; Handle a new received message
;;;
to new-message [msg car-info is-return-message]
if(msg = "update")
[
set-conflicting-cars car-info is-return-message
]
end
to set-conflicting-cars [car-info is-return-message]
;remove past conflict car
if conflicting-cars != 0
[if any? conflicting-cars with [ [id] of car-info = [id] of self ]
[set conflicting-cars conflicting-cars with [ [id] of car-info != [id] of self]]]
if is-conflicting-car car-info
[ifelse conflicting-cars != 0
[if not (any? conflicting-cars with [ [pxcor] of self = [pxcor] of car-info and [pycor] of self = [pycor] of car-info ])
[set conflicting-cars (turtle-set conflicting-cars car-info)
if not is-return-message
[send-message-to-car [id] of car-info "update" self]]]
[set conflicting-cars (turtle-set car-info)
if not is-return-message
[send-message-to-car [id] of car-info "update" self]
]
]
end
to set-conflicting-car
let my-number 1
let iterate-cars conflicting-cars
let temp-car 0
ifelse conflicting-cars != 0
[ask conflicting-cars
[
if up-car? and [up-car?] of myself
[if pycor < [pycor] of myself
[set my-number my-number + 1]]
if not up-car? and not [up-car?] of myself
[if pxcor > [pxcor] of myself
[set my-number my-number + 1]]
]
ask conflicting-cars
[
if not up-car? and [up-car?] of myself
[set iterate-cars iterate-cars with [ not [up-car?] of self]
while [my-number > 0 and count (iterate-cars with-max [pxcor]) != 0]
[set temp-car one-of (iterate-cars with-max [pxcor] )
set iterate-cars iterate-cars with [ [id] of temp-car != [id] of self]
set my-number my-number - 1]]
if up-car? and not [up-car?] of myself
[set iterate-cars iterate-cars with [ [up-car?] of self]
while [my-number > 0 and count (iterate-cars with-min [pycor]) != 0]
[set temp-car one-of (iterate-cars with-min [pycor] )
set iterate-cars iterate-cars with [ [id] of temp-car != [id] of self]
set my-number my-number - 1]]
]
ifelse temp-car != 0 and my-number = 0
[set conflicting-car temp-car]
[set conflicting-car 0]
;show "lol"
;show conflicting-car
]
[set conflicting-car 0]
end
to set-passing-first-variable
let my-dis 0
let conf-dis 0
let conf-id id
if conflicting-car != 0
[let conf-car one-of cars with [id = conf-id ]
ifelse up-car?
[set my-dis one-of [pycor] of next-cross - pycor
set conf-dis one-of [pxcor] of next-cross - [pxcor] of conf-car]
[set my-dis one-of [pxcor] of next-cross - pxcor
set conf-dis one-of [pycor] of next-cross - [pycor] of conf-car]
if my-dis < 0
[set my-dis my-dis * -1]
if conf-dis < 0
[set conf-dis conf-dis * -1]
ifelse speed != 0 and [speed] of conflicting-car != 0
[ifelse (my-dis / speed) < (conf-dis / [speed] of conf-car) or ((my-dis / speed) = (conf-dis / [speed] of conf-car) and id < [id] of conf-car)
[set i-go-first true
ask cars[
if id = [id] of [conflicting-car] of myself
[set i-go-first false]
]]
[set i-go-first false
ask cars[
if id = [id] of [conflicting-car] of myself
[set i-go-first true]
]]]
[ifelse my-dis < conf-dis or (my-dis = conf-dis and id < [id] of conf-car)
[set i-go-first true
ask cars[
if id = [id] of [conflicting-car] of myself
[set i-go-first false]
]]
[set i-go-first false
ask cars[
if id = [id] of [conflicting-car] of myself
[set i-go-first true]
]]]
]
if conflicting-car = 0
[set i-go-first true]
end
to adjust-velocity
let my-dis 0
let conf-dis 0
let my-dis-front 0
let conf-id id
if conflicting-car != 0
[let conf-car one-of cars with [id = conf-id]
ifelse up-car?
[set my-dis one-of [pycor] of next-cross - pycor
set conf-dis one-of [pxcor] of next-cross - [pxcor] of conf-car
set my-dis-front one-of [pycor] of next-cross - (pycor - 1)]
[set my-dis one-of [pxcor] of next-cross - pxcor
set conf-dis one-of [pycor] of next-cross - [pycor] of conf-car
set my-dis-front one-of [pxcor] of next-cross - (pxcor + 1)]
if my-dis < 0
[set my-dis my-dis * -1]
if conf-dis < 0
[set conf-dis conf-dis * -1]
if speed != 0 and [speed] of conf-car != 0
[if ((my-dis / speed) = (conf-dis / [speed] of conf-car) and i-go-first = false) or ( (my-dis-front / speed) = (conf-dis / [speed] of conf-car) and i-go-first = false)
[slow-down]]
]
end
to-report is-conflicting-car [car-info]
ifelse id != [id] of car-info and ([pxcor] of next-cross = [pxcor] of ([next-cross] of car-info)) and ([pycor] of next-cross = [pycor] of ([next-cross] of car-info))
[report true
]
[report false]
end
to-report is-in-front-car [car-info]
ifelse id != [id] of car-info and ((not up-car? and ([pxcor] of next-cross > [pxcor] of car-info) and (pycor = [pycor] of car-info)) or (up-car? and (pxcor = [pxcor] of car-info) and ([pycor] of next-cross < [pycor] of car-info)))
[report true]
[report false]
end
; Things bellow are for A*
; Patch report to estimate the total expected cost of the path starting from1
; in Start, passing through it, and reaching the #Goal
to-report Total-expected-cost [#Goal]
report Cost-path + Heuristic #Goal
end
; Patch report to reurtn the heuristic (expected length) from the current patch
; to the #Goal
to-report Heuristic [#Goal]
let val (distance #Goal)
if HeuristicOption = "NumberOfNearTurtles+Distance"
[set val val + ((count turtles in-radius 2))]
report val
end
; A* algorithm. Inputs:
; - #Start : starting point of the search.
; - #Goal : the goal to reach.
; - #valid-map : set of agents (patches) valid to visit.
; Returns:
; - If there is a path : list of the agents of the path.
; - Otherwise : false
to-report A* [#Start #Goal #valid-map]
; clear all the information in the agents
ask #valid-map with [visited?]
[
set father nobody
set Cost-path 0
set visited? false
set active? false
]
; Active the staring point to begin the searching loop
ask #Start
[
set father self
set visited? true
set active? true
]
; exists? indicates if in some instant of the search there are no options to
; continue. In this case, there is no path connecting #Start and #Goal
let exists? true
; The searching loop is executed while we don't reach the #Goal and we think
; a path exists
while [not [visited?] of #Goal and exists?]
[
; We only work on the valid pacthes that are active
let options #valid-map with [active?]
; If any
ifelse any? options
[
; Take one of the active patches with minimal expected cost
ask min-one-of options [Total-expected-cost #Goal]
[
; Store its real cost (to reach it) to compute the real cost
; of its children
let Cost-path-father Cost-path
; and deactivate it, because its children will be computed right now
set active? false
; Compute its valid neighbors
let #car self
let valid-neighbors neighbors with [member? self #valid-map]
ifelse 18 = pxcor
[ifelse -18 = pycor
[set valid-neighbors valid-neighbors with [([pxcor] of #car - [pxcor] of self > 0 and [pycor] of #car = [pycor] of self) or ([pycor] of #car - [pycor] of self < 0 and [pxcor] of #car = [pxcor] of self)]]
[set valid-neighbors valid-neighbors with [([pxcor] of #car - [pxcor] of self > 0 and [pycor] of #car = [pycor] of self) or ([pycor] of #car - [pycor] of self > 0 and [pxcor] of #car = [pxcor] of self)]]]
[ifelse -18 = pycor
[set valid-neighbors valid-neighbors with [([pxcor] of #car - [pxcor] of self < 0 and [pycor] of #car = [pycor] of self) or ([pycor] of #car - [pycor] of self < 0 and [pxcor] of #car = [pxcor] of self)]]
[set valid-neighbors valid-neighbors with [([pxcor] of #car - [pxcor] of self < 0 and [pycor] of #car = [pycor] of self) or ([pycor] of #car - [pycor] of self > 0 and [pxcor] of #car = [pxcor] of self)]]]
ask valid-neighbors
[
; There are 2 types of valid neighbors:
; - Those that have never been visited (therefore, the
; path we are building is the best for them right now)
; - Those that have been visited previously (therefore we
; must check if the path we are building is better or not,
; by comparing its expected length with the one stored in
; the patch)
; One trick to work with both type uniformly is to give for the
; first case an upper bound big enough to be sure that the new path
; will always be smaller.
let t ifelse-value visited? [ Total-expected-cost #Goal] [2 ^ 20]
; If this temporal cost is worse than the new one, we substitute the
; information in the patch to store the new one (with the neighbors
; of the first case, it will be always the case)
if t > (Cost-path-father + distance myself + Heuristic #Goal)
[
; The current patch becomes the father of its neighbor in the new path
set father myself
set visited? true
set active? true
; and store the real cost in the neighbor from the real cost of its father
set Cost-path Cost-path-father + distance father
set Final-Cost precision Cost-path 3
]
]
]
]
; If there are no more options, there is no path between #Start and #Goal
[
set exists? false
]
]
; After the searching loop, if there exists a path
ifelse exists?
[
; We extract the list of patches in the path, form #Start to #Goal
; by jumping back from #Goal to #Start by using the fathers of every patch
let current #Goal
set Final-Cost (precision [Cost-path] of #Goal 3)
let rep (list current)
While [current != #Start]
[
set current [father] of current
set rep fput current rep
]
report rep
]
[
; Otherwise, there is no path, and we return False
report false
]
end
; Copyright 2003 Uri Wilensky.
; See Info tab for full copyright and license.
@#$#@#$#@
GRAPHICS-WINDOW
327
10
668
352
-1
-1
9.0
1
12
1
1
1
0
1
1
1
-18
18
-18
18
1
1
1
ticks
30.0
PLOT
453
377
671
541
Average Wait Time of Cars
Time
Average Wait
0.0
100.0
0.0
5.0
true
false
"" ""
PENS
"default" 1.0 0 -16777216 true "" "plot mean [wait-time] of turtles"
PLOT
228
377
444
542
Average Speed of Cars
Time
Average Speed
0.0
100.0
0.0
1.0
true
false
"set-plot-y-range 0 speed-limit" ""
PENS
"default" 1.0 0 -16777216 true "" "plot mean [speed] of turtles"
SLIDER
108
35
205
68
grid-size-y
grid-size-y
1
9
9.0
1
1
NIL
HORIZONTAL
SLIDER
12
35
106
68
grid-size-x
grid-size-x
1
9
9.0
1
1
NIL
HORIZONTAL
SWITCH
12
107
107
140
power?
power?
1
1
-1000
SLIDER
12
71
293
104
num-cars
num-cars
1
400
150.0
1
1
NIL
HORIZONTAL
PLOT
5
376
219
540
Stopped Cars
Time
Stopped Cars
0.0
100.0
0.0
100.0
true
false
"set-plot-y-range 0 num-cars" ""