Skip to content

Commit 5da6cc9

Browse files
committed
style: formatting + cleanup useless dependencies
1 parent e68043d commit 5da6cc9

4 files changed

Lines changed: 123 additions & 73 deletions

File tree

Project.toml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
name = "DecisionFocusedLearningAlgorithms"
22
uuid = "46d52364-bc3b-4fac-a992-eb1d3ef2de15"
3-
authors = ["Members of JuliaDecisionFocusedLearning and contributors"]
43
version = "0.2.0"
4+
authors = ["Members of JuliaDecisionFocusedLearning and contributors"]
5+
6+
[workspace]
7+
projects = ["docs", "test"]
58

69
[deps]
710
DecisionFocusedLearningBenchmarks = "2fbe496a-299b-4c81-bab5-c44dfc55cf20"
811
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
9-
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
1012
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
1113
InferOpt = "4846b161-c94e-4150-8dac-c7ae193c601f"
12-
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
1314
MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54"
1415
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1516
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
@@ -21,10 +22,8 @@ ValueHistories = "98cad3c8-aec3-5f06-8e41-884608649ab7"
2122
[compat]
2223
DecisionFocusedLearningBenchmarks = "0.5.0, 0.6"
2324
DocStringExtensions = "0.9.5"
24-
Documenter = "1.17.0"
2525
Flux = "0.16.9"
2626
InferOpt = "0.7.1"
27-
Literate = "2.21.0"
2827
MLUtils = "0.4.8"
2928
Plots = "1.41.6"
3029
ProgressMeter = "1.11.0"
@@ -33,6 +32,3 @@ Statistics = "1.11.1"
3332
UnicodePlots = "3.8.2"
3433
ValueHistories = "0.5.6"
3534
julia = "1.11"
36-
37-
[workspace]
38-
projects = ["docs", "test"]

src/algorithms/MirrorDescent/mirror_descent.jl

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ Generate a dataset for the provided benchmark and train a DFLPolicy using the Mi
2222
# Core training method
2323
"""
2424

25-
2625
function train_policy(
2726
algorithm::MirrorDescent,
2827
benchmark::ExogenousStochasticBenchmark;
2928
dataset_size=30,
3029
epochs=10,
3130
iterations=10,
32-
κ = 1.0,
31+
κ=1.0,
3332
metrics::Tuple=(),
3433
seed=nothing,
3534
)
36-
3735
train_dataset = generate_dataset(benchmark, dataset_size; seed=seed)
3836

3937
# Initialize model and create policy
@@ -44,8 +42,8 @@ function train_policy(
4442
# vector because we store one history per iteration
4543
histories_per_iteration = MVHistory[]
4644

47-
anticipative_solver = generate_anticipative_solver(benchmark;)
48-
parametric_anticipative_solver = generate_parametric_anticipative_solver(benchmark;)
45+
anticipative_solver = generate_anticipative_solver(benchmark;)
46+
parametric_anticipative_solver = generate_parametric_anticipative_solver(benchmark;)
4947

5048
# perturb = true correspond to "real" iterations of mirror descent
5149
# we compute solutions with the penalized anticipative solver + perturbation
@@ -63,21 +61,25 @@ function train_policy(
6361
perturb = true
6462
end
6563

66-
6764
# Generate anticipative solutions as training data
6865
augmented_dataset = augment_dataset(
69-
algorithm.inner_algorithm, benchmark, train_dataset, model, anticipative_solver, parametric_anticipative_solver;
70-
κ = κ, perturb = perturb
66+
algorithm.inner_algorithm,
67+
benchmark,
68+
train_dataset,
69+
model,
70+
anticipative_solver,
71+
parametric_anticipative_solver;
72+
κ=κ,
73+
perturb=perturb,
7174
)
7275

73-
7476
# Train policy on augmented dataset
7577
history = train_policy!(
7678
algorithm.inner_algorithm,
7779
policy,
7880
augmented_dataset;
79-
epochs = epochs,
80-
metrics = metrics,
81+
epochs=epochs,
82+
metrics=metrics,
8183
maximizer_kwargs=sample -> sample.context,
8284
)
8385

@@ -87,52 +89,45 @@ function train_policy(
8789
return histories_per_iteration, policy
8890
end
8991

90-
9192
function augment_dataset(
9293
algorithm::PerturbedFenchelYoungLossImitation,
9394
bench::AbstractStochasticBenchmark,
9495
train_dataset::AbstractArray,
9596
model,
9697
anticipative_solver,
9798
parametric_anticipative_solver;
98-
κ = 1.0,
99-
perturb = false
99+
κ=1.0,
100+
perturb=false,
100101
)
101-
102102
(; nb_samples, ε, threaded, training_optimizer, seed) = algorithm
103103

104104
augmented_dataset = Vector{DataSample}()
105105

106106
if perturb
107107
perturbed_maximizer = PerturbedAdditive(
108-
parametric_anticipative_solver; ε=κ*ε, nb_samples=nb_samples
108+
parametric_anticipative_solver; ε=κ * ε, nb_samples=nb_samples
109109
)
110110
end
111111

112-
113112
for sample in train_dataset
114-
115113
θ = model(sample.x)
116114

117115
if perturb
118116
if is_minimization_problem(bench)
119-
y = perturbed_maximizer(-κ*θ; scenario = sample.scenario, context = sample)
117+
y = perturbed_maximizer(-κ * θ; scenario=sample.scenario, context=sample)
120118
else
121-
y = perturbed_maximizer*θ; scenario = sample.scenario, context = sample)
119+
y = perturbed_maximizer * θ; scenario=sample.scenario, context=sample)
122120
end
123121
else
124-
y = anticipative_solver(sample.scenario; context = sample)
122+
y = anticipative_solver(sample.scenario; context=sample)
125123
end
126124

127125
augmented_datasample = DataSample(;
128-
x = sample.x,
129-
y,
130-
instance = sample.context,
131-
extra = sample.extra
126+
x=sample.x, y, instance=sample.context, extra=sample.extra
132127
)
133128

134129
push!(augmented_dataset, augmented_datasample)
135130
end
136131

137132
return augmented_dataset
138-
end
133+
end

src/algorithms/mirror_descent/mirror_descent.jl

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ function train_policy!(
4343
κ=1.0,
4444
metrics::Tuple=(),
4545
verbose::Bool=false,
46-
imitation_start::Bool=true
46+
imitation_start::Bool=true,
4747
)
48-
4948
augmented_dataset = train_dataset
5049
return map(1:iterations) do n_it
5150
if verbose
@@ -55,8 +54,13 @@ function train_policy!(
5554
perturb = n_it > 1 || !imitation_start
5655

5756
augmented_dataset = augment_dataset(
58-
benchmark, augmented_dataset, policy.statistical_model, anticipative_solver, perturbed_anticipative_solver;
59-
κ=κ, perturb=perturb
57+
benchmark,
58+
augmented_dataset,
59+
policy.statistical_model,
60+
anticipative_solver,
61+
perturbed_anticipative_solver;
62+
κ=κ,
63+
perturb=perturb,
6064
)
6165

6266
train_policy!(
@@ -95,8 +99,6 @@ This high-level function handles all setup from the benchmark and returns a trai
9599
- `context_per_instance`: number of contexts per instance.
96100
"""
97101

98-
99-
100102
function train_policy(
101103
algorithm::MirrorDescent,
102104
benchmark::ExogenousStochasticBenchmark;
@@ -111,24 +113,47 @@ function train_policy(
111113
model_kwargs=(;),
112114
maximizer_kwargs=(;),
113115
solver_kwargs=(;),
114-
nb_scenarios = 1,
115-
context_per_instance = 1,
116+
nb_scenarios=1,
117+
context_per_instance=1,
116118
)
117-
train_dataset = generate_dataset(benchmark, dataset_size; nb_scenarios=nb_scenarios, contexts_per_instance=context_per_instance, seed=seed)
119+
train_dataset = generate_dataset(
120+
benchmark,
121+
dataset_size;
122+
nb_scenarios=nb_scenarios,
123+
contexts_per_instance=context_per_instance,
124+
seed=seed,
125+
)
118126

119127
model = generate_statistical_model(benchmark; seed=seed, model_kwargs...)
120128
maximizer = generate_maximizer(benchmark; maximizer_kwargs...)
121129
policy = DFLPolicy(model, maximizer)
122130

123131
anticipative_solver = generate_anticipative_solver(benchmark; solver_kwargs...)
124-
parametric_anticipative_solver = generate_parametric_anticipative_solver(benchmark; solver_kwargs...)
132+
parametric_anticipative_solver = generate_parametric_anticipative_solver(
133+
benchmark; solver_kwargs...
134+
)
125135
(; nb_samples, ε, threaded, seed) = algorithm.inner_algorithm
126-
perturbed_anticipative_solver = PerturbedAdditive((θ; scenario, kwargs...) -> parametric_anticipative_solver(θ, scenario; kwargs...); ε=κ*ε, nb_samples=nb_samples, seed=seed, threaded=threaded)
127-
136+
perturbed_anticipative_solver = PerturbedAdditive(
137+
(θ; scenario, kwargs...) -> parametric_anticipative_solver(θ, scenario; kwargs...);
138+
ε=κ * ε,
139+
nb_samples=nb_samples,
140+
seed=seed,
141+
threaded=threaded,
142+
)
128143

129144
histories_per_iteration = train_policy!(
130-
benchmark, algorithm, policy, train_dataset, anticipative_solver, perturbed_anticipative_solver;
131-
epochs=epochs, iterations=iterations, κ=κ, metrics=metrics, verbose=verbose, imitation_start=imitation_start
145+
benchmark,
146+
algorithm,
147+
policy,
148+
train_dataset,
149+
anticipative_solver,
150+
perturbed_anticipative_solver;
151+
epochs=epochs,
152+
iterations=iterations,
153+
κ=κ,
154+
metrics=metrics,
155+
verbose=verbose,
156+
imitation_start=imitation_start,
132157
)
133158

134159
return histories_per_iteration, policy
@@ -141,28 +166,42 @@ function augment_dataset(
141166
anticipative_solver,
142167
perturbed_anticipative_solver;
143168
κ=1.0,
144-
perturb=false
169+
perturb=false,
145170
)
146171
return _augment_dataset(
147172
Val(fieldtype(eltype(train_dataset), :y) !== Nothing),
148-
bench, train_dataset, model, anticipative_solver, perturbed_anticipative_solver;
149-
κ=κ, perturb=perturb
173+
bench,
174+
train_dataset,
175+
model,
176+
anticipative_solver,
177+
perturbed_anticipative_solver;
178+
κ=κ,
179+
perturb=perturb,
150180
)
151181
end
152182

153183
# Raw dataset (samples have no y) → create new DataSamples
154184
function _augment_dataset(
155185
::Val{false},
156-
bench, train_dataset, model, anticipative_solver, perturbed_anticipative_solver;
157-
κ=1.0, perturb=false
186+
bench,
187+
train_dataset,
188+
model,
189+
anticipative_solver,
190+
perturbed_anticipative_solver;
191+
κ=1.0,
192+
perturb=false,
158193
)
159194
return map(train_dataset) do sample
160195
θ = model(sample.x)
161196
if perturb
162197
if is_minimization_problem(bench)
163-
y = perturbed_anticipative_solver(-κ*θ; scenario=sample.scenario, sample.context...)
198+
y = perturbed_anticipative_solver(
199+
-κ * θ; scenario=sample.scenario, sample.context...
200+
)
164201
else
165-
y = perturbed_anticipative_solver*θ; scenario=sample.scenario, sample.context...)
202+
y = perturbed_anticipative_solver(
203+
κ * θ; scenario=sample.scenario, sample.context...
204+
)
166205
end
167206
else
168207
y = anticipative_solver(sample.scenario; sample.context...)
@@ -174,16 +213,25 @@ end
174213
# Augmented dataset (samples already have y) → update y in place
175214
function _augment_dataset(
176215
::Val{true},
177-
bench, train_dataset, model, anticipative_solver, perturbed_anticipative_solver;
178-
κ=1.0, perturb=false
216+
bench,
217+
train_dataset,
218+
model,
219+
anticipative_solver,
220+
perturbed_anticipative_solver;
221+
κ=1.0,
222+
perturb=false,
179223
)
180224
for (i, sample) in enumerate(train_dataset)
181225
θ = model(sample.x)
182226
if perturb
183227
if is_minimization_problem(bench)
184-
y = perturbed_anticipative_solver(-κ*θ; scenario=sample.scenario, sample.context...)
228+
y = perturbed_anticipative_solver(
229+
-κ * θ; scenario=sample.scenario, sample.context...
230+
)
185231
else
186-
y = perturbed_anticipative_solver*θ; scenario=sample.scenario, sample.context...)
232+
y = perturbed_anticipative_solver(
233+
κ * θ; scenario=sample.scenario, sample.context...
234+
)
187235
end
188236
else
189237
y = anticipative_solver(sample.scenario; sample.context...)
@@ -193,4 +241,4 @@ function _augment_dataset(
193241
train_dataset[i] = DataSample(sample; y=y_converted)
194242
end
195243
return train_dataset
196-
end
244+
end

0 commit comments

Comments
 (0)