Skip to content

Commit 6d8d4ab

Browse files
authored
Merge pull request #26 from BatyLeo/better-routing
Update stochastic routing and log algorithm info
2 parents 216e446 + e03baf7 commit 6d8d4ab

File tree

7 files changed

+40
-14
lines changed

7 files changed

+40
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
scripts
77
.vscode
88
debug.jl
9+
*.jld2

CITATION.bib

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ @misc{ConstrainedShortestPaths.jl
22
author = {Léo Baty and contributors},
33
title = {ConstrainedShortestPaths.jl},
44
url = {https://github.com/BatyLeo/ConstrainedShortestPaths.jl},
5-
version = {v0.4.0},
5+
version = {v0.5.0},
66
year = {2024},
7-
month = {8}
7+
month = {9}
88
}

Project.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ConstrainedShortestPaths"
22
uuid = "b3798467-87dc-4d99-943d-35a1bd39e395"
33
authors = ["Léo Baty and contributors"]
4-
version = "0.4.0"
4+
version = "0.5.0"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
@@ -15,7 +15,7 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1515
DataStructures = "0.18"
1616
DocStringExtensions = "0.9"
1717
Graphs = "1"
18-
PiecewiseLinearFunctions = "0.1"
18+
PiecewiseLinearFunctions = "0.3"
1919
SparseArrays = "<0.0.1,1"
2020
Statistics = "<0.0.1,1"
2121
julia = "1.10"
@@ -26,6 +26,7 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
2626
GLPK = "60bf3e95-4087-53dc-ae20-288a0d20c6a6"
2727
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
2828
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
29+
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
2930
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
3031
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
3132
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
@@ -36,4 +37,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3637
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
3738

3839
[targets]
39-
test = ["Aqua", "Documenter", "GLPK", "Graphs", "JET", "JuMP", "JuliaFormatter", "Plots", "Random", "SparseArrays", "StableRNGs", "Test", "UnicodePlots"]
40+
test = ["Aqua", "Documenter", "GLPK", "Graphs", "JET", "JLD2", "JuMP", "JuliaFormatter", "Plots", "Random", "SparseArrays", "StableRNGs", "Test", "UnicodePlots"]

src/algorithms.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ function generalized_a_star(instance::CSPInstance, bounds; kwargs...)
4848
c_star = Inf
4949
p_star = [origin_vertex]
5050

51+
nb_cuts_with_bounds = 0
52+
nb_cuts_with_dominance = 0
53+
5154
while !isempty(L)
5255
p = dequeue!(L)
5356
v = p[end]
@@ -64,6 +67,7 @@ function generalized_a_star(instance::CSPInstance, bounds; kwargs...)
6467
end
6568
forward_resources[q] = rq
6669
c = instance.cost_function(rq, bounds[w])
70+
6771
if c < c_star # cut using bounds
6872
if w == destination_vertex # if destination is reached
6973
c_star = c
@@ -72,11 +76,16 @@ function generalized_a_star(instance::CSPInstance, bounds; kwargs...)
7276
remove_dominated!(M[w], rq)
7377
push!(M[w], rq)
7478
enqueue!(L, q => c)
79+
else
80+
nb_cuts_with_dominance += 1
7581
end
82+
else
83+
nb_cuts_with_bounds += 1
7684
end
7785
end
7886
end
79-
return (; p_star, c_star)
87+
info = (; nb_cuts_with_bounds, nb_cuts_with_dominance)
88+
return (; p_star, c_star, info, bounds)
8089
end
8190

8291
"""
@@ -100,6 +109,7 @@ function generalized_a_star(instance::ForwardCSPInstance; kwargs...)
100109
push!(M[origin_vertex], forward_resources[empty_path])
101110
c_star = Inf
102111
p_star = [origin_vertex]
112+
nb_cuts_with_dominance = 0
103113

104114
while !isempty(L)
105115
p = dequeue!(L)
@@ -126,10 +136,13 @@ function generalized_a_star(instance::ForwardCSPInstance; kwargs...)
126136
remove_dominated!(M[w], rq)
127137
push!(M[w], rq)
128138
enqueue!(L, q => c)
139+
else
140+
nb_cuts_with_dominance += 1
129141
end
130142
end
131143
end
132-
return (; p_star, c_star)
144+
info = (; nb_cuts_with_dominance)
145+
return (; p_star, c_star, info)
133146
end
134147

135148
"""

src/examples/stochastic_routing.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,27 @@ function (f::StochasticForwardFunction)(q::StochasticForwardResource)
5454
return StochasticForwardResource(new_c, new_xi, new_λ), true
5555
end
5656

57-
function _backward_scenario(g::PiecewiseLinearFunction, delay::Float64, slack::Float64)
57+
function _backward_scenario(g::PiecewiseLinearFunction, delay::Float64, slack::Float64, λᵥ)
5858
f = if slack == Inf
5959
piecewise_linear()
6060
else
6161
piecewise_linear(1.0, slack, delay)
6262
end
63-
return f + g f
63+
return f + g f - λᵥ
6464
end
6565

6666
function (f::StochasticBackwardFunction)(q::StochasticBackwardResource)
6767
return StochasticBackwardResource(
6868
[
69-
_backward_scenario(g, delay, slack) for
69+
_backward_scenario(g, delay, slack, f.λ_value) for
7070
(g, delay, slack) in zip(q.g, f.delays, f.slacks)
7171
],
7272
f.λ_value + q.λ,
7373
)
7474
end
7575

7676
function stochastic_cost(fr::StochasticForwardResource, br::StochasticBackwardResource)
77-
λ_sum = fr.λ + br.λ
77+
λ_sum = fr.λ
7878
cp = fr.c + mean(gj(Rj) for (gj, Rj) in zip(br.g, fr.xi))
7979
return cp - λ_sum
8080
end

test/examples/stochastic_routing.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ConstrainedShortestPaths
22
using Graphs
33
using SparseArrays
4+
using JLD2
45

56
@testset "Path digraph" begin
67
m = 5 # number of scenarios
@@ -132,9 +133,9 @@ end
132133
@testset "Random graphs" begin
133134
n = 5
134135
for nb_scenarios in 1:5
135-
for nb_vertices in 10:15
136-
for i in 1:n
137-
rng = StableRNG(i)
136+
for nb_vertices in 10:1:15
137+
for seed in 1:n
138+
rng = StableRNG(seed)
138139
graph = random_acyclic_digraph(
139140
nb_vertices, rng; all_connected_to_source_and_destination=true
140141
)
@@ -163,6 +164,9 @@ end
163164
graph, slack_matrix, delays, initial_paths; bounding=false
164165
)
165166
@test obj2 _obj2
167+
# if !(obj2 ≈ _obj2)
168+
# jldsave("debug.jld2"; graph, slack_matrix, delays, initial_paths)
169+
# end
166170

167171
# Exact resolution
168172
obj, sol = solve_scenarios(graph, slack_matrix, delays)

test/utils.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,19 @@ function stochastic_PLNE(g, slacks, delays, initial_paths; bounding=true)
101101
new_paths = Vector{Int}[]
102102
cons = []
103103

104+
# c = 0
104105
while true
106+
# c += 1
105107
optimize!(model)
106108
λ_val = value.(λ)
107109
(; c_star, p_star) = stochastic_routing_shortest_path(
108110
g, slacks, delays, λ_val; bounding
109111
)
112+
# if c == 2
113+
# jldsave("debug2.jld2"; graph, slacks, delays, λ_val, c_star, p_star)
114+
# end
115+
# @info λ_val
116+
# @info (; c_star, p_star)
110117
full_cost =
111118
c_star + vehicle_cost + sum(λ_val[v] for v in job_indices if v in p_star)
112119
@assert path_cost(p_star, slacks, delays) + vehicle_cost full_cost

0 commit comments

Comments
 (0)