Skip to content

Commit 16dba83

Browse files
authored
it seems Cbc was at fault
1 parent 3d26c67 commit 16dba83

File tree

4 files changed

+266
-17
lines changed

4 files changed

+266
-17
lines changed

Project.toml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,3 @@ JuMP = "1"
2626
LEMONGraphs = "0.1.0"
2727
MathOptInterface = "1"
2828
julia = "1.10"
29-
30-
[extras]
31-
Cbc = "9961bab8-2fa3-5c5a-9d89-47fab24efd76"
32-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
33-
34-
[targets]
35-
test = ["Test", "Cbc"]

test/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[deps]
2-
Cbc = "9961bab8-2fa3-5c5a-9d89-47fab24efd76"
32
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
3+
HiGHS = "87dc4568-4c63-4d18-b0c0-bb2238e4078b"
44
Hungarian = "e91730f6-4275-51fb-a7a0-7064cfbd3b39"
55
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

test/REQUIRE

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/runtests.jl

Lines changed: 265 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,141 @@
11
using Graphs
22
using GraphsMatching
33
using Test
4-
using Cbc
4+
using HiGHS
55
using JuMP
66
using LinearAlgebra: I
77

8-
@static if !Sys.iswindows() && Sys.ARCH == :x86_64
8+
if !Sys.iswindows() && Sys.ARCH == :x86_64
99
using Pkg
1010
Pkg.add("BlossomV")
1111
import BlossomV # to test the extension
1212
end
1313

1414
@testset "GraphsMatching" begin
1515

16+
@testset "maximum_weight_matching_reduction" begin
17+
N = Int(1e2)
18+
g = complete_graph(N)
19+
for i = 1:10
20+
w = randn(N, N)
21+
w = transpose(w) * w
22+
match_sol = maximum_weight_matching(
23+
g,
24+
HiGHS,
25+
w,
26+
)
27+
match_red = maximum_weight_matching_reduction(g, w)
28+
sol = Edge[]
29+
for i = 1:N
30+
if (match_sol.mate[i] > 0)
31+
push!(sol, Edge(i, match_sol.mate[i]))
32+
end
33+
end
34+
@test sol == match_red
35+
end
36+
end
37+
38+
@testset "maximum_weight_matching" begin
39+
g = complete_graph(3)
40+
w = [
41+
1 2 1
42+
1 1 1
43+
3 1 1
44+
]
45+
match = maximum_weight_matching(
46+
g,
47+
HiGHS,
48+
w,
49+
)
50+
@test match.mate[1] == 3
51+
@test match.weight 3
52+
53+
g = complete_graph(3)
54+
w = zeros(3, 3)
55+
w[1, 2] = 1
56+
w[3, 2] = 1
57+
w[1, 3] = 1
58+
match = maximum_weight_matching(
59+
g,
60+
HiGHS,
61+
w,
62+
)
63+
@test match.weight 1
64+
65+
66+
g = Graph(4)
67+
add_edge!(g, 1, 3)
68+
add_edge!(g, 1, 4)
69+
add_edge!(g, 2, 4)
70+
71+
w = zeros(4, 4)
72+
w[1, 3] = 1
73+
w[1, 4] = 3
74+
w[2, 4] = 1
1675

76+
match = maximum_weight_matching(
77+
g,
78+
HiGHS,
79+
w,
80+
)
81+
@test match.weight 3
82+
@test match.mate[1] == 4
83+
@test match.mate[2] == -1
84+
@test match.mate[3] == -1
85+
@test match.mate[4] == 1
86+
87+
g = Graph(4)
88+
add_edge!(g, 1, 2)
89+
add_edge!(g, 2, 3)
90+
add_edge!(g, 3, 1)
91+
add_edge!(g, 3, 4)
92+
match = maximum_weight_matching(
93+
g,
94+
HiGHS,
95+
)
96+
@test match.weight 2
97+
@test match.mate[1] == 2
98+
@test match.mate[2] == 1
99+
@test match.mate[3] == 4
100+
@test match.mate[4] == 3
17101

18-
@testset "maximum_weight_maximal_matching" begin
102+
w = zeros(4, 4)
103+
w[1, 2] = 1
104+
w[2, 3] = 1
105+
w[1, 3] = 1
106+
w[3, 4] = 1
107+
108+
match = maximum_weight_matching(
109+
g,
110+
HiGHS,
111+
w,
112+
)
113+
@test match.weight 2
114+
@test match.mate[1] == 2
115+
@test match.mate[2] == 1
116+
@test match.mate[3] == 4
117+
@test match.mate[4] == 3
118+
119+
w = zeros(4, 4)
120+
w[1, 2] = 1
121+
w[2, 3] = 1
122+
w[1, 3] = 5
123+
w[3, 4] = 1
124+
125+
match = maximum_weight_matching(
126+
g,
127+
HiGHS,
128+
w,
129+
)
130+
@test match.weight 5
131+
@test match.mate[1] == 3
132+
@test match.mate[2] == -1
133+
@test match.mate[3] == 1
134+
@test match.mate[4] == -1
135+
end
136+
137+
138+
@testset "maximum_weight_maximal_matching" begin
19139

20140
g = complete_bipartite_graph(2, 2)
21141
w = zeros(4, 4)
@@ -27,7 +147,7 @@ end
27147
g,
28148
w,
29149
algorithm = LPAlgorithm(),
30-
optimizer = optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0),
150+
optimizer = HiGHS,
31151
)
32152
@test match.weight 21
33153
@test match.mate[1] == 3
@@ -45,7 +165,7 @@ end
45165
g,
46166
w,
47167
algorithm = LPAlgorithm(),
48-
optimizer = optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0),
168+
optimizer = HiGHS,
49169
)
50170
@test match.weight 11.5
51171
@test match.mate[1] == 4
@@ -65,7 +185,7 @@ end
65185
g,
66186
w,
67187
algorithm = LPAlgorithm(),
68-
optimizer = optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0),
188+
optimizer = HiGHS,
69189
cutoff = 0,
70190
)
71191
@test match.weight 11.5
@@ -86,7 +206,7 @@ end
86206
g,
87207
w,
88208
algorithm = LPAlgorithm(),
89-
optimizer = optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0),
209+
optimizer = HiGHS,
90210
cutoff = 0,
91211
)
92212
@test match.weight 12
@@ -96,7 +216,144 @@ end
96216
@test match.mate[4] == -1
97217
@test match.mate[5] == 2
98218
@test match.mate[6] == 1
99-
219+
220+
221+
g = complete_bipartite_graph(2, 2)
222+
w = zeros(4, 4)
223+
w[1, 3] = 10.0
224+
w[1, 4] = 1.0
225+
w[2, 3] = 2.0
226+
w[2, 4] = 11.0
227+
match = maximum_weight_maximal_matching(g, w, algorithm = HungarianAlgorithm())
228+
@test match.weight 21
229+
@test match.mate[1] == 3
230+
@test match.mate[3] == 1
231+
@test match.mate[2] == 4
232+
@test match.mate[4] == 2
233+
234+
g = complete_graph(3)
235+
w = zeros(3, 3)
236+
@test !is_bipartite(g)
237+
@test_throws ErrorException maximum_weight_maximal_matching(
238+
g,
239+
w,
240+
algorithm = HungarianAlgorithm(),
241+
)
242+
243+
g = complete_bipartite_graph(2, 4)
244+
w = zeros(6, 6)
245+
w[1, 3] = 10
246+
w[1, 4] = 0.5
247+
w[2, 3] = 11
248+
w[2, 4] = 1
249+
match = maximum_weight_maximal_matching(g, w, algorithm = HungarianAlgorithm())
250+
@test match.weight 11.5
251+
252+
g = Graph(4)
253+
add_edge!(g, 1, 3)
254+
add_edge!(g, 1, 4)
255+
add_edge!(g, 2, 4)
256+
w = zeros(4, 4)
257+
w[1, 3] = 1
258+
w[1, 4] = 3
259+
w[2, 4] = 1
260+
match = maximum_weight_maximal_matching(g, w, algorithm = HungarianAlgorithm())
261+
@test match.weight 2
262+
100263
end
101264

265+
266+
267+
@testset "minimum_weight_perfect_matching" begin
268+
algos = Any[LEMONMWPMAlgorithm()]
269+
if !Sys.iswindows() && Sys.ARCH == :x86_64
270+
push!(algos, BlossomVAlgorithm())
271+
end
272+
for algorithm in algos
273+
274+
w = Dict(Edge(1, 2) => 500)
275+
g = Graph(2)
276+
add_edge!(g, 1, 2)
277+
match = minimum_weight_perfect_matching(g, w, algorithm)
278+
@test match.mate[1] == 2
279+
280+
281+
w = Dict(
282+
Edge(1, 2) => 500,
283+
Edge(1, 3) => 600,
284+
Edge(2, 3) => 700,
285+
Edge(3, 4) => 100,
286+
Edge(2, 4) => 1000,
287+
)
288+
289+
g = complete_graph(4)
290+
match = minimum_weight_perfect_matching(g, w, algorithm)
291+
@test match.mate[1] == 2
292+
@test match.mate[2] == 1
293+
@test match.mate[3] == 4
294+
@test match.mate[4] == 3
295+
@test match.weight 600
296+
297+
w = Dict(
298+
Edge(1, 2) => 500,
299+
Edge(1, 3) => 400,
300+
Edge(2, 3) => 300,
301+
Edge(3, 4) => 1000,
302+
Edge(2, 4) => 1000,
303+
)
304+
g = complete_graph(4)
305+
match = minimum_weight_perfect_matching(g, w, algorithm)
306+
@test match.mate[1] == 3
307+
@test match.mate[2] == 4
308+
@test match.mate[3] == 1
309+
@test match.mate[4] == 2
310+
@test match.weight 1400
311+
312+
g = complete_bipartite_graph(2, 2)
313+
w = Dict{Edge,Float64}()
314+
w[Edge(1, 3)] = -10
315+
w[Edge(1, 4)] = -0.5
316+
w[Edge(2, 3)] = -11
317+
w[Edge(2, 4)] = -1
318+
319+
match = minimum_weight_perfect_matching(g, w, algorithm)
320+
@test match.mate[1] == 4
321+
@test match.mate[4] == 1
322+
@test match.mate[2] == 3
323+
@test match.mate[3] == 2
324+
@test match.weight -11.5
325+
326+
327+
g = complete_graph(4)
328+
w = Dict{Edge,Float64}()
329+
w[Edge(1, 3)] = 10
330+
w[Edge(1, 4)] = 0.5
331+
w[Edge(2, 3)] = 11
332+
w[Edge(2, 4)] = 2
333+
w[Edge(1, 2)] = 100
334+
335+
match = minimum_weight_perfect_matching(g, w, 50, algorithm)
336+
@test match.mate[1] == 4
337+
@test match.mate[4] == 1
338+
@test match.mate[2] == 3
339+
@test match.mate[3] == 2
340+
@test match.weight 11.5
341+
342+
# Issue #5
343+
g = Graph(Graph([Edge(1, 2), Edge(2, 3), Edge(4, 1)]))
344+
w = Dict(Edge(1, 2) => 2.0, Edge(2, 3) => 2.0, Edge(1, 4) => 2.0)
345+
match = minimum_weight_perfect_matching(g, w)
346+
@test match.mate == [4, 3, 2, 1]
347+
@test match.weight == 4.0
348+
349+
g = Graph([Edge(1, 2)])
350+
wFloat = Dict(Edge(1, 2) => 2.0)
351+
wInt = Dict(Edge(1, 2) => 2)
352+
matchFloat = minimum_weight_perfect_matching(g, wFloat, algorithm)
353+
matchInt = minimum_weight_perfect_matching(g, wInt, algorithm)
354+
@test matchFloat.mate == matchInt.mate
355+
@test matchFloat.weight == matchInt.weight
356+
357+
end
358+
end
102359
end

0 commit comments

Comments
 (0)