/
consistency.jl
209 lines (182 loc) · 6.6 KB
/
consistency.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import LinearAlgebra: I
export consistent_nlss
"""
consistent_nlss(nlps; exclude=[hess, hprod, hess_coord])
Check that the all `nls`s of the vector `nlss` are consistent, in the sense that
- Their counters are the same.
- Their `meta` information is the same.
- The API functions return the same output given the same input.
In other words, if you create two models of the same problem, they should be consistent.
By default, the functions `hess`, `hprod` and `hess_coord` (and therefore associated functions) are excluded from this check, since some models don't implement them.
"""
function consistent_nlss(
nlss;
exclude = [hess, hess_coord, jth_hess, jth_hess_coord, jth_hprod, ghjvprod],
linear_api = false,
test_slack = true,
test_ff = true,
)
consistent_nls_counters(nlss)
consistent_counters(nlss, linear_api = linear_api)
consistent_nls_functions(nlss, exclude = exclude)
consistent_nls_counters(nlss)
consistent_counters(nlss, linear_api = linear_api)
for nls in nlss
reset!(nls)
end
consistent_functions(nlss, linear_api = linear_api, exclude = exclude)
if test_slack && has_inequalities(nlss[1])
reset!.(nlss)
slack_nlss = SlackNLSModel.(nlss)
consistent_nls_functions(slack_nlss, exclude = exclude)
consistent_nls_counters(slack_nlss)
consistent_counters(slack_nlss, linear_api = linear_api)
consistent_functions(
slack_nlss,
linear_api = linear_api,
exclude = [jth_hess, jth_hess_coord, jth_hprod] ∪ exclude,
)
end
if test_ff
reset!.(nlss)
ff_nlss = FeasibilityFormNLS.(nlss)
consistent_nls_functions(ff_nlss, exclude = exclude)
consistent_nls_counters(ff_nlss)
consistent_counters(ff_nlss, linear_api = false)
consistent_functions(
ff_nlss,
linear_api = false,
exclude = [jth_hess, jth_hess_coord, jth_hprod] ∪ exclude,
)
end
end
function consistent_nls_counters(nlss)
N = length(nlss)
V = zeros(Int, N)
for field in fieldnames(NLSCounters)
field == :counters && continue
@testset "Field $field" begin
V = [eval(field)(nls) for nls in nlss]
@test all(V .== V[1])
end
end
V = [sum_counters(nls) for nls in nlss]
@test all(V .== V[1])
end
function consistent_nls_functions(nlss; rtol = 1.0e-8, exclude = [])
N = length(nlss)
n = nls_meta(nlss[1]).nvar
m = nls_meta(nlss[1]).nequ
tmp_n = zeros(n)
tmp_m = zeros(m)
x = 10 * [-(-1.0)^i for i = 1:n]
if !(residual in exclude)
Fs = Any[residual(nls, x) for nls in nlss]
for i = 1:N
for j = (i + 1):N
@test isapprox(Fs[i], Fs[j], rtol = rtol)
end
r = residual!(nlss[i], x, tmp_m)
@test isapprox(r, Fs[i], rtol = rtol)
@test isapprox(Fs[i], tmp_m, rtol = rtol)
end
end
if intersect([jac_residual, jac_coord_residual], exclude) == []
Js = Any[jac_residual(nls, x) for nls in nlss]
for i = 1:N
for j = (i + 1):N
@test isapprox(Js[i], Js[j], rtol = rtol)
end
V = jac_coord_residual(nlss[i], x)
I, J = jac_structure_residual(nlss[i])
@test length(I) == length(J) == length(V) == nlss[i].nls_meta.nnzj
I2, J2 = copy(I), copy(J)
jac_structure_residual!(nlss[i], I2, J2)
@test I == I2
@test J == J2
tmp_V = zeros(nlss[i].nls_meta.nnzj)
jac_coord_residual!(nlss[i], x, tmp_V)
@test tmp_V == V
end
end
if intersect([jac_op_residual, jprod_residual, jtprod_residual], exclude) == []
J_ops = Any[jac_op_residual(nls, x) for nls in nlss]
Jv, Jtv = zeros(m), zeros(n)
J_ops_inplace = Any[jac_op_residual!(nls, x, Jv, Jtv) for nls in nlss]
v = [-(-1.0)^i for i = 1:n]
Jps = Any[jprod_residual(nls, x, v) for nls in nlss]
for i = 1:N
for j = (i + 1):N
@test isapprox(Jps[i], Jps[j], rtol = rtol)
end
jps = jprod_residual!(nlss[i], x, v, tmp_m)
@test isapprox(jps, Jps[i], rtol = rtol)
@test isapprox(Jps[i], tmp_m, rtol = rtol)
@test isapprox(Jps[i], J_ops[i] * v, rtol = rtol)
@test isapprox(Jps[i], J_ops_inplace[i] * v, rtol = rtol)
rows, cols = jac_structure_residual(nlss[i])
vals = jac_coord_residual(nlss[i], x)
jprod_residual!(nlss[i], rows, cols, vals, v, tmp_m)
@test isapprox(Jps[i], tmp_m, rtol = rtol)
end
v = [-(-1.0)^i for i = 1:m]
Jtps = Any[jtprod_residual(nls, x, v) for nls in nlss]
for i = 1:N
for j = (i + 1):N
@test isapprox(Jtps[i], Jtps[j], rtol = rtol)
end
jtps = jtprod_residual!(nlss[i], x, v, tmp_n)
@test isapprox(jtps, Jtps[i], rtol = rtol)
@test isapprox(Jtps[i], tmp_n, rtol = rtol)
@test isapprox(Jtps[i], J_ops[i]' * v, rtol = rtol)
@test isapprox(Jtps[i], J_ops_inplace[i]' * v, rtol = rtol)
rows, cols = jac_structure_residual(nlss[i])
vals = jac_coord_residual(nlss[i], x)
jtprod_residual!(nlss[i], rows, cols, vals, v, tmp_n)
@test isapprox(Jtps[i], tmp_n, rtol = rtol)
end
end
if intersect([hess_residual, hprod_residual, hess_op_residual], exclude) == []
v = [-(-1.0)^i for i = 1:n]
w = [-(-1.0)^i for i = 1:m]
Hs = Any[hess_residual(nls, x, w) for nls in nlss]
Hsi = Any[sum(jth_hess_residual(nls, x, i) * w[i] for i = 1:m) for nls in nlss]
for i = 1:N
for j = (i + 1):N
@test isapprox(Hs[i], Hs[j], rtol = rtol)
end
@test isapprox(Hs[i], Hsi[i], rtol = rtol)
if !(hess_coord_residual in exclude)
V = hess_coord_residual(nlss[i], x, w)
I, J = hess_structure_residual(nlss[i])
@test length(I) == length(J) == length(V) == nlss[i].nls_meta.nnzh
@test Symmetric(sparse(I, J, V, n, n), :L) == Hs[i]
I2, J2 = copy(I), copy(J)
hess_structure_residual!(nlss[i], I2, J2)
@test I == I2
@test J == J2
tmp_V = zeros(nlss[i].nls_meta.nnzh)
hess_coord_residual!(nlss[i], x, w, tmp_V)
@test tmp_V == V
end
end
for k = 1:m
Hs = Any[jth_hess_residual(nls, x, k) for nls in nlss]
Hvs = Any[hprod_residual(nls, x, k, v) for nls in nlss]
Hops = Any[hess_op_residual(nls, x, k) for nls in nlss]
Hiv = zeros(n)
Hops_inplace = Any[hess_op_residual!(nls, x, k, Hiv) for nls in nlss]
for i = 1:N
for j = (i + 1):N
@test isapprox(Hs[i], Hs[j], rtol = rtol)
@test isapprox(Hvs[i], Hvs[j], rtol = rtol)
end
hvs = hprod_residual!(nlss[i], x, k, v, tmp_n)
@test isapprox(hvs, Hvs[i], rtol = rtol)
@test isapprox(Hvs[i], tmp_n, rtol = rtol)
@test isapprox(Hvs[i], Hops[i] * v, rtol = rtol)
@test isapprox(Hvs[i], Hops_inplace[i] * v, rtol = rtol)
end
end
end
end