-
Notifications
You must be signed in to change notification settings - Fork 9
/
traffic_interaction_scene.py
2070 lines (2050 loc) · 105 KB
/
traffic_interaction_scene.py
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
import numpy as np
import matplotlib.pyplot as plt
import os
import copy as cp
import random
random.seed(0)
import math
class TrafficInteraction:
# vm = 0; % minimum
# velocity
# v0 = 10; % initial
# velocity
# vM = 13; % maximum
# velocity
# am = -3; % minimum
# acceleration
# aM = 3; % maximum
def __init__(self, arrive_time, dis_ctl, args, deltaT=0.1, vm=5, vM=13, am=-3, aM=3, v0=10, diff_max=220,
lane_cw=2.5,
loc_con=True, show_col=False, virtual_l=True, lane_num=12):
# 坐标轴,车道0 从左到右, 车道1,从右到左 车道2,从下到上 车道3 从上到下
# dis_ctl
# -dis_ctl 0 dis_ctl
# -dis_ctl
self.virtual_l = virtual_l
self.virtual_data = {}
self.show_col = show_col
self.loc_con = loc_con
self.collision_thr = args.collision_thr
self.choose_veh = 15
self.safe_distance = 20
self.vm = vm
self.vM = vM
self.am = am
self.aM = aM
self.v0 = v0
self.lane_cw = lane_cw
self.lane_num = lane_num
self.intention_re = 0
self.thr = pow(self.vM - self.vm, 2) / 4 / self.aM + 2.2
self.choose_veh_info = [[] for i in range(self.lane_num)]
self.veh_info_record = [[] for i in range(self.lane_num)]
if self.lane_num == 3:
# T字形
self.lane_info = [
[dis_ctl - 2 * lane_cw, 3.1415 / 2 * 3 * lane_cw, -(dis_ctl - 2 * lane_cw)], # 左转
[dis_ctl - 2 * lane_cw, 4 * lane_cw, -(dis_ctl - 2 * lane_cw)], # 直行
[dis_ctl - 2 * lane_cw, 3.1415 / 2 * lane_cw, -(dis_ctl - 2 * lane_cw)] # 右转
]
self.lane2lane = [
[2, 4, 5],
[2],
[4, 0, 1],
[4],
[0, 2, 3],
[0]
]
self.intention = [
[1, 2],
[0, 1],
[0, 2]
]
elif self.lane_num == 4:
# 单车道
self.lane_info = [
[dis_ctl - 2 * lane_cw, 3.1415 / 2 * 3 * lane_cw, -(dis_ctl - 2 * lane_cw)], # 左转
[dis_ctl - 2 * lane_cw, 4 * lane_cw, -(dis_ctl - 2 * lane_cw)], # 直行
[dis_ctl - 2 * lane_cw, 3.1415 / 2 * lane_cw, -(dis_ctl - 2 * lane_cw)] # 右转
]
# 顺序:如果左转直行和右转同时交汇,按照左转,直行,右转的顺序排列
self.lane2lane = [
[10, 6, 9, 3, 7, 4, 8], # 0
[10, 6, 3, 4, 9, 5], # 1
[6, 10], # 2
[1, 9, 0, 6, 10, 7, 11], # 3
[1, 9, 6, 7, 0, 8], # 4
[9, 1], # 5
[4, 0, 3, 9, 1, 10, 2], # 6
[4, 0, 9, 10, 3, 11], # 7
[0, 4], # 8
[7, 3, 6, 0, 4, 1, 5], # 9
[7, 3, 0, 1, 6, 2], # 10
[3, 7] # 11
]
self.direction_num = 12
self.direction = [
[6, 7, 8],
[0, 1, 2],
[9, 10, 11],
[3, 4, 5]
]
self.alpha = math.atan((4 - math.sqrt(2)) / (4 + math.sqrt(2))) # 图中alpha的值
self._alpha = math.atan((4 + math.sqrt(2)) / (4 - math.sqrt(2)))
self.beta = math.atan(2 / math.sqrt(5)) # 图中beta的值
self._beta = math.atan(math.sqrt(5) / 2)
self.gama = math.atan(1 / 2 * math.sqrt(2)) # 图中gamma的值
elif self.lane_num == 8:
# 两车道
self.lane_info = [
[dis_ctl - 4 * lane_cw, 3.1415 / 2 * 5 * lane_cw, -(dis_ctl - 4 * lane_cw)],
[dis_ctl - 4 * lane_cw, 8 * lane_cw, -(dis_ctl - 4 * lane_cw)],
[dis_ctl - 4 * lane_cw, 3.1415 / 2 * lane_cw, -(dis_ctl - 4 * lane_cw)]
]
self.lane2lane = [
[14, 4, 13, 12, 9, 10, 5], # 0
[14, 13, 8, 4, 5, 6, 12], # 1
[14, 13, 8, 4, 5, 6, 7], # 2
[14], # 3
[2, 8, 1, 0, 13, 14, 9], # 4
[2, 1, 12, 8, 9, 10, 0], # 5
[2, 1, 12, 8, 9, 10, 11], # 6
[2], # 7
[6, 12, 5, 4, 1, 2, 13], # 8
[6, 5, 0, 12, 13, 14, 4], # 9
[6, 5, 0, 12, 13, 14, 15], # 10
[6], # 11
[10, 0, 9, 8, 5, 6, 1], # 12
[10, 9, 4, 0, 1, 2, 8], # 13
[10, 9, 4, 0, 1, 2, 3], # 14
[10] # 15
]
self.intention = [
[0, 1],
[1, 2],
[0, 1],
[1, 2],
[0, 1],
[1, 2],
[0, 1],
[1, 2]
]
self.direction_num = 16
self.direction = [
[0, 1, -1],
[-1, 2, 3],
[4, 5, -1],
[-1, 6, 7],
[8, 9, -1],
[-1, 10, 11],
[12, 13, -1],
[-1, 14, 15]
]
elif self.lane_num == 12:
# 三车道
self.lane_info = [
[dis_ctl - 6 * lane_cw, 3.1415 / 2 * 7 * lane_cw, -(dis_ctl - 6 * lane_cw)],
[dis_ctl - 6 * lane_cw, 12 * lane_cw, -(dis_ctl - 6 * lane_cw)],
[dis_ctl - 6 * lane_cw, 3.1415 / 2 * lane_cw, -(dis_ctl - 6 * lane_cw)]
]
self.lane2lane = [
[10, 3, 9, 7],
[10, 6, 3, 4],
[],
[1, 6, 0, 10],
[1, 9, 6, 7],
[],
[4, 9, 3, 1],
[4, 0, 9, 10],
[],
[7, 0, 6, 4],
[7, 3, 0, 1],
[]
]
self.direction_num = 12
self.direction = [
[0, -1, -1],
[-1, 1, -1],
[-1, -1, 2],
[3, -1, -1],
[-1, 4, -1],
[-1, -1, 5],
[6, -1, -1],
[-1, 7, -1],
[-1, -1, 8],
[9, -1, -1],
[-1, 10, -1],
[-1, -1, 11]
]
self.cita = (2 * math.sqrt(10) - 6) * self.lane_cw # 曲线交点据x轴或y轴的距离
self.alpha = math.atan((6 * self.lane_cw + self.cita) / (3 * self.lane_cw)) # 交点与在半圆中中的角度(大的一个)
self.beta = math.pi / 2 - self.alpha # 交点与在半圆中中的角度(小的一个)
self.gama = math.atan((math.sqrt(13) * self.lane_cw) / (6 * self.lane_cw)) # 两圆交点在半角中的角度(小的)
self._gama = math.pi / 2 - self.gama
self.closer_veh_num = args.o_agent_num
self.c_mode = args.c_mode
self.merge_p = [
[0, 0, self.lane_cw, -self.lane_cw],
[0, 0, -self.lane_cw, self.lane_cw],
[-self.lane_cw, self.lane_cw, 0, 0],
[self.lane_cw, -self.lane_cw, 0, 0]
]
self.arrive_time = arrive_time
self.current_time = 0
self.passed_veh = 0
self.passed_veh_step_total = 0
self.virtual_lane = []
self.virtual_lane_4 = [[] for i in range(self.direction_num)]
self.virtual_lane_real_p = [[] for i in range(self.direction_num)]
self.closer_cars = []
self.closer_same_l_car = [-1, -1]
self.deltaT = deltaT
self.dis_control = dis_ctl
self.veh_num = [0 for i in range(self.lane_num)] # 每个车道车的数量
self.veh_rec = [0 for i in range(self.lane_num)] # 每个车道车的总数量
self.input = [0 for i in range(4)] # 每个入口车的总数量
self.veh_info = [[] for i in range(self.lane_num)]
self.diff_max = diff_max
self.collision = False
self.id_seq = 0
self.delete_veh = []
init = True
while init:
for i in range(self.lane_num):
if self.veh_num[i] > 0:
init = False
if init:
self.scene_update()
def scene_update(self):
self.current_time += self.deltaT
collisions = 0
estm_collisions = 0
re_state = []
reward = []
collisions_per_veh = []
actions = []
ids = []
jerks = []
self.delete_veh.clear()
for i in range(self.lane_num):
if len(self.veh_info[i]) > 0:
for index, direction in enumerate(self.direction[i]):
if direction == -1:
continue
self.virtual_lane_4[direction].clear()
self.virtual_lane_real_p[direction].clear()
for _itr in self.virtual_lane:
# 目标车道
if _itr[1] == i:
self.virtual_lane_real_p[direction].append([_itr[0], _itr[1], _itr[2],
self.veh_info[_itr[1]][_itr[2]]["v"],
direction])
if self.direction[_itr[1]][_itr[3]] == direction:
# 同一车道直接添加 p, i, j, v
self.virtual_lane_4[direction].append([_itr[0], _itr[1], _itr[2],
self.veh_info[_itr[1]][_itr[2]]["v"], direction])
else:
if self.veh_info[_itr[1]][_itr[2]]["p"] - \
self.lane_info[self.veh_info[_itr[1]][_itr[2]]['intention']][1] > 0:
virtual_dis = self.veh_info[_itr[1]][_itr[2]]["p"] - \
self.lane_info[self.veh_info[_itr[1]][_itr[2]]['intention']][1] + \
self.lane_info[index][1]
self.virtual_lane_4[direction].append(
[virtual_dis, _itr[1], _itr[2], self.veh_info[_itr[1]][_itr[2]]["v"],
direction])
elif self.direction[_itr[1]][_itr[3]] in self.lane2lane[direction]:
# 与之相交的车道
virtual_d, choose = self.get_virtual_distance(self.direction[_itr[1]][_itr[3]], direction,
_itr[0])
if choose:
self.virtual_lane_real_p[direction].append([_itr[0], _itr[1], _itr[2],
self.veh_info[_itr[1]][_itr[2]]["v"],
direction])
for virtual_temp in range(len(virtual_d)):
self.virtual_lane_4[direction].append([virtual_d[virtual_temp], _itr[1], _itr[2],
self.veh_info[_itr[1]][_itr[2]]["v"],
self.direction[_itr[1]][_itr[3]]])
self.virtual_lane_4[direction] = sorted(self.virtual_lane_4[direction], key=lambda item: item[0])
self.virtual_lane_real_p[direction] = sorted(self.virtual_lane_real_p[direction],
key=lambda item: item[0])
for j, item in enumerate(self.veh_info[i]):
if self.veh_info[i][j]["intention"] == index:
if self.veh_info[i][j]["seq_in_lane"] == self.choose_veh:
self.choose_veh_info[i].append(
[self.current_time, self.veh_info[i][j]["p"], self.veh_info[i][j]["v"],
self.veh_info[i][j]["action"]])
t_distance = 2
d_distance = 10
if self.veh_info[i][j]["control"]:
self.veh_info_record[i][item["seq_in_lane"]].append(
[self.current_time, item["p"], item["v"], item["a"]]
)
sta, virtual_lane = self.get_state(i, j, self.virtual_lane_4[direction], direction)
self.virtual_lane_4[direction] = virtual_lane
self.veh_info[i][j]["state"] = cp.deepcopy(sta)
re_state.append(np.array(sta))
actions.append([state[2] for state in sta])
ids.append([i, j])
self.veh_info[i][j]["count"] += 1
closer_car = self.closer_cars[0]
if closer_car[0] >= 0:
id_seq_temp = [temp_item[1:3] for temp_item in self.virtual_lane_4[direction]]
if [closer_car[0], closer_car[1]] not in id_seq_temp:
index_closer = -1
else:
index_closer = id_seq_temp.index([closer_car[0], closer_car[1]])
d_distance = abs(
self.veh_info[i][j]["p"] - self.virtual_lane_4[direction][index_closer][0])
self.veh_info[i][j]["closer_p"] = self.virtual_lane_4[direction][index_closer][0]
if d_distance != 0:
t_distance = (self.veh_info[i][j]["p"] -
self.virtual_lane_4[direction][index_closer][0]) / \
(self.veh_info[i][j]["v"] -
self.veh_info[closer_car[0]][closer_car[1]]["v"] +
0.0001)
else:
self.veh_info[i][j]["closer_p"] = 150
vw = 2.0
cw = 3.0
r_ = 0
if 0 < t_distance < 4:
r_ += 1 / np.tanh(-t_distance / 4.0)
r_ -= pow(self.veh_info[i][j]["jerk"] / self.deltaT, 2) / 3600.0 * cw
if d_distance < 10:
r_ += np.log(pow(d_distance / 10, 5) + 0.00001)
r_ += (self.veh_info[i][j]["v"] - self.vm) / float(self.aM - self.am) * vw
reward.append(min(20, max(-20, r_)))
self.veh_info[i][j]["jerk_sum"] += abs(self.veh_info[i][j]["jerk"] / self.deltaT)
if 0 <= closer_car[0]:
veh_choose = self.veh_info[i][j]
veh_closer = self.veh_info[closer_car[0]][closer_car[1]]
p_choose = self.get_p(veh_choose["p"], i, self.veh_info[i][j]["intention"])
p_closer = self.get_p(veh_closer["p"], closer_car[0],
self.veh_info[closer_car[0]][closer_car[1]]["intention"])
d_distance = np.sqrt(
np.power((p_closer[0] - p_choose[0]), 2) + np.power((p_closer[1] - p_choose[1]),
2)
)
if abs(d_distance) < self.collision_thr:
self.veh_info[i][j]["collision"] += 1 # 发生碰撞
self.veh_info[closer_car[0]][closer_car[1]]["collision"] += 1 # 发生碰撞
if self.veh_info[i][j]["finish"]:
self.veh_info[i][j]["control"] = False
collisions += self.veh_info[i][j]["collision"]
estm_collisions += self.veh_info[i][j]["estm_collision"]
collisions_per_veh.append(
[self.veh_info[i][j]["collision"], self.veh_info[i][j]["estm_collision"]])
if self.veh_info[i][j]["p"] < -self.dis_control + int(
(self.lane_num + 1) / 2) * self.lane_cw or self.veh_info[i][j][
"collision"] > 0:
# 驶出交通路口, 删除该车辆
if self.veh_info[i][j]["collision"] > 0:
reward[-1] = -10
self.veh_info[i][j]["Done"] = True
self.delete_veh.append([i, j])
self.veh_info[i][j]["vir_header"] = [-1, -1]
elif self.veh_info[i][j]["p"] < 0 and self.veh_info[i][j]["control"]:
self.veh_info[i][j]["Done"] = True
self.veh_info[i][j]["finish"] = True
self.veh_info[i][j]["control"] = False
self.veh_info[i][j]["vir_header"] = [-1, -1]
self.veh_info[i][j]["lock"] = False
self.passed_veh += 1
reward[-1] = 5
jerks.append(self.veh_info[i][j]["jerk_sum"])
self.passed_veh_step_total += self.veh_info[i][j]["step"]
# 添加新车
self.add_new_veh(i)
# if self.show_col:
# print("add new car:", i, self.veh_num[i] - 1)
self.virtual_lane.clear()
lock = 0
for i in range(self.lane_num):
for j, veh in enumerate(self.veh_info[i]):
if veh["control"] and not self.veh_info[i][j]["lock"]:
if self.check_lock(i, j):
lock += 1
for v in self.virtual_lane_4[0]:
v_name = "%s_%s" % (v[1], self.veh_info[v[1]][v[2]]["seq_in_lane"])
if v_name not in self.virtual_data:
self.virtual_data[v_name] = []
self.virtual_data[v_name].append([self.current_time, v[0], v[3]])
return ids, re_state, reward, actions, collisions, estm_collisions, collisions_per_veh, jerks, lock
def add_new_veh(self, i):
if self.current_time >= self.arrive_time[self.veh_rec[i]][i]:
state_total = np.zeros((self.closer_veh_num + 1, (self.closer_veh_num + 1) * 4))
intention = 1 # 默认
random.seed()
if self.lane_num == 3:
intention = self.intention[i][random.randint(0, 1)]
elif self.lane_num == 4:
# intention = random.randint(0, 2)
intention = self.intention_re % 3
self.intention_re += 1
elif self.lane_num == 8:
intention = self.intention[i][random.randint(0, 1)]
# intention = self.intention[i][self.intention_re % 2]
self.intention_re += 1
elif self.lane_num == 12:
intention = i % 3
p = sum(self.lane_info[intention][0:2])
self.veh_info[i].append(
{
"intention": intention, # 随机生成意向0~2分别表示左转,直行和右转
"buffer": [],
"route": self.direction[i][intention],
"count": 0,
"Done": False,
"p": p,
"jerk": 0,
"jerk_sum": 0,
"lock_a": 0,
"lock": False,
"vir_header": [-1, -1],
"vir_dis": 100,
"v": self.v0,
"a": 0,
"action": 0,
"closer_p": 150,
"lane": i,
"header": False,
"reward": 10,
"dis_front": 50,
"seq_in_lane": self.veh_rec[i],
"control": True,
"state": state_total,
"step": 0,
"collision": 0,
"finish": False,
"estm_collision": 0,
"estm_arrive_time": abs(p / self.v0),
"id_info": [self.id_seq, self.veh_num[i]]
})
# "id_info":[在所有车中的出现次序,在当前车道中的出现次序]
self.veh_num[i] += 1
self.veh_rec[i] += 1
self.input[i % 4] += 1
self.veh_info_record[i].append([])
self.id_seq += 1
def delete_vehicle(self):
# 删除旧车
self.delete_veh = sorted(self.delete_veh, key=lambda item: -item[1])
for d_i in self.delete_veh:
if len(self.veh_info[d_i[0]]) > d_i[1]:
self.veh_info[d_i[0]].pop(d_i[1])
if self.veh_num[d_i[0]] > 0:
self.veh_num[d_i[0]] -= 1
else:
print("except!!!")
# 返回两车在按照碰撞点为原点的情况下的距离,lane1和p1为遍历到的车辆,lane2为每次遍历固定的lane
def get_virtual_distance(self, lane1, lane2, p1):
virtual_d = []
thr = 0
# if self.lane_num==4:
# thr = -5
choose = False
if self.lane_num == 4:
if lane2 in [0, 3, 6, 9]:
if lane1 == self.lane2lane[lane2][0]:
delta_d1 = p1 - (4 * self.lane_cw - 3 * self.lane_cw * math.cos(self.gama))
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + (3 * self.lane_cw * (0.5 * 3.1415 - self.gama)))
choose = True
# if lane1 == self.lane2lane[lane2][1]:
# delta_d1 = p1 - (1.5 * 3.1415) * self.lane_cw * (self.alpha / (0.5 * 3.1415))
# if delta_d1 > thr:
# virtual_d.append(abs(delta_d1) + (1.5 * 3.1415 * self.lane_cw * (self._alpha / (0.5 * 3.1415))))
# choose = True
if lane1 == self.lane2lane[lane2][2]:
delta_d1 = p1 - 1.5 * 3.1415 * self.lane_cw * self.beta / (0.5 * 3.1415)
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + (1.5 * 3.1415 * self.lane_cw * self._beta / (0.5 * 3.1415)))
choose = True
if lane1 == self.lane2lane[lane2][3]:
delta_d1 = p1 - 1.5 * 3.1415 * self.lane_cw * self._beta / (0.5 * 3.1415)
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + 1.5 * 3.1415 * self.lane_cw * self.beta / (0.5 * 3.1415))
choose = True
if lane1 == self.lane2lane[lane2][1]:
delta_d1 = p1 - (1.5 * 3.1415) * self.lane_cw * (self._alpha / (0.5 * 3.1415))
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + (1.5 * 3.1415) * self.lane_cw * (self.alpha / (0.5 * 3.1415)))
choose = True
if lane1 == self.lane2lane[lane2][4]:
delta_d1 = p1 - 3 * self.lane_cw * math.cos(self.gama)
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + (1.5 * 3.1415 * self.lane_cw * (self.gama / (0.5 * 3.1415))))
choose = True
if lane1 == self.lane2lane[lane2][5]:
delta_d1 = p1
if delta_d1 > thr:
virtual_d.append(p1)
choose = True
if lane1 == self.lane2lane[lane2][6]:
delta_d1 = p1
if delta_d1 > thr:
virtual_d.append(p1)
choose = True
elif lane2 in [1, 4, 7, 10]:
if lane1 == self.lane2lane[lane2][0]:
delta_d1 = p1 - self.lane_cw
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + 3 * self.lane_cw)
choose = True
elif lane1 == self.lane2lane[lane2][1]:
delta_d1 = p1 - 1.5 * 3.1415 * self.lane_cw * self.gama / (0.5 * 3.1415)
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + 3 * self.lane_cw * math.cos(self.gama))
choose = True
elif lane1 == self.lane2lane[lane2][2]:
delta_d1 = p1 - 1.5 * 3.1415 * self.lane_cw * (0.5 * 3.1415 - self.gama) / (0.5 * 3.1415)
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + (4 * self.lane_cw - 3 * self.lane_cw * math.cos(self.gama)))
choose = True
elif lane1 == self.lane2lane[lane2][3]:
delta_d1 = p1 - 3 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + self.lane_cw)
choose = True
elif lane1 == self.lane2lane[lane2][4]:
delta_d1 = p1
if delta_d1 > thr:
virtual_d.append(p1)
choose = True
elif lane1 == self.lane2lane[lane2][5]:
delta_d1 = p1
if delta_d1 > thr:
virtual_d.append(p1)
choose = True
elif lane2 in [2, 5, 8, 11]:
if lane1 == self.lane2lane[lane2][0]:
delta_d1 = p1
if delta_d1 > thr:
virtual_d.append(p1)
choose = True
if lane1 == self.lane2lane[lane2][1]:
delta_d1 = p1
if delta_d1 > thr:
virtual_d.append(p1)
choose = True
elif self.lane_num == 8:
# 左转车道
# [14, 4, 13, 12, 9, 10, 5]
if lane2 in [0, 4, 8, 12]:
if lane1 == self.lane2lane[lane2][0]:
delta_d1 = p1 - (8 * self.lane_cw - math.sqrt(24) * self.lane_cw)
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + math.atan(math.sqrt(24)) * 5 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][1]:
delta_d1 = p1 - math.atan(3 / 4) * 5 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + math.atan(4 / 3) * 5 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][2]:
delta_d1 = p1 - 4 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + math.atan(4 / 3) * 5 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][3]: # lane2==0 lane1==12
# ! delta_d1 = p1 - 5 * self.lane_cw
delta_d1 = p1 - math.atan(4 / 3) * 5 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + math.atan(3 / 4) * 5 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][4]:
delta_d1 = p1 - 4 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + math.atan(3 / 4) * 5 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][5]:
delta_d1 = p1 - math.sqrt(24) * self.lane_cw
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + math.atan(1 / math.sqrt(24)) * 5 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][6]:
delta_d1 = p1
if delta_d1 > thr:
virtual_d.append(p1)
choose = True
# 左边车道直行车道
# [14, 13, 8, 4, 5, 6, 12], # 1
elif lane2 in [1, 5, 9, 13]:
if lane1 == self.lane2lane[lane2][0]:
delta_d1 = p1 - 3 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(delta_d1 + 7 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][1]:
delta_d1 = p1 - 3 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + 5 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][2]:
delta_d1 = p1 - math.atan(3 / 4) * 5 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + 4 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][3]:
delta_d1 = p1 - math.atan(4 / 3) * 5 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + 4 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][4]:
delta_d1 = p1 - 5 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(delta_d1 + 3 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][5]:
delta_d1 = p1 - 5 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(delta_d1 + self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][6]:
delta_d1 = p1
if delta_d1 > thr:
virtual_d.append(p1)
choose = True
# 右边车道直行车道
elif lane2 in [2, 6, 10, 14]:
# [14, 13, 8, 4, 5, 6, 7], # 2
# if lane2 in [0, 4, 8, 12]:
if lane1 == self.lane2lane[lane2][0]:
delta_d1 = p1 - self.lane_cw
if delta_d1 > thr:
virtual_d.append(delta_d1 + 7 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][1]:
delta_d1 = p1 - self.lane_cw
if delta_d1 > thr:
virtual_d.append(delta_d1 + 5 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][2]:
delta_d1 = p1 - math.atan(1 / math.sqrt(24)) * 5 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + math.sqrt(24) * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][3]:
delta_d1 = p1 - math.atan(math.sqrt(24)) * 5 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(abs(delta_d1) + 8 * self.lane_cw - math.sqrt(24) * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][4]:
delta_d1 = p1 - 7 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(delta_d1 + 3 * self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][5]:
delta_d1 = p1 - 7 * self.lane_cw
if delta_d1 > thr:
virtual_d.append(delta_d1 + self.lane_cw)
choose = True
if lane1 == self.lane2lane[lane2][6]:
delta_d1 = p1
if delta_d1 > thr:
virtual_d.append(p1)
choose = True
# 右转车道
elif lane2 in [3, 7, 11, 15]:
if lane1 == self.lane2lane[lane2][0]:
delta_d1 = p1
if delta_d1 > thr:
virtual_d.append(p1)
choose = True
elif self.lane_num == 12:
# if lane2 in [1, 4, 7, 10]:
# if lane1 == self.lane2lane[lane2][0]:
# # 据碰撞点的距离
# delta_d1 = p1 - 3 * self.lane_cw
# # delta_d2 = p2 - 9 * self.lane_cw
# # delta_d = delta_d1 - delta_d2
# if delta_d1 > thr:
# virtual_d.append(9 * self.lane_cw + delta_d1)
# choose = True
# elif lane1 == self.lane2lane[lane2][1]:
# # 角度为alpha段的圆弧长度
# beta_d = self.beta * 7 * self.lane_cw
# delta_d1 = p1 - beta_d
# # delta_d2 = p2 - 6 * self.lane_cw - self.cita
# # delta_d = delta_d1 - delta_d2
# if delta_d1 > thr:
# virtual_d.append(6 * self.lane_cw - self.cita + delta_d1)
# choose = True
# elif lane1 == self.lane2lane[lane2][2]:
# # 角度为beta段的圆弧长度
# alpha_d = self.alpha * 7 * self.lane_cw
# delta_d1 = p1 - alpha_d
# # delta_d2 = p2 - 6 * self.lane_cw + self.cita
# # delta_d = delta_d1 - delta_d2
# if delta_d1 > thr:
# virtual_d.append(6 * self.lane_cw - self.cita + delta_d1)
# choose = True
# elif lane1 == self.lane2lane[lane2][3]:
# delta_d1 = p1 - 9 * self.lane_cw
# # delta_d2 = p2 - 3 * self.lane_cw
# # delta_d = delta_d1 - delta_d2
# if delta_d1 > thr:
# virtual_d.append(3 * self.lane_cw + delta_d1)
# choose = True
# else:
# if p1 > thr:
# virtual_d.append(p1)
# choose = True
# elif lane2 in [0, 3, 6, 9]:
# if lane1 == self.lane2lane[lane2][0]:
# delta_d1 = p1 - 6 * self.lane_cw + self.cita
# # delta_d2 = p2 - self.alpha * 7 * self.lane_cw
# # delta_d = delta_d1 - delta_d2
# if delta_d1 > thr:
# virtual_d.append(self.alpha * 7 * self.lane_cw + delta_d1)
# choose = True
# elif lane1 == self.lane2lane[lane2][1]:
# delta_d1 = p1 - self.gama * 7 * self.lane_cw
# # delta_d2 = p2 - self._gama * 7 * self.lane_cw
# # delta_d = delta_d1 - delta_d2
# if delta_d1 > thr:
# virtual_d.append(self._gama * 7 * self.lane_cw + delta_d1)
# choose = True
# elif lane1 == self.lane2lane[lane2][2]:
# delta_d1 = p1 - self._gama * 7 * self.lane_cw
# # delta_d2 = p2 - self.gama * 7 * self.lane_cw
# # delta_d = delta_d1 - delta_d2
# if delta_d1 > thr:
# virtual_d.append(self.gama * 7 * self.lane_cw + delta_d1)
# choose = True
# else:
# delta_d1 = p1 - 6 * self.lane_cw - self.cita
# # delta_d2 = p2 - self.beta * 7 * self.lane_cw
# # delta_d = delta_d1 - delta_d2
# if delta_d1 > thr:
# virtual_d.append(self.beta * 7 * self.lane_cw + delta_d1)
# choose = True
# else:
# if p1 > thr:
# virtual_d.append(p1)
# choose = True
if lane2 in [1, 4, 7, 10]:
if lane1 == self.lane2lane[lane2][0]:
# 据碰撞点的距离
delta_d1 = p1 - 3 * self.lane_cw
# delta_d2 = p2 - 9 * self.lane_cw
# delta_d = delta_d1 - delta_d2
if delta_d1 > thr:
virtual_d.append(9 * self.lane_cw + delta_d1)
choose = True
elif lane1 == self.lane2lane[lane2][1]:
# 角度为alpha段的圆弧长度
beta_d = self.beta * 7 * self.lane_cw
delta_d1 = p1 - beta_d
# delta_d2 = p2 - 6 * self.lane_cw - self.cita
# delta_d = delta_d1 - delta_d2
if delta_d1 > thr:
virtual_d.append(6 * self.lane_cw + self.cita + delta_d1)
choose = True
elif lane1 == self.lane2lane[lane2][2]:
# 角度为beta段的圆弧长度
alpha_d = self.alpha * 7 * self.lane_cw
delta_d1 = p1 - alpha_d
# delta_d2 = p2 - 6 * self.lane_cw + self.cita
# delta_d = delta_d1 - delta_d2
if delta_d1 > thr:
virtual_d.append(6 * self.lane_cw - self.cita + delta_d1)
choose = True
elif lane1 == self.lane2lane[lane2][3]:
delta_d1 = p1 - 9 * self.lane_cw
# delta_d2 = p2 - 3 * self.lane_cw
# delta_d = delta_d1 - delta_d2
if delta_d1 > thr:
virtual_d.append(3 * self.lane_cw + delta_d1)
choose = True
else:
if p1 > 0:
virtual_d.append(p1)
choose = True
elif lane2 in [0, 3, 6, 9]:
if lane1 == self.lane2lane[lane2][0]:
delta_d1 = p1 - 6 * self.lane_cw + self.cita
# delta_d2 = p2 - self.alpha * 7 * self.lane_cw
# delta_d = delta_d1 - delta_d2
if delta_d1 > thr:
virtual_d.append(self.alpha * 7 * self.lane_cw + delta_d1)
choose = True
elif lane1 == self.lane2lane[lane2][1]:
delta_d1 = p1 - self.gama * 7 * self.lane_cw
# delta_d2 = p2 - self._gama * 7 * self.lane_cw
# delta_d = delta_d1 - delta_d2
if delta_d1 > thr:
virtual_d.append(self._gama * 7 * self.lane_cw + delta_d1)
choose = True
elif lane1 == self.lane2lane[lane2][2]:
delta_d1 = p1 - self._gama * 7 * self.lane_cw
# delta_d2 = p2 - self.gama * 7 * self.lane_cw
# delta_d = delta_d1 - delta_d2
if delta_d1 > thr:
virtual_d.append(self.gama * 7 * self.lane_cw + delta_d1)
choose = True
else:
delta_d1 = p1 - 6 * self.lane_cw - self.cita
# delta_d2 = p2 - self.beta * 7 * self.lane_cw
# delta_d = delta_d1 - delta_d2
if delta_d1 > thr:
virtual_d.append(self.beta * 7 * self.lane_cw + delta_d1)
choose = True
else:
if p1 > 0:
virtual_d.append(p1)
choose = True
return virtual_d, choose
# 根据车道的位置计算其真实位置
def get_p(self, p, i, intention):
# x, y, yaw(与x轴正方向夹角)
new_p = [0, 0, 0]
# car_info = self.veh_info[i][j]
intention_info = intention
if self.lane_num == 3:
if i == 0:
# 直行
if intention_info == 1:
new_p[0] = -1 * p + 2 * self.lane_cw
new_p[1] = -1 * self.lane_cw
new_p[2] = 0
# 右转
else:
if p > self.lane_info[2][1]:
new_p[0] = -1 * (p - self.lane_info[2][1] + 2 * self.lane_cw)
new_p[1] = -1 * self.lane_cw
new_p[2] = 0
elif p > 0:
beta_temp = p / self.lane_cw
delta_y = math.sin(beta_temp) * self.lane_cw
delta_x = math.cos(beta_temp) * self.lane_cw
new_p[0] = -1 * (2 * self.lane_cw - delta_x)
new_p[1] = -1 * (2 * self.lane_cw - delta_y)
new_p[2] = beta_temp + 1.5 * 3.1415
else:
new_p[0] = -1 * self.lane_cw
new_p[1] = -1 * (-1 * p + 2 * self.lane_cw)
new_p[2] = 1.5 * 3.1415
elif i == 1:
# 左转
if intention_info == 0:
# 没到交叉口中
if p > self.lane_info[0][1]:
new_p[0] = 1 * (p - self.lane_info[0][1] + 2 * self.lane_cw)
new_p[1] = 1 * self.lane_cw
new_p[2] = 3.1415
elif p > 0:
beta_temp = p / (3 * self.lane_cw) # rad
delta_y = math.sin(beta_temp) * 3 * self.lane_cw
delta_x = math.cos(beta_temp) * 3 * self.lane_cw
new_p[0] = -1 * (delta_x - 2 * self.lane_cw)
new_p[1] = -1 * (2 * self.lane_cw - delta_y)
new_p[2] = 3.1415 / 2 - beta_temp + 3.1415
else:
new_p[0] = -1 * self.lane_cw
new_p[1] = -1 * (-1 * p + 2 * self.lane_cw)
new_p[2] = 1.5 * 3.1415
# 直行
else:
new_p[0] = p - 2 * self.lane_cw
new_p[1] = 1 * self.lane_cw
new_p[2] = 3.1415
else:
# 左转
if intention_info == 0:
# 没到交叉口中
if p > self.lane_info[0][1]:
new_p[0] = 1 * self.lane_cw
new_p[1] = -1 * (p - self.lane_info[0][1] + 2 * self.lane_cw)
new_p[2] = 3.1415 / 2
elif p > 0:
beta_temp = p / (3 * self.lane_cw) # rad
delta_x = math.sin(beta_temp) * 3 * self.lane_cw
delta_y = math.cos(beta_temp) * 3 * self.lane_cw
new_p[0] = 1 * (delta_x - 2 * self.lane_cw)
new_p[1] = -1 * (2 * self.lane_cw - delta_y)
new_p[2] = 3.1415 / 2 - beta_temp + 3.1415 / 2
else:
new_p[0] = -1 * (-1 * p + 2 * self.lane_cw)
new_p[1] = self.lane_cw
new_p[2] = 3.1415
# 右转
else:
if p > self.lane_info[2][1]:
new_p[0] = 1 * self.lane_cw
new_p[1] = -1 * (p - self.lane_info[2][1] + 2 * self.lane_cw)
new_p[2] = 3.1415 / 2
elif p > 0:
beta_temp = p / self.lane_cw
delta_x = math.sin(beta_temp) * self.lane_cw
delta_y = math.cos(beta_temp) * self.lane_cw
new_p[0] = 1 * (2 * self.lane_cw - delta_x)
new_p[1] = -1 * (2 * self.lane_cw - delta_y)
new_p[2] = beta_temp
else:
new_p[0] = -1 * p + 2 * self.lane_cw
new_p[1] = -1 * self.lane_cw
new_p[2] = 0
elif self.lane_num == 4:
if i == 0:
# 左转
if intention_info == 0:
# 没到交叉口中
if p > self.lane_info[0][1]:
new_p[0] = -1 * (p - self.lane_info[0][1] + 2 * self.lane_cw)
new_p[1] = -1 * self.lane_cw
new_p[2] = 0
elif p > 0:
beta_temp = p / (3 * self.lane_cw) # rad
delta_y = math.sin(beta_temp) * 3 * self.lane_cw
delta_x = math.cos(beta_temp) * 3 * self.lane_cw
new_p[0] = 1 * (delta_x - 2 * self.lane_cw)
new_p[1] = 1 * (2 * self.lane_cw - delta_y)
new_p[2] = 3.1415 / 2 - beta_temp
else:
new_p[0] = self.lane_cw
new_p[1] = -1 * p + 2 * self.lane_cw
new_p[2] = 3.1415 / 2
# 直行
elif intention_info == 1:
new_p[0] = -1 * p + 2 * self.lane_cw
new_p[1] = -1 * self.lane_cw
new_p[2] = 0
# 右转
else:
if p > self.lane_info[2][1]:
new_p[0] = -1 * (p - self.lane_info[2][1] + 2 * self.lane_cw)
new_p[1] = -1 * self.lane_cw
new_p[2] = 0
elif p > 0:
beta_temp = p / self.lane_cw
delta_y = math.sin(beta_temp) * self.lane_cw
delta_x = math.cos(beta_temp) * self.lane_cw
new_p[0] = -1 * (2 * self.lane_cw - delta_x)
new_p[1] = -1 * (2 * self.lane_cw - delta_y)
new_p[2] = beta_temp + 1.5 * 3.1415
else:
new_p[0] = -1 * self.lane_cw
new_p[1] = -1 * (-1 * p + 2 * self.lane_cw)
new_p[2] = 1.5 * 3.1415
elif i == 1:
# 左转
if intention_info == 0:
# 没到交叉口中
if p > self.lane_info[0][1]:
new_p[0] = 1 * (p - self.lane_info[0][1] + 2 * self.lane_cw)
new_p[1] = 1 * self.lane_cw
new_p[2] = 3.1415
elif p > 0:
beta_temp = p / (3 * self.lane_cw) # rad
delta_y = math.sin(beta_temp) * 3 * self.lane_cw
delta_x = math.cos(beta_temp) * 3 * self.lane_cw
new_p[0] = -1 * (delta_x - 2 * self.lane_cw)
new_p[1] = -1 * (2 * self.lane_cw - delta_y)
new_p[2] = 3.1415 / 2 - beta_temp + 3.1415
else:
new_p[0] = -1 * self.lane_cw
new_p[1] = -1 * (-1 * p + 2 * self.lane_cw)
new_p[2] = 1.5 * 3.1415
# 直行
elif intention_info == 1:
new_p[0] = p - 2 * self.lane_cw
new_p[1] = 1 * self.lane_cw
new_p[2] = 3.1415
# 右转
else:
if p > self.lane_info[2][1]:
new_p[0] = 1 * (p - self.lane_info[2][1] + 2 * self.lane_cw)
new_p[1] = 1 * self.lane_cw
new_p[2] = 3.1415
elif p > 0:
beta_temp = p / self.lane_cw
delta_y = math.sin(beta_temp) * self.lane_cw
delta_x = math.cos(beta_temp) * self.lane_cw
new_p[0] = 1 * (2 * self.lane_cw - delta_x)
new_p[1] = 1 * (2 * self.lane_cw - delta_y)
new_p[2] = beta_temp + 3.1415 / 2
else:
new_p[0] = 1 * self.lane_cw
new_p[1] = -1 * p + 2 * self.lane_cw
new_p[2] = 3.1415 / 2
elif i == 2:
# 左转
if intention_info == 0:
# 没到交叉口中
if p > self.lane_info[0][1]:
new_p[0] = 1 * self.lane_cw
new_p[1] = -1 * (p - self.lane_info[0][1] + 2 * self.lane_cw)
new_p[2] = 3.1415 / 2
elif p > 0:
beta_temp = p / (3 * self.lane_cw) # rad
delta_x = math.sin(beta_temp) * 3 * self.lane_cw
delta_y = math.cos(beta_temp) * 3 * self.lane_cw
new_p[0] = 1 * (delta_x - 2 * self.lane_cw)
new_p[1] = -1 * (2 * self.lane_cw - delta_y)
new_p[2] = 3.1415 / 2 - beta_temp + 3.1415 / 2
else:
new_p[0] = -1 * (-1 * p + 2 * self.lane_cw)
new_p[1] = self.lane_cw
new_p[2] = 3.1415
# 直行
elif intention_info == 1:
new_p[0] = self.lane_cw