-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
$(TYPEDEF) | ||
Coloring{K}(graph; weights=UnitWeight()) | ||
The [Vertex Coloring](https://queracomputing.github.io/GenericTensorNetworks.jl/dev/generated/Coloring/) problem. | ||
Positional arguments | ||
------------------------------- | ||
* `graph` is the problem graph. | ||
* `weights` are associated with the edges of the `graph`, default to `UnitWeight()`. | ||
""" | ||
struct Coloring{K, WT<:Union{UnitWeight, Vector}} <:AbstractProblem | ||
graph::SimpleGraph{Int64} | ||
weights::WT | ||
function Coloring{K}(graph::SimpleGraph{Int64}, weights::Union{UnitWeight, Vector}=UnitWeight()) where {K} | ||
@assert weights isa UnitWeight || length(weights) == ne(graph) "length of weights must be equal to the number of edges $(ne(graph)), got: $(length(weights))" | ||
new{K, typeof(weights)}(graph, weights) | ||
end | ||
end | ||
Base.:(==)(a::Coloring, b::Coloring) = a.graph == b.graph && a.weights == b.weights | ||
|
||
variables(gp::Coloring{K}) where K = collect(1:nv(gp.graph)) | ||
flavors(::Type{<:Coloring{K}}) where K = collect(0:K-1) # colors | ||
num_flavors(c::Type{<:Coloring{K}}) where K = length(flavors(c)) # number of colors | ||
terms(gp::Coloring{K}) where K = [[minmax(e.src,e.dst)...] for e in Graphs.edges(gp.graph)] # return the edges of the graph | ||
|
||
|
||
# weights interface | ||
# ?whether weights information would be useful here? I think in sat-coloring, we only need to consider unweighted graphs | ||
parameters(c::Coloring) = c.weights | ||
set_parameters(c::Coloring{K}, weights) where K = Coloring{K}(c.graph, weights) | ||
|
||
# utilities | ||
""" | ||
evaluate(c::Coloring, config) | ||
Compute the energy of the vertex coloring configuration `config`, the energy is the number of violated edges. | ||
""" | ||
function evaluate(c::Coloring, config) | ||
@assert length(config) == nv(c.graph) | ||
coloring_energy(terms(c), c.weights,config) | ||
end | ||
|
||
coloring_energy(terms::AbstractVector, weights::AbstractVector, config) = sum([(config[e[1]] == config[e[2]]) * w for (e, w) in zip(terms, weights)]) | ||
coloring_energy(terms::AbstractVector,::UnitWeight, config) = sum([(config[e[1]] == config[e[2]]) for e in terms]) | ||
|
||
|
||
""" | ||
is_vertex_coloring(graph::SimpleGraph, config) | ||
Returns true if the coloring specified by config is a valid one, i.e. does not violate the contraints of vertices of an edges having different colors. | ||
""" | ||
function is_vertex_coloring(graph::SimpleGraph, config) | ||
for e in edges(graph) | ||
config[e.src] == config[e.dst] && return false | ||
end | ||
return true | ||
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
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,32 @@ | ||
using Test, ProblemReductions, Graphs | ||
using ProblemReductions: Coloring, SimpleGraph, add_edge!, UnitWeight, variables, flavors, num_flavors, | ||
terms, evaluate, coloring_energy, is_vertex_coloring,set_parameters,parameters | ||
|
||
# create a graph | ||
|
||
@testset "coloring" begin | ||
# constructor function | ||
@test flavors(Coloring{3}) == [0,1,2] | ||
@test num_flavors(Coloring{3}) == 3 | ||
|
||
g = SimpleGraph(4) | ||
add_edge!(g, 1, 2) | ||
add_edge!(g, 2, 3) | ||
add_edge!(g, 3, 4) | ||
add_edge!(g, 4, 1) | ||
c = Coloring{3}(g,UnitWeight()) | ||
@test c.graph == g && c.weights isa UnitWeight | ||
@test variables(c) == [1, 2, 3, 4] | ||
@test terms(c)==[[1, 2],[1,4],[2, 3], [3, 4]] | ||
|
||
# weights interface | ||
@test parameters(c) == UnitWeight() | ||
@test set_parameters(c, [1, 2, 2, 1]) == Coloring{3}(g, [1, 2, 2, 1]) | ||
|
||
# evaluate,here I found the definition of Config is not clear, so I can't test the evaluate function | ||
@test evaluate(c,[0, 1, 2, 0]) == 1 | ||
@test coloring_energy(terms(c),[1, 3, 2, 5],[0, 1, 2, 0]) == 3 | ||
|
||
|
||
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 |
---|---|---|
|
@@ -6,4 +6,8 @@ end | |
|
||
@testset "SpinGlass" begin | ||
include("SpinGlass.jl") | ||
end | ||
|
||
@testset "Coloring" begin | ||
include("Coloring.jl") | ||
end |