-
Notifications
You must be signed in to change notification settings - Fork 3
/
Figure_LargeScale.m
344 lines (289 loc) · 12.1 KB
/
Figure_LargeScale.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
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
% =========================================================================
% Process large-scale simulation results
%
% *Attention*: need simulation data to run this code.
% If there are no simulation results yet, please run main_LargeScale
% first.
% =========================================================================
clc; clear; close all;
% Data set
data_str = '1'; % 1. random ovm 2. manual ovm 3. homogeneous ovm
% Mix or not
mix = 1; % 0. all HDVs; 1. mix
% Type of the controller
controller_type = 3; % 1. centralized DeeP-LCC 2. MPC 3.distributed DeeP-LCC
% Choise of the weight coefficients
weight_choice = 3;
% Whether update equilibrium velocity
update_equilibrium_bool = 0;
% Type for HDV car-following model
hdv_type = 1; % 1. OVM 2. IDM
% Perturbation type
per_type = 3; % 1. sinuoid perturbation 2. small brake perturbation 3. large brake perturbation
% 4. larger brake perturbation
% 5. Perturbation on a vehicle in the middle of the platoon
% Time horizon for DeeP-LCC
T = 600;
% data number
i_data = 1;
% Whether print figures
output_bool = 1;
% ID
load('_data/ID_LargeScale.mat'); % record ID
ID_str = num2str(ID);
ID_str(find(ID_str==' ')) = '';
pos_cav = find(ID==1); % position of CAVs
n_vehicle = length(ID); % number of vehicles
n_cav = length(pos_cav); % number of CAVs
n_hdv = n_vehicle-n_cav; % number of HDVs
% Parameters in Simulation
total_time = 200; % Total Simulation Time
Tstep = 0.05; % Time Step
total_time_step = total_time/Tstep;
if mix
load(['_data\simulation_data\','LargeScale','_',num2str(i_data),'_ControllerType_',...
num2str(controller_type),'_PerType_',num2str(per_type),'_T_',num2str(T),...
'_UpdateEquilibrium_',num2str(update_equilibrium_bool),'_Weight_',num2str(weight_choice),'.mat']);
else
load(['_data\simulation_data\LargeScale_HDVs','_PerType_',num2str(per_type),'.mat']);
end
% -------------------------------------------------------------------------
% Plot Results
%--------------------------------------------------------------------------
% Simulation Time
begin_time = 0.05;
end_time = 200;
display_time = 150;
color_gray = [190 190 190]/255;
color_red = [244, 53, 124]/255;
color_blue = [67, 121, 227]/255;
color_black = [0 0 0];
color_orange = [255,132,31]/255;
label_size = 18;
total_size = 16;
line_width = 2;
line_width_3d = 0.7;
load('_data/ColorMap_RedWhiteBlue.mat');
color_cav = mymap_red_white_blue(linspace(108,76,5),:);
% Velocity
figure;
id_cav = 1;
plot(begin_time:Tstep:end_time,S(begin_time/Tstep:end_time/Tstep,1,2),'Color',color_black,'linewidth',line_width-0.5); hold on;
for i = 1:n_vehicle
if ID(i) == 0
plot(begin_time:Tstep:end_time,S(begin_time/Tstep:end_time/Tstep,i+1,2),'Color',color_gray,'linewidth',line_width-0.5); hold on; % line for velocity of HDVs
end
end
for i = 1:n_vehicle
if ID(i) == 1
velocity_plot(id_cav) = plot(begin_time:Tstep:end_time,S(begin_time/Tstep:end_time/Tstep,i+1,2),'Color',color_blue,'linewidth',line_width); hold on; % line for velocity of CAVs
id_cav = id_cav+1;
end
end
grid on;
% if mix
% l = legend(velocity_plot,'CAV 1','CAV 2','CAV 3','CAV 4','CAV 5');
% l.Interpreter = 'latex';
% l.FontSize = total_size;
% l.Box = 'off';
% l.Orientation = 'horizontal';
% l.Location = 'north';
% end
set(gca,'TickLabelInterpreter','latex','fontsize',total_size);
set(gca,'YLim',[8 22]);
set(gca,'XLim',[0 end_time]);
xl = xlabel('$t$ [$\mathrm{s}$]','fontsize',label_size,'Interpreter','latex','Color','k');
yl = ylabel('Velocity [$\mathrm{m/s}$]','fontsize',label_size,'Interpreter','latex','Color','k');
set(gcf,'Position',[250 150 750 300]);
% Spacing
figure;
id_cav = 1;
for i = 1:n_vehicle
if ID(i) == 1
plot(begin_time:Tstep:end_time,S(begin_time/Tstep:end_time/Tstep,i,1)-S(begin_time/Tstep:end_time/Tstep,i+1,1),'Color',color_blue,'linewidth',line_width); hold on; % line for velocity of CAVs
id_cav = id_cav+1;
end
end
grid on;
set(gca,'TickLabelInterpreter','latex','fontsize',total_size);
set(gca,'YLim',[0 30]);
set(gca,'XLim',[0 end_time]);
xl = xlabel('$t$ [$\mathrm{s}$]','fontsize',label_size,'Interpreter','latex','Color','k');
yl = ylabel('Spacing [$\mathrm{m}$]','fontsize',label_size,'Interpreter','latex','Color','k');
set(gcf,'Position',[250 450 500 300]);
fig = gcf;
fig.PaperPositionMode = 'auto';
% if output_bool
% if mix
% print(gcf,['figures/Controller_',num2str(controller_type),'_data_',num2str(i_data),'_T_',num2str(T),'_Tini_',num2str(Tini),'_N_',num2str(N),'_Spacing'],'-dpng','-r300');
% else
% print(gcf,'figures/HDVs_Spacing','-dpng','-r300');
% end
% end
if mix
% Computation time
figure;
plot(begin_time:Tstep:display_time,computation_time(begin_time/Tstep:display_time/Tstep),'Color',color_black,'linewidth',line_width/3); hold on;
set(gca,'TickLabelInterpreter','latex','fontsize',total_size);
plot(begin_time:Tstep:display_time,ones(length(begin_time:Tstep:display_time))*mean(computation_time(begin_time/Tstep:display_time/Tstep-1)),'--','Color',color_blue,'linewidth',line_width);
set(gca,'YLim',[0 0.12]);
set(gca,'XLim',[1.5 display_time]);
grid on;
xl = xlabel('$t$ [$\mathrm{s}$]','fontsize',label_size,'Interpreter','latex','Color','k');
ylabel('Computation Time [$\mathrm{s}$]','fontsize',label_size,'Interpreter','latex','Color','k');
%title(['Average time: ',num2str(mean(computation_time(begin_time/Tstep:display_time/Tstep-1)))],'fontsize',label_size,'Interpreter','latex','Color','k')
set(gcf,'Position',[650 150 750 300]);
fig = gcf;
fig.PaperPositionMode = 'auto';
fprintf('Mean computation time is %6.4f s\n', mean(computation_time(begin_time/Tstep:display_time/Tstep-1)));
if output_bool
print(gcf,'figures/LargeScale_ComputationTime','-painters','-dpng','-r600');
end
% Iteration number
if controller_type == 3 %|| controller_type == 4 || controller_type == 5
figure;
plot(begin_time:Tstep:display_time,iteration_num(begin_time/Tstep:display_time/Tstep),'Color',color_black,'linewidth',line_width/3); hold on;
set(gca,'TickLabelInterpreter','latex','fontsize',total_size);
plot(begin_time:Tstep:display_time,ones(length(begin_time:Tstep:display_time))*mean(iteration_num(begin_time/Tstep:display_time/Tstep-1)),'--','Color',color_blue,'linewidth',line_width);
set(gca,'YLim',[0 12]);
set(gca,'XLim',[1.5 display_time]);
grid on;
xl = xlabel('$t$ [$\mathrm{s}$]','fontsize',label_size,'Interpreter','latex','Color','k');
ylabel('Iteration number','fontsize',label_size,'Interpreter','latex','Color','k');
%title(['Average iteration: ',num2str(mean(iteration_num(Tini:display_time/Tstep-1)))],'fontsize',label_size,'Interpreter','latex','Color','k')
set(gcf,'Position',[1050 150 750 300]);
fig = gcf;
fig.PaperPositionMode = 'auto';
fprintf('Mean iteration number is %6.4f s\n', mean(iteration_num(begin_time/Tstep:display_time/Tstep-1)));
if output_bool
print(gcf,'figures/LargeScale_IterationNumber','-painters','-dpng','-r600');
end
end
end
% Shock wave figure
% Type 1
figure;
for i=1:n_vehicle
scatter((Tstep:Tstep:end_time)',S(:,i,1),15,S(:,i,2),'filled');
hold on;
end
colormap(jet);
colormap(flipud(colormap));
caxis([5 15]);
hcb = colorbar;
hcb.TickLabelInterpreter = 'latex';
hcb.FontSize = total_size;
hcb.Label.String = 'Velocity [$\mathrm{m/s}$]';
hcb.Label.FontSize = label_size;
hcb.Label.Interpreter = 'latex';
set(gca,'TickLabelInterpreter','latex','fontsize',total_size);
set(gca,'XLim',[0 120]);
set(gca,'YLim',[-1000 500]);
xl = xlabel('$t$ [$\mathrm{s}$]','fontsize',label_size,'Interpreter','latex','Color','k');
yl = ylabel('Position [$\mathrm{m}$]','fontsize',label_size,'Interpreter','latex','Color','k');
grid on;
set(gcf,'Position',[1550 850 550 400]);
fig = gcf;
fig.PaperPositionMode = 'auto';
if output_bool
if mix
print(gcf,'figures/LargeScale_CAVs_Wave','-painters','-dpng','-r600');
else
print(gcf,'figures/LargeScale_HDVs_Wave','-painters','-dpng','-r600');
end
end
% Type 2
% figure;
%
% [t_grid,p_grid] = meshgrid(0:0.1:120,-1000:1:500);
% t_vector = repmat(Tstep:Tstep:end_time,1,n_vehicle);
% p_vector = S(:,1,1)';
% for i = 2:n_vehicle
% p_vector = [p_vector S(:,i,1)'];
% end
% v_vector = S(:,1,2)';
% for i = 2:n_vehicle
% v_vector = [v_vector S(:,i,2)'];
% end
% v_grid = griddata(t_vector,p_vector,v_vector,t_grid,p_grid);
% mesh(t_grid,p_grid,v_grid);
% colormap(jet);
% colormap(flipud(colormap));
% caxis([5 15]);
% view([0,90]);
%
%
% hcb = colorbar;
% hcb.TickLabelInterpreter = 'latex';
% hcb.FontSize = total_size;
% hcb.Label.String = 'Velocity [$\mathrm{m/s}$]';
% hcb.Label.FontSize = label_size;
% hcb.Label.Interpreter = 'latex';
%
% set(gca,'TickLabelInterpreter','latex','fontsize',total_size);
% set(gca,'XLim',[0 120]);
% set(gca,'YLim',[-1000 500]);
%
% xl = xlabel('$t$ [$\mathrm{s}$]','fontsize',label_size,'Interpreter','latex','Color','k');
% yl = ylabel('Position [$\mathrm{m}$]','fontsize',label_size,'Interpreter','latex','Color','k');
%
% set(gcf,'Position',[1550 750 550 400]);
% Type 3
figure;
id_cav = 1;
plot3(zeros(total_time_step,1),begin_time:Tstep:end_time,S(begin_time/Tstep:end_time/Tstep,1,2),'Color',color_black,'linewidth',line_width/2); hold on;
for i = 1:n_vehicle
if ~mix
plot3(i*ones(total_time_step,1),begin_time:Tstep:end_time,S(begin_time/Tstep:end_time/Tstep,i+1,2),'Color',color_gray,'linewidth',line_width_3d); hold on; % line for velocity of HDVs
elseif ID(i) == 0
hdv_velocity_plot = plot3(i*ones(total_time_step,1),begin_time:Tstep:end_time,S(begin_time/Tstep:end_time/Tstep,i+1,2),'Color',color_gray,'linewidth',line_width_3d); hold on; % line for velocity of HDVs
end
end
if mix
for i = 1:n_vehicle
if ID(i) == 1
cav_velocity_plot = plot3(i*ones(total_time_step,1),begin_time:Tstep:end_time,S(begin_time/Tstep:end_time/Tstep,i+1,2),'Color',color_blue,'linewidth',line_width_3d); hold on; % line for velocity of CAVs
id_cav = id_cav+1;
end
end
end
grid on;
set(gca,'YLim',[0 150]);
set(gca,'ZLim',[0 30]);
set(gca,'TickLabelInterpreter','latex','fontsize',total_size);
xl = xlabel('Vehicle No.','fontsize',label_size,'Interpreter','latex','Color','k');
yl = ylabel('$t$ [$\mathrm{s}$]','fontsize',label_size,'Interpreter','latex','Color','k');
zl = zlabel('Velocity [$\mathrm{m/s}$]','fontsize',label_size,'Interpreter','latex','Color','k');
xl.Rotation = 30;
yl.Position = [-13 75 -5];
yl.Rotation = -10;
if mix
l = legend([hdv_velocity_plot,cav_velocity_plot],'HDV','CAV');
l.Interpreter = 'latex';
l.FontSize = total_size;
l.Box = 'off';
l.Orientation = 'horizontal';
l.Position = [0.65 0.7 0.1 0.1];
end
view([301 46]);
set(gcf,'Position',[650 850 800 400]);
fig = gcf;
fig.PaperPositionMode = 'auto';
if output_bool
if mix
print(gcf,'figures/LargeScale_CAVs_Velocity','-painters','-dpng','-r600');
else
print(gcf,'figures/LargeScale_HDVs_Velocity','-painters','-dpng','-r600');
end
end
% ======================
% Calculate fuel consumption
% ======================
fuel = 0;
for i=begin_time/Tstep:display_time/Tstep
R = 0.333 + 0.00108*S(i,2:end,2).^2 + 1.2*S(i,2:end,3);
F = 0.444 + 0.09*R.*S(i,2:end,2) + 0.054 * max(0,S(i,2:end,3)).^2.*S(i,2:end,2);
F(R <= 0) = 0.444;
fuel = fuel + sum(F)*Tstep;
end
fprintf('Fuel consumption is %6.4f mL \n', fuel);