|
| 1 | +module Argmax2D |
| 2 | + |
| 3 | +using ..Utils |
| 4 | +using Colors: Colors |
| 5 | +using DocStringExtensions: TYPEDEF, TYPEDFIELDS, TYPEDSIGNATURES |
| 6 | +using Flux: Chain, Dense |
| 7 | +using LaTeXStrings: @L_str |
| 8 | +using LinearAlgebra: dot, norm |
| 9 | +using Plots: Plots |
| 10 | +using Random: Random, MersenneTwister |
| 11 | + |
| 12 | +include("polytope.jl") |
| 13 | + |
| 14 | +""" |
| 15 | +$TYPEDEF |
| 16 | +
|
| 17 | +Argmax becnhmark on a 2d polytope. |
| 18 | +
|
| 19 | +# Fields |
| 20 | +$TYPEDFIELDS |
| 21 | +""" |
| 22 | +struct Argmax2DBenchmark{E,R} <: AbstractBenchmark |
| 23 | + "number of features" |
| 24 | + nb_features::Int |
| 25 | + "true mapping between features and costs" |
| 26 | + encoder::E |
| 27 | + "" |
| 28 | + polytope_vertex_range::R |
| 29 | +end |
| 30 | + |
| 31 | +function Base.show(io::IO, bench::Argmax2DBenchmark) |
| 32 | + (; nb_features) = bench |
| 33 | + return print(io, "Argmax2DBenchmark(nb_features=$nb_features)") |
| 34 | +end |
| 35 | + |
| 36 | +""" |
| 37 | +$TYPEDSIGNATURES |
| 38 | +
|
| 39 | +Custom constructor for [`Argmax2DBenchmark`](@ref). |
| 40 | +""" |
| 41 | +function Argmax2DBenchmark(; nb_features::Int=5, seed=nothing, polytope_vertex_range=[6]) |
| 42 | + Random.seed!(seed) |
| 43 | + model = Chain(Dense(nb_features => 2; bias=false), vec) |
| 44 | + return Argmax2DBenchmark(nb_features, model, polytope_vertex_range) |
| 45 | +end |
| 46 | + |
| 47 | +maximizer(θ; instance) = instance[argmax(dot(θ, v) for v in instance)] |
| 48 | + |
| 49 | +""" |
| 50 | +$TYPEDSIGNATURES |
| 51 | +
|
| 52 | +Generate a dataset for the [`Argmax2DBenchmark`](@ref). |
| 53 | +""" |
| 54 | +function Utils.generate_dataset( |
| 55 | + bench::Argmax2DBenchmark, dataset_size=10; seed=nothing, rng=MersenneTwister(seed) |
| 56 | +) |
| 57 | + (; nb_features, encoder, polytope_vertex_range) = bench |
| 58 | + return map(1:dataset_size) do _ |
| 59 | + x = randn(rng, nb_features) |
| 60 | + θ_true = encoder(x) |
| 61 | + θ_true ./= 2 * norm(θ_true) |
| 62 | + instance = build_polytope(rand(rng, polytope_vertex_range); shift=rand(rng)) |
| 63 | + y_true = maximizer(θ_true; instance) |
| 64 | + return DataSample(; x=x, θ_true=θ_true, y_true=y_true, instance=instance) |
| 65 | + end |
| 66 | +end |
| 67 | + |
| 68 | +""" |
| 69 | +$TYPEDSIGNATURES |
| 70 | +
|
| 71 | +Maximizer for the [`Argmax2DBenchmark`](@ref). |
| 72 | +""" |
| 73 | +function Utils.generate_maximizer(::Argmax2DBenchmark) |
| 74 | + return maximizer |
| 75 | +end |
| 76 | + |
| 77 | +""" |
| 78 | +$TYPEDSIGNATURES |
| 79 | +
|
| 80 | +Generate a statistical model for the [`Argmax2DBenchmark`](@ref). |
| 81 | +""" |
| 82 | +function Utils.generate_statistical_model( |
| 83 | + bench::Argmax2DBenchmark; seed=nothing, rng=MersenneTwister(seed) |
| 84 | +) |
| 85 | + Random.seed!(rng, seed) |
| 86 | + (; nb_features) = bench |
| 87 | + model = Chain(Dense(nb_features => 2; bias=false), vec) |
| 88 | + return model |
| 89 | +end |
| 90 | + |
| 91 | +""" |
| 92 | +$TYPEDSIGNATURES |
| 93 | +
|
| 94 | +Plot the data sample for the [`Argmax2DBenchmark`](@ref). |
| 95 | +""" |
| 96 | +function Utils.plot_data( |
| 97 | + ::Argmax2DBenchmark, sample::DataSample; θ_true=sample.θ_true, kwargs... |
| 98 | +) |
| 99 | + (; instance) = sample |
| 100 | + pl = init_plot() |
| 101 | + plot_polytope!(pl, instance) |
| 102 | + plot_objective!(pl, θ_true) |
| 103 | + return plot_maximizer!(pl, θ_true, instance, maximizer) |
| 104 | +end |
| 105 | + |
| 106 | +export Argmax2DBenchmark |
| 107 | + |
| 108 | +end |
0 commit comments