Skip to content

Commit 521ae9d

Browse files
committed
test: connect and improve tests
1 parent e3d6cca commit 521ae9d

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ InferOpt = "4846b161-c94e-4150-8dac-c7ae193c601f"
77
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
88
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
99
MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54"
10+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1011
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1112
ValueHistories = "98cad3c8-aec3-5f06-8e41-884608649ab7"
1213

test/mirror_descent.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ using Test
44
using ValueHistories
55
using 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
108161
end

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ using DecisionFocusedLearningAlgorithms
1313
@testset "DAgger" begin
1414
include("dagger.jl")
1515
end
16+
17+
@testset "MirrorDescent" begin
18+
include("mirror_descent.jl")
19+
end
1620
end

0 commit comments

Comments
 (0)