Skip to content

Commit 10552d5

Browse files
committed
fix configuration and add a new example
1 parent fe73c8e commit 10552d5

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GraphTensorNetworks"
22
uuid = "0978c8c2-34f6-49c7-9826-ea2cc20dabd2"
33
authors = ["GiggleLiu <cacate0129@gmail.com> and contributors"]
4-
version = "0.2.0"
4+
version = "0.2.1"
55

66
[deps]
77
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"

examples/unitdisk_graph.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# # Solving independent set for a weighted unit disk graph
2+
using GraphTensorNetworks, Graphs
3+
4+
# This example shows how to check the MIS degeneracy (another way of saying counting) of a weight unit disk graph.
5+
# Let us construct a unit disk graph by specifying the locations of nodes, the unit disk radius is 1.5,
6+
# which means two vertices within distance 1.5 are connected.
7+
# For each vertex, we assign a weight to it.
8+
locations = [(6,-3),(1, -1), (0,0), (6,-2), (1,1), (2,0),
9+
(2,2), (2,-2), (3,1), (3,-2), (4,1), (4,-1),
10+
(5, 1), (5, -1), (6,0), (6,-1), (7,-1), (7, -4),
11+
(8, -2), (8, -4), (9,-3)]
12+
13+
14+
weights = [0.7439015222121296, 0.722970984162338,
15+
0.9502792990276312, 0.6617352332568173,
16+
0.8592866066961992, 1.0, 1.0, 1.0, 1.0,
17+
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
18+
1.0, 1.0, 1.0, 1.0, 1.0]
19+
20+
graph = unit_disk_graph(locations, 1.5)
21+
22+
show_graph(graph; locs=locations)
23+
24+
# ## Find the best configurations
25+
# We can easily check the MIS degeneracy by computing the [`CountingMax`](@ref) property.
26+
counting_mapped = solve(IndependentSet(graph; weights=weights), CountingMax())[]
27+
28+
# The MIS degeneracy is the second field, which is 3.
29+
# We can compute the [`ConfigsMax`](@ref) to enumerate all configurations with maximum independent set size.
30+
31+
configs_mapped = solve(IndependentSet(graph; weights=weights), ConfigsMax())[]
32+
33+
# * Solution 1
34+
show_graph(graph; locs=locations, vertex_colors=
35+
[iszero(configs_mapped.c[1][i]) ? "white" : "red" for i=1:nv(graph)])
36+
37+
# * Solution 2
38+
show_graph(graph; locs=locations, vertex_colors=
39+
[iszero(configs_mapped.c[2][i]) ? "white" : "red" for i=1:nv(graph)])
40+
41+
# * Solution 3
42+
show_graph(graph; locs=locations, vertex_colors=
43+
[iszero(configs_mapped.c[3][i]) ? "white" : "red" for i=1:nv(graph)])

src/bounding.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ function backward_tropical(mode, ixs, @nospecialize(xs::Tuple), iy, @nospecializ
2424
niy = ixs[i]
2525
if mode isa AllConfigs
2626
mask = zeros(Bool, size(xs[i]))
27-
mask .= inv.(einsum(EinCode(nixs, niy), nxs, size_dict)) .<= xs[i] .* Tropical(largest_k(mode)-1)
27+
# here, we set a threshold `1e-15` to avoid round off errors.
28+
mask .= inv.(einsum(EinCode(nixs, niy), nxs, size_dict)) .<= xs[i] .* Tropical(largest_k(mode)-1+1e-15)
2829
push!(masks, mask)
2930
elseif mode isa SingleConfig
3031
A = zeros(eltype(xs[i]), size(xs[i]))
@@ -43,7 +44,7 @@ function onehotmask(A::AbstractArray{T}, X::AbstractArray{T}) where T
4344
mask = falses(size(A)...)
4445
found = false
4546
@inbounds for j=1:length(A)
46-
if X[j] == inv(A[j]) && !found
47+
if X[j] inv(A[j]) && !found
4748
mask[j] = true
4849
found = true
4950
else

src/configurations.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ function best_solutions(gp::GraphProblem; all=false, usecuda=false)
1717
ymask = CuArray(ymask)
1818
end
1919
if all
20-
xs = generate_tensors(fx_solutions(gp, CountingTropical{Int64}, all), gp)
20+
# we use `Float64` types because we want to support weighted graphs.
21+
xs = generate_tensors(fx_solutions(gp, CountingTropical{Float64,Float64}, all), gp)
2122
return bounding_contract(AllConfigs{1}(), gp.code, xst, ymask, xs)
2223
else
2324
@assert ndims(ymask) == 0

src/networks/IndependentSet.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ end
1616

1717
function IndependentSet(g::SimpleGraph; weights=UnWeighted(), openvertices=(), optimizer=GreedyMethod(), simplifier=nothing)
1818
@assert weights isa UnWeighted || length(weights) == nv(g)
19-
rawcode = EinCode(([[i] for i in Graphs.vertices(g)]..., # labels for vertex tensors
20-
[[minmax(e.src,e.dst)...] for e in Graphs.edges(g)]...), collect(Int, openvertices)) # labels for edge tensors
19+
rawcode = EinCode([[[i] for i in Graphs.vertices(g)]..., # labels for vertex tensors
20+
[[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)
2222
IndependentSet(code, nv(g), weights)
2323
end

0 commit comments

Comments
 (0)