|
| 1 | +module Ranking |
| 2 | + |
| 3 | +using ..Utils |
| 4 | +using DocStringExtensions: TYPEDEF, TYPEDFIELDS, TYPEDSIGNATURES |
| 5 | +using Flux: Chain, Dense |
| 6 | +using Random |
| 7 | + |
| 8 | +""" |
| 9 | +$TYPEDEF |
| 10 | +
|
| 11 | +Benchmark problem with an argmax as the CO algorithm. |
| 12 | +
|
| 13 | +# Fields |
| 14 | +$TYPEDFIELDS |
| 15 | +""" |
| 16 | +struct RankingBenchmark <: AbstractBenchmark |
| 17 | + "iinstances dimension, total number of classes" |
| 18 | + instance_dim::Int |
| 19 | + "number of features" |
| 20 | + nb_features::Int |
| 21 | +end |
| 22 | + |
| 23 | +function Base.show(io::IO, bench::RankingBenchmark) |
| 24 | + (; instance_dim, nb_features) = bench |
| 25 | + return print( |
| 26 | + io, "RankingBenchmark(instance_dim=$instance_dim, nb_features=$nb_features)" |
| 27 | + ) |
| 28 | +end |
| 29 | + |
| 30 | +function RankingBenchmark(; instance_dim::Int=10, nb_features::Int=5) |
| 31 | + return RankingBenchmark(instance_dim, nb_features) |
| 32 | +end |
| 33 | + |
| 34 | +""" |
| 35 | +$TYPEDSIGNATURES |
| 36 | +
|
| 37 | +Compute the vector `r` such that `rᵢ` is the rank of `θᵢ` in `θ`. |
| 38 | +""" |
| 39 | +function ranking(θ::AbstractVector; rev::Bool=false, kwargs...) |
| 40 | + return invperm(sortperm(θ; rev=rev)) |
| 41 | +end |
| 42 | + |
| 43 | +""" |
| 44 | +$TYPEDSIGNATURES |
| 45 | +
|
| 46 | +Return a top k maximizer. |
| 47 | +""" |
| 48 | +function Utils.generate_maximizer(bench::RankingBenchmark) |
| 49 | + return ranking |
| 50 | +end |
| 51 | + |
| 52 | +""" |
| 53 | +$TYPEDSIGNATURES |
| 54 | +
|
| 55 | +Generate a dataset of labeled instances for the subset selection problem. |
| 56 | +The mapping between features and cost is identity. |
| 57 | +""" |
| 58 | +function Utils.generate_dataset(bench::RankingBenchmark, dataset_size::Int=10; seed::Int=0) |
| 59 | + (; instance_dim, nb_features) = bench |
| 60 | + rng = MersenneTwister(seed) |
| 61 | + features = [randn(rng, Float32, nb_features, instance_dim) for _ in 1:dataset_size] |
| 62 | + mapping = Chain(Dense(nb_features => 1; bias=false), vec) |
| 63 | + costs = mapping.(features) |
| 64 | + solutions = ranking.(costs) |
| 65 | + return [ |
| 66 | + DataSample(; x, θ_true, y_true) for |
| 67 | + (x, θ_true, y_true) in zip(features, costs, solutions) |
| 68 | + ] |
| 69 | +end |
| 70 | + |
| 71 | +""" |
| 72 | +$TYPEDSIGNATURES |
| 73 | +
|
| 74 | +Initialize a linear model for `bench` using `Flux`. |
| 75 | +""" |
| 76 | +function Utils.generate_statistical_model(bench::RankingBenchmark; seed=0) |
| 77 | + Random.seed!(seed) |
| 78 | + (; nb_features) = bench |
| 79 | + return Chain(Dense(nb_features => 1; bias=false), vec) |
| 80 | +end |
| 81 | + |
| 82 | +export RankingBenchmark |
| 83 | + |
| 84 | +end |
0 commit comments