Skip to content

Commit 0fcd98f

Browse files
committed
maximizer, statistical model and tests
1 parent f451364 commit 0fcd98f

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

Project.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2727
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
2828

2929
[compat]
30-
ConstrainedShortestPaths = "0.5.1"
3130
DataDeps = "0.7"
3231
Distributions = "0.25"
3332
DocStringExtensions = "0.9"

src/StochasticVehicleScheduling/StochasticVehicleScheduling.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using DocStringExtensions: TYPEDEF, TYPEDFIELDS, TYPEDSIGNATURES
55
using ConstrainedShortestPaths:
66
stochastic_routing_shortest_path, stochastic_routing_shortest_path_with_threshold
77
using Distributions: Distribution, LogNormal, Uniform
8+
using Flux: Chain, Dense
89
using Graphs:
910
AbstractGraph,
1011
SimpleDiGraph,
@@ -44,6 +45,8 @@ include("solution/solution.jl")
4445
include("solution/exact_algorithms/mip.jl")
4546
include("solution/exact_algorithms/column_generation.jl")
4647

48+
include("maximizer.jl")
49+
4750
"""
4851
$TYPEDFIELDS
4952
@@ -80,8 +83,12 @@ function Utils.generate_dataset(
8083
]
8184
end
8285

83-
function Utils.generate_maximizer(bench::StochasticVehicleSchedulingBenchmark) end
84-
function Utils.generate_statistical_model(bench::StochasticVehicleSchedulingBenchmark) end
86+
function Utils.generate_maximizer(bench::StochasticVehicleSchedulingBenchmark)
87+
return vsp_maximizer
88+
end
89+
function Utils.generate_statistical_model(bench::StochasticVehicleSchedulingBenchmark)
90+
return Chain(Dense(20 => 1; bias=false), vec)
91+
end
8592

8693
export StochasticVehicleSchedulingBenchmark
8794

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
$TYPEDSIGNATURES
3+
4+
Given arcs weights θ, solve the deterministic VSP problem associated to `instance`.
5+
"""
6+
function vsp_maximizer(
7+
θ::AbstractVector; instance::Instance, model_builder=highs_model, silent=true
8+
)
9+
(; graph) = instance
10+
11+
model = model_builder()
12+
silent && set_silent(model)
13+
14+
nb_nodes = nv(graph)
15+
job_indices = 2:(nb_nodes - 1)
16+
17+
@variable(model, y[i=1:nb_nodes, j=1:nb_nodes; has_edge(graph, i, j)], Bin)
18+
19+
@objective(
20+
model,
21+
Max,
22+
sum(θ[i] * y[src(edge), dst(edge)] for (i, edge) in enumerate(edges(graph)))
23+
)
24+
25+
@constraint(
26+
model,
27+
flow[i in job_indices],
28+
sum(y[j, i] for j in inneighbors(graph, i)) ==
29+
sum(y[i, j] for j in outneighbors(graph, i))
30+
)
31+
@constraint(
32+
model, demand[i in job_indices], sum(y[j, i] for j in inneighbors(graph, i)) == 1
33+
)
34+
35+
optimize!(model)
36+
37+
solution = falses(ne(graph))
38+
for (i, edge) in enumerate(edges(graph))
39+
if isapprox(value(y[edge.src, edge.dst]), 1; atol=1e-3)
40+
solution[i] = true
41+
end
42+
end
43+
44+
return solution
45+
end

test/vsp.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@testitem "Stochastic VSP" begin
2+
using DecisionFocusedLearningBenchmarks
3+
using DecisionFocusedLearningBenchmarks.StochasticVehicleScheduling
4+
using Graphs
5+
6+
b = StochasticVehicleSchedulingBenchmark(; nb_tasks=25, nb_scenarios=10)
7+
8+
N = 50
9+
dataset = generate_dataset(b, N)
10+
@test length(dataset) == N
11+
12+
maximizer = generate_maximizer(b)
13+
model = generate_statistical_model(b)
14+
15+
for sample in dataset
16+
x = sample.x
17+
instance = sample.instance
18+
E = ne(instance.graph)
19+
@test size(x) == (20, E)
20+
θ = model(x)
21+
@test length(θ) == E
22+
y = maximizer(θ; instance=instance)
23+
@test length(y) == E
24+
solution = StochasticVehicleScheduling.Solution(y, instance)
25+
@test is_feasible(solution, instance)
26+
end
27+
end

0 commit comments

Comments
 (0)