Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/networks/Coloring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function Coloring{K}(g::SimpleGraph; weights=NoWeight(), openvertices=(), fixedv
rawcode = EinCode(([[i] for i in Graphs.vertices(g)]..., # labels for vertex tensors
[[minmax(e.src,e.dst)...] for e in Graphs.edges(g)]...), collect(Int, openvertices)) # labels for edge tensors
code = _optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier)
Coloring{K}(code, g, weights, fixedvertices)
Coloring{K}(code, g, weights, Dict{Int,Int}(fixedvertices))
end

flavors(::Type{<:Coloring{K}}) where K = collect(0:K-1)
Expand Down Expand Up @@ -69,4 +69,4 @@ function is_vertex_coloring(graph::SimpleGraph, config)
config[e.src] == config[e.dst] && return false
end
return true
end
end
2 changes: 1 addition & 1 deletion src/networks/DominatingSet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end
function DominatingSet(g::SimpleGraph; weights=NoWeight(), openvertices=(), fixedvertices=Dict{Int,Int}(), optimizer=GreedyMethod(), simplifier=nothing)
@assert weights isa NoWeight || length(weights) == nv(g)
rawcode = EinCode(([[Graphs.neighbors(g, v)..., v] for v in Graphs.vertices(g)]...,), collect(Int, openvertices))
DominatingSet(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), g, weights, fixedvertices)
DominatingSet(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), g, weights, Dict{Int,Int}(fixedvertices))
end

flavors(::Type{<:DominatingSet}) = [0, 1]
Expand Down
2 changes: 1 addition & 1 deletion src/networks/IndependentSet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function IndependentSet(g::SimpleGraph; weights=NoWeight(), openvertices=(), fix
rawcode = EinCode([[[i] for i in Graphs.vertices(g)]..., # labels for vertex tensors
[[minmax(e.src,e.dst)...] for e in Graphs.edges(g)]...], collect(Int, openvertices)) # labels for edge tensors
code = _optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier)
IndependentSet(code, g, weights, fixedvertices)
IndependentSet(code, g, weights, Dict{Int,Int}(fixedvertices))
end

flavors(::Type{<:IndependentSet}) = [0, 1]
Expand Down
6 changes: 3 additions & 3 deletions src/networks/Matching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ struct Matching{CT<:AbstractEinsum, WT<:Union{NoWeight,Vector}} <: GraphProblem
code::CT
graph::SimpleGraph{Int}
weights::WT
fixedvertices::Dict{Int,Int}
fixedvertices::Dict{Tuple{Int,Int},Int}
end

function Matching(g::SimpleGraph; weights=NoWeight(), openvertices=(),fixedvertices=Dict{Int,Int}(), optimizer=GreedyMethod(), simplifier=nothing)
function Matching(g::SimpleGraph; weights=NoWeight(), openvertices=(),fixedvertices=Dict{Tuple{Int,Int},Int}(), optimizer=GreedyMethod(), simplifier=nothing)
@assert weights isa NoWeight || length(weights) == ne(g)
edges = [minmax(e.src,e.dst) for e in Graphs.edges(g)]
rawcode = EinCode(vcat([[s] for s in edges], # labels for edge tensors
[[minmax(i,j) for j in neighbors(g, i)] for i in Graphs.vertices(g)]),
collect(Tuple{Int,Int}, openvertices))
Matching(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), g, weights, fixedvertices)
Matching(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), g, weights, Dict{Tuple{Int,Int},Int}(fixedvertices))
end

flavors(::Type{<:Matching}) = [0, 1]
Expand Down
2 changes: 1 addition & 1 deletion src/networks/MaxCut.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ end
function MaxCut(g::SimpleGraph; weights=NoWeight(), openvertices=(), fixedvertices=Dict{Int,Int}(), optimizer=GreedyMethod(), simplifier=nothing)
@assert weights isa NoWeight || length(weights) == ne(g)
rawcode = EinCode([[minmax(e.src,e.dst)...] for e in Graphs.edges(g)], collect(Int, openvertices)) # labels for edge tensors
MaxCut(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), g, weights, fixedvertices)
MaxCut(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), g, weights, Dict{Int,Int}(fixedvertices))
end

flavors(::Type{<:MaxCut}) = [0, 1]
Expand Down
2 changes: 1 addition & 1 deletion src/networks/MaximalIS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end
function MaximalIS(g::SimpleGraph; weights=NoWeight(), openvertices=(), optimizer=GreedyMethod(), simplifier=nothing, fixedvertices=Dict{Int,Int}())
@assert weights isa NoWeight || length(weights) == nv(g)
rawcode = EinCode(([[Graphs.neighbors(g, v)..., v] for v in Graphs.vertices(g)]...,), collect(Int, openvertices))
MaximalIS(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), g, weights, fixedvertices)
MaximalIS(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), g, weights, Dict{Int,Int}(fixedvertices))
end

flavors(::Type{<:MaximalIS}) = [0, 1]
Expand Down
6 changes: 3 additions & 3 deletions src/networks/PaintShop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct PaintShop{CT<:AbstractEinsum,LT} <: GraphProblem
fixedvertices::Dict{LT,Int}
end

function paintshop_from_pairs(pairs::AbstractVector{Tuple{Int,Int}}; openvertices=(), optimizer=GreedyMethod(), simplifier=nothing, fixedvertices=Dict{LT,Int}())
function paintshop_from_pairs(pairs::AbstractVector{Tuple{Int,Int}}; openvertices=(), optimizer=GreedyMethod(), simplifier=nothing, fixedvertices=Dict())
n = length(pairs)
@assert sort!(vcat(collect.(pairs)...)) == collect(1:2n)
sequence = zeros(Int, 2*n)
Expand All @@ -61,7 +61,7 @@ function PaintShop(sequence::AbstractVector{T}; openvertices=(), fixedvertices=D
[[sequence[i], sequence[i+1]] for i=1:n-1], # labels for edge tensors
),
collect(T, openvertices))
PaintShop(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), sequence, isfirst, fixedvertices)
PaintShop(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), sequence, isfirst, Dict{T,Int}(fixedvertices))
end

flavors(::Type{<:PaintShop}) = [0, 1]
Expand Down Expand Up @@ -106,4 +106,4 @@ function paint_shop_coloring_from_config(p::PaintShop{CT,LT}, config) where {CT,
return map(1:length(p.sequence)) do i
p.isfirst[i] ? d[p.sequence[i]] : ~d[p.sequence[i]]
end
end
end
2 changes: 1 addition & 1 deletion src/networks/Satisfiability.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ end

function Satisfiability(cnf::CNF{T}; weights=NoWeight(), openvertices=(), optimizer=GreedyMethod(), simplifier=nothing, fixedvertices=Dict{T,Int}()) where T
rawcode = EinCode([[getfield.(c.vars, :name)...] for c in cnf.clauses], collect(T, openvertices))
Satisfiability(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), cnf, weights, fixedvertices)
Satisfiability(_optimize_code(rawcode, uniformsize_fix(rawcode, 2, fixedvertices), optimizer, simplifier), cnf, weights, Dict{T,Int}(fixedvertices))
end

flavors(::Type{<:Satisfiability}) = [0, 1] # false, true
Expand Down
2 changes: 1 addition & 1 deletion src/networks/SetCovering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function SetCovering(sets::AbstractVector{Vector{ET}}; weights=NoWeight(), openv

code = EinCode([[[i] for i=1:nsets]...,
[count[e] for e in elements]...], collect(Int,openvertices))
SetCovering(_optimize_code(code, uniformsize_fix(code, 2, fixedvertices), optimizer, simplifier), sets, weights, fixedvertices)
SetCovering(_optimize_code(code, uniformsize_fix(code, 2, fixedvertices), optimizer, simplifier), sets, weights, Dict{ET,Int}(fixedvertices))
end

function cover_count(sets)
Expand Down
2 changes: 1 addition & 1 deletion src/networks/SetPacking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function SetPacking(sets::AbstractVector{Vector{ET}}; weights=NoWeight(), openve
nsets = length(sets)
@assert weights isa NoWeight || length(weights) == nsets
code = EinCode(vcat([[i] for i=1:nsets], [[i,j] for i=1:nsets,j=1:nsets if j>i && !isempty(sets[i] ∩ sets[j])]), collect(Int,openvertices))
SetPacking(_optimize_code(code, uniformsize_fix(code, 2, openvertices), optimizer, simplifier), sets, weights, fixedvertices)
SetPacking(_optimize_code(code, uniformsize_fix(code, 2, openvertices), optimizer, simplifier), sets, weights, Dict{ET,Int}(fixedvertices))
end

flavors(::Type{<:SetPacking}) = [0, 1]
Expand Down
4 changes: 2 additions & 2 deletions test/networks/Coloring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ using Test, GenericTensorNetworks, Graphs
add_edge!(g, i, j)
end
code = Coloring{3}(g; optimizer=GreedyMethod())
res = best_solutions(code; all=true)[]
res = GenericTensorNetworks.best_solutions(code; all=true)[]
@test length(res.c.data) == 12
g = smallgraph(:petersen)
code = Coloring{3}(g; optimizer=GreedyMethod())
res = best_solutions(code; all=true)[]
res = GenericTensorNetworks.best_solutions(code; all=true)[]
@test length(res.c.data) == 120

c = solve(code, SingleConfigMax())[]
Expand Down
8 changes: 7 additions & 1 deletion test/networks/Matching.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using Test, GenericTensorNetworks, Graphs
using GenericTensorNetworks: solutions

@testset "enumerating - matching" begin
g = smallgraph(:petersen)
code = Matching(g; optimizer=GreedyMethod())
code = Matching(g; optimizer=GreedyMethod(), fixedvertices=Dict())
res = solutions(code, CountingTropicalF64; all=true)[]
@test res.n == 5
@test length(res.c.data) == 6
code = Matching(g; optimizer=GreedyMethod(), fixedvertices=Dict((1,2)=>1))
res = solutions(code, CountingTropicalF64; all=true)[]
@test res.n == 5
k = findfirst(x->x==(1,2), labels(code))
@test length(res.c.data) == 2 && res.c.data[1][k] == 1 && res.c.data[2][k] == 1
end

@testset "match polynomial" begin
Expand Down
2 changes: 1 addition & 1 deletion test/networks/MaxCut.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end
add_edge!(g, i, j)
end
code = MaxCut(g; optimizer=GreedyMethod())
res = best_solutions(code; all=true)[]
res = GenericTensorNetworks.best_solutions(code; all=true)[]
@test length(res.c.data) == 2
@test cut_size(g, res.c.data[1]) == 5
end
Expand Down
1 change: 1 addition & 0 deletions test/networks/MaximalIS.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GenericTensorNetworks, Test, Graphs
using GenericTensorNetworks: graph_polynomial

@testset "counting maximal IS" begin
g = random_regular_graph(20, 3)
Expand Down
4 changes: 2 additions & 2 deletions test/networks/Satisfiability.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ end
gp = Satisfiability(cnf)

@test solve(gp, SizeMax())[].n == 4.0
res = best_solutions(gp; all=true)[].c.data
res = GenericTensorNetworks.best_solutions(gp; all=true)[].c.data
for i=0:1<<6-1
v = StaticBitVector(Bool[i>>(k-1) & 1 for k=1:6])
if v ∈ res
Expand All @@ -54,7 +54,7 @@ end
gp = Satisfiability(cnf; weights=fill(2, length(cnf)))

@test solve(gp, SizeMax())[].n == 8.0
res = best_solutions(gp; all=true)[].c.data
res = GenericTensorNetworks.best_solutions(gp; all=true)[].c.data
for i=0:1<<6-1
v = StaticBitVector(Bool[i>>(k-1) & 1 for k=1:6])
if v ∈ res
Expand Down
2 changes: 1 addition & 1 deletion test/networks/SetPacking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using GenericTensorNetworks, Test, Graphs
@testset "set packing" begin
sets = [[1, 2, 5], [1, 3], [2, 4], [3, 6], [2, 3, 6]] # each set is a vertex
gp = SetPacking(sets; optimizer=GreedyMethod())
res = best_solutions(gp; all=true)[]
res = GenericTensorNetworks.best_solutions(gp; all=true)[]
@test res.n == 2
@test BitVector(Bool[0,0,1,1,0]) ∈ res.c.data
@test BitVector(Bool[1,0,0,1,0]) ∈ res.c.data
Expand Down