-
Notifications
You must be signed in to change notification settings - Fork 6
Optimal coloring algorithm with JuMP formulation #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
dadb75e
Make any coloring algorithm compatible with SMC
gdalle 4d01552
Fix StackOverflow
gdalle 48c483a
Run tests on 1.11
gdalle 6cdcbf8
Start working on optimal coloring
gdalle b344414
Fix tests
gdalle 0e5c18b
Improve feasibility check
gdalle e49b468
Format
gdalle df35321
Fix
gdalle 12a246f
Fix
gdalle a6b1ee8
Typo
gdalle 4ca6507
Use HiGHS
gdalle 223e99f
Remove OptimalColoringAlgorithm
gdalle c9d3ebc
Remove test deps
gdalle 6ce99ff
Better comments
gdalle f6539fb
Merge branch 'gd/abstractcoloring' into gd/optimal
gdalle 0185130
Merge branch 'main' into gd/optimal
gdalle 34f59ef
Minimize diff
gdalle e688728
Fix
gdalle 23e518d
Fix
gdalle eb383cd
Update src/optimal.jl
gdalle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,80 @@ | ||
| module SparseMatrixColoringsJuMPExt | ||
|
|
||
| using ADTypes: ADTypes | ||
| using JuMP: | ||
| Model, | ||
| is_solved_and_feasible, | ||
| optimize!, | ||
| primal_status, | ||
| set_silent, | ||
| set_start_value, | ||
| value, | ||
| @variable, | ||
| @constraint, | ||
| @objective | ||
| using JuMP | ||
| import MathOptInterface as MOI | ||
| using SparseMatrixColorings: | ||
| BipartiteGraph, OptimalColoringAlgorithm, nb_vertices, neighbors, pattern, vertices | ||
|
|
||
| function optimal_distance2_coloring( | ||
| bg::BipartiteGraph, | ||
| ::Val{side}, | ||
| optimizer::O; | ||
| silent::Bool=true, | ||
| assert_solved::Bool=true, | ||
| ) where {side,O} | ||
| other_side = 3 - side | ||
| n = nb_vertices(bg, Val(side)) | ||
| model = Model(optimizer) | ||
| silent && set_silent(model) | ||
| # one variable per vertex to color, removing some renumbering symmetries | ||
| @variable(model, 1 <= color[i=1:n] <= i, Int) | ||
| # one variable to count the number of distinct colors | ||
| @variable(model, ncolors, Int) | ||
| @constraint(model, [ncolors; color] in MOI.CountDistinct(n + 1)) | ||
| # distance-2 coloring: neighbors of the same vertex must have distinct colors | ||
| for i in vertices(bg, Val(other_side)) | ||
| neigh = neighbors(bg, Val(other_side), i) | ||
| @constraint(model, color[neigh] in MOI.AllDifferent(length(neigh))) | ||
| end | ||
| # minimize the number of distinct colors (can't use maximum because they are not necessarily numbered contiguously) | ||
| @objective(model, Min, ncolors) | ||
| # actual solving step where time is spent | ||
| optimize!(model) | ||
| if assert_solved | ||
| # assert feasibility and optimality | ||
| @assert is_solved_and_feasible(model) | ||
| else | ||
| # only assert feasibility | ||
| @assert primal_status(model) == MOI.FEASIBLE_POINT | ||
| end | ||
| # native solver solutions are floating point numbers | ||
| color_int = round.(Int, value.(color)) | ||
| # remap to 1:cmax in case they are not contiguous | ||
| true_ncolors = 0 | ||
| remap = fill(0, maximum(color_int)) | ||
| for c in color_int | ||
| if remap[c] == 0 | ||
| true_ncolors += 1 | ||
| remap[c] = true_ncolors | ||
| end | ||
| end | ||
| return remap[color_int] | ||
| end | ||
|
|
||
| function ADTypes.column_coloring(A::AbstractMatrix, algo::OptimalColoringAlgorithm) | ||
| bg = BipartiteGraph(A) | ||
| return optimal_distance2_coloring( | ||
| bg, Val(2), algo.optimizer; algo.silent, algo.assert_solved | ||
| ) | ||
| end | ||
|
|
||
| function ADTypes.row_coloring(A::AbstractMatrix, algo::OptimalColoringAlgorithm) | ||
| bg = BipartiteGraph(A) | ||
| return optimal_distance2_coloring( | ||
| bg, Val(1), algo.optimizer; algo.silent, algo.assert_solved | ||
| ) | ||
| end | ||
|
|
||
| end |
This file contains hidden or 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 hidden or 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,34 @@ | ||
| """ | ||
| OptimalColoringAlgorithm | ||
|
|
||
| Coloring algorithm that relies on mathematical programming with [JuMP](https://jump.dev/) to find an optimal coloring. | ||
|
|
||
| !!! warning | ||
| This algorithm is only available when JuMP is loaded. If you encounter a method error, run `import JuMP` in your REPL and try again. | ||
| It only works for nonsymmetric, unidirectional colorings problems. | ||
|
|
||
| !!! danger | ||
| The coloring problem is NP-hard, so it is unreasonable to expect an optimal solution in reasonable time for large instances. | ||
|
|
||
| # Constructor | ||
|
|
||
| OptimalColoringAlgorithm(optimizer; silent::Bool=true, assert_solved::Bool=true) | ||
|
|
||
| The `optimizer` argument can be any JuMP-compatible optimizer. | ||
| However, the problem formulation is best suited to CP-SAT optimizers like [MiniZinc](https://github.com/jump-dev/MiniZinc.jl). | ||
| You can use [`optimizer_with_attributes`](https://jump.dev/JuMP.jl/stable/api/JuMP/#optimizer_with_attributes) to set solver-specific parameters. | ||
|
|
||
| # Keyword arguments | ||
|
|
||
| - `silent`: whether to suppress solver output | ||
| - `assert_solved`: whether to check that the solver found an optimal solution (as opposed to running out of time for example) | ||
| """ | ||
| struct OptimalColoringAlgorithm{O} <: ADTypes.AbstractColoringAlgorithm | ||
| optimizer::O | ||
| silent::Bool | ||
| assert_solved::Bool | ||
| end | ||
|
|
||
| function OptimalColoringAlgorithm(optimizer; silent::Bool=true, assert_solved::Bool=true) | ||
| return OptimalColoringAlgorithm(optimizer, silent, assert_solved) | ||
| end | ||
This file contains hidden or 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 hidden or 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,46 @@ | ||
| using SparseArrays | ||
| using SparseMatrixColorings | ||
| using StableRNGs | ||
| using Test | ||
| using JuMP | ||
| using MiniZinc | ||
| using HiGHS | ||
|
|
||
| rng = StableRNG(0) | ||
|
|
||
| asymmetric_params = vcat( | ||
| [(10, 20, p) for p in (0.0:0.1:0.5)], [(20, 10, p) for p in (0.0:0.1:0.5)] | ||
| ) | ||
|
|
||
| algo = GreedyColoringAlgorithm() | ||
| optalgo = OptimalColoringAlgorithm(() -> MiniZinc.Optimizer{Float64}("highs"); silent=false) | ||
|
|
||
| @testset "Column coloring" begin | ||
| problem = ColoringProblem(; structure=:nonsymmetric, partition=:column) | ||
| for (m, n, p) in asymmetric_params | ||
| A = sprand(rng, m, n, p) | ||
| result = coloring(A, problem, algo) | ||
| optresult = coloring(A, problem, optalgo) | ||
| @test ncolors(result) >= ncolors(optresult) | ||
| end | ||
| end | ||
|
|
||
| @testset "Row coloring" begin | ||
| problem = ColoringProblem(; structure=:nonsymmetric, partition=:row) | ||
| for (m, n, p) in asymmetric_params | ||
| A = sprand(rng, m, n, p) | ||
| result = coloring(A, problem, algo) | ||
| optresult = coloring(A, problem, optalgo) | ||
| @test ncolors(result) >= ncolors(optresult) | ||
| end | ||
| end | ||
|
|
||
| @testset "Too big" begin | ||
| A = sprand(rng, Bool, 100, 100, 0.1) | ||
| optalgo_timelimit = OptimalColoringAlgorithm( | ||
| optimizer_with_attributes(HiGHS.Optimizer, "time_limit" => 10.0); # 1 second | ||
| silent=false, | ||
| assert_solved=false, | ||
| ) | ||
| @test_throws AssertionError coloring(A, ColoringProblem(), optalgo_timelimit) | ||
| end |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.