Skip to content

Commit bc07490

Browse files
committed
add graph to problem as a field
1 parent 170989e commit bc07490

File tree

7 files changed

+28
-25
lines changed

7 files changed

+28
-25
lines changed

src/networks/Coloring.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ The [Vertex Coloring](https://psychic-meme-f4d866f8.pages.github.io/dev/tutorial
77
"""
88
struct Coloring{K,CT<:AbstractEinsum} <: GraphProblem
99
code::CT
10-
nv::Int
10+
graph::SimpleGraph{Int}
1111
end
12-
Coloring{K}(code::ET, nv::Int) where {K,ET<:AbstractEinsum} = Coloring{K,ET}(code, nv)
12+
Coloring{K}(code::ET, graph::SimpleGraph) where {K,ET<:AbstractEinsum} = Coloring{K,ET}(code, graph)
1313
# same network layout as independent set.
1414
function Coloring{K}(g::SimpleGraph; openvertices=(), optimizer=GreedyMethod(), simplifier=nothing) where K
1515
rawcode = EinCode(([[i] for i in Graphs.vertices(g)]..., # labels for vertex tensors
1616
[[minmax(e.src,e.dst)...] for e in Graphs.edges(g)]...), collect(Int, openvertices)) # labels for edge tensors
1717
code = _optimize_code(rawcode, uniformsize(rawcode, 2), optimizer, simplifier)
18-
Coloring{K}(code, nv(g))
18+
Coloring{K}(code, g)
1919
end
2020

2121
flavors(::Type{<:Coloring{K}}) where K = collect(0:K-1)
2222
get_weights(::Coloring{K}, i::Int) where K = ones(Int, K)
23-
terms(gp::Coloring) = getixsv(gp.code)[1:gp.nv]
24-
labels(gp::Coloring) = [1:gp.nv...]
23+
terms(gp::Coloring) = getixsv(gp.code)[1:nv(gp.graph)]
24+
labels(gp::Coloring) = [1:nv(gp.graph)...]
2525

2626
function generate_tensors(x::T, c::Coloring{K}) where {K,T}
2727
ixs = getixsv(c.code)
2828
return add_labels!(map(1:length(ixs)) do i
29-
i <= c.nv ? coloringv(T, K) .^ get_weights(c, i) : coloringb(x, K)
29+
i <= nv(c.graph) ? coloringv(T, K) .^ get_weights(c, i) : coloringb(x, K)
3030
end, ixs, labels(c))
3131
end
3232

src/networks/DominatingSet.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ In the constructor, `weights` are the weights of vertices.
99
"""
1010
struct DominatingSet{CT<:AbstractEinsum,WT<:Union{NoWeight, Vector}} <: GraphProblem
1111
code::CT
12+
graph::SimpleGraph{Int}
1213
weights::WT
1314
end
1415

1516
function DominatingSet(g::SimpleGraph; weights=NoWeight(), openvertices=(), optimizer=GreedyMethod(), simplifier=nothing)
1617
@assert weights isa NoWeight || length(weights) == nv(g)
1718
rawcode = EinCode(([[Graphs.neighbors(g, v)..., v] for v in Graphs.vertices(g)]...,), collect(Int, openvertices))
18-
DominatingSet(_optimize_code(rawcode, uniformsize(rawcode, 2), optimizer, simplifier), weights)
19+
DominatingSet(_optimize_code(rawcode, uniformsize(rawcode, 2), optimizer, simplifier), g, weights)
1920
end
2021

2122
flavors(::Type{<:DominatingSet}) = [0, 1]

src/networks/IndependentSet.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ In the constructor, `weights` are the weights of vertices.
1010
"""
1111
struct IndependentSet{CT<:AbstractEinsum,WT<:Union{NoWeight, Vector}} <: GraphProblem
1212
code::CT
13-
nv::Int
13+
graph::SimpleGraph{Int}
1414
weights::WT
1515
end
1616

@@ -19,21 +19,21 @@ function IndependentSet(g::SimpleGraph; weights=NoWeight(), openvertices=(), opt
1919
rawcode = EinCode([[[i] for i in Graphs.vertices(g)]..., # labels for vertex tensors
2020
[[minmax(e.src,e.dst)...] for e in Graphs.edges(g)]...], collect(Int, openvertices)) # labels for edge tensors
2121
code = _optimize_code(rawcode, uniformsize(rawcode, 2), optimizer, simplifier)
22-
IndependentSet(code, nv(g), weights)
22+
IndependentSet(code, g, weights)
2323
end
2424

2525
flavors(::Type{<:IndependentSet}) = [0, 1]
2626
get_weights(gp::IndependentSet, i::Int) = [0, gp.weights[i]]
27-
terms(gp::IndependentSet) = getixsv(gp.code)[1:gp.nv]
28-
labels(gp::IndependentSet) = [1:gp.nv...]
27+
terms(gp::IndependentSet) = getixsv(gp.code)[1:nv(gp.graph)]
28+
labels(gp::IndependentSet) = [1:nv(gp.graph)...]
2929

3030
# generate tensors
3131
function generate_tensors(x::T, gp::IndependentSet) where T
32-
gp.nv == 0 && return []
32+
nv(gp.graph) == 0 && return []
3333
ixs = getixsv(gp.code)
3434
# we only add labels at vertex tensors
35-
return vcat(add_labels!([misv(Ref(x) .^ get_weights(gp, i)) for i=1:gp.nv], ixs[1:gp.nv], labels(gp)),
36-
[misb(T, length(ix)) for ix in ixs[gp.nv+1:end]] # if n!=2, it corresponds to set packing problem.
35+
return vcat(add_labels!([misv(Ref(x) .^ get_weights(gp, i)) for i=1:nv(gp.graph)], ixs[1:nv(gp.graph)], labels(gp)),
36+
[misb(T, length(ix)) for ix in ixs[nv(gp.graph)+1:end]] # if n!=2, it corresponds to set packing problem.
3737
)
3838
end
3939

@@ -70,7 +70,8 @@ julia> res = best_solutions(gp; all=true)[]
7070
function set_packing(sets; weights=NoWeight(), openvertices=(), optimizer=GreedyMethod(), simplifier=nothing)
7171
n = length(sets)
7272
code = EinCode(vcat([[i] for i=1:n], [[i,j] for i=1:n,j=1:n if j>i && !isempty(sets[i] sets[j])]), collect(Int,openvertices))
73-
IndependentSet(_optimize_code(code, uniformsize(code, 2), optimizer, simplifier), n, weights)
73+
# NOTE: we use a dummy graph here, which should be a hypergraph!
74+
IndependentSet(_optimize_code(code, uniformsize(code, 2), optimizer, simplifier), SimpleGraph(n), weights)
7475
end
7576

7677
"""

src/networks/Matching.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@ The [Vertex matching](https://psychic-meme-f4d866f8.pages.github.io/dev/tutorial
77
"""
88
struct Matching{CT<:AbstractEinsum} <: GraphProblem
99
code::CT
10-
ne::Int
10+
graph::SimpleGraph{Int}
1111
end
1212

1313
function Matching(g::SimpleGraph; openvertices=(), optimizer=GreedyMethod(), simplifier=nothing)
1414
edges = [minmax(e.src,e.dst) for e in Graphs.edges(g)]
1515
rawcode = EinCode(vcat([[s] for s in edges], # labels for edge tensors
1616
[[minmax(i,j) for j in neighbors(g, i)] for i in Graphs.vertices(g)]),
1717
collect(Tuple{Int,Int}, openvertices))
18-
Matching(_optimize_code(rawcode, uniformsize(rawcode, 2), optimizer, simplifier), length(edges))
18+
Matching(_optimize_code(rawcode, uniformsize(rawcode, 2), optimizer, simplifier), g)
1919
end
2020

2121
flavors(::Type{<:Matching}) = [0, 1]
2222
get_weights(::Matching, i::Int) = [0, 1]
23-
terms(gp::Matching) = getixsv(gp.code)[1:gp.ne]
23+
terms(gp::Matching) = getixsv(gp.code)[1:ne(gp.graph)]
2424
labels(gp::Matching) = getindex.(terms(gp))
2525

2626
function generate_tensors(x::T, m::Matching) where T
27-
m.ne == 0 && return []
27+
ne(m.graph) == 0 && return []
2828
ixs = getixsv(m.code)
2929
tensors = AbstractArray{T}[]
3030
for i=1:length(ixs)
3131
ix = ixs[i]
32-
if i<=m.ne
32+
if i<=ne(m.graph)
3333
@assert length(ix) == 1
3434
t = Ref(x) .^ get_weights(m, i) # x is defined on edges.
3535
else

src/networks/MaxCut.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ In the constructor, `weights` are the weights of edges.
99
"""
1010
struct MaxCut{CT<:AbstractEinsum,WT<:Union{NoWeight, Vector}} <: GraphProblem
1111
code::CT
12-
nv::Int
12+
graph::SimpleGraph{Int}
1313
weights::WT
1414
end
1515
function MaxCut(g::SimpleGraph; weights=NoWeight(), openvertices=(), optimizer=GreedyMethod(), simplifier=nothing)
1616
@assert weights isa NoWeight || length(weights) == ne(g)
1717
rawcode = EinCode([[minmax(e.src,e.dst)...] for e in Graphs.edges(g)], collect(Int, openvertices)) # labels for edge tensors
18-
MaxCut(_optimize_code(rawcode, uniformsize(rawcode, 2), optimizer, simplifier), nv(g), weights)
18+
MaxCut(_optimize_code(rawcode, uniformsize(rawcode, 2), optimizer, simplifier), g, weights)
1919
end
2020

2121
flavors(::Type{<:MaxCut}) = [0, 1]
2222
get_weights(gp::MaxCut, i::Int) = [0, gp.weights[i]]
2323
terms(gp::MaxCut) = getixsv(gp.code)
24-
labels(gp::MaxCut) = [1:gp.nv...]
24+
labels(gp::MaxCut) = [1:nv(gp.graph)...]
2525

2626
function generate_tensors(x::T, gp::MaxCut) where T
2727
ixs = getixsv(gp.code)

src/networks/MaximalIS.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ The [maximal independent set](https://psychic-meme-f4d866f8.pages.github.io/dev/
88
"""
99
struct MaximalIS{CT<:AbstractEinsum,WT<:Union{NoWeight, Vector}} <: GraphProblem
1010
code::CT
11+
graph::SimpleGraph
1112
weights::WT
1213
end
1314

1415
function MaximalIS(g::SimpleGraph; weights=NoWeight(), openvertices=(), optimizer=GreedyMethod(), simplifier=nothing)
1516
@assert weights isa NoWeight || length(weights) == nv(g)
1617
rawcode = EinCode(([[Graphs.neighbors(g, v)..., v] for v in Graphs.vertices(g)]...,), collect(Int, openvertices))
17-
MaximalIS(_optimize_code(rawcode, uniformsize(rawcode, 2), optimizer, simplifier), weights)
18+
MaximalIS(_optimize_code(rawcode, uniformsize(rawcode, 2), optimizer, simplifier), g, weights)
1819
end
1920

2021
flavors(::Type{<:MaximalIS}) = [0, 1]

test/configurations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ end
3232

3333
@testset "enumerating" begin
3434
rawcode = IndependentSet(smallgraph(:petersen); optimizer=nothing)
35-
optcode = IndependentSet(optimize_code(rawcode.code, uniformsize(rawcode.code, 2), GreedyMethod()), 10, NoWeight())
35+
optcode = IndependentSet(optimize_code(rawcode.code, uniformsize(rawcode.code, 2), GreedyMethod()), smallgraph(:petersen), NoWeight())
3636
for code in [rawcode, optcode]
3737
res0 = max_size(code)
3838
_, res1 = max_size_count(code)

0 commit comments

Comments
 (0)