-
Notifications
You must be signed in to change notification settings - Fork 7
/
create_figures.R
467 lines (393 loc) · 16.2 KB
/
create_figures.R
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
# create_figures.R
# Reads in all simulation output files from 'output/' folder
# Creates and exports summary figures
#---------- Setup ----------#
library(tidyverse)
library(readxl)
library(RColorBrewer)
theme_set(theme_bw())
#theme_set(theme_bw(base_size = 22))
theme_set(theme_bw(base_size = 11))
setwd("~/Documents/flexible-routing") # Set to file directory
outpath <- "C:/Users/hanzh/Documents/GitHub/flexible-routing/figures/" # Set to relative location of folder for figures
num_sims <- 6000 # As set in Python simulation code
#---------- Dataset Preparation ----------#
# Get simulation output files
#files <- list.files('output', pattern = '.xlsx')
# Import and combine datasets with individual runs
#sims <- data.frame()
#for (i in 1:length(files)){
# df <- read_xlsx(paste('output/', files[i], sep = ''), sheet = 1)
# sims <- rbind(sims, df)
#}
sims = read_xlsx('C:/Users/hanzh/Documents/GitHub/flexible-routing/output/results_2021-08-31_16-24-47.xlsx', sheet=1)
colnames(sims) = c('ID','Scenario','Customers','Strategy','Metric','Value')
# Rename scenarios
sims[(sims$Scenario == 'baseline'),]$Scenario = 'Baseline'
#sims[(sims$Scenario == 'baseline_k3'),]$Scenario = 'Medium Overlap'
#sims[(sims$Scenario == 'baseline_k1'),]$Scenario = 'Small Overlap'
#sims[(sims$Scenario == 'short_route'),]$Scenario = 'Short Route'
#sims[(sims$Scenario == 'long_route'),]$Scenario = 'Long Route'
#sims[(sims$Scenario == 'stochastic_customers'),]$Scenario = 'Stoch. Cust.'
#sims[(sims$Scenario == 'binomial'),]$Scenario = 'Bin. Demand'
#sims[(sims$Scenario == 'low_capacity'),]$Scenario = 'Low Capacity'
#sims[(sims$Scenario == 'high_capacity'),]$Scenario = 'High Capacity'
# Rename and reorder routing strategies
sims[(sims$Strategy == 'dedicated'),]$Strategy = 'Dedicated'
sims[(sims$Strategy == 'overlapped'),]$Strategy = 'AO'
sims[(sims$Strategy == 'overlapped closed'),]$Strategy = 'RAO'
sims[(sims$Strategy == 'reoptimization'),]$Strategy = 'Reoptimization'
sims[(sims$Strategy == 'fully flexible'),]$Strategy = 'FO'
sims[(sims$Strategy == 'fully flexible closed'),]$Strategy = 'RFO'
sims$Strategy = factor(sims$Strategy,
levels = c('Dedicated', 'AO', 'FO', 'RAO', 'RFO', 'Reoptimization'))
# Rename metrics
sims[(sims$Metric == 'total cost'),]$Metric = 'Total Cost'
sims[(sims$Metric == 'circular cost'),]$Metric = 'Circular Cost'
sims[(sims$Metric == 'radial cost'),]$Metric = 'Radial Cost'
sims[(sims$Metric == 'trip count'),]$Metric = 'Trip Count'
# Update ID to represent individual simulations
n_strat <- length(unique(sims$Strategy))
n_metric <- length(unique(sims$Metric))
sims <- sims %>%
group_by(Scenario, Customers) %>%
mutate(ID = rep(c(1:num_sims), each = n_strat*n_metric)) %>%
ungroup()
#---------- Baseline Graphs (Open Chain) ----------#
# Total cost
sims %>%
filter(Scenario == 'Baseline',
Metric == 'Total Cost',
Strategy %in% c('Dedicated','AO','FO','Reoptimization')) %>%
group_by(Customers, Strategy) %>%
summarise(Value = mean(Value)) %>%
ggplot() +
aes(x = Customers, y = Value, group = Strategy,
linetype = Strategy, shape = Strategy) +
geom_line(size = 0.5) + geom_point(size = 2) +
expand_limits(y=0) +
labs(x = 'Number of Customers', y = 'Cost') +
theme(aspect.ratio = 0.75)
ggsave(paste(outpath, 'total_cost.png', sep=''))
# Relative to Reoptimization
reopt <- sims %>%
filter(Scenario == "Baseline",
Metric == 'Total Cost',
Strategy == 'Reoptimization') %>%
group_by(Customers) %>%
summarise(avg_reopt = mean(Value))
sims %>%
filter(Scenario == "Baseline",
Metric == 'Total Cost',
Strategy %in% c('Dedicated','AO','FO','Reoptimization')) %>%
merge(reopt) %>%
group_by(Customers, Strategy) %>%
summarise(Value = mean(Value)/mean(avg_reopt)) %>%
ggplot() +
aes(x = Customers, y = Value, group = Strategy,
linetype = Strategy, shape = Strategy) +
geom_line(size = 0.5) + geom_point(size = 2) +
labs(x = 'Number of Customers', y = 'Cost (Rel. to Reoptimization)') +
expand_limits(y=0) +
scale_color_brewer(palette = "Dark2") +
theme(aspect.ratio = 0.75)
ggsave(paste(outpath, 'rel_cost.png', sep=''))
# Combined: Total & Relative to Reoptimization
sims %>%
filter(Scenario == "Baseline",
Metric == 'Total Cost',
Strategy %in% c('Dedicated','AO','FO','Reoptimization')) %>%
merge(reopt) %>%
merge(reopt) %>%
group_by(Customers, Strategy) %>%
summarise(`Total Cost` = mean(Value),
`Relative Cost` = mean(Value)/mean(avg_reopt)) %>%
gather(Metric, Value, `Total Cost`, `Relative Cost`) %>%
mutate(Metric = factor(Metric, levels = c("Total Cost", "Relative Cost"))) %>%
ggplot() +
aes(x = Customers, y = Value, group = Strategy,
linetype = Strategy, shape = Strategy) +
geom_line(size = 0.5) + geom_point(size = 2) +
labs(x = 'Number of Customers', y = 'Cost') +
expand_limits(y=0) +
facet_wrap(Metric ~., scales = 'free') +
theme(aspect.ratio = 1.25)
ggsave(paste(outpath, 'combined_total_rel_cost.png', sep=''))
# Circular and Radial Cost
sims %>%
filter(Scenario == 'Baseline',
Metric %in% c('Circular Cost', 'Radial Cost'),
Strategy %in% c('Dedicated','AO','FO','Reoptimization')) %>%
group_by(Customers, Strategy, Metric) %>%
summarise(Value = mean(Value)) %>%
ggplot() +
aes(x = Customers, y = Value, group = Strategy,
linetype = Strategy, shape = Strategy) +
geom_line(size = 0.5) + geom_point(size = 2) +
labs(x = 'Number of Customers', y = 'Cost') +
facet_wrap(Metric ~., nrow=1) +
theme(aspect.ratio = 2) +
scale_fill_grey(start = 0.4) +
theme(aspect.ratio = 1.25)
ggsave(paste(outpath, 'cost_breakdown.png', sep=''))
sims %>%
filter(Scenario == 'Baseline',
Metric %in% c('Circular Cost', 'Radial Cost'),
Strategy %in% c('Dedicated','AO','FO','Reoptimization')) %>%
group_by(Customers, Strategy, Metric) %>%
summarise(Value = mean(Value)) %>%
ggplot() +
aes(x = Customers, y = Value,
group = Metric, fill = Metric) +
geom_area() +
labs(x = 'Number of Customers', y = 'Cost') +
facet_wrap(Strategy ~., nrow=1) +
theme(aspect.ratio = 2, legend.position = 'bottom', legend.title = element_blank()) +
scale_fill_grey(start = 0.4)
ggsave(paste(outpath, 'cost_breakdown_area.png', sep=''))
# Number of trips
sims %>%
filter(Scenario == 'Baseline',
Metric == 'Trip Count',
Strategy %in% c('Dedicated','AO','FO','Reoptimization')) %>%
mutate(Customers = factor(Customers)) %>%
group_by(Customers, Strategy) %>%
summarise(Value = mean(Value)) %>%
ggplot() +
aes(x = Customers, y = Value, fill = Strategy) +
geom_bar(stat = 'identity', position = 'dodge') +
labs(x = 'Number of Customers', y = 'Trip Count') +
theme(aspect.ratio = 0.75) +
scale_fill_grey(start = 0.3)
#ggsave(paste(outpath, 'trips.png', sep=''))
# Distribution of individual runs' costs by strategy
sims %>%
filter(Scenario == 'Baseline',
Customers %in% c(5,20,80),
Metric == 'Total Cost') %>%
group_by(Customers, Strategy) %>%
summarise(mean(Value),
median(Value),
sd(Value))
sims %>%
filter(Scenario == 'Baseline',
Customers %in% c(5,20,80),
Metric == 'Total Cost',
Strategy %in% c('Dedicated','AO','FO','Reoptimization')) %>%
mutate(`Number of Customers` = factor(Customers)) %>%
ggplot() +
aes(x = Value, fill=`Number of Customers`) +
geom_histogram(color='black', bins = 50) +
facet_grid(Strategy~.) +
labs(x = 'Total Cost', y = 'Count') +
theme(aspect.ratio = 0.25, legend.position="top") +
scale_fill_grey(start = 0.3, end = 0.9)
ggsave(paste(outpath, 'hist_total.png', sep=''))
# Percent of sims where AO did better than dedicated
sims %>%
filter(Scenario == 'Baseline', Metric == 'Total Cost') %>%
spread(Strategy, Value) %>%
group_by(Scenario, Customers) %>%
summarise(`Lower Cost` = 100*sum(AO < Dedicated) / num_sims,
`Equal Cost` = 100*sum(AO == Dedicated) / num_sims,
`Higher Cost` = 100*sum(AO > Dedicated) / num_sims)
#---------- Baseline Graphs (Closed Chain) ----------#
# Total cost
sims %>%
filter(Scenario == 'Baseline',
Metric == 'Total Cost',
Strategy %in% c('Dedicated','AO','RAO','FO','RFO','Reoptimization')) %>%
mutate(Customers= factor(Customers)) %>%
group_by(Customers, Strategy) %>%
summarise(Value = mean(Value)) %>%
ggplot() +
aes(x = Customers, y = Value, fill = Strategy) +
geom_bar(stat = 'identity', position = 'dodge') +
expand_limits(y=0) +
labs(x = 'Number of Customers', y = 'Cost') +
theme(aspect.ratio = 1) +
scale_fill_grey(start = 0.3)
ggsave(paste(outpath, 'total_cost_CLOSED.png', sep=''))
# Relative to Reoptimization -- LINE
reopt <- sims %>%
filter(Scenario == "Baseline",
Metric == 'Total Cost',
Strategy == 'Reoptimization') %>%
group_by(Customers) %>%
summarise(avg_reopt = mean(Value))
sims %>%
filter(Scenario == "Baseline",
Metric == 'Total Cost',
Strategy %in% c('AO','FO','RAO','RFO')) %>%
merge(reopt) %>%
mutate(Customers= factor(Customers)) %>%
group_by(Customers, Strategy) %>%
summarise(Value = mean(Value)/mean(avg_reopt) - 1) %>%
ggplot() +
aes(x = Customers, y = Value, group = Strategy,
linetype = Strategy, shape = Strategy) +
geom_line(size = 0.5) + geom_point(size = 2) +
labs(x = 'Number of Customers', y = 'Cost (% Above Reoptimization)') +
expand_limits(y=0) +
theme(aspect.ratio = 0.75) +
scale_y_continuous(labels = scales::percent) +
scale_fill_grey(start = 0.3)
ggsave(paste(outpath, 'rel_cost_CLOSED_line.png', sep=''))
# Relative to Reoptimization -- BAR
reopt <- sims %>%
filter(Scenario == "Baseline",
Metric == 'Total Cost',
Strategy == 'Reoptimization') %>%
group_by(Customers) %>%
summarise(avg_reopt = mean(Value))
sims %>%
filter(Scenario == "Baseline",
Metric == 'Total Cost',
Strategy %in% c('AO','FO','RAO','RFO')) %>%
merge(reopt) %>%
mutate(Customers= factor(Customers)) %>%
group_by(Customers, Strategy) %>%
summarise(Value = mean(Value)/mean(avg_reopt) - 1) %>%
ggplot() +
aes(x = Customers, y = Value, fill = Strategy) +
geom_bar(stat = 'identity', position = 'dodge') +
labs(x = 'Number of Customers', y = 'Cost (% Above Reoptimization)') +
expand_limits(y=0) +
theme(aspect.ratio = 0.75) +
scale_y_continuous(labels = scales::percent) +
scale_fill_grey(start = 0.3)
ggsave(paste(outpath, 'rel_cost_CLOSED_bar.png', sep=''))
###################### OLD (BEFORE 2021) #############################
#---------- Scenario Comparisons ----------#
#--- Overlap size ---#
# Compare overlapped strategies for baseline, small overlap, and medium overlap
# scenarios to dedicated strategy (same for all scenarios)
sims %>%
filter(Scenario %in% c('Baseline', 'Medium Overlap', 'Small Overlap'),
Metric != 'Trip Count') %>%
group_by(Scenario, Customers, Strategy, Metric) %>%
summarise(Value = mean(Value)) %>%
ungroup() %>%
spread(Strategy, Value) %>%
mutate(Value = Overlapped / Dedicated,
Customers = factor(Customers)) %>%
ggplot() +
aes(x = Customers, y = Value, fill = Scenario) +
geom_bar(stat = 'identity', position = 'dodge') +
geom_hline(yintercept = 1.0, alpha = 0.75, linetype = 'dashed') +
labs(x = 'Number of Customers', y = 'Cost (Rel. to Dedicated)') +
facet_wrap(Metric ~.) +
theme(aspect.ratio = 1.25) +
scale_fill_brewer(palette = "Dark2")
#scale_fill_grey(start = 0.3)
ggsave(paste(outpath, 'overlap_size_cost.png', sep=''))
#---Capacity---#
sims %>%
filter(Scenario %in% c('Baseline', 'Low Capacity', 'High Capacity'),
Metric == 'Total Cost') %>%
mutate(Customers = factor(Customers)) %>%
group_by(Scenario, Customers, Strategy) %>%
summarise(Value = mean(Value)) %>%
spread(Scenario, Value) %>%
mutate(`High Capacity` = (`High Capacity`) / Baseline,
`Low Capacity` = (`Low Capacity`) / Baseline) %>%
gather(Scenario, Value, Baseline, `High Capacity`, `Low Capacity`) %>%
filter(Scenario != 'Baseline') %>%
ggplot() +
aes(x = Customers, y = Value, fill = Scenario) +
geom_bar(stat = 'identity', position = 'dodge') +
geom_hline(yintercept = 1.0, alpha = 0.75, linetype = 'dashed') +
labs(x = 'Number of Customers', y = 'Cost (Rel. to Baseline)') +
facet_wrap(Strategy ~.) +
theme(aspect.ratio = 1.25) +
scale_fill_brewer(palette = "Dark2")
#scale_fill_grey(start = 0.3)
ggsave(paste(outpath, 'capacity_cost.png', sep=''))
#---Route size---#
# Circular and radial cost breakdown
sims %>%
filter(Scenario %in% c('Baseline', 'Long Route', 'Short Route'),
Metric %in% c('Circular Cost', 'Radial Cost'),
!Customers %in% c(4,5)) %>%
group_by(Customers, Strategy, Scenario, Metric) %>%
summarise(Value = mean(Value)) %>%
ggplot() +
aes(x = Customers, y = Value,
group = Metric, fill = Metric) +
geom_area() +
labs(x = 'Number of Customers', y = 'Cost') +
facet_grid(Strategy ~ Scenario) +
scale_fill_brewer(palette = "Dark2") +
expand_limits(x = 0, y = 0) +
theme(aspect.ratio = 0.75)
#scale_fill_grey(start = 0.4)
#ggsave(paste(outpath, 'route_size_cost_breakdown.png', sep=''))
# Combined: Total & Relative to Reoptimization
reopt_routesize <- sims %>%
filter(Scenario %in% c('Baseline', 'Long Route', 'Short Route'),
Metric == 'Total Cost',
Strategy == 'Reoptimization',
!Customers %in% c(4,5)) %>%
group_by(Customers, Scenario) %>%
summarise(avg_reopt = mean(Value))
sims %>%
filter(Scenario %in% c('Baseline', 'Long Route', 'Short Route'),
Metric == 'Total Cost',
!Customers %in% c(4,5)) %>%
merge(reopt_routesize) %>%
group_by(Customers, Strategy, Scenario) %>%
summarise(`Total Cost` = mean(Value),
`Relative Cost` = mean(Value)/mean(avg_reopt)) %>%
gather(Metric, Value, `Total Cost`, `Relative Cost`) %>%
mutate(Metric = factor(Metric, levels = c("Total Cost", "Relative Cost"))) %>%
ggplot() +
aes(x = Customers, y = Value,
group = Strategy, color = Strategy,
linetype = Strategy, shape = Strategy) +
geom_line(size = 1) + geom_point(size = 2) +
labs(x = 'Number of Customers', y = 'Cost') +
expand_limits(y=0) +
facet_grid(Metric ~ Scenario, scales = 'free') +
scale_color_brewer(palette = "Dark2") +
expand_limits(x = 0, y = 0) +
theme(aspect.ratio = 1.25)
ggsave(paste(outpath, 'route_size_cost.png', sep=''))
#--- Demand distributions ---#
# Percent of sims where overlapped did better than dedicated
share_table <- sims %>%
filter(Scenario %in% c('Baseline', 'Bin. Demand', 'Stoch. Cust.'),
Metric == 'Total Cost') %>%
spread(Strategy, Value) %>%
group_by(Scenario, Customers) %>%
summarise(`Lower Cost` = 100*sum(Overlapped < Dedicated) / num_sims,
`Equal Cost` = 100*sum(Overlapped == Dedicated) / num_sims,
`Higher Cost` = 100*sum(Overlapped > Dedicated) / num_sims)
share_table
share_table %>%
gather(`Overlapped Cost`, Percent, `Lower Cost`, `Equal Cost`, `Higher Cost`) %>%
#mutate(Customers = factor(Customers)) %>%
ggplot() +
aes(x = Customers, y = Percent, color = Scenario, group = Scenario,
linetype = Scenario, shape = Scenario) +
geom_line(size = 1) + geom_point(size = 2) +
labs(x = 'Number of Customers') +
facet_wrap(`Overlapped Cost` ~.) +
scale_color_brewer(palette = "Dark2") +
expand_limits(x = 0, y = 0) +
theme(aspect.ratio = 1.25)
ggsave(paste(outpath, 'dem_scen_cost.png', sep=''))
# Comparison of scenarios' distributions within individual strategies
sims %>%
filter(Scenario %in% c('Baseline', 'Bin. Demand', 'Stoch. Cust.'),
Customers %in% c(10,80),
Metric == 'Total Cost') %>%
mutate(`Number of Customers` = factor(Customers)) %>%
ggplot() +
aes(x = Value, fill=`Number of Customers`) +
geom_histogram(color='black', bins = 40) +
facet_grid(Scenario~Strategy) +
labs(x = 'Total Cost', y = 'Count') +
scale_fill_brewer(palette = "Dark2") +
theme(aspect.ratio = 0.5, legend.position = 'top')
#scale_fill_grey(start = 0.4)
ggsave(paste(outpath, 'dem_scen_hists.png', sep=''))