Skip to content

Commit

Permalink
cleanup and fix fixed size shortest path
Browse files Browse the repository at this point in the history
  • Loading branch information
BatyLeo committed Oct 16, 2024
1 parent fdbc713 commit 29434e7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
21 changes: 2 additions & 19 deletions src/FixedSizeShortestPath/FixedSizeShortestPath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ function Utils.generate_dataset(
[rand(rng, Uniform{type}(1 - ν, 1 + ν), E) for _ in 1:dataset_size]
end
costs = [
(1 .+ (3 .+ B * zᵢ ./ type(sqrt(p))) .^ deg) .* ξᵢ for (ξᵢ, zᵢ) in zip(ξ, features)
-(1 .+ (3 .+ B * zᵢ ./ type(sqrt(p))) .^ deg) .* ξᵢ for (ξᵢ, zᵢ) in zip(ξ, features)
]

shortest_path_maximizer = Utils.generate_maximizer(bench)

# Label solutions
solutions = shortest_path_maximizer.(.-costs)
solutions = shortest_path_maximizer.(costs)
return [DataSample(; x=x, θ=θ, y=y) for (x, θ, y) in zip(features, costs, solutions)]
end

Expand All @@ -145,23 +145,6 @@ function Utils.generate_statistical_model(bench::FixedSizeShortestPathBenchmark)
return Chain(Dense(p, ne(graph)))
end

function objective_value(::FixedSizeShortestPathBenchmark, θ, y)
return dot(θ, y)
end

function Utils.compute_gap(
bench::FixedSizeShortestPathBenchmark, model, features, costs, solutions, maximizer
)
res = 0.0
for (x, ȳ, θ̄) in zip(features, solutions, costs)
θ = model(x)
y = maximizer(θ)
val = objective_value(bench, θ̄, ȳ)
res += (objective_value(bench, θ̄, y) - val) / val
end
return res / length(features)
end

export FixedSizeShortestPathBenchmark

end
6 changes: 5 additions & 1 deletion test/fixed_size_shortest_path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
model = generate_statistical_model(b)
maximizer = generate_maximizer(b)

gap = compute_gap(b, dataset, model, maximizer)
@test gap >= 0

for sample in dataset
x = sample.x
θ_true = sample.θ
y_true = sample.y
@test all(θ_true .< 0)
@test size(x) == (p,)
@test length(θ_true) == A
@test length(y_true) == A
@test isnothing(sample.instance)
@test all(y_true .== maximizer(-θ_true))
@test all(y_true .== maximizer(θ_true))
θ = model(x)
@test length(θ) == length(θ_true)
y = maximizer(θ)
Expand Down

0 comments on commit 29434e7

Please sign in to comment.