-
Notifications
You must be signed in to change notification settings - Fork 1
/
switch_lane.m
213 lines (195 loc) · 8.12 KB
/
switch_lane.m
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
function switch_lane()
global plaza; % all car on this cell
global sd_lane; % self_driving lanes chosen
W = size(plaza,1);
switch_ = 0;
for lanes = 1:W
cars = cell2mat(plaza(lanes));
num = size(cars,1);
if num == 0
continue;
end
car_ind = 1;
%for car_ind = 1: num
while car_ind <= num
type = cars(car_ind,3);
pos = cars(car_ind,2);
if car_ind ~= 1
dist = cars(car_ind-1,2)-pos;
else
car_ind = car_ind + 1;
continue;
end
cars_l = [];
cars_r = [];
if lanes ~= 1 && lanes ~= W % not left most or right most
cars_l = cell2mat(plaza(lanes-1));
cars_r = cell2mat(plaza(lanes+1));
% check nearest car and car after index
[la, lb] = find_nearest_car(pos, cars_l);
[ra, rb] = find_nearest_car(pos, cars_r);
% compute safe distance
if la ~= 0
safe_l = safe_distance(cars(car_ind,1), cars_l(la,1), type);
end
if ra ~= 0
safe_r = safe_distance(cars(car_ind,1), cars_r(ra,1), type);
end
% check car after distance
% 1. no pre car la = 0 2. no post car lb = 0
if la == 0
l_dist = inf;
if lb ~= 0 && (after_dist_available(cars(car_ind,:), cars_l(lb,:)) ~= 1)
l_dist = 0;
end
else
l_dist = pre_available(cars_l(la,2), pos, safe_l);
if lb ~= 0 && (after_dist_available(cars(car_ind,:), cars_l(lb,:)) ~= 1)
l_dist = 0;
end
if (size(find(lanes-1 == sd_lane),2) ~= 0) && type == 0
l_dist = 0;
end
end
if ra == 0
r_dist = inf;
if rb ~= 0 && (after_dist_available(cars(car_ind,:), cars_r(rb,:)) ~= 1)
r_dist = 0;
end
else
r_dist = pre_available(cars_r(ra,2), pos, safe_r);
if rb ~= 0 && (after_dist_available(cars(car_ind,:), cars_r(rb,:)) ~= 1)
r_dist = 0;
end
if (size(find(lanes+1 == sd_lane),2) ~= 0) && type == 0
r_dist = 0;
end
end
% find smallest in dist, l_dist, r_dist
[d, ind] = max([dist l_dist r_dist]);
if (ind == 2 && (r_dist ~= l_dist)) ||... % left is better and not draw
(r_dist == l_dist &&... % draw
type == 1 &&... % self-driving car
la ~= 0 && cars_l(la,3) == 1) % front is self-driving car
% switch to left lane
switch_ = 1;
if la ~= 0
if lb ~= 0
cars_l = [cars_l(1:la, :); ...
cars(car_ind, :);...
cars_l(lb:size(cars_l,1),:)];% ˛ĺČë
else
cars_l = [cars_l(:,:); cars(car_ind,:)];
end
else
cars_l = [cars(car_ind,:);cars_l(:,:)];
end
cars(car_ind, :) = [];
elseif ind == 3
switch_ = 1;
% switch to right lane
if ra ~= 0
if rb ~= 0
cars_r = [cars_r(1:ra, :); ...
cars(car_ind, :);...
cars_r(rb:size(cars_r,1),:)];% ˛ĺČë
else
cars_r = [cars_r(:,:); cars(car_ind,:)];
end
else
cars_r = [cars(car_ind,:);cars_r(:,:)];
end
cars(car_ind, :) = [];
end
elseif lanes == 1
cars_r = cell2mat(plaza(lanes+1));
% check nearest car and car after index
[ra, rb] = find_nearest_car(pos, cars_r);
if ra == 0
r_dist = inf;
if rb ~= 0 && (after_dist_available(cars(car_ind,:), cars_r(rb,:)) ~= 1)
r_dist = 0;
end
else
safe_r = safe_distance(cars(car_ind,1), cars_r(ra,1), type);
r_dist = pre_available(cars_r(ra,2), pos, safe_r);
if rb ~= 0 && (after_dist_available(cars(car_ind,:), cars_r(rb,:)) ~= 1)
r_dist = 0;
end
if (size(find(lanes+1 == sd_lane),2) ~= 0) && type == 0
r_dist = 0;
end
end
% find smallest in dist, l_dist, r_dist
[d, ind] = max([dist r_dist]);
if ind == 2
switch_ = 1;
if ra ~= 0
if rb ~= 0
cars_r = [cars_r(1:ra, :); ...
cars(car_ind, :);...
cars_r(rb:size(cars_r,1),:)];% ˛ĺČë
else
cars_r = [cars_r(:,:); cars(car_ind,:)];
end
else
cars_r = [cars(car_ind,:);cars_r(:,:)];
end
cars(car_ind, :) = [];
end
elseif lanes == W
cars_l = cell2mat(plaza(lanes-1));
% check nearest car and car after index
[la, lb] = find_nearest_car(pos, cars_l);
if la == 0
l_dist = inf;
if lb ~= 0 && (after_dist_available(cars(car_ind,:), cars_l(lb,:)) ~= 1)
l_dist = 0;
end
else
safe_l = safe_distance(cars(car_ind,1), cars_l(la,1), type);
l_dist = pre_available(cars_l(la,2), pos, safe_l);
if lb ~= 0 && (after_dist_available(cars(car_ind,:), cars_l(lb,:)) ~= 1)
l_dist = 0;
end
if (size(find(lanes-1 == sd_lane),2) ~= 0) && type == 0
l_dist = 0;
end
end
% find smallest in dist, l_dist, r_dist
[d, ind] = max([dist l_dist]);
if ind == 2
switch_ = 1;
% switch to left lane
if la ~= 0
if lb ~= 0
cars_l = [cars_l(1:la, :); ...
cars(car_ind, :);...
cars_l(lb:size(cars_l,1),:)];% ˛ĺČë
else
cars_l = [cars_l(:,:); cars(car_ind,:)];
end
else
cars_l = [cars(car_ind,:);cars_l(:,:)];
end
cars(car_ind, :) = [];
end
end
if lanes ~= 1
% cars_l
plaza(lanes-1) = mat2cell(cars_l, size(cars_l,1), 4);
end
if lanes ~= W
% cars_r
plaza(lanes+1) = mat2cell(cars_r, size(cars_r,1), 4);
end
plaza(lanes) = mat2cell(cars, size(cars,1), 4);
num = size(cars,1);
if switch_ == 1
car_ind = car_ind-1;
end
switch_ = 0;
car_ind = car_ind + 1;
end
end
end