Skip to content

Commit 95ecfcc

Browse files
committed
update
1 parent 5ca4bfc commit 95ecfcc

11 files changed

Lines changed: 202 additions & 129 deletions

File tree

src/DynamicAssortment/DynamicAssortment.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ end
110110
"""
111111
$TYPEDSIGNATURES
112112
113-
Creates an [`Environment`](@ref) from an [`Instance`](@ref) of the dynamic assortment benchmark.
114-
The seed of the environment is randomly generated using the provided random number generator.
113+
Creates an [`Environment`](@ref) for the dynamic assortment benchmark.
114+
The instance and seed are randomly generated using the provided random number generator.
115115
"""
116116
function Utils.generate_environment(
117-
::DynamicAssortmentBenchmark, instance::Instance, rng::AbstractRNG; kwargs...
117+
b::DynamicAssortmentBenchmark, rng::AbstractRNG; kwargs...
118118
)
119+
instance = Instance(b, rng)
119120
seed = rand(rng, 1:typemax(Int))
120121
return Environment(instance; seed)
121122
end

src/DynamicVehicleScheduling/DynamicVehicleScheduling.jl

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ using Random: Random, AbstractRNG, MersenneTwister, seed!, randperm
1818
using Requires: @require
1919
using Statistics: mean, quantile
2020

21+
"""
22+
$TYPEDEF
23+
24+
Abstract type for dynamic vehicle scheduling benchmarks.
25+
26+
# Fields
27+
$TYPEDFIELDS
28+
"""
29+
@kwdef struct DynamicVehicleSchedulingBenchmark <: AbstractDynamicBenchmark{true}
30+
"maximum number of customers entering the system per epoch"
31+
max_requests_per_epoch::Int = 10
32+
"time between decision and dispatch of a vehicle"
33+
Δ_dispatch::Float64 = 1.0
34+
"duration of an epoch"
35+
epoch_duration::Float64 = 1.0
36+
"whether to use two-dimensional features"
37+
two_dimensional_features::Bool = false
38+
end
39+
2140
include("utils.jl")
2241

2342
# static vsp stuff
@@ -38,46 +57,35 @@ include("anticipative_solver.jl")
3857
include("features.jl")
3958
include("policy.jl")
4059

41-
"""
42-
$TYPEDEF
43-
44-
Abstract type for dynamic vehicle scheduling benchmarks.
45-
46-
# Fields
47-
$TYPEDFIELDS
48-
"""
49-
@kwdef struct DynamicVehicleSchedulingBenchmark <: AbstractDynamicBenchmark{true}
50-
"maximum number of customers entering the system per epoch"
51-
max_requests_per_epoch::Int = 10
52-
"time between decision and dispatch of a vehicle"
53-
Δ_dispatch::Float64 = 1.0
54-
"duration of an epoch"
55-
epoch_duration::Float64 = 1.0
56-
"whether to use two-dimensional features"
57-
two_dimensional_features::Bool = false
58-
end
59-
6060
"""
6161
$TYPEDSIGNATURES
6262
63-
Generate a dataset for the dynamic vehicle scheduling benchmark.
64-
Returns a vector of [`DataSample`](@ref) objects, each containing an [`Instance`](@ref).
65-
The dataset is generated from pre-existing DVRPTW files.
63+
Generate environments for the dynamic vehicle scheduling benchmark.
64+
Reads from pre-existing DVRPTW files and creates [`DVSPEnv`](@ref) environments.
6665
"""
67-
function Utils.generate_dataset(b::DynamicVehicleSchedulingBenchmark, dataset_size::Int=1)
66+
function Utils.generate_environments(
67+
b::DynamicVehicleSchedulingBenchmark,
68+
n::Int;
69+
seed=nothing,
70+
rng=MersenneTwister(seed),
71+
kwargs...,
72+
)
6873
(; max_requests_per_epoch, Δ_dispatch, epoch_duration, two_dimensional_features) = b
6974
files = readdir(datadep"dvrptw"; join=true)
70-
dataset_size = min(dataset_size, length(files))
75+
n = min(n, length(files))
7176
return [
72-
DataSample(;
73-
instance=Instance(
77+
generate_environment(
78+
b,
79+
Instance(
7480
read_vsp_instance(files[i]);
7581
max_requests_per_epoch,
7682
Δ_dispatch,
7783
epoch_duration,
7884
two_dimensional_features,
7985
),
80-
) for i in 1:dataset_size
86+
rng;
87+
kwargs...,
88+
) for i in 1:n
8189
]
8290
end
8391

@@ -87,7 +95,7 @@ $TYPEDSIGNATURES
8795
Creates an environment from an [`Instance`](@ref) of the dynamic vehicle scheduling benchmark.
8896
The seed of the environment is randomly generated using the provided random number generator.
8997
"""
90-
function Utils.generate_environment(
98+
function generate_environment(
9199
::DynamicVehicleSchedulingBenchmark, instance::Instance, rng::AbstractRNG; kwargs...
92100
)
93101
seed = rand(rng, 1:typemax(Int))
@@ -107,16 +115,6 @@ end
107115
"""
108116
$TYPEDSIGNATURES
109117
110-
Generate a scenario for the dynamic vehicle scheduling benchmark.
111-
This is a wrapper around the generic scenario generation function.
112-
"""
113-
function Utils.generate_scenario(b::DynamicVehicleSchedulingBenchmark, args...; kwargs...)
114-
return Utils.generate_scenario(args...; kwargs...)
115-
end
116-
117-
"""
118-
$TYPEDSIGNATURES
119-
120118
Generate an anticipative solution for the dynamic vehicle scheduling benchmark.
121119
The solution is computed using the anticipative solver with the benchmark's feature configuration.
122120
"""
@@ -131,6 +129,21 @@ end
131129
"""
132130
$TYPEDSIGNATURES
133131
132+
Return the anticipative solver for the dynamic vehicle scheduling benchmark.
133+
The callable takes a scenario and solver kwargs (including `instance`) and returns a
134+
training trajectory as a `Vector{DataSample}`.
135+
"""
136+
function Utils.generate_anticipative_solver(::DynamicVehicleSchedulingBenchmark)
137+
return (scenario; instance, kwargs...) -> begin
138+
env = DVSPEnv(instance, scenario)
139+
_, trajectory = anticipative_solver(env; reset_env=false, kwargs...)
140+
return trajectory
141+
end
142+
end
143+
144+
"""
145+
$TYPEDSIGNATURES
146+
134147
Generate baseline policies for the dynamic vehicle scheduling benchmark.
135148
Returns a tuple containing:
136149
- `lazy`: A policy that dispatches vehicles only when they are ready

src/DynamicVehicleScheduling/environment.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ function DVSPEnv(instance::Instance; seed=nothing)
2323
return DVSPEnv(instance, initial_state, scenario, rng, seed)
2424
end
2525

26+
"""
27+
$TYPEDSIGNATURES
28+
29+
Constructor for [`DVSPEnv`](@ref) from a pre-existing scenario.
30+
"""
31+
function DVSPEnv(instance::Instance, scenario::Scenario; seed=nothing)
32+
rng = MersenneTwister(seed)
33+
initial_state = DVSPState(instance; scenario[1]...)
34+
return DVSPEnv(instance, initial_state, scenario, rng, seed)
35+
end
36+
2637
currrent_epoch(env::DVSPEnv) = current_epoch(env.state)
2738
epoch_duration(env::DVSPEnv) = epoch_duration(env.instance)
2839
last_epoch(env::DVSPEnv) = last_epoch(env.instance)

src/DynamicVehicleScheduling/scenario.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
struct Scenario
32
"indices of the new requests in each epoch"
43
indices::Vector{Vector{Int}}
@@ -51,6 +50,8 @@ function Utils.generate_scenario(
5150
return Scenario(new_indices, new_service_time, new_start_time)
5251
end
5352

54-
function Utils.generate_scenario(sample::DataSample; kwargs...)
55-
return Utils.generate_scenario(sample.instance; kwargs...)
53+
function Utils.generate_scenario(
54+
::DynamicVehicleSchedulingBenchmark, rng::AbstractRNG; instance, kwargs...
55+
)
56+
return generate_scenario(instance; rng)
5657
end

src/Maintenance/Maintenance.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,11 @@ end
114114
"""
115115
$TYPEDSIGNATURES
116116
117-
Creates an [`Environment`](@ref) from an [`Instance`](@ref) of the maintenance benchmark.
118-
The seed of the environment is randomly generated using the provided random number generator.
117+
Creates an [`Environment`](@ref) for the maintenance benchmark.
118+
The instance and seed are randomly generated using the provided random number generator.
119119
"""
120-
function Utils.generate_environment(
121-
::MaintenanceBenchmark, instance::Instance, rng::AbstractRNG; kwargs...
122-
)
120+
function Utils.generate_environment(b::MaintenanceBenchmark, rng::AbstractRNG; kwargs...)
121+
instance = Instance(b, rng)
123122
seed = rand(rng, 1:typemax(Int))
124123
return Environment(instance; seed)
125124
end

src/Utils/Utils.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export generate_scenario
3333
export generate_environment, generate_environments
3434
export generate_policies
3535
export generate_anticipative_solution
36+
export generate_instance_samples, generate_environment_samples
3637

3738
export plot_data, compute_gap
3839
export grid_graph, get_path, path_to_matrix

0 commit comments

Comments
 (0)