Skip to content

Commit 6e5cb75

Browse files
committed
use an rng and seed for city generation
1 parent c2feb23 commit 6e5cb75

File tree

5 files changed

+59
-26
lines changed

5 files changed

+59
-26
lines changed

src/StochasticVehicleScheduling/StochasticVehicleScheduling.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using DocStringExtensions: TYPEDEF, TYPEDFIELDS, TYPEDSIGNATURES
55
using Distributions: Distribution, LogNormal, Uniform
66
using Graphs: AbstractGraph, SimpleDiGraph, add_edge!, nv, ne, edges, src, dst
77
using Printf: @printf
8+
using Random: Random, AbstractRNG, MersenneTwister
89
using SparseArrays: sparse
910
using Statistics: quantile, mean
1011

src/StochasticVehicleScheduling/instance/city.jl

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,35 @@ function create_random_city(; # TODO: use an rng here
7979
district_σ=default_district_σ,
8080
task_μ=default_task_μ,
8181
task_σ=default_task_σ,
82+
seed=nothing,
83+
rng=MersenneTwister(0),
8284
city_kwargs...,
8385
)
86+
Random.seed!(rng, seed)
8487
city = City(; city_kwargs...)
85-
init_districts!(city, district_μ, district_σ)
86-
init_tasks!(city, αᵥ_low, αᵥ_high, first_begin_time, last_begin_time, task_μ, task_σ)
87-
generate_scenarios!(city)
88+
init_districts!(city, district_μ, district_σ; rng=rng)
89+
init_tasks!(
90+
city, αᵥ_low, αᵥ_high, first_begin_time, last_begin_time, task_μ, task_σ; rng=rng
91+
)
92+
generate_scenarios!(city; rng=rng)
8893
compute_perturbed_end_times!(city)
8994
return city
9095
end
9196

92-
function init_districts!(city::City, district_μ::Distribution, district_σ::Distribution)
97+
"""
98+
$TYPEDSIGNATURES
99+
100+
Initialize the districts of the city.
101+
"""
102+
function init_districts!(
103+
city::City, district_μ::Distribution, district_σ::Distribution; rng::AbstractRNG
104+
)
93105
nb_scenarios = size(city.scenario_inter_area_factor, 1)
94106
nb_district_per_edge = city.width ÷ city.district_width
95107
for x in 1:nb_district_per_edge
96108
for y in 1:nb_district_per_edge
97-
μ = rand(district_μ)
98-
σ = rand(district_σ)
109+
μ = rand(rng, district_μ)
110+
σ = rand(rng, district_σ)
99111
city.districts[x, y] = District(;
100112
random_delay=LogNormal(μ, σ), nb_scenarios=nb_scenarios
101113
)
@@ -104,14 +116,20 @@ function init_districts!(city::City, district_μ::Distribution, district_σ::Dis
104116
return nothing
105117
end
106118

119+
"""
120+
$TYPEDSIGNATURES
121+
122+
Draw the tasks of the city.
123+
"""
107124
function init_tasks!(
108125
city::City,
109126
αᵥ_low::Real,
110127
αᵥ_high::Real,
111128
first_begin_time::Real,
112129
last_begin_time::Real,
113130
task_μ::Distribution,
114-
task_σ::Distribution,
131+
task_σ::Distribution;
132+
rng::AbstractRNG,
115133
)
116134
nb_scenarios = size(city.scenario_inter_area_factor, 1)
117135

@@ -120,16 +138,17 @@ function init_tasks!(
120138
travel_time_multiplier_distribution = Uniform(αᵥ_low, αᵥ_high)
121139

122140
for i_task in 1:(city.nb_tasks)
123-
start_point = draw_random_point(point_distribution)
124-
end_point = draw_random_point(point_distribution)
141+
start_point = draw_random_point(point_distribution; rng=rng)
142+
end_point = draw_random_point(point_distribution; rng=rng)
125143

126-
start_time = rand(start_time_distribution)
144+
start_time = rand(rng, start_time_distribution)
127145
end_time =
128146
start_time +
129-
rand(travel_time_multiplier_distribution) * distance(start_point, end_point)
147+
rand(rng, travel_time_multiplier_distribution) *
148+
distance(start_point, end_point)
130149

131-
μ = rand(task_μ)
132-
σ = rand(task_σ)
150+
μ = rand(rng, task_μ)
151+
σ = rand(rng, task_σ)
133152
random_delay = LogNormal(μ, σ)
134153

135154
city.tasks[i_task + 1] = Task(;
@@ -180,29 +199,40 @@ function get_district(point::Point, city::City)
180199
trunc(Int, point.y / city.district_width) + 1
181200
end
182201

183-
function generate_scenarios!(city::City)
202+
"""
203+
$TYPEDSIGNATURES
204+
205+
Draw all delay scenarios for the city.
206+
"""
207+
function generate_scenarios!(city::City; rng::AbstractRNG)
184208
# roll all tasks
185209
for task in city.tasks
186-
roll(task)
210+
roll(task, rng)
187211
end
188212

189213
# roll all districts
190214
for district in city.districts
191-
roll(district)
215+
roll(district, rng)
192216
end
193217

194218
# roll inter-district
195219
nb_scenarios, nb_hours = size(city.scenario_inter_area_factor)
196220
for s in 1:nb_scenarios
197221
previous_delay = 0.0
198222
for h in 1:nb_hours
199-
previous_delay = (previous_delay + 0.1) * rand(city.random_inter_area_factor) # TODO : study formula
223+
previous_delay =
224+
(previous_delay + 0.1) * rand(rng, city.random_inter_area_factor)
200225
city.scenario_inter_area_factor[s, h] = previous_delay
201226
end
202227
end
203228
return nothing
204229
end
205230

231+
"""
232+
$TYPEDSIGNATURES
233+
234+
Compute the end times of the tasks for each scenario.
235+
"""
206236
function compute_perturbed_end_times!(city::City)
207237
nb_scenarios = size(city.scenario_inter_area_factor, 1)
208238

src/StochasticVehicleScheduling/instance/district.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ $TYPEDSIGNATURES
2828
2929
Return one scenario of future delay given current delay and delay distribution.
3030
"""
31-
function scenario_next_delay(previous_delay::Real, random_delay::Distribution)
32-
return previous_delay / 2.0 + rand(random_delay)
31+
function scenario_next_delay(
32+
previous_delay::Real, random_delay::Distribution, rng::AbstractRNG
33+
)
34+
return previous_delay / 2.0 + rand(rng, random_delay)
3335
end
3436

3537
"""
@@ -38,14 +40,14 @@ $TYPEDSIGNATURES
3840
Populate `scenario_delay` with delays drawn from `random_delay` distribution
3941
for each (scenario, hour) pair.
4042
"""
41-
function roll(district::District) # TODO: use an rng
43+
function roll(district::District, rng::AbstractRNG)
4244
nb_scenarios, nb_hours = size(district.scenario_delay)
4345
# Loop on scenarios
4446
for s in 1:nb_scenarios
4547
previous_delay = 0.0
4648
# Loop on hours
4749
for h in 1:nb_hours
48-
previous_delay = scenario_next_delay(previous_delay, district.random_delay)
50+
previous_delay = scenario_next_delay(previous_delay, district.random_delay, rng)
4951
district.scenario_delay[s, h] = previous_delay
5052
end
5153
end

src/StochasticVehicleScheduling/instance/task.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ $TYPEDSIGNATURES
6868
Populate `scenario_start_time` with delays drawn from the `random_delay` distribution of
6969
the given task for each scenario.
7070
"""
71-
function roll(task::Task) # TODO: use an rng
71+
function roll(task::Task, rng::AbstractRNG)
7272
S = nb_scenarios(task)
73-
task.scenario_start_time .= task.start_time .+ rand(task.random_delay, S)
73+
task.scenario_start_time .= task.start_time .+ rand(rng, task.random_delay, S)
7474
return nothing
7575
end
7676

src/StochasticVehicleScheduling/utils.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ $TYPEDSIGNATURES
1313
1414
Returns a Point with random x and y, drawn from distrib.
1515
"""
16-
function draw_random_point(distrib::Distribution) # TODO: use an rng
17-
return Point(rand(distrib), rand(distrib))
16+
function draw_random_point(distrib::Distribution; rng)
17+
return Point(rand(rng, distrib), rand(rng, distrib))
1818
end
1919

2020
"""
@@ -49,7 +49,7 @@ function find_first_one(A::AbstractVector)
4949
return nothing
5050
end
5151

52-
# Config stuff
52+
# Config stuff, probably not needed in this package
5353
# """
5454
# recursive_namedtuple(x)
5555

0 commit comments

Comments
 (0)