-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDynamicVehicleScheduling.jl
More file actions
168 lines (144 loc) · 4.64 KB
/
DynamicVehicleScheduling.jl
File metadata and controls
168 lines (144 loc) · 4.64 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
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
"""
$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
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")
"""
$TYPEDSIGNATURES
Generate environments for the dynamic vehicle scheduling benchmark.
Reads from pre-existing DVRPTW files and creates [`DVSPEnv`](@ref) environments.
"""
function Utils.generate_environments(
b::DynamicVehicleSchedulingBenchmark,
n::Int;
seed=nothing,
rng=MersenneTwister(seed),
kwargs...,
)
(; max_requests_per_epoch, Δ_dispatch, epoch_duration, two_dimensional_features) = b
files = readdir(datadep"dvrptw"; join=true)
n = min(n, length(files))
return [
generate_environment(
b,
Instance(
read_vsp_instance(files[i]);
max_requests_per_epoch,
Δ_dispatch,
epoch_duration,
two_dimensional_features,
),
rng;
kwargs...,
) for i in 1:n
]
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 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
Return the anticipative solver for the dynamic vehicle scheduling benchmark.
The callable takes an environment and solver kwargs and returns a training trajectory
as a `Vector{DataSample}`. Set `reset_env=true` (default) to reset the environment
before solving, or `reset_env=false` to plan from the current state.
"""
function Utils.generate_anticipative_solver(::DynamicVehicleSchedulingBenchmark)
return (env; reset_env=true, kwargs...) -> begin
_, trajectory = anticipative_solver(env; reset_env, kwargs...)
return trajectory
end
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_baseline_policies(::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