@@ -4,6 +4,19 @@ using Test
44using ValueHistories
55using Statistics: mean
66
7+ function _val_obj_metric (benchmark, val_data)
8+ return FunctionMetric (:val_obj , val_data) do ctx, data
9+ vals = map (data) do s
10+ θ = ctx. policy. statistical_model (s. x)
11+ y = ctx. policy. maximizer (θ; s. context... )
12+ return Float64 (
13+ DecisionFocusedLearningBenchmarks. objective_value (benchmark, s, y)
14+ )
15+ end
16+ return (val_obj= mean (vals),)
17+ end
18+ end
19+
720@testset " MirrorDescent Training" begin
821 @testset " MirrorDescent - ContextualStochasticArgmax basic" begin
922 benchmark = ContextualStochasticArgmaxBenchmark ()
@@ -105,4 +118,44 @@ using Statistics: mean
105118
106119 @test all (haskey (h, :epoch ) for h in histories)
107120 end
121+
122+ @testset " MirrorDescent - trained beats untrained (ContextualStochasticArgmax)" begin
123+ benchmark = ContextualStochasticArgmaxBenchmark ()
124+ val_data = generate_dataset (benchmark, 50 ; seed= 99 )
125+
126+ histories, _ = train_policy (
127+ MirrorDescent (),
128+ benchmark;
129+ dataset_size= 20 ,
130+ epochs= 5 ,
131+ iterations= 5 ,
132+ seed= 0 ,
133+ metrics= (_val_obj_metric (benchmark, val_data),),
134+ )
135+
136+ obj_untrained = get (histories[1 ], :val_obj )[2 ][1 ]
137+ obj_trained = get (histories[end ], :val_obj )[2 ][end ]
138+
139+ @test obj_trained > obj_untrained
140+ end
141+
142+ @testset " MirrorDescent - trained beats untrained (StochasticVehicleScheduling)" begin
143+ benchmark = StochasticVehicleSchedulingBenchmark ()
144+ val_data = generate_dataset (benchmark, 10 ; seed= 99 )
145+
146+ histories, _ = train_policy (
147+ MirrorDescent (),
148+ benchmark;
149+ dataset_size= 10 ,
150+ epochs= 5 ,
151+ iterations= 2 ,
152+ seed= 0 ,
153+ metrics= (_val_obj_metric (benchmark, val_data),),
154+ )
155+
156+ obj_untrained = get (histories[1 ], :val_obj )[2 ][1 ]
157+ obj_trained = get (histories[end ], :val_obj )[2 ][end ]
158+
159+ @test obj_trained < obj_untrained
160+ end
108161end
0 commit comments