-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot.jl
More file actions
666 lines (588 loc) · 19 KB
/
plot.jl
File metadata and controls
666 lines (588 loc) · 19 KB
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
function plot_instancee(env::DVSPEnv; kwargs...)
return plot_instance(env.instance.static_instance; kwargs...)
end
"""
$TYPEDSIGNATURES
Plot a given DVSPState showing depot, must-dispatch requests, and postponable requests.
"""
function plot_state(
state::DVSPState;
customer_markersize=6,
depot_markersize=8,
alpha_depot=0.8,
depot_color=:lightgreen,
must_dispatch_color=:red,
postponable_color=:lightblue,
show_axis_labels=true,
markerstrokewidth=0.5,
kwargs...,
)
# Get coordinates from the state instance
coordinates = coordinate(state)
start_times = start_time(state)
# Extract x and y coordinates
x = [p.x for p in coordinates]
y = [p.y for p in coordinates]
# Create the plot
plot_args = Dict(
:legend => :topleft, :title => "DVSP State - Epoch $(state.current_epoch)"
)
if show_axis_labels
plot_args[:xlabel] = "x coordinate"
plot_args[:ylabel] = "y coordinate"
end
# Merge with kwargs
for (k, v) in kwargs
plot_args[k] = v
end
fig = plot(; plot_args...)
# Plot depot (always the first coordinate)
scatter!(
fig,
[x[1]],
[y[1]];
label="Depot",
markercolor=depot_color,
marker=:rect,
markersize=depot_markersize,
alpha=alpha_depot,
markerstrokewidth=markerstrokewidth,
)
# Plot must-dispatch customers
if sum(state.is_must_dispatch) > 0
must_dispatch_indices = findall(state.is_must_dispatch)
scatter!(
fig,
x[must_dispatch_indices],
y[must_dispatch_indices];
label="Must-dispatch requests",
markercolor=must_dispatch_color,
marker=:star5,
markersize=customer_markersize,
marker_z=start_times[must_dispatch_indices],
colormap=:plasma,
markerstrokewidth=markerstrokewidth,
)
end
# Plot postponable customers
if sum(state.is_postponable) > 0
postponable_indices = findall(state.is_postponable)
scatter!(
fig,
x[postponable_indices],
y[postponable_indices];
label="Postponable requests",
markercolor=postponable_color,
marker=:utriangle,
markersize=customer_markersize,
marker_z=start_times[postponable_indices],
colormap=:viridis,
markerstrokewidth=markerstrokewidth,
)
end
return fig
end
"""
$TYPEDSIGNATURES
Plot a given DVSPState with routes overlaid, showing depot, requests, and vehicle routes.
Routes should be provided as a vector of vectors, where each inner vector contains the
indices of locations visited by that route (excluding the depot).
"""
function plot_routes(
state::DVSPState,
routes::Vector{Vector{Int}};
route_colors=nothing,
route_linewidth=3, # Increased from 2 to 3
route_alpha=0.7,
show_route_labels=true,
kwargs...,
)
# Start with the basic state plot
fig = plot_state(state; kwargs...)
# Get coordinates for route plotting
coordinates = coordinate(state)
x = [p.x for p in coordinates]
y = [p.y for p in coordinates]
# Depot coordinates (always first)
x_depot = x[1]
y_depot = y[1]
# Default route colors if not provided
if isnothing(route_colors)
route_colors = [:blue, :purple, :orange, :brown, :pink, :gray, :olive, :cyan]
end
# Plot each route
for (route_idx, route) in enumerate(routes)
if !isempty(route)
# Create route path: depot -> customers -> depot
route_x = vcat(x_depot, x[route], x_depot)
route_y = vcat(y_depot, y[route], y_depot)
# Select color for this route
color = route_colors[(route_idx - 1) % length(route_colors) + 1]
# Plot the route with more visible styling
label = show_route_labels ? "Route $route_idx" : nothing
plot!(
fig,
route_x,
route_y;
# color=color,
linewidth=route_linewidth,
alpha=1.0, # Make routes fully opaque
label=label,
linestyle=:solid,
)
end
end
return fig
end
"""
$TYPEDSIGNATURES
Plot a given DVSPState with routes overlaid. This version accepts routes as a single
vector where routes are separated by depot visits (index 1).
"""
function plot_routes(state::DVSPState, routes::Vector{Int}; kwargs...)
# Convert single route vector to vector of route vectors
route_vectors = Vector{Int}[]
current_route = Int[]
for location in routes
if location == 1 # Depot visit indicates end of route
if !isempty(current_route)
push!(route_vectors, copy(current_route))
empty!(current_route)
end
else
push!(current_route, location)
end
end
# Add the last route if it doesn't end with depot
if !isempty(current_route)
push!(route_vectors, current_route)
end
return plot_routes(state, route_vectors; kwargs...)
end
"""
$TYPEDSIGNATURES
Plot a given DVSPState with routes overlaid. This version accepts routes as a BitMatrix
where entry (i,j) = true indicates an edge from location i to location j.
"""
function plot_routes(state::DVSPState, routes::BitMatrix; kwargs...)
route_vectors = decode_bitmatrix_to_routes(routes)
return plot_routes(state, route_vectors; kwargs...)
end
"""
$TYPEDSIGNATURES
Plot multiple epochs side by side from a vector of DataSample objects.
Each DataSample should contain an instance (DVSPState) and optionally y_true (routes).
All subplots will use the same xlims and ylims to show the dynamics clearly.
"""
function plot_epochs(
data_samples::Vector{<:DataSample};
plot_routes_flag=true,
cols=nothing,
figsize=(1800, 600),
margin=0.05,
legend_margin_factor=0.15,
titlefontsize=14,
guidefontsize=12,
legendfontsize=11,
tickfontsize=10,
show_axis_labels=false,
show_colorbar=false,
kwargs...,
)
n_epochs = length(data_samples)
if n_epochs == 0
error("No data samples provided")
end
# Determine grid layout
if isnothing(cols)
cols = min(n_epochs, 3) # Default to max 3 columns
end
rows = ceil(Int, n_epochs / cols)
# Calculate global xlims and ylims from all states
all_coordinates = []
for sample in data_samples
if !isnothing(sample.instance)
coords = coordinate(sample.instance)
append!(all_coordinates, coords)
end
end
if isempty(all_coordinates)
error("No valid coordinates found in data samples")
end
xlims = (
minimum(p.x for p in all_coordinates) - margin,
maximum(p.x for p in all_coordinates) + margin,
)
# Add extra margin at the top for legend space
y_min = minimum(p.y for p in all_coordinates) - margin
y_max = maximum(p.y for p in all_coordinates) + margin
y_range = y_max - y_min
legend_margin = y_range * legend_margin_factor
ylims = (y_min, y_max + legend_margin)
# Calculate global color limits for consistent scaling across subplots
all_start_times = []
for sample in data_samples
if !isnothing(sample.instance)
times = start_time(sample.instance)
append!(all_start_times, times)
end
end
clims = if !isempty(all_start_times)
(minimum(all_start_times), maximum(all_start_times))
else
(0.0, 1.0) # Default range
end
# Create subplots
plots = []
for (i, sample) in enumerate(data_samples)
state = sample.instance
if isnothing(state)
# Create empty plot if no state
fig = plot(;
xlims=xlims,
ylims=ylims,
title="Epoch $i (No Data)",
titlefontsize=titlefontsize,
guidefontsize=guidefontsize,
tickfontsize=tickfontsize,
legend=false,
kwargs...,
)
else
# Plot with or without routes
if plot_routes_flag && !isnothing(sample.y_true)
fig = plot_routes(
state,
sample.y_true;
xlims=xlims,
ylims=ylims,
clims=clims,
colorbar=false,
title="Epoch $(state.current_epoch)",
titlefontsize=titlefontsize,
guidefontsize=guidefontsize,
legendfontsize=legendfontsize,
tickfontsize=tickfontsize,
show_axis_labels=show_axis_labels,
markerstrokewidth=0.5,
show_route_labels=false,
kwargs...,
)
else
fig = plot_state(
state;
xlims=xlims,
ylims=ylims,
clims=clims,
colorbar=false,
title="Epoch $(state.current_epoch)",
titlefontsize=titlefontsize,
guidefontsize=guidefontsize,
legendfontsize=legendfontsize,
tickfontsize=tickfontsize,
show_axis_labels=show_axis_labels,
markerstrokewidth=0.5,
kwargs...,
)
end
end
push!(plots, fig)
end
# Calculate dynamic figure size if not specified
if figsize == (1800, 600) # Using default size
plot_width = 600 * cols
plot_height = 500 * rows
figsize = (plot_width, plot_height)
end
# Combine plots in a grid layout with optional shared colorbar
if show_colorbar
combined_plot = plot(
plots...;
layout=(rows, cols),
size=figsize,
link=:both,
colorbar=:right,
clims=clims,
)
else
combined_plot = plot(
plots...; layout=(rows, cols), size=figsize, link=:both, clims=clims
)
end
return combined_plot
end
"""
$TYPEDSIGNATURES
Plot multiple epochs side by side, optionally filtering to specific epoch indices.
"""
function plot_epochs(
data_samples::Vector{<:DataSample}, epoch_indices::Vector{Int}; kwargs...
)
filtered_samples = data_samples[epoch_indices]
return plot_epochs(filtered_samples; kwargs...)
end
"""
$TYPEDSIGNATURES
Create an animated GIF showing the evolution of states and routes over epochs.
Each frame shows the state and routes for one epoch.
"""
function animate_epochs(
data_samples::Vector{<:DataSample};
filename="dvsp_animation.gif",
fps=1,
figsize=(800, 600),
margin=0.1,
legend_margin_factor=0.2,
titlefontsize=16,
guidefontsize=14,
legendfontsize=12,
tickfontsize=11,
show_axis_labels=true,
kwargs...,
)
n_epochs = length(data_samples)
if n_epochs == 0
error("No data samples provided")
end
# Calculate global limits for consistent scaling
all_coordinates = []
for sample in data_samples
if !isnothing(sample.instance)
coords = coordinate(sample.instance)
append!(all_coordinates, coords)
end
end
if isempty(all_coordinates)
error("No valid coordinates found in data samples")
end
xlims = (
minimum(p.x for p in all_coordinates) - margin,
maximum(p.x for p in all_coordinates) + margin,
)
# Add extra margin at the top for legend space
y_min = minimum(p.y for p in all_coordinates) - margin
y_max = maximum(p.y for p in all_coordinates) + margin
y_range = y_max - y_min
legend_margin = y_range * legend_margin_factor
ylims = (y_min, y_max + legend_margin)
# Calculate global color limits
all_start_times = []
for sample in data_samples
if !isnothing(sample.instance)
times = start_time(sample.instance)
append!(all_start_times, times)
end
end
clims = if !isempty(all_start_times)
(minimum(all_start_times), maximum(all_start_times))
else
(0.0, 1.0)
end
# Helper function to check if routes exist and are non-empty
function has_routes(routes)
if isnothing(routes)
return false
elseif routes isa Vector{Vector{Int}}
return any(!isempty(route) for route in routes)
elseif routes isa Vector{Int}
return !isempty(routes)
elseif routes isa BitMatrix
return any(routes)
else
return false
end
end
# Create frame plan: determine which epochs have routes
frame_plan = []
for (epoch_idx, sample) in enumerate(data_samples)
# Always add state frame
push!(frame_plan, (epoch_idx, :state))
# Add routes frame only if routes exist
if has_routes(sample.y_true)
push!(frame_plan, (epoch_idx, :routes))
end
end
total_frames = length(frame_plan)
# Create animation with dynamic frame plan
anim = @animate for frame_idx in 1:total_frames
epoch_idx, frame_type = frame_plan[frame_idx]
sample = data_samples[epoch_idx]
state = sample.instance
if isnothing(state)
# Empty frame for missing data
plot(;
xlims=xlims,
ylims=ylims,
title="Epoch $epoch_idx (No Data)",
titlefontsize=titlefontsize,
guidefontsize=guidefontsize,
tickfontsize=tickfontsize,
legend=false,
size=figsize,
kwargs...,
)
else
if frame_type == :routes
# Show state with routes
plot_routes(
state,
sample.y_true;
xlims=xlims,
ylims=ylims,
clims=clims,
title="Epoch $(state.current_epoch) - Routes Dispatched",
titlefontsize=titlefontsize,
guidefontsize=guidefontsize,
legendfontsize=legendfontsize,
tickfontsize=tickfontsize,
show_axis_labels=show_axis_labels,
markerstrokewidth=0.5,
show_route_labels=false,
size=figsize,
kwargs...,
)
else # frame_type == :state
# Show state only
plot_state(
state;
xlims=xlims,
ylims=ylims,
clims=clims,
title="Epoch $(state.current_epoch) - Available Requests",
titlefontsize=titlefontsize,
guidefontsize=guidefontsize,
legendfontsize=legendfontsize,
tickfontsize=tickfontsize,
show_axis_labels=show_axis_labels,
markerstrokewidth=0.5,
size=figsize,
kwargs...,
)
end
end
end
# Save as GIF
gif(anim, filename; fps=fps)
return anim
end
# """
# $TYPEDSIGNATURES
# Plot the environment of a DVSPEnv, restricted to the given `epoch_indices` (all epoch if not given).
# """
# function plot_environment(
# env::DVSPEnv;
# customer_markersize=4,
# depot_markersize=7,
# alpha_depot=0.8,
# depot_color=:lightgreen,
# epoch_indices=nothing,
# kwargs...,
# )
# draw_all_epochs!(env)
# epoch_appearance = env.request_epoch
# coordinates = coordinate(get_state(env))
# epoch_indices = isnothing(epoch_indices) ? get_epoch_indices(env) : epoch_indices
# xlims = (minimum(c.x for c in coordinates), maximum(c.x for c in coordinates))
# ylims = (minimum(c.y for c in coordinates), maximum(c.y for c in coordinates))
# fig = plot(;
# legend=:topleft,
# xlabel="x coordinate",
# ylabel="y coordinate",
# xlims,
# ylims,
# kwargs...,
# )
# for epoch in epoch_indices
# requests = findall(epoch_appearance .== epoch)
# x = [coordinates[request].x for request in requests]
# y = [coordinates[request].y for request in requests]
# scatter!(
# fig, x, y; label="Epoch $epoch", marker=:circle, markersize=customer_markersize
# )
# end
# scatter!(
# fig,
# [coordinates[1].x],
# [coordinates[1].y];
# label="Depot",
# markercolor=depot_color,
# marker=:rect,
# markersize=depot_markersize,
# alpha=alpha_depot,
# )
# return fig
# end
# """
# $TYPEDSIGNATURES
# Plot the given `routes`` for a VSP `state`.
# """
# function plot_epoch(state::DVSPState, routes; kwargs...)
# (; coordinate, start_time) = state.instance
# x_depot = coordinate[1].x
# y_depot = coordinate[1].y
# X = [p.x for p in coordinate]
# Y = [p.y for p in coordinate]
# markersize = 5
# fig = plot(;
# legend=:topleft, xlabel="x", ylabel="y", clim=(0.0, maximum(start_time)), kwargs...
# )
# for route in routes
# x_points = vcat(x_depot, X[route], x_depot)
# y_points = vcat(y_depot, Y[route], y_depot)
# plot!(fig, x_points, y_points; label=nothing)
# end
# scatter!(
# fig,
# [x_depot],
# [y_depot];
# label="depot",
# markercolor=:lightgreen,
# markersize,
# marker=:rect,
# )
# if sum(state.is_postponable) > 0
# scatter!(
# fig,
# X[state.is_postponable],
# Y[state.is_postponable];
# label="Postponable customers",
# marker_z=start_time[state.is_postponable],
# markersize,
# colormap=:turbo,
# marker=:utriangle,
# )
# end
# if sum(state.is_must_dispatch) > 0
# scatter!(
# fig,
# X[state.is_must_dispatch],
# Y[state.is_must_dispatch];
# label="Must-dispatch customers",
# marker_z=start_time[state.is_must_dispatch],
# markersize,
# colormap=:turbo,
# marker=:star5,
# )
# end
# return fig
# end
# """
# $TYPEDSIGNATURES
# Create a plot of routes for each epoch.
# """
# function plot_routes(env::DVSPEnv, routes; epoch_indices=nothing, kwargs...)
# reset!(env)
# epoch_indices = isnothing(epoch_indices) ? get_epoch_indices(env) : epoch_indices
# coordinates = env.config.static_instance.coordinate
# xlims = (minimum(c.x for c in coordinates), maximum(c.x for c in coordinates))
# ylims = (minimum(c.y for c in coordinates), maximum(c.y for c in coordinates))
# figs = map(epoch_indices) do epoch
# s = next_epoch!(env)
# fig = plot_epoch(
# s, state_route_from_env_routes(env, routes[epoch]); xlims, ylims, kwargs...
# )
# apply_decision!(env, routes[epoch])
# return fig
# end
# return figs
# end