Skip to content

Commit 4d3713c

Browse files
committed
set packing
1 parent aa015c9 commit 4d3713c

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

notebooks/tutorial.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ begin
1111
using Revise, GraphTensorNetworks
1212
end
1313

14+
# ╔═╡ 6c7f88d2-2491-4272-b0b6-83fe7778f81a
15+
md"## High level interfaces"
16+
1417
# ╔═╡ 83fc0a3c-ef51-4d5c-9c46-baa645f80fd2
1518
locs = [rand(2) for i=1:70];
1619

@@ -23,15 +26,21 @@ vizconfig(g; locs=locs, unit=0.5)
2326
# ╔═╡ b6e916e2-7d43-4085-a3c8-5c57069e8384
2427
gp = Independence(g; optmethod=:auto);
2528

29+
# ╔═╡ 1a58dea3-14fa-4be7-b087-45ed9bad2407
30+
gp
31+
2632
# ╔═╡ 03840394-3a5b-4c24-8b52-15f7e400f900
2733
timespace_complexity(gp)
2834

2935
# ╔═╡ 11c71de9-caa9-40c6-84cb-a0f29d873805
30-
results = solutions(gp, CountingTropical{Float64}; all=false)
36+
results = solve(gp, "config max"; usecuda=false)
3137

3238
# ╔═╡ edfc5600-9a4a-4657-b165-8eb3e8540edf
3339
vizconfig(g; locs=locs, unit=0.5, config=results[].c.data)
3440

41+
# ╔═╡ 47e7a144-44c0-4770-8f82-1292b0aad8bc
42+
md"## Lower level interfaces"
43+
3544
# ╔═╡ 3666f629-7a87-4b50-aaf1-cc128d598aaf
3645
md"## Different Algebras"
3746

@@ -150,13 +159,16 @@ one(typeof(ce2))
150159

151160
# ╔═╡ Cell order:
152161
# ╠═54d71646-ffad-11eb-3866-5f1c0bc5d0bf
162+
# ╟─6c7f88d2-2491-4272-b0b6-83fe7778f81a
153163
# ╠═83fc0a3c-ef51-4d5c-9c46-baa645f80fd2
154164
# ╠═1522938d-d60b-4133-8625-5a09615a7b26
155165
# ╠═05ac5610-4a1d-4433-ba0d-1d13fd8a6733
156166
# ╠═b6e916e2-7d43-4085-a3c8-5c57069e8384
167+
# ╠═1a58dea3-14fa-4be7-b087-45ed9bad2407
157168
# ╠═03840394-3a5b-4c24-8b52-15f7e400f900
158169
# ╠═11c71de9-caa9-40c6-84cb-a0f29d873805
159170
# ╠═edfc5600-9a4a-4657-b165-8eb3e8540edf
171+
# ╟─47e7a144-44c0-4770-8f82-1292b0aad8bc
160172
# ╟─3666f629-7a87-4b50-aaf1-cc128d598aaf
161173
# ╟─51f0f187-d14d-48ab-ab30-96e71d796959
162174
# ╟─a5b63d08-2917-4e9f-9c5c-b5f17181d2aa

src/graph_polynomials.jl

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using OMEinsum: NestedEinsum, getixs, getiy
33
using FFTW
44
using LightGraphs
55

6-
export contractx, contractf, graph_polynomial, optimize_code
6+
export contractx, contractf, graph_polynomial
77

88
"""
99
graph_polynomial(problem, method; usecuda=false, kwargs...)
@@ -103,15 +103,26 @@ end
103103
############### Problem specific implementations ################
104104
### independent set ###
105105
function generate_tensors(fx, gp::Independence)
106-
ixs = getixs(flatten(gp.code))
106+
flatten_code = flatten(gp.code)
107+
ixs = getixs(flatten_code)
108+
n = length(labels(flatten_code))
107109
T = typeof(fx(ixs[1][1]))
108-
return map(ixs) do ix
109-
# if the tensor rank is 1, create a vertex tensor.
110-
# otherwise the tensor rank must be 2, create a bond tensor.
111-
length(ix)==1 ? misv(fx(ix[1])) : misb(T)
110+
return Tuple(map(enumerate(ixs)) do (i, ix)
111+
if i <= n
112+
misv(fx(ix[1]))
113+
else
114+
misb(T, length(ix)) # if n!=2, it corresponds to set packing problem.
115+
end
116+
end)
117+
end
118+
function misb(::Type{T}, n::Integer=2) where T
119+
res = zeros(T, fill(2, n)...)
120+
res[1] = one(T)
121+
for i=1:n
122+
res[1+1<<(i-1)] = one(T)
112123
end
124+
return res
113125
end
114-
misb(::Type{T}) where T = [one(T) one(T); one(T) zero(T)]
115126
misv(val::T) where T = [one(T), val]
116127

117128
graph_polynomial_maxorder(gp::Independence; usecuda) = Int(sum(contractx(gp, TropicalF64(1.0); usecuda=usecuda)).n)

src/networks.jl

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export Independence, MaximalIndependence, Matching, Coloring
1+
export Independence, MaximalIndependence, Matching, Coloring, optimize_code, set_packing
22
const EinTypes = Union{EinCode,NestedEinsum}
33

44
abstract type GraphProblem end
@@ -114,4 +114,27 @@ OMEinsum.timespace_complexity(gp::GraphProblem) = timespace_complexity(gp.code,
114114
for T in [:Independence, :Matching, :MaximalIndependence]
115115
@eval bondsize(gp::$T) = 2
116116
end
117-
bondsize(gp::Coloring{K}) where K = K
117+
bondsize(gp::Coloring{K}) where K = K
118+
119+
"""
120+
set_packing(sets; kwargs...)
121+
122+
Set packing is a generalization of independent set problem to hypergraphs.
123+
Calling this function will return you an `Independence` instance.
124+
`sets` are a vector of vectors, each element being a vertex in the independent set problem. `kwargs` are code optimization parameters.
125+
126+
### Example
127+
```julia
128+
julia> sets = [[1, 2, 5], [1, 3], [2, 4], [3, 6], [2, 3, 6]]; # each set is a vertex
129+
130+
julia> gp = set_packing(sets; optmethod=:auto);
131+
132+
julia> res = optimalsolutions(gp; all=true)[]
133+
(2, {10010, 00110, 01100})ₜ
134+
```
135+
"""
136+
function set_packing(sets; kwargs...)
137+
n = length(sets)
138+
code = EinCode(([(i,) for i=1:n]..., [(i,j) for i=1:n,j=1:n if j>i && !isempty(sets[i] sets[j])]...), ())
139+
Independence(optimize_code(code; kwargs...))
140+
end

test/configurations.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,14 @@ end
4242
@test res5.n == res0
4343
@test res5.c.data res2.c.data
4444
end
45+
end
46+
47+
@testset "set packing" begin
48+
sets = [[1, 2, 5], [1, 3], [2, 4], [3, 6], [2, 3, 6]] # each set is a vertex
49+
gp = set_packing(sets; optmethod=:auto)
50+
res = optimalsolutions(gp; all=true)[]
51+
@test res.n == 2
52+
@test BitVector(Bool[0,0,1,1,0]) res.c.data
53+
@test BitVector(Bool[1,0,0,1,0]) res.c.data
54+
@test BitVector(Bool[0,1,1,0,0]) res.c.data
4555
end

0 commit comments

Comments
 (0)