/
runtests.jl
84 lines (69 loc) · 2.19 KB
/
runtests.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using NonsmoothOptim
using Test
@testset "NonsmoothOptim.jl" begin
@testset "Subgradient" begin
@testset "1D: abs(x)" begin
f = (x) -> abs(x[1])
g = (x) -> [(x[1] < 0) ? -1.0 : 1.0]
@testset "Minimise 1D" begin
p = UnconstrainedNonSmoothProblem(f, g, 1, Minimise)
a = SubgradientMethod(ConstantStepSize(1.0), 2)
x0 = [10.0]
x1 = solve(p, a, x0)
@test length(x1) == 1
@test x1[1] ≈ 8.0
end
@testset "Minimise 1D (start at optimum)" begin
p = UnconstrainedNonSmoothProblem(f, g, 1, Minimise)
a = SubgradientMethod(ConstantStepSize(1.0), 2)
x0 = [0.0]
x1 = solve(p, a, x0)
@test length(x1) == 1
@test x1[1] ≈ x0[1]
end
@testset "Maximise 1D" begin
p = UnconstrainedNonSmoothProblem(f, g, 1, Maximise)
a = SubgradientMethod(ConstantStepSize(1.0), 2)
x0 = [10.0]
x1 = solve(p, a, x0)
@test length(x1) == 1
@test x1[1] ≈ 12.0
end
@testset "Maximise 1D (2)" begin
p = UnconstrainedNonSmoothProblem(f, g, 1, Maximise)
a = SubgradientMethod(ConstantStepSize(1.0), 2)
x0 = [-10.0]
x1 = solve(p, a, x0)
@test length(x1) == 1
@test x1[1] ≈ -12.0
end
@testset "Info callback" begin
cb_called = false
cb = (k, f, x, g, f_best, x_best, δt) -> begin
cb_called = true
end
p = UnconstrainedNonSmoothProblem(f, g, 1, Maximise)
a = SubgradientMethod(ConstantStepSize(1.0), 2)
solve(p, a, [0.0], info_callback=cb)
@test cb_called
end
end
end
@testset "Projected subgradient" begin
@testset "1D: abs(x)" begin
f = (x) -> abs(x[1])
g = (x) -> [(x[1] < 0) ? -1.0 : 1.0]
proj = (x) -> [200.0]
@testset "Minimise 1D" begin
p = ProjectedConstrainedNonSmoothProblem(f, g, proj, 1, Minimise)
a = ProjectedSubgradientMethod(ConstantStepSize(1.0), 2)
x0 = [10.0] # Infeasible solution! Must be projected!
x1 = solve(p, a, x0)
@test length(x1) == 1
@test x1[1] ≈ 200.0
end
end
end
@testset "Bundle" begin
end
end