-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefine_complex_intersection.py
More file actions
1425 lines (1202 loc) · 56 KB
/
define_complex_intersection.py
File metadata and controls
1425 lines (1202 loc) · 56 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
#!/usr/bin/python3
# -*- coding: utf-8
#
# define_complex_intersection.py defines a complex intersection for the
# traffic simulator.
# Copyright © 2025 by John Sauter <John_Sauter@systemeyescomputerstore.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/>.
# The author's contact information is as follows:
# John Sauter
# System Eyes Computer Store
# 20A Northwest Blvd. Ste 345
# Nashua, NH 03063-4066
# telephone: (603) 424-1188
# e-mail: John_Sauter@systemeyescomputerstore.com
import math
import pprint
import fractions
import pathlib
import json
import argparse
parser = argparse.ArgumentParser (
formatter_class=argparse.RawDescriptionHelpFormatter,
description=('Define a traffic intersection.'),
epilog=('Copyright © 2025 by John Sauter' + '\n' +
'License GPL3+: GNU GPL version 3 or later; ' + '\n' +
'see <http://gnu.org/licenses/gpl.html> for the full text ' +
'of the license.' + '\n' +
'This is free software: ' +
'you are free to change and redistribute it. ' + '\n' +
'There is NO WARRANTY, to the extent permitted by law. ' + '\n' +
'\n'))
parser.add_argument ('--version', action='version',
version='define_complex_intersection 0.65 2025-12-25',
help='print the version number and exit')
parser.add_argument ('--trace-file', metavar='trace_file',
help='write trace output to the specified file')
parser.add_argument ('--input-file', metavar='input_file',
help='read the toggle and times names from the ' +
'specified file as JSON')
parser.add_argument ('--output-file', metavar='output_file',
help='write intersection to the specified file as JSON')
parser.add_argument ('--waiting-limit', type=int, metavar='waiting_limit',
help='max wait time before getting green preference ' +
'for turning green; default 60 seconds.')
parser.add_argument ('--verbose', type=int, metavar='verbosity_level',
help='control the amount of output from the program: ' +
'1 is normal, 0 suppresses summary messages')
do_trace = False
trace_file_name = ""
do_input = False
input_file_name = ""
do_output = False
output_file_name = ""
waiting_limit = 60
verbosity_level = 1
error_counter = 0
# Verbosity_level and table level:
# 1 only errors (and statistics if requested)
# 2 add lamp changes, script actions, and vehicles and pedestrians
# arriving, leaving and reaching milestones
# 3 add state changes and blocking
# 4 add toggle and sensor changes
# 5 add lots of other items for debugging
# 6 add tests of toggles
# Parse the command line.
arguments = parser.parse_args ()
arguments = vars(arguments)
if (arguments ['trace_file'] != None):
do_trace = True
trace_file_name = arguments ['trace_file']
trace_file_name = pathlib.Path(trace_file_name)
trace_file = open (trace_file_name, 'w')
if (arguments ['input_file'] != None):
do_input = True
input_file_name = arguments ['input_file']
input_file_name = pathlib.Path(input_file_name)
if (arguments ['output_file'] != None):
do_output = True
output_file_name = arguments ['output_file']
output_file_name = pathlib.Path(output_file_name)
if (arguments ['waiting_limit'] != None):
waiting_limit = arguments ['waiting_limit']
if (arguments ['verbose'] != None):
verbosity_level = int(arguments ['verbose'])
# The conversion factor from miles per hour to feet per second:
mph_to_fps = fractions.Fraction(5280, 60*60)
# Read the finite state machine to get the timer and toggle names.
if (do_input):
input_file = open (input_file_name, 'r')
finite_state_machine = json.load (input_file)
input_file.close()
toggle_names = finite_state_machine["toggles"]
timer_names = finite_state_machine["timer names"]
else:
finite_state_machine = dict()
toggle_names = list()
timer_names = list()
# Build the finite state machines for the signal faces:
signal_face_names = ( "A", "psw", "pse", "B", "C", "D", "E", "pnw", "pne",
"F", "G", "H", "J" )
# Set the duration of each timer in each signal face.
timer_durations = dict()
for signal_face_name in ("A", "psw", "pse", "D", "E", "pnw", "pne", "H", "J"):
timer_full_name = signal_face_name + "/" + "Red Limit"
timer_durations[timer_full_name] = ("inf",)
for signal_face_name in ("B", "C", "F", "G"):
timer_full_name = signal_face_name + "/" + "Left Flashing Yellow Waiting"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Left Flashing Yellow Limit"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Minimum Left Flashing Yellow"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Red Limit"
timer_durations[timer_full_name] = ("60.000",)
timer_full_name = signal_face_name + "/" + "Maximum Green"
timer_durations[timer_full_name] = ("60.000",)
timer_full_name = signal_face_name + "/" + "Minimum Green"
timer_durations[timer_full_name] = ("12.000",)
timer_full_name = signal_face_name + "/" + "Passage"
timer_durations[timer_full_name] = ("3.500", ("1.000", "Maximum Green",
"10.000", "7.000"))
timer_full_name = signal_face_name + "/" + "Red Clearance"
timer_durations[timer_full_name] = ("1.000",)
timer_full_name = signal_face_name + "/" + "Green Limit"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Yellow Change"
timer_durations[timer_full_name] = ("5.000",)
timer_full_name = signal_face_name + "/" + "Green Delay Approaching"
timer_durations[timer_full_name] = ("0.000",)
timer_full_name = signal_face_name + "/" + "Green Delay Present"
timer_durations[timer_full_name] = ("0.000",)
for signal_face_name in ("A", "E"):
timer_full_name = signal_face_name + "/" + "Left Flashing Yellow Waiting"
timer_durations[timer_full_name] = ("15.000",)
timer_full_name = signal_face_name + "/" + "Left Flashing Yellow Limit"
timer_durations[timer_full_name] = ("45.000",)
timer_full_name = signal_face_name + "/" + "Minimum Left Flashing Yellow"
timer_durations[timer_full_name] = ("5.000",)
timer_full_name = signal_face_name + "/" + "Maximum Green"
timer_durations[timer_full_name] = ("20.000",)
timer_full_name = signal_face_name + "/" + "Minimum Green"
timer_durations[timer_full_name] = ("5.000",)
timer_full_name = signal_face_name + "/" + "Passage"
timer_durations[timer_full_name] = ("1.900",)
timer_full_name = signal_face_name + "/" + "Red Clearance"
timer_durations[timer_full_name] = ("1.000",)
timer_full_name = signal_face_name + "/" + "Green Limit"
timer_durations[timer_full_name] = ("45.000",)
timer_full_name = signal_face_name + "/" + "Yellow Change"
timer_durations[timer_full_name] = ("3.500",)
timer_full_name = signal_face_name + "/" + "Green Delay Approaching"
timer_durations[timer_full_name] = ("0.000",)
timer_full_name = signal_face_name + "/" + "Green Delay Present"
timer_durations[timer_full_name] = ("0.000",)
for signal_face_name in ("D"):
timer_full_name = signal_face_name + "/" + "Left Flashing Yellow Waiting"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Left Flashing Yellow Limit"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Minimum Left Flashing Yellow"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Maximum Green"
timer_durations[timer_full_name] = ("30.000",)
timer_full_name = signal_face_name + "/" + "Minimum Green"
timer_durations[timer_full_name] = ("7.000",)
timer_full_name = signal_face_name + "/" + "Passage"
timer_durations[timer_full_name] = ("1.900",)
timer_full_name = signal_face_name + "/" + "Red Clearance"
timer_durations[timer_full_name] = ("1.500",)
timer_full_name = signal_face_name + "/" + "Green Limit"
timer_durations[timer_full_name] = ("60.000",)
timer_full_name = signal_face_name + "/" + "Yellow Change"
timer_durations[timer_full_name] = ("3.000",)
timer_full_name = signal_face_name + "/" + "Green Delay Approaching"
timer_durations[timer_full_name] = ("3.000",)
timer_full_name = signal_face_name + "/" + "Green Delay Present"
timer_durations[timer_full_name] = ("3.000",)
for signal_face_name in ("H"):
timer_full_name = signal_face_name + "/" + "Left Flashing Yellow Waiting"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Left Flashing Yellow Limit"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Minimum Left Flashing Yellow"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Maximum Green"
timer_durations[timer_full_name] = ("30.000",)
timer_full_name = signal_face_name + "/" + "Minimum Green"
timer_durations[timer_full_name] = ("7.000",)
timer_full_name = signal_face_name + "/" + "Passage"
timer_durations[timer_full_name] = ("1.900",)
timer_full_name = signal_face_name + "/" + "Red Clearance"
timer_durations[timer_full_name] = ("1.500",)
timer_full_name = signal_face_name + "/" + "Green Limit"
timer_durations[timer_full_name] = ("60.000",)
timer_full_name = signal_face_name + "/" + "Yellow Change"
timer_durations[timer_full_name] = ("3.000",)
timer_full_name = signal_face_name + "/" + "Green Delay Approaching"
timer_durations[timer_full_name] = ("0.000",)
timer_full_name = signal_face_name + "/" + "Green Delay Present"
timer_durations[timer_full_name] = ("0.000",)
for signal_face_name in ("J"):
timer_full_name = signal_face_name + "/" + "Left Flashing Yellow Waiting"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Left Flashing Yellow Limit"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Minimum Left Flashing Yellow"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Maximum Green"
timer_durations[timer_full_name] = ("30.000",)
timer_full_name = signal_face_name + "/" + "Minimum Green"
timer_durations[timer_full_name] = ("7.000",)
timer_full_name = signal_face_name + "/" + "Passage"
timer_durations[timer_full_name] = ("1.900",)
timer_full_name = signal_face_name + "/" + "Red Clearance"
timer_durations[timer_full_name] = ("1.000",)
timer_full_name = signal_face_name + "/" + "Green Limit"
timer_durations[timer_full_name] = ("60.000",)
timer_full_name = signal_face_name + "/" + "Yellow Change"
timer_durations[timer_full_name] = ("3.000",)
timer_full_name = signal_face_name + "/" + "Green Delay Approaching"
timer_durations[timer_full_name] = ("0.000",)
timer_full_name = signal_face_name + "/" + "Green Delay Present"
timer_durations[timer_full_name] = ("0.000",)
for signal_face_name in ("pse", "psw", "pne", "pnw"):
timer_full_name = signal_face_name + "/" + "Left Flashing Yellow Waiting"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Left Flashing Yellow Limit"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Minimum Left Flashing Yellow"
timer_durations[timer_full_name] = ("inf",)
timer_full_name = signal_face_name + "/" + "Maximum Green"
timer_durations[timer_full_name] = ("10.000",)
timer_full_name = signal_face_name + "/" + "Minimum Green"
timer_durations[timer_full_name] = ("6.000",)
timer_full_name = signal_face_name + "/" + "Passage"
timer_durations[timer_full_name] = ("1.000",)
timer_full_name = signal_face_name + "/" + "Red Clearance"
timer_durations[timer_full_name] = ("3.000",)
timer_full_name = signal_face_name + "/" + "Green Limit"
timer_durations[timer_full_name] = ("60.000",)
timer_full_name = signal_face_name + "/" + "Yellow Change"
timer_durations[timer_full_name] = ("25.000",)
timer_full_name = signal_face_name + "/" + "Green Delay Approaching"
timer_durations[timer_full_name] = ("0.000",)
timer_full_name = signal_face_name + "/" + "Green Delay Present"
timer_durations[timer_full_name] = ("0.000",)
signal_faces_list = list()
signal_faces_dict = dict()
for signal_face_name in signal_face_names:
signal_face = dict()
signal_face["name"] = signal_face_name
toggles_list = list()
for toggle_name in toggle_names:
toggle = dict()
toggle["name"] = toggle_name
toggle["value"] = False
toggle["important"] = True
toggles_list.append(toggle)
signal_face["toggles"] = toggles_list
timers_list = list()
for timer_name in timer_names:
timer = dict()
timer["name"] = timer_name
timer["state"] = "off"
timer["signal face name"] = signal_face_name
timer_full_name = signal_face_name + "/" + timer_name
timer["duration"] = timer_durations[timer_full_name]
match timer_name:
case "Red Clearance" | "Yellow Change" | "Minimum Green" | \
"Passage" | "Maximum Green" | \
"Green Limit" | "Green Delay Approaching" | \
"Green Delay Present" \
"Left Flashing Yellow Waiting" | "Left Flashing Yellow Limit" | \
"Minimum Left Flashing Yellow" | "Red Limit":
important = True
case _:
important = False
timer["important"] = important
timers_list.append(timer)
signal_face["timers"] = timers_list
signal_faces_list.append(signal_face)
signal_faces_dict[signal_face_name] = signal_face
if (do_trace):
trace_file.write ("Timer durations:\n")
pprint.pprint (timer_durations, trace_file)
trace_file.write ("\n")
# Construct the conflict and partial conflict tables.
for signal_face in signal_faces_list:
partial_conflict_set = None
match signal_face["name"]:
case "A":
conflict_set = ("psw", "pse", "D", "F", "G", "H", "J")
partial_conflict_set = ("psw", "pse", "D", "H", "J")
case "pse" | "psw":
conflict_set = ("A", "B", "C", "D", "F", "G", "J")
case "B" | "C":
conflict_set = ("psw", "pse", "D", "E", "pnw", "pne", "H")
case "D":
conflict_set = ("A", "psw", "pse", "B", "C", "E", "pnw", "pne", "F",
"G", "H", "J")
case "E":
conflict_set = ("B", "C", "D", "pne", "pnw", "H")
partial_conflict_set = ("D", "pnw", "pne", "H")
case "pne" | "pnw":
conflict_set = ("B", "C", "D", "E", "F", "G", "H")
case "F" | "G":
conflict_set = ("A", "pse", "psw", "D", "pne", "pnw", "H", "J")
case "H":
conflict_set = ("A", "B", "C", "D", "E", "pne", "pnw", "F", "G")
case "J":
conflict_set = ("A", "pse", "psw", "D", "F", "G")
if (partial_conflict_set == None):
partial_conflict_set = conflict_set
signal_face["conflicts"] = conflict_set
signal_face["partial conflicts"] = partial_conflict_set
# Limit the time a signal face stays red while it is waiting to
# turn green. This is a tradeoff between throughput and maximum
# waiting time for a vehicle or pedestrian.
for signal_face in signal_faces_list:
match signal_face["name"]:
case "A" | "psw" | "pse" | "D" | "E" | "pnw" | "pne" | "H" | "J":
signal_face["waiting limit"] = waiting_limit
case "B" | "C" | "F" | "G":
signal_face["waiting limit"] = waiting_limit / 2;
# Construct the travel paths. A traffic element appears at the first
# milestone, then proceeds to each following milestone. When it reaches
# the last milestone it vanishes from the simulation.
car_length = 15
car_width = 5
truck_length = 40
truck_width = 8
approach_sensor_long_distance = 365
approach_sensor_short_distance = 120
long_lane_length = 528
short_lane_length = 450
very_short_lane_length = 40
lane_width = 12
crosswalk_width = 6
# Subroutine to find the top and bottom of a lane.
# The top is the place where traffic elements stop if they cannot
# enter the intersection from their entrance lane and where traffic elements
# leaving the intersection enter their exit lane.
# The bottom is the other end of the lane, where vehicles enter or leave
# the simulation.
lane_names = ("A", "B", "C", "D", "E", "F", "G", "H", "J", "1", "2", "3", "4",
"5", "6", "psw", "pse", "pnw", "pne")
def find_lane_info (lane_name):
global lane_width
global long_lane_length
global short_lane_length
global very_short_lane_length
center_y = 0
center_x = 0
match lane_name:
case "1":
top_x = center_x - (2.0 * lane_width)
top_y = center_y + (4.0 * lane_width)
bottom_x = top_x
bottom_y = top_y + long_lane_length
case "2":
top_x = center_x - (1.0 * lane_width)
top_y = center_y + (4.0 * lane_width)
bottom_x = top_x
bottom_y = top_y + long_lane_length
case "A":
top_x = center_x - (0.0 * lane_width)
top_y = center_y + (4.0 * lane_width)
bottom_x = top_x
bottom_y = top_y + short_lane_length
case "B":
top_x = center_x + (1.0 * lane_width)
top_y = center_y + (4.0 * lane_width)
bottom_x = top_x
bottom_y = top_y + long_lane_length
case "C":
top_x = center_x + (2.0 * lane_width)
top_y = center_y + (4.0 * lane_width)
bottom_x = top_x
bottom_y = top_y + long_lane_length
case "3":
top_x = center_x + (5.0 * lane_width)
top_y = center_y + (0.5 * lane_width)
bottom_x = top_x + long_lane_length
bottom_y = top_y
case "D":
top_x = center_x + (5.0 * lane_width)
top_y = center_y - (0.5 * lane_width)
bottom_x = top_x + long_lane_length
bottom_y = top_y
case "4":
top_x = center_x + (2.0 * lane_width)
top_y = center_y - (4.0 * lane_width)
bottom_x = top_x
bottom_y = top_y - long_lane_length
case "5":
top_x = center_x + (1.0 * lane_width)
top_y = center_y - (4.0 * lane_width)
bottom_x = top_x
bottom_y = top_y - long_lane_length
case "E":
top_x = center_x + (0.0 * lane_width)
top_y = center_y - (4.0 * lane_width)
bottom_x = top_x
bottom_y = top_y - short_lane_length
case "F":
top_x = center_x - (1.0 * lane_width)
top_y = center_y - (4.0 * lane_width)
bottom_x = top_x
bottom_y = top_y - long_lane_length
case "G":
top_x = center_x - (2.0 * lane_width)
top_y = center_y - (4.0 * lane_width)
bottom_x = top_x
bottom_y = top_y - long_lane_length
case "6":
top_x = center_x - (5.0 * lane_width)
top_y = center_y - (1.0 * lane_width)
bottom_x = top_x - long_lane_length
bottom_y = top_y
case "H":
top_x = center_x - (5.0 * lane_width)
top_y = center_y + (0.0 * lane_width)
bottom_x = top_x - long_lane_length
bottom_y = top_y
case "J":
top_x = center_x - (5.0 * lane_width)
top_y = center_y + (1.0 * lane_width)
bottom_x = top_x - very_short_lane_length
bottom_y = top_y
case "psw":
top_x = center_x - (4.0 * lane_width)
top_y = center_y + (3.5 * lane_width)
bottom_x = top_x - (1.0 * lane_width)
bottom_y = top_y
case "pse":
top_x = center_x + (4.0 * lane_width)
top_y = center_y + (3.5 * lane_width)
bottom_x = top_x + (1.0 * lane_width)
bottom_y = top_y
case "pnw":
top_x = center_x - (4.0 * lane_width)
top_y = center_y - (3.5 * lane_width)
bottom_x = top_x - (1.0 * lane_width)
bottom_y = top_y
case "pne":
top_x = center_x + (4.0 * lane_width)
top_y = center_y - (3.5 * lane_width)
bottom_x = top_x + (1.0 * lane_width)
bottom_y = top_y
case _:
top_x = None
top_y = None
bottom_x = None
bottom_y = None
return (top_x, top_y, bottom_x, bottom_y)
# Construct the travel paths. Each valid path through the intersection
# has an entry lane and an exit lane. It also has milestones which
# the traffic elements pass through on their way from the entrance to the exit.
# Some travel paths have a shape which must be empty of vehicles
# or of vehicles moving a certain way before a permissive turn can be taken.
# Construct the shape of the intersection.
max_x = None
max_y = None
min_x = None
min_y = None
for lane_name in lane_names:
intersection_x, intersection_y, *bottom = find_lane_info (lane_name)
if (max_x == None):
max_x = intersection_x
if (intersection_x > max_x):
max_x = intersection_x
if (min_x == None):
min_x = intersection_x
if (intersection_x < min_x):
min_x = intersection_x
if (max_y == None):
max_y = intersection_y
if (intersection_y > max_y):
max_y = intersection_y
if (min_y == None):
min_y = intersection_y
if (intersection_y < min_y):
min_y = intersection_y
intersection_shape = (min_x, min_y, max_x, max_y)
if (do_trace):
trace_file.write ("Intersection shape:\n")
pprint.pprint ((min_x, min_y, max_x, max_y), trace_file)
pprint.pprint (intersection_shape, trace_file)
travel_paths = dict()
for entry_lane_name in ("A", "psw", "pse", "B", "C", "D", "E", "pnw", "pne",
"F", "G", "H", "J"):
entry_lane_info = find_lane_info(entry_lane_name)
entry_start_x = entry_lane_info[2]
entry_start_y = entry_lane_info[3]
entry_intersection_x = entry_lane_info[0]
entry_intersection_y = entry_lane_info[1]
match entry_lane_name:
case "A":
adjacent_lane_name = "B"
case "E":
adjacent_lane_name = "F"
case "J":
adjacent_lane_name = "H"
case _:
adjacent_lane_name = None
if (adjacent_lane_name != None):
adjacent_lane_info = find_lane_info(adjacent_lane_name)
adjacent_start_x = adjacent_lane_info[2]
adjacent_start_y = adjacent_lane_info[3]
for exit_lane_name in ("1", "2", "pse", "psw", "3", "4", "5", "pne", "pnw",
"6"):
exit_lane_info = find_lane_info(exit_lane_name)
exit_intersection_x = exit_lane_info[0]
exit_intersection_y = exit_lane_info[1]
exit_end_x = exit_lane_info[2]
exit_end_y = exit_lane_info[3]
travel_path_name = entry_lane_name + exit_lane_name
travel_path = dict()
travel_path["name"] = travel_path_name
travel_path["entry lane name"] = entry_lane_name
travel_path["exit lane name"] = exit_lane_name
permissive_turn_info = None
permissive_distance = 250
travel_path_valid = False
permissive_colors = None
green_colors = None
match travel_path_name:
case "A6":
# Northbound left turn
travel_path_valid = True
milestones = (
(adjacent_lane_name, adjacent_start_x, adjacent_start_y),
(adjacent_lane_name, adjacent_start_x, entry_start_y),
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x,
entry_intersection_y - (car_length)),
("intersection", (exit_intersection_x + car_length),
exit_intersection_y),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
permissive_turn_shape = (
entry_intersection_x - (2.5 * lane_width),
entry_intersection_y - (3 * lane_width) - permissive_distance,
entry_intersection_x - (0.5 * lane_width), entry_intersection_y)
permissive_turn_info = (("present", permissive_turn_shape),)
permissive_colors = ("Flashing Left Arrow Yellow (lower)",)
green_colors = ("Steady Left Arrow Green",)
case "A1" | "A2":
# Northbound U turn
travel_path_valid = True
milestones = (
(adjacent_lane_name, adjacent_start_x, adjacent_start_y),
(adjacent_lane_name, adjacent_start_x, entry_start_y),
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x,
entry_intersection_y - (car_length)),
("intersection", (entry_intersection_x + exit_intersection_x)/2.0,
entry_intersection_y - (2.0 * car_length)),
("intersection", exit_intersection_x,
exit_intersection_y - car_length),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
permissive_turn_shape = (
entry_intersection_x - (2.5 * lane_width),
entry_intersection_y - (3 * lane_width) - permissive_distance,
entry_intersection_x - (0.5 * lane_width), entry_intersection_y)
permissive_turn_info = (("present", permissive_turn_shape),)
permissive_colors = ("Flashing Left Arrow Yellow (lower)",)
green_colors = ("Steady Left Arrow Green",)
case "E4" | "E5":
# Southbound U turn
travel_path_valid = True
milestones = (
(adjacent_lane_name, adjacent_start_x, adjacent_start_y),
(adjacent_lane_name, adjacent_start_x, entry_start_y),
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x,
entry_intersection_y + (car_length)),
("intersection", (entry_intersection_x + exit_intersection_x)/2.0,
entry_intersection_y + (2.0 * car_length)),
("intersection", exit_intersection_x,
exit_intersection_y + car_length),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
permissive_turn_shape = (
entry_intersection_x + (0.5 * lane_width), entry_intersection_y,
entry_intersection_x + (2.5 * lane_width),
entry_intersection_y + (3.0 * lane_width) + permissive_distance)
permissive_turn_info = (("present", permissive_turn_shape),)
permissive_colors = ("Flashing Left Arrow Yellow (lower)",)
green_colors = ("Steady Left Arrow Green",)
case "E3":
# Southbound left turn
travel_path_valid = True
milestones = (
(adjacent_lane_name, adjacent_start_x, adjacent_start_y),
(adjacent_lane_name, adjacent_start_x, entry_start_y),
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x,
entry_intersection_y + (car_length)),
("intersection", exit_intersection_x - car_length,
exit_intersection_y),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
permissive_turn_shape = (
entry_intersection_x + (0.5 * lane_width), entry_intersection_y,
entry_intersection_x + (2.5 * lane_width),
entry_intersection_y + (3.0 * lane_width) + permissive_distance)
permissive_turn_info = (("present", permissive_turn_shape),)
permissive_colors = ("Flashing Left Arrow Yellow (lower)",)
green_colors = ("Steady Left Arrow Green",)
case "B5" | "C4":
# Northbound through lanes
travel_path_valid = True
milestones = (
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
green_colors = ("Steady Circular Green",)
case "F2" | "G1":
# Soundbound through lanes
travel_path_valid = True
milestones = (
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
green_colors = ("Steady Circular Green",)
case "C3":
# Northbound right turn
travel_path_valid = True
milestones = (
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x,
entry_intersection_y - car_length),
("intersection", exit_intersection_x - car_length,
exit_intersection_y),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
permissive_turn_shape = (
entry_intersection_x - (lane_width / 2),
exit_intersection_y - (lane_width / 2),
exit_intersection_x + (lane_width * 2),
entry_intersection_y + (lane_width / 2))
permissive_turn_info = (("moving East", intersection_shape),
("present", permissive_turn_shape))
permissive_colors = ("Steady Circular Red", "Steady Circular Yellow")
green_colors = ("Steady Circular Green",)
case "G6":
# Soundbound right turn
travel_path_valid = True
milestones = (
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x,
entry_intersection_y + car_length),
("intersection", exit_intersection_x + car_length,
exit_intersection_y),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
permissive_turn_shape = (
exit_intersection_x - (lane_width * 2),
entry_intersection_y,
exit_intersection_x + (lane_width * 2),
exit_intersection_y + (lane_width / 2))
permissive_turn_info = (("moving West", intersection_shape),
("present", permissive_turn_shape))
permissive_colors = ("Steady Circular Red", "Steady Circular Yellow")
green_colors = ("Steady Circular Green",)
case "D2" | "D1":
# Westbound left turn
travel_path_valid = True
milestones = (
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x - (2 * car_length),
entry_intersection_y),
("intersection", exit_intersection_x,
exit_intersection_y - car_length),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
green_colors = ("Steady Circular Green",)
case "D6":
# Westbound straight through
travel_path_valid = True
milestones = (
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
green_colors = ("Steady Circular Green",)
case "D3":
# Westbound U turn
travel_path_valid = True
milestones = (
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x - car_length,
entry_intersection_y),
("intersection", entry_intersection_x - (2.0 * car_length),
(entry_intersection_y + exit_intersection_y) / 2.0),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
green_colors = ("Steady Circular Green",)
case "D4" | "D5":
# Westbound right turn
travel_path_valid = True
milestones = (
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x - car_length,
entry_intersection_y),
("intersection", exit_intersection_x,
exit_intersection_y + car_length),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
permissive_turn_shape = (
exit_intersection_x - (lane_width / 2),
exit_intersection_y - (lane_width * 2),
exit_intersection_x + (lane_width / 2),
entry_intersection_y + (2.0 * lane_width) + permissive_distance)
permissive_turn_info = (("moving East", intersection_shape),
("present", permissive_turn_shape))
permissive_colors = ("Steady Circular Red", "Steady Circular Yellow")
green_colors = ("Steady Circular Green",)
case "H4" | "H5":
# Eastbound left turn
travel_path_valid = True
milestones = (
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x + (2 * car_length),
entry_intersection_y),
("intersection", exit_intersection_x,
exit_intersection_y + car_length),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
green_colors = ("Steady Left Arrow Green" +
" and Steady Circular Green")
case "H6":
# Eastbound U turn
travel_path_valid = True
milestones = (
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x + car_length,
entry_intersection_y),
("intersection", entry_intersection_x + (2.0 * car_length),
(entry_intersection_y + exit_intersection_y)/2.0),
("intersection", exit_intersection_x + car_length,
exit_intersection_y),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
green_colors = ("Steady Left Arrow Green" +
" and Steady Circular Green")
case "H3":
# Eastbound striaght through
travel_path_valid = True
milestones = (
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
green_colors = ("Steady Left Arrow Green" +
" and Steady Circular Green")
case "J1" | "J2":
# Eastbound right turn
travel_path_valid = True
milestones = (
(adjacent_lane_name, adjacent_start_x, adjacent_start_y),
(adjacent_lane_name, entry_start_x - (1.0 * car_length),
adjacent_start_y),
(entry_lane_name, entry_start_x, entry_start_y),
(entry_lane_name, entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x, entry_intersection_y),
("intersection", entry_intersection_x + car_length,
entry_intersection_y),
("intersection", exit_intersection_x,
exit_intersection_y - car_length),
("intersection", exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_intersection_x, exit_intersection_y),
(exit_lane_name, exit_end_x, exit_end_y))
green_colors = ("Steady Right Arrow Green",)
case "psepsw" | "pnepnw":
# pedestrian crossing westbound:
# Pedestrians cross in both directions without conflict.
# We model this by having westbound
# pedestrians walk on the north side of the crosswalk.
travel_path_valid = True
milestones = (
(entry_lane_name, entry_start_x,
entry_start_y - (crosswalk_width / 4.0)),
(entry_lane_name, entry_intersection_x,
entry_intersection_y - (crosswalk_width / 4.0)),
("crosswalk", entry_intersection_x,
entry_intersection_y - (crosswalk_width / 4.0)),
("crosswalk", exit_intersection_x,
exit_intersection_y - (crosswalk_width / 4.0)),
(exit_lane_name, exit_intersection_x,
exit_intersection_y - (crosswalk_width / 4.0)),
(exit_lane_name, exit_end_x, exit_end_y - (crosswalk_width / 4.0)))
green_colors = ("Walk",)
case "pswpse" | "pnwpne":
# Pedestrian crossing eastbound
# Eastbound pedestrians walk on the south side of the crosswalk.
travel_path_valid = True
milestones = (