-
Notifications
You must be signed in to change notification settings - Fork 24
/
ceg_test.jl
209 lines (194 loc) · 6.62 KB
/
ceg_test.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
using Test
using SumOfSquares
const CEG = SumOfSquares.Certificate.Sparsity.ChordalExtensionGraph
function fill_in_3_nodes_test(G, x, y, z)
@test CEG.fill_in(G, x) == 0
@test CEG.fill_in(G, y) == 0
@test CEG.fill_in(G, z) == 0
CEG.add_edge!(G, x, y)
@test CEG.fill_in(G, x) == 0
@test CEG.fill_in(G, y) == 0
@test CEG.fill_in(G, z) == 0
CEG.add_edge!(G, y, z)
@test CEG.fill_in(G, x) == 0
@test CEG.fill_in(G, y) == 1
@test CEG.fill_in(G, z) == 0
@testset "Disable y" begin
H = copy(G)
CEG.disable_node!(H, y)
@test CEG.fill_in(H, x) == 0
@test CEG.fill_in(H, z) == 0
CEG.add_edge!(H, z, x)
@test CEG.fill_in(H, x) == 0
@test CEG.fill_in(H, z) == 0
end
CEG.add_edge!(G, z, x)
@test CEG.fill_in(G, x) == 0
@test CEG.fill_in(G, y) == 0
@test CEG.fill_in(G, z) == 0
end
function fill_in_4_nodes_test(G, w, x, y, z)
@test CEG.fill_in(G, w) == 0
@test CEG.fill_in(G, x) == 0
@test CEG.fill_in(G, y) == 0
@test CEG.fill_in(G, z) == 0
CEG.add_edge!(G, w, x)
@test CEG.fill_in(G, w) == 0
@test CEG.fill_in(G, x) == 0
@test CEG.fill_in(G, y) == 0
@test CEG.fill_in(G, z) == 0
CEG.add_edge!(G, y, z)
@test CEG.fill_in(G, w) == 0
@test CEG.fill_in(G, x) == 0
@test CEG.fill_in(G, y) == 0
@test CEG.fill_in(G, z) == 0
CEG.add_edge!(G, w, y)
@test CEG.fill_in(G, w) == 1
@test CEG.fill_in(G, x) == 0
@test CEG.fill_in(G, y) == 1
@test CEG.fill_in(G, z) == 0
@testset "wz first" begin
H = copy(G)
CEG.add_edge!(H, w, z)
@test CEG.fill_in(H, w) == 2
@test CEG.fill_in(H, x) == 0
@test CEG.fill_in(H, y) == 0
@test CEG.fill_in(H, z) == 0
@testset "Disable w" begin
I = copy(H)
CEG.add_edge!(I, x, z)
@test CEG.fill_in(I, x) == 0
@test CEG.fill_in(I, y) == 0
@test CEG.fill_in(I, z) == 1
CEG.add_edge!(I, x, y)
@test CEG.fill_in(I, x) == 0
@test CEG.fill_in(I, y) == 0
@test CEG.fill_in(I, z) == 0
end
CEG.add_edge!(H, x, z)
@test CEG.fill_in(H, w) == 1
@test CEG.fill_in(H, x) == 0
@test CEG.fill_in(H, y) == 0
@test CEG.fill_in(H, z) == 1
CEG.add_edge!(H, x, y)
@test CEG.fill_in(H, w) == 0
@test CEG.fill_in(H, x) == 0
@test CEG.fill_in(H, y) == 0
@test CEG.fill_in(H, z) == 0
end
CEG.add_edge!(G, x, z)
@test CEG.fill_in(G, w) == 1
@test CEG.fill_in(G, x) == 1
@test CEG.fill_in(G, y) == 1
@test CEG.fill_in(G, z) == 1
@testset "xy then wz" begin
H = copy(G)
CEG.add_edge!(H, x, y)
@test CEG.fill_in(H, w) == 0
@test CEG.fill_in(H, x) == 1
@test CEG.fill_in(H, y) == 1
@test CEG.fill_in(H, z) == 0
CEG.add_edge!(H, w, z)
@test CEG.fill_in(H, w) == 0
@test CEG.fill_in(H, x) == 0
@test CEG.fill_in(H, y) == 0
@test CEG.fill_in(H, z) == 0
end
CEG.add_edge!(G, w, z)
@test CEG.fill_in(G, w) == 1
@test CEG.fill_in(G, x) == 0
@test CEG.fill_in(G, y) == 0
@test CEG.fill_in(G, z) == 1
@testset "Disable w" begin
H = copy(G)
@test CEG.fill_in(H, x) == 0
@test CEG.fill_in(H, y) == 0
@test CEG.fill_in(H, z) == 1
CEG.add_edge!(H, x, y)
@test CEG.fill_in(H, x) == 0
@test CEG.fill_in(H, y) == 0
@test CEG.fill_in(H, z) == 0
end
CEG.add_edge!(G, x, y)
@test CEG.fill_in(G, w) == 0
@test CEG.fill_in(G, x) == 0
@test CEG.fill_in(G, y) == 0
@test CEG.fill_in(G, z) == 0
end
@testset "Chordal Extensions" begin
@testset "CEG.LabelledGraph" begin
@test CEG.LabelledGraph{Int}() isa CEG.LabelledGraph{Int}
@test_throws ErrorException CEG.LabelledGraph()
G = CEG.LabelledGraph{Int}()
@test CEG.add_node!(G, 1) == 1
@test CEG.add_edge!(G, (1, 2)) == (1, 2)
@test CEG.add_edge!.(G, [(1, 2), (2, 3)]) isa Vector{Tuple{Int, Int}}
@test CEG.add_clique!(G, [1, 2, 3]) == nothing
end
@testset "show" begin
G = CEG.LabelledGraph{Symbol}()
@test sprint(show, G) == "Symbol-LabelledGraph with\nNodes:\nEdges:\n"
CEG.add_edge!(G, :x, :y)
@test sprint(show, G) isa String
end
@testset "sub_sets" begin
G = CEG.LabelledGraph{Symbol}()
CEG.add_clique!(G, [:x, :y, :z])
@test CEG.neighbors(G.graph, 1) == Set([2, 3])
end
@testset "fill_in with 3 nodes" begin
G = CEG.Graph()
x = CEG.add_node!(G)
y = CEG.add_node!(G)
z = CEG.add_node!(G)
fill_in_3_nodes_test(copy(G), x, y, z)
fill_in_3_nodes_test(CEG.FillInCache(copy(G)), x, y, z)
end
@testset "fill_in with 4 nodes" begin
G = CEG.Graph()
w = CEG.add_node!(G)
x = CEG.add_node!(G)
y = CEG.add_node!(G)
z = CEG.add_node!(G)
fill_in_4_nodes_test(copy(G), w, x, y, z)
fill_in_4_nodes_test(CEG.FillInCache(copy(G)), w, x, y, z)
end
@testset "cliques" begin
G = CEG.LabelledGraph{Symbol}()
CEG.add_edge!(G, :x, :y)
CEG.add_edge!(G, :y, :z)
@test CEG.fill_in.(G.graph, [1, 2, 3]) == [0, 1, 0]
@test !CEG.is_clique(G.graph, [1, 2, 3])
@test CEG.is_clique(G.graph, [1, 2])
end
@testset "chordal" begin
G = CEG.LabelledGraph{Symbol}()
CEG.add_edge!.(G, [(:x, :y), (:y, :z)])
H, cliques = CEG.chordal_extension(G, CEG.GreedyFillIn())
@test H.int2n == G.int2n
@test H.graph.neighbors == G.graph.neighbors
@test cliques == [[:y, :z], [:x, :y]]
G = CEG.LabelledGraph{Int}()
CEG.add_edge!.(G, [(1, 2), (1, 3), (3, 4), (2, 4)])
H, cliques = CEG.chordal_extension(G, CEG.GreedyFillIn())
@test length.(cliques) == [3, 3]
end
@testset "Add edges between two `chordal_extension` calls" begin
G = CEG.LabelledGraph{Int}()
CEG.add_node!.(G, [1, 2, 3, 4, 5])
CEG.add_edge!(G, 1, 2)
CEG.add_edge!(G, 2, 3)
CEG.add_edge!(G, 3, 4)
CEG.add_edge!(G, 1, 4)
H, cliques = CEG.chordal_extension(G, CEG.GreedyFillIn())
@test cliques == [[2, 3, 4], [1, 2, 4], [5]]
@test isempty(G.graph.disabled)
@test isempty(H.graph.disabled)
CEG.add_edge!(H, 1, 5)
I, cliques = CEG.chordal_extension(H, CEG.GreedyFillIn())
@test cliques == [[1, 2, 4], [2, 3, 4], [1, 5]]
@test isempty(G.graph.disabled)
@test isempty(H.graph.disabled)
@test isempty(I.graph.disabled)
end
end