Skip to content

Commit

Permalink
bump everything (#2)
Browse files Browse the repository at this point in the history
* bump everything

* switch "solver" to "optimizer"
  • Loading branch information
etiennedeg authored May 30, 2022
1 parent c6d930f commit 7d14c0b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 52 deletions.
10 changes: 5 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GraphsMatching"
uuid = "c3af3a8c-b79e-4b01-bf44-c718d7e0e0d6"
authors = ["JuliaGraphs"]
version = "0.1.0"
version = "0.2.0"

[deps]
BlossomV = "6c721016-9dae-5d90-abf6-67daaccb2332"
Expand All @@ -14,10 +14,10 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
BlossomV = "0.4"
Graphs = "1.4.0"
Hungarian = "0.4"
JuMP = "0.20"
MathOptInterface = "0.9"
Graphs = "1.4, 1.7"
Hungarian = "0.4, 0.6"
JuMP = "1"
MathOptInterface = "1"
julia = "1"

[extras]
Expand Down
8 changes: 4 additions & 4 deletions src/lp.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function maximum_weight_maximal_matching_lp(g::Graph, solver::JuMP.OptimizerFactory, w::AbstractMatrix{T}, cutoff::R) where {T<:Real, R<:Real}
return maximum_weight_maximal_matching_lp(g, solver, cutoff_weights(w, cutoff))
function maximum_weight_maximal_matching_lp(g::Graph, optimizer, w::AbstractMatrix{T}, cutoff::R) where {T<:Real, R<:Real}
return maximum_weight_maximal_matching_lp(g, optimizer, cutoff_weights(w, cutoff))
end

function maximum_weight_maximal_matching_lp(g::Graph, solver::JuMP.OptimizerFactory, w::AbstractMatrix{T}) where {T<:Real}
function maximum_weight_maximal_matching_lp(g::Graph, optimizer, w::AbstractMatrix{T}) where {T<:Real}
# TODO support for graphs with zero degree nodes
# TODO apply separately on each connected component
bpmap = bipartite_map(g)
Expand All @@ -26,7 +26,7 @@ function maximum_weight_maximal_matching_lp(g::Graph, solver::JuMP.OptimizerFact
end
end

model = Model(solver)
model = Model(optimizer)
@variable(model, x[1:length(w)] >= 0)

for i in v1
Expand Down
6 changes: 3 additions & 3 deletions src/maximum_weight_matching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ Returns MatchingResult containing:
function maximum_weight_matching end

function maximum_weight_matching(g::Graph,
solver::JuMP.OptimizerFactory,
optimizer,
w::AbstractMatrix{U} = default_weights(g)) where {U <:Real}

model = Model(with_optimizer(solver))
model = Model(optimizer)
n = nv(g)
edge_list = collect(edges(g))

Expand All @@ -40,7 +40,7 @@ function maximum_weight_matching(g::Graph,
end
end
end

if is_bipartite(g)
@variable(model, x[edge_list] >= 0) # no need to enforce integrality
else
Expand Down
49 changes: 23 additions & 26 deletions src/maximum_weight_maximal_matching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ Forces the maximum_weight_maximal_matching function to use a linear programming
struct LPAlgorithm <: AbstractMaximumWeightMaximalMatchingAlgorithm end

function maximum_weight_maximal_matching(
g::Graph,
w::AbstractMatrix{T},
algorithm::LPAlgorithm,
solver = nothing
g::Graph,
w::AbstractMatrix{T},
algorithm::LPAlgorithm,
optimizer = nothing
) where {T<:Real}
if ! isa(solver, JuMP.OptimizerFactory)
error("The keyword argument solver must be an JuMP.OptimizerFactory, as accepted by JuMP.")
end

return maximum_weight_maximal_matching_lp(g, solver, w)
return maximum_weight_maximal_matching_lp(g, optimizer, w)
end

"""
Expand All @@ -30,10 +27,10 @@ Forces the maximum_weight_maximal_matching function to use the Hungarian algorit
struct HungarianAlgorithm <: AbstractMaximumWeightMaximalMatchingAlgorithm end

function maximum_weight_maximal_matching(
g::Graph,
w::AbstractMatrix{T},
algorithm::HungarianAlgorithm,
solver = nothing
g::Graph,
w::AbstractMatrix{T},
algorithm::HungarianAlgorithm,
optimizer = nothing
) where {T<:Real}
return maximum_weight_maximal_matching_hungarian(g, w)
end
Expand All @@ -50,34 +47,34 @@ Edges in `g` not present in `w` will not be considered for the matching.
A `cutoff` keyword argument can be given, to reduce computational times
excluding edges with weights lower than the cutoff.
Finally, a specific algorithm can be chosen (`algorithm` keyword argument);
each algorithm has specific dependencies. For instance:
Finally, a specific algorithm can be chosen (`algorithm` keyword argument);
each algorithm has specific dependencies. For instance:
- If `algorithm=HungarianAlgorithm()` (the default), the package Hungarian.jl is used.
This algorithm is always polynomial in time, with complexity O(n³).
- If `algorithm=LPAlgorithm()`, the package JuMP.jl and one of its supported solvers is required.
- If `algorithm=HungarianAlgorithm()` (the default), the package Hungarian.jl is used.
This algorithm is always polynomial in time, with complexity O(n³).
- If `algorithm=LPAlgorithm()`, the package JuMP.jl and one of its supported solvers is required.
In this case, the algorithm relies on a linear relaxation on of the matching problem, which is
guaranteed to have integer solution on bipartite graphs. A solver must be provided with
the `solver` keyword parameter.
guaranteed to have integer solution on bipartite graphs. A solver must be provided with
the `optimizer` keyword parameter.
The returned object is of type `MatchingResult`.
"""
function maximum_weight_maximal_matching(
g::Graph,
w::AbstractMatrix{T};
g::Graph,
w::AbstractMatrix{T};
cutoff = nothing,
algorithm::AbstractMaximumWeightMaximalMatchingAlgorithm = HungarianAlgorithm(),
solver = nothing
algorithm::AbstractMaximumWeightMaximalMatchingAlgorithm = HungarianAlgorithm(),
optimizer = nothing
) where {T<:Real}

if cutoff != nothing && ! isa(cutoff, Real)
error("The cutoff value must be of type Real or nothing.")
end

if cutoff != nothing
return maximum_weight_maximal_matching(g, cutoff_weights(w, cutoff), algorithm, solver)
return maximum_weight_maximal_matching(g, cutoff_weights(w, cutoff), algorithm, optimizer)
else
return maximum_weight_maximal_matching(g, w, algorithm, solver)
return maximum_weight_maximal_matching(g, w, algorithm, optimizer)
end
end

Expand All @@ -96,4 +93,4 @@ function cutoff_weights(w::AbstractMatrix{T}, cutoff::R) where {T<:Real, R<:Real
wnew
end

@deprecate maximum_weight_maximal_matching(g::Graph, solver::JuMP.OptimizerFactory, w::AbstractMatrix{T}, cutoff::R) where {T<:Real, R<:Real} maximum_weight_maximal_matching(g, w, algorithm=LPAlgorithm(), cutoff=cutoff, solver=solver)
@deprecate maximum_weight_maximal_matching(g::Graph, optimizer, w::AbstractMatrix{T}, cutoff::R) where {T<:Real, R<:Real} maximum_weight_maximal_matching(g, w, algorithm=LPAlgorithm(), cutoff=cutoff, optimizer=optimizer)
28 changes: 14 additions & 14 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using LinearAlgebra: I
1 1 1
3 1 1
]
match = maximum_weight_matching(g, with_optimizer(Cbc.Optimizer, logLevel=0), w)
match = maximum_weight_matching(g, optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0), w)
@test match.mate[1] == 3
@test match.weight 3

Expand All @@ -23,7 +23,7 @@ using LinearAlgebra: I
w[1,2] = 1
w[3,2] = 1
w[1,3] = 1
match = maximum_weight_matching(g,with_optimizer(Cbc.Optimizer, logLevel=0),w)
match = maximum_weight_matching(g, optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0),w)
@test match.weight 1


Expand All @@ -37,7 +37,7 @@ using LinearAlgebra: I
w[1,4] = 3
w[2,4] = 1

match = maximum_weight_matching(g,with_optimizer(Cbc.Optimizer, logLevel=0),w)
match = maximum_weight_matching(g, optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0),w)
@test match.weight 3
@test match.mate[1] == 4
@test match.mate[2] == -1
Expand All @@ -49,7 +49,7 @@ using LinearAlgebra: I
add_edge!(g, 2,3)
add_edge!(g, 3,1)
add_edge!(g, 3,4)
match = maximum_weight_matching(g,with_optimizer(Cbc.Optimizer, logLevel=0))
match = maximum_weight_matching(g, optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0))
@test match.weight 2
@test match.mate[1] == 2
@test match.mate[2] == 1
Expand All @@ -62,7 +62,7 @@ using LinearAlgebra: I
w[1,3] = 1
w[3,4] = 1

match = maximum_weight_matching(g,with_optimizer(Cbc.Optimizer, logLevel=0), w)
match = maximum_weight_matching(g, optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0), w)
@test match.weight 2
@test match.mate[1] == 2
@test match.mate[2] == 1
Expand All @@ -75,7 +75,7 @@ using LinearAlgebra: I
w[1,3] = 5
w[3,4] = 1

match = maximum_weight_matching(g,with_optimizer(Cbc.Optimizer, logLevel=0),w)
match = maximum_weight_matching(g, optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0),w)
@test match.weight 5
@test match.mate[1] == 3
@test match.mate[2] == -1
Expand All @@ -92,7 +92,7 @@ end
w[1,4] = 1.
w[2,3] = 2.
w[2,4] = 11.
match = maximum_weight_maximal_matching(g, w, algorithm=LPAlgorithm(), solver=with_optimizer(Cbc.Optimizer, logLevel=0))
match = maximum_weight_maximal_matching(g, w, algorithm=LPAlgorithm(), optimizer=optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0))
@test match.weight 21
@test match.mate[1] == 3
@test match.mate[3] == 1
Expand All @@ -105,7 +105,7 @@ end
w[1,4] = 0.5
w[2,3] = 11
w[2,4] = 1
match = maximum_weight_maximal_matching(g, w, algorithm=LPAlgorithm(), solver=with_optimizer(Cbc.Optimizer, logLevel=0))
match = maximum_weight_maximal_matching(g, w, algorithm=LPAlgorithm(), optimizer=optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0))
@test match.weight 11.5
@test match.mate[1] == 4
@test match.mate[4] == 1
Expand All @@ -120,7 +120,7 @@ end
w[2,4] = 1
w[2,5] = -1
w[2,6] = -1
match = maximum_weight_maximal_matching(g, w, algorithm=LPAlgorithm(), solver=with_optimizer(Cbc.Optimizer, logLevel=0), cutoff=0)
match = maximum_weight_maximal_matching(g, w, algorithm=LPAlgorithm(), optimizer=optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0), cutoff=0)
@test match.weight 11.5
@test match.mate[1] == 4
@test match.mate[4] == 1
Expand All @@ -135,16 +135,16 @@ end
w[1,6] = 1
w[1,5] = -1

match = maximum_weight_maximal_matching(g, w, algorithm=LPAlgorithm(), solver=with_optimizer(Cbc.Optimizer, logLevel=0), cutoff=0)
match = maximum_weight_maximal_matching(g, w, algorithm=LPAlgorithm(), optimizer=optimizer_with_attributes(Cbc.Optimizer, "LogLevel" => 0), cutoff=0)
@test match.weight 12
@test match.mate[1] == 6
@test match.mate[2] == 5
@test match.mate[3] == -1
@test match.mate[4] == -1
@test match.mate[5] == 2
@test match.mate[6] == 1


g = complete_bipartite_graph(2, 2)
w = zeros(4, 4)
w[1, 3] = 10.
Expand Down Expand Up @@ -178,10 +178,10 @@ end
add_edge!(g, 2, 4)
w = zeros(4, 4)
w[1, 3] = 1
w[1, 4] = 3
w[1, 4] = 3
w[2, 4] = 1
match = maximum_weight_maximal_matching(g, w, algorithm=HungarianAlgorithm())
@test match.weight 2
@test match.weight 2

end

Expand Down

2 comments on commit 7d14c0b

@etiennedeg
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/61319

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" 7d14c0b345eb701f86ff20236e26a8a050994932
git push origin v0.2.0

Please sign in to comment.