Skip to content

Commit 8ad5004

Browse files
committed
finish mapping
1 parent ed6847e commit 8ad5004

File tree

4 files changed

+67
-15
lines changed

4 files changed

+67
-15
lines changed

src/gadgets.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ function mapped_graph(::WTurn)
297297
return locs, unitdisk_graph(locs, 1.5), [1, 3]
298298
end
299299
Base.size(::WTurn) = (4, 4)
300-
cross_location(::WTurn) = (3,2)
300+
cross_location(::WTurn) = (2,2)
301301
iscon(::WTurn) = false
302302

303303
struct BranchFixB <: CrossPattern end
@@ -409,6 +409,12 @@ for T in [:RotatedGadget, :ReflectedGadget]
409409
end
410410
@eval mis_overhead(p::$T) = mis_overhead(p.gadget)
411411
@eval vertex_overhead(p::$T) = vertex_overhead(p.gadget)
412+
@eval function mapped_entry_to_compact(r::$T)
413+
return mapped_entry_to_compact(r.gadget)
414+
end
415+
@eval function source_entry_to_configs(r::$T)
416+
return source_entry_to_configs(r.gadget)
417+
end
412418
end
413419

414420
function _apply_transform(r::RotatedGadget, locs, center)

src/mapping.jl

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct UGrid
2525
end
2626

2727
export coordinates
28-
Base.:(==)(ug::UGrid, ug2::UGrid) = ug.n == ug2.n && ug.content == ug2.content
28+
Base.:(==)(ug::UGrid, ug2::UGrid) = ug.lines == ug2.lines && ug.content == ug2.content
2929
padding(ug::UGrid) = ug.padding
3030
coordinates(ug::UGrid) = [ci.I for ci in findall(!iszero, ug.content)]
3131

@@ -137,7 +137,7 @@ function unapply_gadgets!(ug::UGrid, tape, configurations)
137137
unapply_gadget!(pattern, ug.content, i, j)
138138
end
139139
cfgs = map(configurations) do c
140-
map_config_copyback!(ug.n, c, ug.padding)
140+
map_config_copyback!(ug, c)
141141
end
142142
return ug, cfgs
143143
end
@@ -178,6 +178,33 @@ function map_config_back!(p::Pattern, i, j, configuration)
178178
return configuration
179179
end
180180

181+
function map_config_copyback!(ug::UGrid, c::AbstractMatrix)
182+
res = zeros(Int, length(ug.lines))
183+
for line in ug.lines
184+
locs = copyline_locations(line; padding=ug.padding)
185+
count = 0
186+
for (iloc, loc) in enumerate(locs)
187+
gi, ci = ug.content[loc...], c[loc...]
188+
if gi == 2
189+
if ci == 2
190+
count += 1
191+
elseif ci == 0
192+
count += 0
193+
else # ci = 1
194+
if c[locs[iloc-1]...] == 0 && c[locs[iloc+1]...] == 0
195+
count += 1
196+
end
197+
end
198+
elseif abs(gi) == 1
199+
count += ci
200+
else
201+
error("check your grid at location ($(locs...))!")
202+
end
203+
end
204+
res[line.vertex] = count - (length(locs) ÷ 2)
205+
end
206+
return res
207+
end
181208
function map_config_copyback!(n::Int, c::AbstractMatrix, padding::Int)
182209
store = copy(c)
183210
s = 4
@@ -352,3 +379,11 @@ function embed_graph2(g::SimpleGraph)
352379
return ug
353380
end
354381

382+
export mis_overhead_copylines
383+
function mis_overhead_copylines(ug::UGrid)
384+
sum(ug.lines) do line
385+
locs = copyline_locations(line; padding=ug.padding)
386+
@assert length(locs) % 2 == 1
387+
length(locs) ÷ 2
388+
end
389+
end

test/mapping.jl

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@ using Graphs, GraphTensorNetworks
33

44
@testset "crossing connect count" begin
55
g = smallgraph(:bull)
6-
ug = embed_graph(g)
7-
for (s, c) in zip([Cross{false}(), Cross{true}(), TShape{true}(), TShape{false}(),
8-
Turn()], [2,4,1,3,4])
6+
ug = embed_graph2(g)
7+
for (s, c) in zip([
8+
Cross{false}(), Cross{true}(), TShape{false}(), TShape{true}(),
9+
Turn(), WTurn(), Branch(), BranchFix(), TCon(), TrivialTurn(),
10+
RotatedGadget(TCon(), 1), ReflectedGadget(Cross{true}(), "y"),
11+
ReflectedGadget(TrivialTurn(), "y"), BranchFixB(),
12+
ReflectedGadget(RotatedGadget(TCon(), 1), "y"),],
13+
[1,0,0,0,
14+
1,1,0,1,1,1,
15+
0, 0,
16+
2, 0,
17+
1,
18+
])
919
@show s
1020
@test sum(match.(Ref(s), Ref(ug.content), (0:size(ug.content, 1))', 0:size(ug.content,2))) == c
1121
end
12-
mug, tape = apply_gadgets!(copy(ug))
22+
mug, tape = apply_crossing_gadgets!(copy(ug))
1323
for s in [Cross{false}(), Cross{true}(), TShape{true}(), TShape{false}(),
1424
Turn()]
1525
@test sum(match.(Ref(s), Ref(mug.content), (0:size(mug.content, 1))', 0:size(mug.content,2))) == 0
@@ -20,11 +30,12 @@ using Graphs, GraphTensorNetworks
2030
end
2131

2232
@testset "map configurations back" begin
23-
for g in [smallgraph(:petersen), smallgraph(:bull), smallgraph(:cubical), smallgraph(:house), smallgraph(:diamond)]
24-
@show g
25-
ug = embed_graph(g)
26-
mis_overhead0 = 2 * nv(g) * (nv(g)-1) + nv(g)
27-
ug2, tape = apply_gadgets!(copy(ug))
33+
for graphname in [:petersen, :bull, :cubical, :house, :diamond]
34+
@show graphname
35+
g = smallgraph(graphname)
36+
ug = embed_graph2(g)
37+
mis_overhead0 = mis_overhead_copylines(ug)
38+
ug2, tape = apply_crossing_gadgets!(copy(ug))
2839
mis_overhead1 = sum(x->mis_overhead(x[1]), tape)
2940
missize_map = solve(Independence(SimpleGraph(ug2)), "size max"; optimizer=TreeSA(ntrials=1, niters=10), simplifier=MergeGreedy())[].n
3041
missize = solve(Independence(g), "size max")[].n

test/pathdecomposition/pathdecomposition.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Random, Test, UnitDiskMapping
2-
using UnitDiskMapping.PathDecomposition: branch_and_bound, vsep, Layout, addremove_format
2+
using UnitDiskMapping.PathDecomposition: branch_and_bound, vsep, Layout
33
using Graphs
44

55
@testset "B & B" begin
@@ -12,8 +12,8 @@ using Graphs
1212
L = branch_and_bound(gi)
1313
@test vsep(L) == 5
1414
@test vsep(Layout(gi, L.vertices)) == 5
15-
arem = addremove_format(L, g)
16-
@test sum(x->length(x[2]), arem) == nv(g)
15+
arem = UnitDiskMapping.remove_order(g, vertices(L))
16+
@test sum(length, arem) == nv(g)
1717
end
1818
g = smallgraph(:tutte)
1919
res = pathwidth(g, Branching())

0 commit comments

Comments
 (0)