|
1 | 1 | using Graphs
|
2 | 2 | using GraphsMatching
|
3 | 3 | using Test
|
4 |
| -using Cbc |
| 4 | +using HiGHS |
5 | 5 | using JuMP
|
6 | 6 | using LinearAlgebra: I
|
7 | 7 |
|
8 |
| -@static if !Sys.iswindows() && Sys.ARCH == :x86_64 |
| 8 | +if !Sys.iswindows() && Sys.ARCH == :x86_64 |
9 | 9 | using Pkg
|
10 | 10 | Pkg.add("BlossomV")
|
11 | 11 | import BlossomV # to test the extension
|
12 | 12 | end
|
13 | 13 |
|
14 | 14 | @testset "GraphsMatching" begin
|
15 | 15 |
|
| 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 |
16 | 75 |
|
| 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 |
17 | 101 |
|
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 |
19 | 139 |
|
20 | 140 | g = complete_bipartite_graph(2, 2)
|
21 | 141 | w = zeros(4, 4)
|
|
27 | 147 | g,
|
28 | 148 | w,
|
29 | 149 | algorithm = LPAlgorithm(),
|
30 |
| - optimizer = optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0), |
| 150 | + optimizer = HiGHS, |
31 | 151 | )
|
32 | 152 | @test match.weight ≈ 21
|
33 | 153 | @test match.mate[1] == 3
|
|
45 | 165 | g,
|
46 | 166 | w,
|
47 | 167 | algorithm = LPAlgorithm(),
|
48 |
| - optimizer = optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0), |
| 168 | + optimizer = HiGHS, |
49 | 169 | )
|
50 | 170 | @test match.weight ≈ 11.5
|
51 | 171 | @test match.mate[1] == 4
|
|
65 | 185 | g,
|
66 | 186 | w,
|
67 | 187 | algorithm = LPAlgorithm(),
|
68 |
| - optimizer = optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0), |
| 188 | + optimizer = HiGHS, |
69 | 189 | cutoff = 0,
|
70 | 190 | )
|
71 | 191 | @test match.weight ≈ 11.5
|
|
86 | 206 | g,
|
87 | 207 | w,
|
88 | 208 | algorithm = LPAlgorithm(),
|
89 |
| - optimizer = optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0), |
| 209 | + optimizer = HiGHS, |
90 | 210 | cutoff = 0,
|
91 | 211 | )
|
92 | 212 | @test match.weight ≈ 12
|
|
96 | 216 | @test match.mate[4] == -1
|
97 | 217 | @test match.mate[5] == 2
|
98 | 218 | @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 | + |
100 | 263 | end
|
101 | 264 |
|
| 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 |
102 | 359 | end
|
0 commit comments