1- mutable struct DummyState <: Optim.AbstractOptimizerState
2- x:: Any
3- x_previous:: Any
4- f_x:: Any
5- f_x_previous:: Any
6- g_x:: Any
1+ mutable struct DummyState{TX,TF,TG} <: Optim.AbstractOptimizerState
2+ x:: TX
3+ x_previous:: TX
4+ f_x:: TF
5+ f_x_previous:: TF
6+ g_x:: TG
77end
88
9- mutable struct DummyStateZeroth <: Optim.ZerothOrderState
10- x:: Any
11- x_previous:: Any
12- f_x:: Any
13- f_x_previous:: Any
14- g_x:: Any
9+ mutable struct DummyStateZeroth{TX,TF} <: Optim.ZerothOrderState
10+ x:: TX
11+ x_previous:: TX
12+ f_x:: TF
13+ f_x_previous:: TF
1514end
1615
1716mutable struct DummyOptions
@@ -81,7 +80,7 @@ mutable struct DummyMethodZeroth <: Optim.ZerothOrderOptimizer end
8180 @test Optim. initial_convergence (ds, opt) == (true , false )
8281
8382 # Zeroth order methods have no gradient -> returns false by default
84- ds = DummyStateZeroth (x1, x0, f1, f0, g )
83+ ds = DummyStateZeroth (x1, x0, f1, f0)
8584 dm = DummyMethodZeroth ()
8685
8786 x = ones (2 )
@@ -92,4 +91,18 @@ mutable struct DummyMethodZeroth <: Optim.ZerothOrderOptimizer end
9291
9392 # should check all other methods as well
9493
94+ for T in (Float32, Float64)
95+ ds = DummyState (T[- 1.3 , 2.5 , - 4.1 ], T[- 1.1 , 2.8 , - 4.0 ], f_x, f0, zeros (3 ))
96+ @test @inferred (Optim. x_abschange (ds)):: T ≈ 0.3
97+ @test iszero ((s -> @allocated (Optim. x_abschange (s)))(ds))
98+ @test @inferred (Optim. x_relchange (ds)):: T ≈ 0.3 / 4.1
99+ @test iszero ((s -> @allocated (Optim. x_relchange (s)))(ds))
100+
101+ # Special case: Empty state
102+ ds = DummyState (T[], T[], f_x, f0, empty (g_x))
103+ @test iszero (@inferred (Optim. x_abschange (ds)):: T )
104+ @test iszero ((s -> @allocated (Optim. x_abschange (s)))(ds))
105+ @test isnan (@inferred (Optim. x_relchange (ds)):: T )
106+ @test iszero ((s -> @allocated (Optim. x_relchange (s)))(ds))
107+ end
95108end
0 commit comments