Skip to content

Commit de6213c

Browse files
authored
Merge pull request #15 from JuliaDecisionFocusedLearning/update-datasample
Rename DataSample fields
2 parents 48a812c + ed8a37b commit de6213c

12 files changed

Lines changed: 36 additions & 33 deletions

File tree

docs/src/tutorials/warcraft.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ train_dataset, test_dataset = dataset[1:45], dataset[46:50]
2525
sample = test_dataset[1]
2626
# `x` correspond to the input features, i.e. the input image (3D array) in the Warcraft benchmark case:
2727
x = sample.x
28-
# `θ` correspond to the true unknown terrain weights. We use the opposite of the true weights in order to formulate the optimization problem as a maximization problem:
29-
θ_true = sample.θ
30-
# `y` correspond to the optimal shortest path, encoded as a binary matrix:
31-
y_true = sample.y
28+
# `θ_true` correspond to the true unknown terrain weights. We use the opposite of the true weights in order to formulate the optimization problem as a maximization problem:
29+
θ_true = sample.θ_true
30+
# `y_true` correspond to the optimal shortest path, encoded as a binary matrix:
31+
y_true = sample.y_true
3232
# `instance` is not used in this benchmark, therefore set to nothing:
3333
isnothing(sample.instance)
3434

@@ -50,7 +50,7 @@ maximizer = generate_maximizer(b; dijkstra=true)
5050
# In the case o fthe Warcraft benchmark, the method has an additional keyword argument to chose the algorithm to use: Dijkstra's algorithm or Bellman-Ford algorithm.
5151
y = maximizer(θ)
5252
# As we can see, currently the pipeline predicts random noise as cell weights, and therefore the maximizer returns a straight line path.
53-
plot_data(b, DataSample(; x, θ, y))
53+
plot_data(b, DataSample(; x, θ_true=θ, y_true=y))
5454
# We can evaluate the current pipeline performance using the optimality gap metric:
5555
starting_gap = compute_gap(b, test_dataset, model, maximizer)
5656

@@ -70,7 +70,7 @@ opt_state = Flux.setup(Adam(1e-3), model)
7070
loss_history = Float64[]
7171
for epoch in 1:50
7272
val, grads = Flux.withgradient(model) do m
73-
sum(loss(m(sample.x), sample.y) for sample in train_dataset) / length(train_dataset)
73+
sum(loss(m(x), y_true) for (; x, y_true) in train_dataset) / length(train_dataset)
7474
end
7575
Flux.update!(opt_state, model, grads[1])
7676
push!(loss_history, val)
@@ -85,4 +85,4 @@ final_gap = compute_gap(b, test_dataset, model, maximizer)
8585
#
8686
θ = model(x)
8787
y = maximizer(θ)
88-
plot_data(b, DataSample(; x, θ, y))
88+
plot_data(b, DataSample(; x, θ_true=θ, y_true=y))

src/FixedSizeShortestPath/FixedSizeShortestPath.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ function Utils.generate_dataset(
132132

133133
# Label solutions
134134
solutions = shortest_path_maximizer.(costs)
135-
return [DataSample(; x=x, θ=θ, y=y) for (x, θ, y) in zip(features, costs, solutions)]
135+
return [
136+
DataSample(; x, θ_true, y_true) for
137+
(x, θ_true, y_true) in zip(features, costs, solutions)
138+
]
136139
end
137140

138141
"""

src/PortfolioOptimization/PortfolioOptimization.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ function Utils.generate_dataset(
110110
maximizer = Utils.generate_maximizer(bench)
111111
solutions = maximizer.(costs)
112112

113-
return [DataSample(; x=x, θ=θ, y=y) for (x, θ, y) in zip(features, costs, solutions)]
113+
return [
114+
DataSample(; x, θ_true, y_true) for
115+
(x, θ_true, y_true) in zip(features, costs, solutions)
116+
]
114117
end
115118

116119
"""

src/SubsetSelection/SubsetSelection.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ function Utils.generate_dataset(
7373
mapping.(features)
7474
end
7575
solutions = top_k.(costs, k)
76-
return [DataSample(; x=x, θ=θ, y=y) for (x, θ, y) in zip(features, costs, solutions)]
76+
return [
77+
DataSample(; x, θ_true, y_true) for
78+
(x, θ_true, y_true) in zip(features, costs, solutions)
79+
]
7780
end
7881

7982
"""

src/Utils/data_sample.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ $TYPEDFIELDS
99
@kwdef struct DataSample{F,S,C,I}
1010
"features"
1111
x::F
12-
"costs"
13-
θ::C = nothing
14-
"solution"
15-
y::S = nothing
16-
"instance"
12+
"target cost parameters (optional)"
13+
θ_true::C = nothing
14+
"target solution (optional)"
15+
y_true::S = nothing
16+
"instance object (optional)"
1717
instance::I = nothing
1818
end

src/Utils/interface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ function compute_gap(
7373

7474
for sample in dataset
7575
x = sample.x
76-
θ̄ = sample.θ
77-
= sample.y
76+
θ̄ = sample.θ_true
77+
= sample.y_true
7878
θ = statistical_model(x)
7979
y = maximizer(θ)
8080
target_obj = objective_value(bench, θ̄, ȳ)

src/Warcraft/Warcraft.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ The keyword argument `θ_true` is used to set the color range of the weights plo
8787
function Utils.plot_data(
8888
::WarcraftBenchmark,
8989
sample::DataSample;
90-
θ_true=sample.θ,
90+
θ_true=sample.θ_true,
9191
θ_title="Weights",
9292
y_title="Path",
9393
kwargs...,
9494
)
95-
(; x, y, θ) = sample
95+
x = sample.x
96+
y = sample.y_true
97+
θ = sample.θ_true
9698
im = dropdims(x; dims=4)
9799
img = convert_image_for_plot(im)
98100
p1 = Plots.plot(

src/Warcraft/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function create_dataset(decompressed_path::String, nb_samples::Int)
4040
]
4141
Y = [BitMatrix(terrain_labels[:, :, i]) for i in 1:N]
4242
WG = [-terrain_weights[:, :, i] for i in 1:N]
43-
return [DataSample(; x, y, θ) for (x, y, θ) in zip(X, Y, WG)]
43+
return [DataSample(; x, y_true, θ_true) for (x, y_true, θ_true) in zip(X, Y, WG)]
4444
end
4545

4646
"""

test/fixed_size_shortest_path.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
@test gap >= 0
1919

2020
for sample in dataset
21-
x = sample.x
22-
θ_true = sample.θ
23-
y_true = sample.y
21+
(; x, θ_true, y_true) = sample
2422
@test all(θ_true .< 0)
2523
@test size(x) == (p,)
2624
@test length(θ_true) == A

test/portfolio_optimization.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
maximizer = generate_maximizer(b)
1111

1212
for sample in dataset
13-
x = sample.x
14-
θ_true = sample.θ
15-
y_true = sample.y
13+
(; x, θ_true, y_true) = sample
1614
@test size(x) == (p,)
1715
@test length(θ_true) == d
1816
@test length(y_true) == d
@@ -24,6 +22,6 @@
2422

2523
y = maximizer(θ)
2624
@test length(y) == d
27-
@test sum(y) <= 1
25+
@test sum(y) <= 1 + 1e-6
2826
end
2927
end

0 commit comments

Comments
 (0)