@@ -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-
100102function 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 )
151181end
152182
153183# Raw dataset (samples have no y) → create new DataSamples
154184function _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
175214function _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