-
Notifications
You must be signed in to change notification settings - Fork 1
/
双车道.py
161 lines (135 loc) · 5.12 KB
/
双车道.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
import time
import random
import datetime
def car_state_change(car,car_front):
n=random.random()
if n < p_slow:
car[1] = max(car[1] - 1, 0)
else:
car[1] = min(car[1] + a, v_max,car_front[2] - car[2] -car_len - 1)
car[2] = car[2] + car[1]
return [car[0],car[1],car[2]]
def lane_change(lane_left,lane_right):
lane_left_last = []
lane_right_last = []
for i in range(len(lane_left)):
lane_left_last.append(lane_left[i])
for i in range(len(lane_right)):
lane_right_last.append(lane_right[i])
#从左向右变道
left_remove = 0
right_insert = 0
for i in range(len(lane_left_last)):
flag = 0
#找到右边合适的两车道
#flag代表找到
#左边i车介于右边j和j+1车之间
for j in range(0,len(lane_right_last) - 1):
if (lane_right_last[j + 1][2] < lane_left_last[i][2] < lane_right_last[j][2]):
flag = 1
break
if (flag == 1 and i!= 0):
gap_left = abs(lane_left_last[i - 1][2] - lane_left_last[i][2]) - car_len - 1
#if v_max > gap_left and gap_right > gap_left
if (v_max > gap_left and lane_left_last[i - 1][2] < lane_right_last[j][2]):
lane_left.pop(i-left_remove)
left_remove+=1
lane_right.insert(j + 1 + right_insert,lane_left_last[i])
right_insert+=1
continue
lane_left_last = sorted(lane_left_last, key=lambda x:x[2],reverse = True)
#左车随机变道
n=random.random()
if (flag == 1 and n < p_change):
#后车不会与本车碰撞
if ((abs(lane_left_last[i][2] - lane_right_last[j + 1][2]) - car_len - 1 > lane_right_last[j + 1][1])):
lane_left.pop(i-left_remove)
left_remove+=1
lane_right.insert(j + 1 + right_insert,lane_left_last[i])
right_insert+=1
right_remove = 0
left_insert = 0
for i in range(len(lane_right_last)):
flag = 0
for j in range(0,len(lane_left_last)- 1):
if (lane_left_last[j + 1][2] < lane_right_last[i][2] < lane_left_last[j][2]):
flag = 1
break
if (flag == 1 and i != 0):
if (v_max > abs(lane_right_last[i - 1][2] - lane_right_last[i][2]) - car_len - 1 and lane_right_last[i - 1][2] < lane_left_last[j][2]):
lane_right.pop(i + right_insert - right_remove)
right_remove+=1
lane_left.insert(j + 1 - left_remove + left_insert,lane_right_last[i])
left_insert+=1
continue
n=random.random()
if (flag == 1 and n < p_change):
if ((abs(lane_right_last[i][2] - lane_left_last[j + 1][2]) - car_len - 1 > lane_left_last[j + 1][1])):
lane_right.pop(i + right_insert - right_remove)
right_remove+=1
lane_left.insert(j + 1 - left_remove + left_insert,lane_right_last[i])
left_insert+=1
return [lane_left,lane_right]
#车出现的概率
p = 1/3
#红灯时间
t_red = 40
#绿灯时间
t_green = 30
#随机慢化概率
p_slow = 1/10
#车长
car_len = 5
#车距
car_between = 2
#车最大速度
v_max = 10
#加速度
a=2
#随机变道概率
p_change = 1/4
#init
#车流总长
car_amount = int(t_red * p)
L_all = car_amount * (car_len + car_between)
#观察点距离
L_look = v_max * 1 + car_len * 2
lane_left = []
lane_right = []
for i in range(0,car_amount):
lane_left.append([str(i+1)+"left",0,-1*(car_len + i*(car_len + car_between))])
lane_right.append([str(i+1)+"right",0,-1*(car_len + i*(car_len + car_between))])
count_left = 1
count_right = 1
print('left:--------------------------------------------------------------')
print(lane_left)
print('right:--------------------------------------------------------------')
print(lane_right)
for i in range(30):
lane_left,lane_right = lane_change(lane_left,lane_right)
n=random.random()
if n < p:
lane_left.append([str(car_amount+count_left) + "left",v_max,min(lane_left[len(lane_left) - 1][2] - v_max - 1 - car_len,-1*(L_all + L_look))])
count_left+=1
n=random.random()
if n < p:
lane_right.append([str(car_amount+count_right) + "right",v_max,min(lane_right[len(lane_right) - 1][2] - v_max - 1 - car_len,-1*(L_all + L_look))])
count_right+=1
lane_left_last = lane_left
for j in range(len(lane_left)):
if j == 0:
car_front = [10000,0,999999]
else:
car_front = lane_left[j - 1]
lane_left[j] = car_state_change(lane_left_last[j],car_front)
lane_right_last = lane_right
for j in range(len(lane_right)):
if j == 0:
car_front = [10000,0,999999]
else:
car_front = lane_right[j - 1]
lane_right[j] = car_state_change(lane_right_last[j],car_front)
print('left:--------------------------------------------------------------')
print(lane_left)
print('right:--------------------------------------------------------------')
print(lane_right)