-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDynamicVehicleScheduling.jl
More file actions
169 lines (143 loc) · 4.76 KB
/
DynamicVehicleScheduling.jl
File metadata and controls
169 lines (143 loc) · 4.76 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
module DynamicVehicleScheduling
using ..Utils
using Base: @kwdef
using DataDeps: @datadep_str
using DocStringExtensions: TYPEDEF, TYPEDFIELDS, TYPEDSIGNATURES
using Flux: Chain, Dense
using Graphs
using HiGHS
using InferOpt: LinearMaximizer
using IterTools: partition
using JSON
using JuMP
using Plots: plot, plot!, scatter!, @animate, Plots, gif
using Printf: @printf, @sprintf
using Random: Random, AbstractRNG, MersenneTwister, seed!, randperm
using Requires: @require
using Statistics: mean, quantile
include("utils.jl")
# static vsp stuff
include("static_vsp/instance.jl")
include("static_vsp/parsing.jl")
include("static_vsp/solution.jl")
include("static_vsp/plot.jl")
include("instance.jl")
include("state.jl")
include("scenario.jl")
include("environment.jl")
include("plot.jl")
include("maximizer.jl")
include("anticipative_solver.jl")
include("features.jl")
include("policy.jl")
"""
$TYPEDEF
Abstract type for dynamic vehicle scheduling benchmarks.
# Fields
$TYPEDFIELDS
"""
@kwdef struct DynamicVehicleSchedulingBenchmark <: AbstractDynamicBenchmark{true}
"maximum number of customers entering the system per epoch"
max_requests_per_epoch::Int = 10
"time between decision and dispatch of a vehicle"
Δ_dispatch::Float64 = 1.0
"duration of an epoch"
epoch_duration::Float64 = 1.0
"whether to use two-dimensional features"
two_dimensional_features::Bool = false
end
"""
$TYPEDSIGNATURES
Generate a dataset for the dynamic vehicle scheduling benchmark.
Returns a vector of [`DataSample`](@ref) objects, each containing an [`Instance`](@ref).
The dataset is generated from pre-existing DVRPTW files.
"""
function Utils.generate_dataset(b::DynamicVehicleSchedulingBenchmark, dataset_size::Int=1)
(; max_requests_per_epoch, Δ_dispatch, epoch_duration, two_dimensional_features) = b
files = readdir(datadep"dvrptw"; join=true)
dataset_size = min(dataset_size, length(files))
return [
DataSample(;
info=Instance(
read_vsp_instance(files[i]);
max_requests_per_epoch,
Δ_dispatch,
epoch_duration,
two_dimensional_features,
),
) for i in 1:dataset_size
]
end
"""
$TYPEDSIGNATURES
Creates an environment from an [`Instance`](@ref) of the dynamic vehicle scheduling benchmark.
The seed of the environment is randomly generated using the provided random number generator.
"""
function Utils.generate_environment(
::DynamicVehicleSchedulingBenchmark, instance::Instance, rng::AbstractRNG; kwargs...
)
seed = rand(rng, 1:typemax(Int))
return DVSPEnv(instance; seed)
end
"""
$TYPEDSIGNATURES
Returns a linear maximizer for the dynamic vehicle scheduling benchmark, of the form:
θ ↦ argmax_{y} θᵀg(y) + h(y)
"""
function Utils.generate_maximizer(::DynamicVehicleSchedulingBenchmark)
return LinearMaximizer(oracle; g, h)
end
"""
$TYPEDSIGNATURES
Generate a scenario for the dynamic vehicle scheduling benchmark.
This is a wrapper around the generic scenario generation function.
"""
function Utils.generate_scenario(b::DynamicVehicleSchedulingBenchmark, args...; kwargs...)
return Utils.generate_scenario(args...; kwargs...)
end
"""
$TYPEDSIGNATURES
Generate an anticipative solution for the dynamic vehicle scheduling benchmark.
The solution is computed using the anticipative solver with the benchmark's feature configuration.
"""
function Utils.generate_anticipative_solution(
b::DynamicVehicleSchedulingBenchmark, args...; kwargs...
)
return anticipative_solver(
args...; kwargs..., two_dimensional_features=b.two_dimensional_features
)
end
"""
$TYPEDSIGNATURES
Generate baseline policies for the dynamic vehicle scheduling benchmark.
Returns a tuple containing:
- `lazy`: A policy that dispatches vehicles only when they are ready
- `greedy`: A policy that dispatches vehicles to the nearest customer
"""
function Utils.generate_policies(b::DynamicVehicleSchedulingBenchmark)
lazy = Policy(
"Lazy",
"Lazy policy that dispatches vehicles only when they are ready.",
lazy_policy,
)
greedy = Policy(
"Greedy",
"Greedy policy that dispatches vehicles to the nearest customer.",
greedy_policy,
)
return (lazy, greedy)
end
"""
$TYPEDSIGNATURES
Generate a statistical model for the dynamic vehicle scheduling benchmark.
The model is a simple linear chain with a single dense layer that maps features to a scalar output.
The input dimension depends on whether two-dimensional features are used (2 features) or not (27 features).
"""
function Utils.generate_statistical_model(
b::DynamicVehicleSchedulingBenchmark; seed=nothing
)
Random.seed!(seed)
return Chain(Dense((b.two_dimensional_features ? 2 : 27) => 1), vec)
end
export DynamicVehicleSchedulingBenchmark
end