From 028fc4b50604755afd3b70fed5f827ae5ec49386 Mon Sep 17 00:00:00 2001 From: BatyLeo Date: Thu, 17 Jul 2025 16:30:29 +0200 Subject: [PATCH 1/5] Improve argmax benchmarks --- src/Argmax/Argmax.jl | 2 ++ src/Argmax2D/Argmax2D.jl | 41 ++++++++++++++++++++++++++++++++++++++++ src/Argmax2D/polytope.jl | 31 ++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/src/Argmax/Argmax.jl b/src/Argmax/Argmax.jl index 0864a96..90dc774 100644 --- a/src/Argmax/Argmax.jl +++ b/src/Argmax/Argmax.jl @@ -40,6 +40,8 @@ function ArgmaxBenchmark(; instance_dim::Int=10, nb_features::Int=5, seed=nothin return ArgmaxBenchmark(instance_dim, nb_features, model) end +Utils.is_minimization_problem(::ArgmaxBenchmark) = false + """ $TYPEDSIGNATURES diff --git a/src/Argmax2D/Argmax2D.jl b/src/Argmax2D/Argmax2D.jl index 8be705a..569fe60 100644 --- a/src/Argmax2D/Argmax2D.jl +++ b/src/Argmax2D/Argmax2D.jl @@ -40,11 +40,21 @@ Custom constructor for [`Argmax2DBenchmark`](@ref). """ function Argmax2DBenchmark(; nb_features::Int=5, seed=nothing, polytope_vertex_range=[6]) Random.seed!(seed) +<<<<<<< Updated upstream model = Chain(Dense(nb_features => 2; bias=false), vec) return Argmax2DBenchmark(nb_features, model, polytope_vertex_range) end maximizer(θ; instance) = instance[argmax(dot(θ, v) for v in instance)] +======= + model = Dense(nb_features => 2; bias=false) + return Argmax2DBenchmark(nb_features, model, polytope_vertex_range) +end + +Utils.is_minimization_problem(::Argmax2DBenchmark) = false + +maximizer(θ; instance, kwargs...) = instance[argmax(dot(θ, v) for v in instance)] +>>>>>>> Stashed changes """ $TYPEDSIGNATURES @@ -56,7 +66,11 @@ function Utils.generate_dataset( ) (; nb_features, encoder, polytope_vertex_range) = bench return map(1:dataset_size) do _ +<<<<<<< Updated upstream x = randn(rng, nb_features) +======= + x = randn(rng, Float32, nb_features) +>>>>>>> Stashed changes θ_true = encoder(x) θ_true ./= 2 * norm(θ_true) instance = build_polytope(rand(rng, polytope_vertex_range); shift=rand(rng)) @@ -84,16 +98,31 @@ function Utils.generate_statistical_model( ) Random.seed!(rng, seed) (; nb_features) = bench +<<<<<<< Updated upstream model = Chain(Dense(nb_features => 2; bias=false), vec) return model end +======= + model = Dense(nb_features => 2; bias=false) + return model +end + +function Utils.plot_data(::Argmax2DBenchmark; instance, θ, kwargs...) + pl = init_plot() + plot_polytope!(pl, instance) + plot_objective!(pl, θ) + return plot_maximizer!(pl, θ, instance, maximizer) +end + +>>>>>>> Stashed changes """ $TYPEDSIGNATURES Plot the data sample for the [`Argmax2DBenchmark`](@ref). """ function Utils.plot_data( +<<<<<<< Updated upstream ::Argmax2DBenchmark, sample::DataSample; θ_true=sample.θ_true, kwargs... ) (; instance) = sample @@ -101,6 +130,18 @@ function Utils.plot_data( plot_polytope!(pl, instance) plot_objective!(pl, θ_true) return plot_maximizer!(pl, θ_true, instance, maximizer) +======= + ::Argmax2DBenchmark, + sample::DataSample; + instance=sample.instance, + θ=sample.θ_true, + kwargs..., +) + pl = init_plot() + plot_polytope!(pl, instance) + plot_objective!(pl, θ) + return plot_maximizer!(pl, θ, instance, maximizer) +>>>>>>> Stashed changes end export Argmax2DBenchmark diff --git a/src/Argmax2D/polytope.jl b/src/Argmax2D/polytope.jl index 830635f..297d68d 100644 --- a/src/Argmax2D/polytope.jl +++ b/src/Argmax2D/polytope.jl @@ -21,18 +21,29 @@ function plot_polytope!(pl, vertices) fillcolor=:gray, fillalpha=0.2, linecolor=:black, +<<<<<<< Updated upstream label=L"\mathrm{conv}(\mathcal{V})", ) end; const logocolors = Colors.JULIA_LOGO_COLORS +======= + label=L"\mathrm{conv}(\mathcal{Y}(x))", + ) +end; + +>>>>>>> Stashed changes function plot_objective!(pl, θ) Plots.plot!( pl, [0.0, θ[1]], [0.0, θ[2]]; +<<<<<<< Updated upstream color=logocolors.purple, +======= + color=Colors.JULIA_LOGO_COLORS.purple, +>>>>>>> Stashed changes arrow=true, lw=2, label=nothing, @@ -47,7 +58,11 @@ function plot_maximizer!(pl, θ, instance, maximizer) pl, [ŷ[1]], [ŷ[2]]; +<<<<<<< Updated upstream color=logocolors.red, +======= + color=Colors.JULIA_LOGO_COLORS.red, +>>>>>>> Stashed changes markersize=9, markershape=:square, label=L"f(\theta)", @@ -76,7 +91,11 @@ end; # fillcolor=:blue, # fillalpha=0.1, # linestyle=:dash, +<<<<<<< Updated upstream # linecolor=logocolors.blue, +======= +# linecolor=Colors.JULIA_LOGO_COLORS.blue, +>>>>>>> Stashed changes # label=L"\mathrm{conv}(\hat{p}(\theta))", # ) # return Plots.scatter!( @@ -84,7 +103,11 @@ end; # map(first, A), # map(last, A); # markersize=25 .* p .^ 0.5, +<<<<<<< Updated upstream # markercolor=logocolors.blue, +======= +# markercolor=Colors.JULIA_LOGO_COLORS.blue, +>>>>>>> Stashed changes # markerstrokewidth=0, # markeralpha=0.4, # label=L"\hat{p}(\theta)", @@ -97,7 +120,11 @@ end; # pl, # [ŷΩ[1]], # [ŷΩ[2]]; +<<<<<<< Updated upstream # color=logocolors.blue, +======= +# color=Colors.JULIA_LOGO_COLORS.blue, +>>>>>>> Stashed changes # markersize=6, # markershape=:hexagon, # label=L"\hat{f}(\theta)", @@ -105,7 +132,11 @@ end; # end; # function compress_distribution!( +<<<<<<< Updated upstream # probadist::FixedAtomsProbabilityDistribution{A,W}; atol=0 +======= +# probadist::DifferentiableExpectations.FixedAtomsProbabilityDistribution{A,W}; atol=0 +>>>>>>> Stashed changes # ) where {A,W} # (; atoms, weights) = probadist # to_delete = Int[] From 756f4705f2a428ed39e98214cc33430e94098d40 Mon Sep 17 00:00:00 2001 From: BatyLeo Date: Thu, 17 Jul 2025 16:54:30 +0200 Subject: [PATCH 2/5] fix --- src/Argmax2D/Argmax2D.jl | 29 ----------------------------- src/Argmax2D/polytope.jl | 33 --------------------------------- 2 files changed, 62 deletions(-) diff --git a/src/Argmax2D/Argmax2D.jl b/src/Argmax2D/Argmax2D.jl index 569fe60..c0dd987 100644 --- a/src/Argmax2D/Argmax2D.jl +++ b/src/Argmax2D/Argmax2D.jl @@ -40,13 +40,6 @@ Custom constructor for [`Argmax2DBenchmark`](@ref). """ function Argmax2DBenchmark(; nb_features::Int=5, seed=nothing, polytope_vertex_range=[6]) Random.seed!(seed) -<<<<<<< Updated upstream - model = Chain(Dense(nb_features => 2; bias=false), vec) - return Argmax2DBenchmark(nb_features, model, polytope_vertex_range) -end - -maximizer(θ; instance) = instance[argmax(dot(θ, v) for v in instance)] -======= model = Dense(nb_features => 2; bias=false) return Argmax2DBenchmark(nb_features, model, polytope_vertex_range) end @@ -54,7 +47,6 @@ end Utils.is_minimization_problem(::Argmax2DBenchmark) = false maximizer(θ; instance, kwargs...) = instance[argmax(dot(θ, v) for v in instance)] ->>>>>>> Stashed changes """ $TYPEDSIGNATURES @@ -66,11 +58,7 @@ function Utils.generate_dataset( ) (; nb_features, encoder, polytope_vertex_range) = bench return map(1:dataset_size) do _ -<<<<<<< Updated upstream - x = randn(rng, nb_features) -======= x = randn(rng, Float32, nb_features) ->>>>>>> Stashed changes θ_true = encoder(x) θ_true ./= 2 * norm(θ_true) instance = build_polytope(rand(rng, polytope_vertex_range); shift=rand(rng)) @@ -98,12 +86,6 @@ function Utils.generate_statistical_model( ) Random.seed!(rng, seed) (; nb_features) = bench -<<<<<<< Updated upstream - model = Chain(Dense(nb_features => 2; bias=false), vec) - return model -end - -======= model = Dense(nb_features => 2; bias=false) return model end @@ -115,22 +97,12 @@ function Utils.plot_data(::Argmax2DBenchmark; instance, θ, kwargs...) return plot_maximizer!(pl, θ, instance, maximizer) end ->>>>>>> Stashed changes """ $TYPEDSIGNATURES Plot the data sample for the [`Argmax2DBenchmark`](@ref). """ function Utils.plot_data( -<<<<<<< Updated upstream - ::Argmax2DBenchmark, sample::DataSample; θ_true=sample.θ_true, kwargs... -) - (; instance) = sample - pl = init_plot() - plot_polytope!(pl, instance) - plot_objective!(pl, θ_true) - return plot_maximizer!(pl, θ_true, instance, maximizer) -======= ::Argmax2DBenchmark, sample::DataSample; instance=sample.instance, @@ -141,7 +113,6 @@ function Utils.plot_data( plot_polytope!(pl, instance) plot_objective!(pl, θ) return plot_maximizer!(pl, θ, instance, maximizer) ->>>>>>> Stashed changes end export Argmax2DBenchmark diff --git a/src/Argmax2D/polytope.jl b/src/Argmax2D/polytope.jl index 297d68d..19b442e 100644 --- a/src/Argmax2D/polytope.jl +++ b/src/Argmax2D/polytope.jl @@ -21,29 +21,16 @@ function plot_polytope!(pl, vertices) fillcolor=:gray, fillalpha=0.2, linecolor=:black, -<<<<<<< Updated upstream - label=L"\mathrm{conv}(\mathcal{V})", - ) -end; - -const logocolors = Colors.JULIA_LOGO_COLORS - -======= label=L"\mathrm{conv}(\mathcal{Y}(x))", ) end; ->>>>>>> Stashed changes function plot_objective!(pl, θ) Plots.plot!( pl, [0.0, θ[1]], [0.0, θ[2]]; -<<<<<<< Updated upstream - color=logocolors.purple, -======= color=Colors.JULIA_LOGO_COLORS.purple, ->>>>>>> Stashed changes arrow=true, lw=2, label=nothing, @@ -58,11 +45,7 @@ function plot_maximizer!(pl, θ, instance, maximizer) pl, [ŷ[1]], [ŷ[2]]; -<<<<<<< Updated upstream - color=logocolors.red, -======= color=Colors.JULIA_LOGO_COLORS.red, ->>>>>>> Stashed changes markersize=9, markershape=:square, label=L"f(\theta)", @@ -91,11 +74,7 @@ end; # fillcolor=:blue, # fillalpha=0.1, # linestyle=:dash, -<<<<<<< Updated upstream -# linecolor=logocolors.blue, -======= # linecolor=Colors.JULIA_LOGO_COLORS.blue, ->>>>>>> Stashed changes # label=L"\mathrm{conv}(\hat{p}(\theta))", # ) # return Plots.scatter!( @@ -103,11 +82,7 @@ end; # map(first, A), # map(last, A); # markersize=25 .* p .^ 0.5, -<<<<<<< Updated upstream -# markercolor=logocolors.blue, -======= # markercolor=Colors.JULIA_LOGO_COLORS.blue, ->>>>>>> Stashed changes # markerstrokewidth=0, # markeralpha=0.4, # label=L"\hat{p}(\theta)", @@ -120,11 +95,7 @@ end; # pl, # [ŷΩ[1]], # [ŷΩ[2]]; -<<<<<<< Updated upstream -# color=logocolors.blue, -======= # color=Colors.JULIA_LOGO_COLORS.blue, ->>>>>>> Stashed changes # markersize=6, # markershape=:hexagon, # label=L"\hat{f}(\theta)", @@ -132,11 +103,7 @@ end; # end; # function compress_distribution!( -<<<<<<< Updated upstream -# probadist::FixedAtomsProbabilityDistribution{A,W}; atol=0 -======= # probadist::DifferentiableExpectations.FixedAtomsProbabilityDistribution{A,W}; atol=0 ->>>>>>> Stashed changes # ) where {A,W} # (; atoms, weights) = probadist # to_delete = Int[] From 56193e303dee66f2f4b666b5a8ccb3abf56b12f7 Mon Sep 17 00:00:00 2001 From: BatyLeo Date: Thu, 17 Jul 2025 17:20:00 +0200 Subject: [PATCH 3/5] fix coverage --- src/Argmax2D/Argmax2D.jl | 7 ++----- test/argmax.jl | 3 +++ test/argmax_2d.jl | 3 +++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Argmax2D/Argmax2D.jl b/src/Argmax2D/Argmax2D.jl index c0dd987..323cbe9 100644 --- a/src/Argmax2D/Argmax2D.jl +++ b/src/Argmax2D/Argmax2D.jl @@ -103,16 +103,13 @@ $TYPEDSIGNATURES Plot the data sample for the [`Argmax2DBenchmark`](@ref). """ function Utils.plot_data( - ::Argmax2DBenchmark, + bench::Argmax2DBenchmark, sample::DataSample; instance=sample.instance, θ=sample.θ_true, kwargs..., ) - pl = init_plot() - plot_polytope!(pl, instance) - plot_objective!(pl, θ) - return plot_maximizer!(pl, θ, instance, maximizer) + return Utils.plot_data(bench; instance, θ, kwargs...) end export Argmax2DBenchmark diff --git a/test/argmax.jl b/test/argmax.jl index 28f404b..44e8c3a 100644 --- a/test/argmax.jl +++ b/test/argmax.jl @@ -14,6 +14,9 @@ model = generate_statistical_model(b) maximizer = generate_maximizer(b) + gap = compute_gap(b, dataset, model, maximizer) + @test gap >= 0 + for (i, sample) in enumerate(dataset) (; x, θ_true, y_true) = sample @test size(x) == (nb_features, instance_dim) diff --git a/test/argmax_2d.jl b/test/argmax_2d.jl index 545efca..06ab2b5 100644 --- a/test/argmax_2d.jl +++ b/test/argmax_2d.jl @@ -13,6 +13,9 @@ model = generate_statistical_model(b) maximizer = generate_maximizer(b) + gap = compute_gap(b, dataset, model, maximizer) + @test gap >= 0 + # Test plot_data figure = plot_data(b, dataset[1]) @test figure isa Plots.Plot From a0cdf3f694677af54e58ce287ed283e104bc52a1 Mon Sep 17 00:00:00 2001 From: BatyLeo Date: Thu, 17 Jul 2025 17:30:13 +0200 Subject: [PATCH 4/5] fix coverage again --- src/Argmax/Argmax.jl | 4 +++- src/Argmax2D/Argmax2D.jl | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Argmax/Argmax.jl b/src/Argmax/Argmax.jl index 90dc774..c4d07bf 100644 --- a/src/Argmax/Argmax.jl +++ b/src/Argmax/Argmax.jl @@ -40,7 +40,9 @@ function ArgmaxBenchmark(; instance_dim::Int=10, nb_features::Int=5, seed=nothin return ArgmaxBenchmark(instance_dim, nb_features, model) end -Utils.is_minimization_problem(::ArgmaxBenchmark) = false +function Utils.is_minimization_problem(::ArgmaxBenchmark) + return false +end """ $TYPEDSIGNATURES diff --git a/src/Argmax2D/Argmax2D.jl b/src/Argmax2D/Argmax2D.jl index 323cbe9..a1c76c4 100644 --- a/src/Argmax2D/Argmax2D.jl +++ b/src/Argmax2D/Argmax2D.jl @@ -44,7 +44,9 @@ function Argmax2DBenchmark(; nb_features::Int=5, seed=nothing, polytope_vertex_r return Argmax2DBenchmark(nb_features, model, polytope_vertex_range) end -Utils.is_minimization_problem(::Argmax2DBenchmark) = false +function Utils.is_minimization_problem(::Argmax2DBenchmark) + return false +end maximizer(θ; instance, kwargs...) = instance[argmax(dot(θ, v) for v in instance)] From 475e7b459b2e792589fd5a996889aec931200352 Mon Sep 17 00:00:00 2001 From: BatyLeo Date: Thu, 17 Jul 2025 17:45:17 +0200 Subject: [PATCH 5/5] cleanup and bump version --- Project.toml | 2 +- src/Argmax2D/polytope.jl | 72 ---------------------------------------- 2 files changed, 1 insertion(+), 73 deletions(-) diff --git a/Project.toml b/Project.toml index 5e156bd..b19c4aa 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "DecisionFocusedLearningBenchmarks" uuid = "2fbe496a-299b-4c81-bab5-c44dfc55cf20" authors = ["Members of JuliaDecisionFocusedLearning"] -version = "0.2.3" +version = "0.2.4" [deps] Colors = "5ae59095-9a9b-59fe-a467-6f913c188581" diff --git a/src/Argmax2D/polytope.jl b/src/Argmax2D/polytope.jl index 19b442e..dd8057a 100644 --- a/src/Argmax2D/polytope.jl +++ b/src/Argmax2D/polytope.jl @@ -51,75 +51,3 @@ function plot_maximizer!(pl, θ, instance, maximizer) label=L"f(\theta)", ) end; - -# function get_angle(v) -# @assert !(norm(v) ≈ 0) -# v = v ./ norm(v) -# if v[2] >= 0 -# return acos(v[1]) -# else -# return π + acos(-v[1]) -# end -# end; - -# function plot_distribution!(pl, probadist) -# A = probadist.atoms -# As = sort(A; by=get_angle) -# p = probadist.weights -# Plots.plot!( -# pl, -# vcat(map(first, As), first(As[1])), -# vcat(map(last, As), last(As[1])); -# fillrange=0, -# fillcolor=:blue, -# fillalpha=0.1, -# linestyle=:dash, -# linecolor=Colors.JULIA_LOGO_COLORS.blue, -# label=L"\mathrm{conv}(\hat{p}(\theta))", -# ) -# return Plots.scatter!( -# pl, -# map(first, A), -# map(last, A); -# markersize=25 .* p .^ 0.5, -# markercolor=Colors.JULIA_LOGO_COLORS.blue, -# markerstrokewidth=0, -# markeralpha=0.4, -# label=L"\hat{p}(\theta)", -# ) -# end; - -# function plot_expectation!(pl, probadist) -# ŷΩ = compute_expectation(probadist) -# return scatter!( -# pl, -# [ŷΩ[1]], -# [ŷΩ[2]]; -# color=Colors.JULIA_LOGO_COLORS.blue, -# markersize=6, -# markershape=:hexagon, -# label=L"\hat{f}(\theta)", -# ) -# end; - -# function compress_distribution!( -# probadist::DifferentiableExpectations.FixedAtomsProbabilityDistribution{A,W}; atol=0 -# ) where {A,W} -# (; atoms, weights) = probadist -# to_delete = Int[] -# for i in length(probadist):-1:1 -# ai = atoms[i] -# for j in 1:(i - 1) -# aj = atoms[j] -# if isapprox(ai, aj; atol=atol) -# weights[j] += weights[i] -# push!(to_delete, i) -# break -# end -# end -# end -# sort!(to_delete) -# deleteat!(atoms, to_delete) -# deleteat!(weights, to_delete) -# return probadist -# end;