-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* independent set * import function * export function * fix export * Add to objective * Objective max --------- Co-authored-by: Guillaume Dalle <[email protected]>
- Loading branch information
1 parent
f2cf29d
commit 2295e2e
Showing
5 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
""" | ||
maximum_independent_set!(model, g; var_name) | ||
Computes in-place in the JuMP model a maximum-weighted independent set of `g`. | ||
An optional `vertex_weights` vector can be passed to the graph, defaulting to uniform weights (computing a maximum size independent set). | ||
""" | ||
function maximum_weight_independent_set!( | ||
model::Model, g::AbstractGraph; binary::Bool=true, var_name, vertex_weights=ones(nv(g)) | ||
) | ||
if is_directed(g) | ||
throw(ArgumentError("The graph must not be directed")) | ||
end | ||
g_vertices = collect(vertices(g)) | ||
f = @variable(model, [g_vertices]; binary=binary, base_name=String(var_name)) | ||
model[Symbol(var_name)] = f | ||
@constraint( | ||
model, | ||
covering_constraint[i=1:nv(g), j=1:nv(g); i ≠ j && has_edge(g, i, j)], | ||
f[i] + f[j] <= 1, | ||
) | ||
obj = objective_function(model) | ||
add_to_expression!(obj, dot(f, vertex_weights)) | ||
@objective(model, Max, obj) | ||
return model | ||
end | ||
|
||
""" | ||
maximum_weight_independent_set(g; optimizer, binary, vertex_weights) | ||
Computes a maximum-weighted independent set or stable set of `g`. | ||
""" | ||
function maximum_weight_independent_set( | ||
g::AbstractGraph; | ||
binary::Bool=true, | ||
vertex_weights=ones(nv(g)), | ||
optimizer=HiGHS.Optimizer, | ||
) | ||
model = Model(optimizer) | ||
set_silent(model) | ||
maximum_weight_independent_set!( | ||
model, g; binary=binary, vertex_weights=vertex_weights, var_name=:stable | ||
) | ||
optimize!(model) | ||
@assert termination_status(model) == OPTIMAL | ||
stable_variables = Vector(model[:stable]) | ||
stable_vertices = findall(v -> value(v) > 0.5, stable_variables) | ||
return stable_vertices | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using GraphsOptim | ||
using Graphs | ||
using Test | ||
|
||
g = Graphs.random_regular_graph(10, 5) | ||
|
||
for _ in 1:10 | ||
vertex_weights = rand(nv(g)) | ||
stable = GraphsOptim.maximum_weight_independent_set(g; vertex_weights=vertex_weights) | ||
if length(stable) > 1 | ||
for idx in 1:(length(stable) - 1) | ||
@test !Graphs.has_edge(g, stable[idx], stable[idx + 1]) | ||
end | ||
end | ||
end | ||
|
||
g2 = complete_graph(3) | ||
add_vertex!(g2) | ||
add_edge!(g2, 3, 4) | ||
stable = GraphsOptim.maximum_weight_independent_set(g2) | ||
@test length(stable) == 2 | ||
@test 4 in stable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters