|
| 1 | +function trust_region_allocs_test(::Type{Solver}, ::Type{S}) where {Solver, S} |
| 2 | + T = eltype(S) |
| 3 | + @testset "Allocation - NLP" begin |
| 4 | + nlp = BROWNDEN(S) |
| 5 | + n = nlp.meta.nvar |
| 6 | + Δ₀ = T(10) |
| 7 | + tr = Solver(S, n, Δ₀) |
| 8 | + x = fill!(S(undef, n), 0) |
| 9 | + d = fill!(S(undef, n), 1) |
| 10 | + f, ft, Δm, slope = T(2), T(1), -T(1), -T(1) |
| 11 | + al = @wrappedallocs aredpred!(tr, nlp, f, ft, Δm, x, d, slope) |
| 12 | + @test al == 0 |
| 13 | + al = @wrappedallocs aredpred!(tr, nlp, ft, ft, Δm, x, d, slope) |
| 14 | + @test al == 0 |
| 15 | + end |
| 16 | + |
| 17 | + @testset "Allocation - NLS" begin |
| 18 | + nlp = LLS(S) |
| 19 | + n = nlp.meta.nvar |
| 20 | + Δ₀ = T(10) |
| 21 | + tr = Solver(S, n, Δ₀) |
| 22 | + x = fill!(S(undef, n), 0) |
| 23 | + d = fill!(S(undef, n), 1) |
| 24 | + f, ft, Δm, slope = T(2), T(1), -T(1), -T(1) |
| 25 | + Fx = fill!(S(undef, nlp.nls_meta.nequ), 1) |
| 26 | + al = @wrappedallocs aredpred!(tr, nlp, Fx, f, ft, Δm, x, d, slope) |
| 27 | + @test al == 0 |
| 28 | + al = @wrappedallocs aredpred!(tr, nlp, Fx, ft, ft, Δm, x, d, slope) |
| 29 | + @test al == 0 |
| 30 | + end |
| 31 | +end |
| 32 | + |
| 33 | +function test_aredpred(nlp, ::Type{Solver}, ::Type{S}) where {Solver, S} |
| 34 | + T = eltype(S) |
| 35 | + x = nlp.meta.x0 |
| 36 | + d = fill!(S(undef, 2), -1) |
| 37 | + xt = x + d |
| 38 | + f = obj(nlp, x) |
| 39 | + ft = obj(nlp, xt) |
| 40 | + Δm = ft - f |
| 41 | + |
| 42 | + tr = Solver(S, 2, T(100)) |
| 43 | + ared, pred = aredpred!(tr, nlp, f, ft, Δm, xt, d, dot(grad(nlp, x), d)) |
| 44 | + @test abs(ared - pred) < 1e-12 |
| 45 | + |
| 46 | + d = fill!(S(undef, 2), -1e-12) |
| 47 | + xt = x + d |
| 48 | + ft = obj(nlp, xt) |
| 49 | + Δm = ft - f |
| 50 | + ared, pred = aredpred!(tr, nlp, f, ft, Δm, xt, d, dot(grad(nlp, x), d)) |
| 51 | + @test abs(ared - pred) < 1e-12 |
| 52 | +end |
| 53 | + |
1 | 54 | @testset "Trust Region" begin |
2 | | - @testset "aredpred-$(nlp.meta.name)" for nlp in ( |
3 | | - ADNLPModel(x -> x[1]^2 + 4 * x[2]^2, ones(2), name = "NLP"), |
4 | | - ADNLSModel(x -> [x[1]; 2 * x[2]], ones(2), 2, name = "NLS"), |
5 | | - ) |
6 | | - x = nlp.meta.x0 |
7 | | - d = -ones(2) |
8 | | - xt = x + d |
9 | | - f = obj(nlp, x) |
10 | | - ft = obj(nlp, xt) |
11 | | - Δm = ft - f |
12 | | - tr = TrustRegion(2, 100.0) |
13 | | - ared, pred = aredpred!(tr, nlp, f, ft, Δm, xt, d, dot(grad(nlp, x), d)) |
14 | | - @test abs(ared - pred) < 1e-12 |
15 | | - tr = TRONTrustRegion(2, 100.0) |
16 | | - ared, pred = aredpred!(tr, nlp, f, ft, Δm, xt, d, dot(grad(nlp, x), d)) |
17 | | - @test abs(ared - pred) < 1e-12 |
18 | | - d = -1e-12 * ones(2) |
19 | | - xt = x + d |
20 | | - ft = obj(nlp, xt) |
21 | | - Δm = ft - f |
22 | | - ared, pred = aredpred!(tr, nlp, f, ft, Δm, xt, d, dot(grad(nlp, x), d)) |
23 | | - @test abs(ared - pred) < 1e-12 |
| 55 | + S = Vector{Float64} |
| 56 | + @testset "aredpred-$(nlp.meta.name) - $S - $TRSolver" for nlp in ( |
| 57 | + ADNLPModel(x -> x[1]^2 + 4 * x[2]^2, fill!(S(undef, 2), 1), name = "NLP", matrix_free = true), |
| 58 | + ADNLSModel(x -> [x[1]; 2 * x[2]], fill!(S(undef, 2), 1), 2, name = "NLS", matrix_free = true), |
| 59 | + ), TRSolver in (TrustRegion, TRONTrustRegion) |
| 60 | + test_aredpred(nlp, TRSolver, S) |
| 61 | + end |
| 62 | + |
| 63 | + if CUDA.functional() |
| 64 | + CUDA.allowscalar() do |
| 65 | + S = CuVector{Float64} |
| 66 | + @testset "aredpred-$(nlp.meta.name) - $S - $TRSolver" for nlp in ( |
| 67 | + ADNLPModel(x -> x[1]^2 + 4 * x[2]^2, fill!(S(undef, 2), 1), name = "NLP", matrix_free = true), |
| 68 | + ADNLSModel(x -> [x[1]; 2 * x[2]], fill!(S(undef, 2), 1), 2, name = "NLS", matrix_free = true), |
| 69 | + ), TRSolver in (TrustRegion, TRONTrustRegion) |
| 70 | + test_aredpred(nlp, TRSolver, S) |
| 71 | + end |
| 72 | + end |
24 | 73 | end |
25 | 74 |
|
26 | 75 | @testset "BasicTrustRegion" begin |
27 | | - Δ₀ = 10.0 |
28 | | - tr = TrustRegion(2, Δ₀) |
29 | | - tr.ratio = 1.0 |
| 76 | + S = Vector{Float64} |
| 77 | + T = eltype(S) |
| 78 | + Δ₀ = T(10) |
| 79 | + tr = TrustRegion(S, 2, Δ₀) |
| 80 | + tr.ratio = T(1) |
30 | 81 | @test acceptable(tr) == true |
31 | 82 |
|
32 | | - tr.ratio = 10.0 |
| 83 | + tr.ratio = T(10) |
33 | 84 | update!(tr, Δ₀) |
34 | 85 | @test tr.radius > Δ₀ |
35 | 86 | reset!(tr) |
36 | | - tr.ratio = -1.0 |
| 87 | + tr.ratio = -T(1) |
37 | 88 | update!(tr, Δ₀) |
38 | 89 | @test tr.radius < Δ₀ |
39 | 90 | reset!(tr) |
40 | 91 |
|
41 | 92 | if VERSION ≥ v"1.7" |
42 | | - @testset "Allocation" begin |
43 | | - nlp = BROWNDEN() |
44 | | - n = nlp.meta.nvar |
45 | | - Δ₀ = 10.0 |
46 | | - tr = TrustRegion(n, Δ₀) |
47 | | - x = zeros(n) |
48 | | - d = ones(n) |
49 | | - f, ft, Δm, slope = 2.0, 1.0, -1.0, -1.0 |
50 | | - al = @wrappedallocs aredpred!(tr, nlp, f, ft, Δm, x, d, slope) |
51 | | - @test al == 0 |
52 | | - al = @wrappedallocs aredpred!(tr, nlp, ft, ft, Δm, x, d, slope) |
53 | | - @test al == 0 |
54 | | - end |
55 | | - |
56 | | - @testset "Allocation - nonlinear least squares" begin |
57 | | - nlp = LLS() |
58 | | - n = nlp.meta.nvar |
59 | | - Δ₀ = 10.0 |
60 | | - tr = TrustRegion(n, Δ₀) |
61 | | - x = zeros(n) |
62 | | - d = ones(n) |
63 | | - f, ft, Δm, slope = 2.0, 1.0, -1.0, -1.0 |
64 | | - Fx = zeros(nlp.nls_meta.nequ) |
65 | | - al = @wrappedallocs aredpred!(tr, nlp, Fx, f, ft, Δm, x, d, slope) |
66 | | - @test al == 0 |
67 | | - al = @wrappedallocs aredpred!(tr, nlp, Fx, ft, ft, Δm, x, d, slope) |
68 | | - @test al == 0 |
69 | | - end |
| 93 | + trust_region_allocs_test(TrustRegion, S) |
70 | 94 | end |
71 | 95 | end |
72 | 96 |
|
73 | 97 | @testset "TRONTrustRegion" begin |
74 | | - Δ₀ = 10.0 |
75 | | - tr = TRONTrustRegion(2, Δ₀) |
76 | | - tr.ratio = 1.0 |
| 98 | + S = Vector{Float64} |
| 99 | + T = eltype(S) |
| 100 | + Δ₀ = T(10) |
| 101 | + tr = TRONTrustRegion(S, 2, Δ₀) |
| 102 | + tr.ratio = T(1) |
77 | 103 | @test acceptable(tr) == true |
78 | 104 |
|
79 | 105 | tr.ratio = tr.acceptance_threshold - 1 |
|
89 | 115 | @test tr.radius < Δ₀ |
90 | 116 | reset!(tr) |
91 | 117 | tr.ratio = tr.increase_threshold + 1 |
92 | | - tr.quad_min = 2.0 |
| 118 | + tr.quad_min = T(2) |
93 | 119 | update!(tr, Δ₀) |
94 | 120 | @test tr.radius > Δ₀ |
95 | 121 |
|
96 | 122 | if VERSION ≥ v"1.7" |
97 | | - @testset "Allocation" begin |
98 | | - nlp = BROWNDEN() |
99 | | - n = nlp.meta.nvar |
100 | | - Δ₀ = 10.0 |
101 | | - tr = TRONTrustRegion(n, Δ₀) |
102 | | - x = zeros(n) |
103 | | - d = ones(n) |
104 | | - f, ft, Δm, slope = 2.0, 1.0, -1.0, -1.0 |
105 | | - al = @wrappedallocs aredpred!(tr, nlp, f, ft, Δm, x, d, slope) |
106 | | - @test al == 0 |
107 | | - al = @wrappedallocs aredpred!(tr, nlp, ft, ft, Δm, x, d, slope) |
108 | | - @test al == 0 |
109 | | - end |
110 | | - |
111 | | - @testset "Allocation - nonlinear least squares" begin |
112 | | - nlp = LLS() |
113 | | - n = nlp.meta.nvar |
114 | | - Δ₀ = 10.0 |
115 | | - tr = TRONTrustRegion(n, Δ₀) |
116 | | - x = zeros(n) |
117 | | - d = ones(n) |
118 | | - f, ft, Δm, slope = 2.0, 1.0, -1.0, -1.0 |
119 | | - Fx = zeros(nlp.nls_meta.nequ) |
120 | | - al = @wrappedallocs aredpred!(tr, nlp, Fx, f, ft, Δm, x, d, slope) |
121 | | - @test al == 0 |
122 | | - al = @wrappedallocs aredpred!(tr, nlp, Fx, ft, ft, Δm, x, d, slope) |
123 | | - @test al == 0 |
124 | | - end |
| 123 | + trust_region_allocs_test(TRONTrustRegion, S) |
125 | 124 | end |
126 | 125 | end |
127 | 126 |
|
128 | | - struct Workspace |
129 | | - ∇fnext::Array{Float64} |
| 127 | + struct Workspace{S} |
| 128 | + ∇fnext::S |
130 | 129 | end |
| 130 | + |
131 | 131 | @testset "ARTrustRegion" begin |
132 | | - Δ₀ = 0.8 |
133 | | - ar = ARTrustRegion(0.8) |
| 132 | + S = Vector{Float64} |
| 133 | + T = eltype(S) |
| 134 | + Δ₀ = T(0.8) |
| 135 | + ar = ARTrustRegion(T(0.8)) |
134 | 136 | @test ar.α₀ == 0.8 |
135 | 137 | @test ar.α == 0.8 |
136 | | - @test ar.max_α ≈ 1 / sqrt(eps(typeof(0.8))) |
| 138 | + @test ar.max_α ≈ 1 / sqrt(eps(T)) |
137 | 139 | @test ar.acceptance_threshold == 0.1 |
138 | 140 | @test ar.increase_threshold == 0.75 |
139 | 141 | @test ar.reduce_threshold == 0.1 |
|
146 | 148 | return grad!(nlp, x, workspace.∇fnext) |
147 | 149 | end |
148 | 150 |
|
149 | | - nlp = BROWNDEN() |
150 | | - f = 1.0 |
151 | | - Δf = 0.2 |
152 | | - Δq = 0.4 |
153 | | - slope = 0.5 |
154 | | - d = [1.0, 2.0, 3.0, 4.0] |
155 | | - xnext = [0.5, 1.5, 2.5, 3.5] |
156 | | - workspace = Workspace([0.8, 1.8, 2.8, 3.8]) |
| 151 | + nlp = BROWNDEN(S) |
| 152 | + f = T(1) |
| 153 | + Δf = T(0.2) |
| 154 | + Δq = T(0.4) |
| 155 | + slope = T(0.5) |
| 156 | + d = S([1.0, 2.0, 3.0, 4.0]) |
| 157 | + xnext = S([0.5, 1.5, 2.5, 3.5]) |
| 158 | + workspace = Workspace(S([0.8, 1.8, 2.8, 3.8])) |
157 | 159 | robust = true |
158 | 160 |
|
159 | 161 | r, good_grad, gnext = SolverTools.compute_r(nlp, f, Δf, Δq, slope, d, xnext, workspace, robust) |
|
162 | 164 | @test good_grad == false |
163 | 165 | @test gnext == [0.8, 1.8, 2.8, 3.8] |
164 | 166 |
|
165 | | - Δq = 1e-13 |
| 167 | + Δq = T(1e-13) |
166 | 168 | r, good_grad, gnext = SolverTools.compute_r(nlp, f, Δf, Δq, slope, d, xnext, workspace, robust) |
167 | 169 |
|
168 | 170 | @test r == 3.833532881060353e19 |
169 | 171 | @test good_grad == true |
170 | 172 | @test gnext == |
171 | 173 | [-909318.2269441625, -3.4465870681692427e6, 47999.033762642575, -2142.750031496445] |
172 | 174 |
|
173 | | - workspace = Workspace([0.8, 1.8, 2.8, 3.8]) |
| 175 | + workspace = Workspace(S([0.8, 1.8, 2.8, 3.8])) |
174 | 176 | robust = false |
175 | 177 | r, good_grad, gnext = SolverTools.compute_r(nlp, f, Δf, Δq, slope, d, xnext, workspace, robust) |
176 | 178 |
|
|
0 commit comments