Skip to content

Commit 4bf7c81

Browse files
committed
test interfaces
1 parent 7a6e6eb commit 4bf7c81

File tree

10 files changed

+529
-161
lines changed

10 files changed

+529
-161
lines changed

notebooks/tropicalwidget_simple.jl

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
### A Pluto.jl notebook ###
2+
# v0.15.1
3+
4+
using Markdown
5+
using InteractiveUtils
6+
7+
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
8+
macro bind(def, element)
9+
quote
10+
local el = $(esc(element))
11+
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
12+
el
13+
end
14+
end
15+
16+
# ╔═╡ 53ce616e-8055-11eb-0caf-e968c041ff4f
17+
begin
18+
using Revise
19+
using Pkg
20+
Pkg.activate(dirname(pwd()))
21+
using LightGraphs # graph support
22+
using PlutoUI
23+
using Viznet, Compose
24+
using TropicalNumbers, OMEinsum, GraphTensorNetworks#, TropicalGEMM
25+
end
26+
27+
# ╔═╡ 100e3eb1-9d5c-4ae0-9a1b-d279927443fc
28+
md"## We have a unit disk gadget"
29+
30+
# ╔═╡ efdc5d3e-58e0-410d-b15a-2363e9c9f156
31+
locs = let
32+
a = 0.12
33+
ymid = xmid = 0.5
34+
X = 0.33
35+
Y = 0.17
36+
D = 0.15
37+
y = [ymid-Y, ymid-Y+D, ymid-a/2, ymid+a/2, ymid+Y-D, ymid+Y]
38+
x = [xmid-X, xmid-X+D, xmid-1.5a, xmid-a/2, xmid+a/2, xmid+1.5a, xmid+X-D, xmid+X]
39+
xmin, xmax, ymin, ymax = x[1], x[end], y[1], y[end]
40+
[(xmid, y[1]), (xmin, ymid), (xmid, ymax), (xmax, ymid),
41+
(x[3], y[3]), (x[4], y[3]),
42+
(x[5], y[3]), (x[6], y[3]), (x[3], y[4]),
43+
(x[4], y[4]), (x[5], y[4]), (x[6], y[4])]
44+
end
45+
46+
# ╔═╡ 9d0ba8b7-5a34-418d-97cf-863f376f8453
47+
graph = unitdisk_graph(locs, 0.23) # SimpleGraph
48+
49+
# ╔═╡ 78ee8772-83e4-40fb-8151-0123370481d9
50+
vizconfig(graph; locs=locs, config=rand(Bool, 12), graphsize=8cm)
51+
52+
# ╔═╡ e068f0b8-7b2c-49b2-94d1-83dead406326
53+
gp = Independence(graph; outputs=(1,2,3,4), optmethod=:greedy)
54+
55+
# ╔═╡ 90c88c7f-e0c7-4845-a8b9-7f7562bd256f
56+
solve(gp, :config_single)
57+
58+
# ╔═╡ f657f321-255e-44f2-a1d5-9093fa8eca28
59+
md"## Obtaining configurations"
60+
61+
# ╔═╡ fdbe425d-5539-4a38-8f8f-aa98fb20ce64
62+
@doc run_task
63+
64+
# ╔═╡ adb6a76a-18a2-46a8-8ad6-352cac1d2efc
65+
res_configs = solve(gp, :config_all);
66+
67+
# ╔═╡ 6ae40df1-f576-44f0-9d48-561dab3cb899
68+
md"""
69+
$(@bind s1 CheckBox()) ``s_1``
70+
$(@bind s2 CheckBox()) ``s_2``
71+
$(@bind s3 CheckBox()) ``s_3``
72+
$(@bind s4 CheckBox()) ``s_4``
73+
"""
74+
75+
# ╔═╡ 21aa560c-d2f0-467b-a0bf-7b77024aeeaa
76+
let
77+
n = length(res_configs[s1+1,s2+1,s3+1,s4+1].c.data)
78+
md"$(@bind index Slider(1:n; show_value=true, default=n))"
79+
end
80+
81+
# ╔═╡ caa3bc03-f6ff-4ebe-9608-518b8924adae
82+
vizconfig(graph; locs=locs,config=res_configs[s1+1,s2+1,s3+1,s4+1].c.data[index], graphsize=8cm)
83+
84+
# ╔═╡ Cell order:
85+
# ╠═53ce616e-8055-11eb-0caf-e968c041ff4f
86+
# ╟─100e3eb1-9d5c-4ae0-9a1b-d279927443fc
87+
# ╠═efdc5d3e-58e0-410d-b15a-2363e9c9f156
88+
# ╠═9d0ba8b7-5a34-418d-97cf-863f376f8453
89+
# ╠═78ee8772-83e4-40fb-8151-0123370481d9
90+
# ╠═e068f0b8-7b2c-49b2-94d1-83dead406326
91+
# ╠═90c88c7f-e0c7-4845-a8b9-7f7562bd256f
92+
# ╟─f657f321-255e-44f2-a1d5-9093fa8eca28
93+
# ╟─fdbe425d-5539-4a38-8f8f-aa98fb20ce64
94+
# ╠═adb6a76a-18a2-46a8-8ad6-352cac1d2efc
95+
# ╟─6ae40df1-f576-44f0-9d48-561dab3cb899
96+
# ╟─21aa560c-d2f0-467b-a0bf-7b77024aeeaa
97+
# ╠═caa3bc03-f6ff-4ebe-9608-518b8924adae

notebooks/tutorial.jl

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
### A Pluto.jl notebook ###
2+
# v0.15.1
3+
4+
using Markdown
5+
using InteractiveUtils
6+
7+
# ╔═╡ 54d71646-ffad-11eb-3866-5f1c0bc5d0bf
8+
begin
9+
using Pkg
10+
Pkg.activate(dirname(pwd()))
11+
using Revise, GraphTensorNetworks
12+
end
13+
14+
# ╔═╡ 83fc0a3c-ef51-4d5c-9c46-baa645f80fd2
15+
locs = [rand(2) for i=1:70];
16+
17+
# ╔═╡ 1522938d-d60b-4133-8625-5a09615a7b26
18+
g = unitdisk_graph(locs, 0.2)
19+
20+
# ╔═╡ 05ac5610-4a1d-4433-ba0d-1d13fd8a6733
21+
vizconfig(g; locs=locs, unit=0.5)
22+
23+
# ╔═╡ b6e916e2-7d43-4085-a3c8-5c57069e8384
24+
gp = Independence(g; optmethod=:auto);
25+
26+
# ╔═╡ 03840394-3a5b-4c24-8b52-15f7e400f900
27+
timespace_complexity(gp)
28+
29+
# ╔═╡ 11c71de9-caa9-40c6-84cb-a0f29d873805
30+
results = solutions(gp, CountingTropical{Float64}; all=false)
31+
32+
# ╔═╡ edfc5600-9a4a-4657-b165-8eb3e8540edf
33+
vizconfig(g; locs=locs, unit=0.5, config=results[].c.data)
34+
35+
# ╔═╡ 3666f629-7a87-4b50-aaf1-cc128d598aaf
36+
md"## Different Algebras"
37+
38+
# ╔═╡ 51f0f187-d14d-48ab-ab30-96e71d796959
39+
md"There are commutative semiring algebras defined in the package. For each type, we have implemented two operations `+` and `*` and two special elements `zero` and `one` on it."
40+
41+
# ╔═╡ a5b63d08-2917-4e9f-9c5c-b5f17181d2aa
42+
md"### Polynomials
43+
`Polynomial` type is defined in package [Polynomials.jl](https://github.com/JuliaMath/Polynomials.jl)
44+
"
45+
46+
# ╔═╡ 0d3b2752-c800-43f3-b241-9c96084f1fc9
47+
xp, yp = Polynomial([1.0, 2.0, 3.0]), Polynomial([2.0, 1.0])
48+
49+
# ╔═╡ e779a111-ba70-4350-88bc-2b94d11741c6
50+
xp + yp
51+
52+
# ╔═╡ 677895f4-be68-4565-97ca-6f4e31c56819
53+
xp * yp
54+
55+
# ╔═╡ e1bc2d30-919a-4675-a674-c82c534c4ccf
56+
zero(Polynomial{Float64,:x})
57+
58+
# ╔═╡ c560a001-8fbb-4fbb-94fc-c68cf5e67c1e
59+
one(Polynomial{Float64,:x})
60+
61+
# ╔═╡ ae3c308d-40cc-4cbe-ac37-f0f2cd0eecbe
62+
md"### Polynomial truncated to largest two orders"
63+
64+
# ╔═╡ 69779c4f-b939-4d0c-8548-1dc31578949c
65+
xp2, yp2 = Max2Poly(2.0, 3.0, 2.0), Max2Poly(2.0, 1.0, 1.0)
66+
67+
# ╔═╡ b7f7453f-445a-40e3-9639-1c93e5b257ff
68+
zero(Max2Poly{Float64,Float64})
69+
70+
# ╔═╡ 9da24765-b182-4f60-a318-d7a808e2872a
71+
one(Max2Poly{Float64,Float64})
72+
73+
# ╔═╡ 731012e5-257f-44e1-8888-16a6cbe50279
74+
zero(Max2Poly{Float64,Float64})
75+
76+
# ╔═╡ 2761c355-2b10-4437-8496-a0c260bbac2c
77+
md"### Tropical algebra"
78+
79+
# ╔═╡ 7c584a2c-ab29-4e04-971b-06aeeb61fd0f
80+
xt, yt = Tropical(2.0), Tropical(3.0)
81+
82+
# ╔═╡ e5b166f4-7307-4712-8790-0eb914ff839c
83+
xt + yt # same as regular `max`
84+
85+
# ╔═╡ 50dadd8b-e73f-461b-baa9-8e1c5052e7fd
86+
xt * yt # same as regular `+`
87+
88+
# ╔═╡ ff6ffae0-dfd4-44ba-bcfc-02435ef4221a
89+
zero(Tropical{Float64})
90+
91+
# ╔═╡ ab5b237c-b4aa-4cc9-91ca-b51a3d3d9d0e
92+
one(Tropical{Float64})
93+
94+
# ╔═╡ ce303530-bbe8-43fc-add0-2481f78a2e9f
95+
md"### Tropical algebra with counting"
96+
97+
# ╔═╡ aefff7b7-f372-4eba-b9c9-effbb547665e
98+
xct, yct = CountingTropical(2.0, 2.0), CountingTropical(3.0, 5.0)
99+
100+
# ╔═╡ 585d7ccb-5f23-4dc0-a482-076721f0be27
101+
xct + yct
102+
103+
# ╔═╡ 82a2eee5-451e-4679-a11a-74a690c13817
104+
xct * yct
105+
106+
# ╔═╡ b26358f3-d752-4df3-9a50-526a91615796
107+
zero(CountingTropical{Float64,Float64})
108+
109+
# ╔═╡ be5b52c5-787f-4521-b14a-25f54b439d79
110+
one(CountingTropical{Float64,Float64})
111+
112+
# ╔═╡ 539503f4-68f5-4f83-918f-d21163e4df6f
113+
md"### Configuration and Set algebra"
114+
115+
# ╔═╡ a77d1d41-3964-41cb-ad1a-71db411537ee
116+
md"Let us first define bit strings"
117+
118+
# ╔═╡ 6fd32061-0a0c-46b7-9bcb-00736d372f8c
119+
bs1, bs2, bs3 = StaticBitVector([1,0,1,1,0]), StaticBitVector([0,0,0,1,1]), StaticBitVector([1,1,1,1,0])
120+
121+
# ╔═╡ 2a800fa6-4967-4d01-aac1-548f852e7cad
122+
cs1, cs2 = ConfigSampler(bs1), ConfigSampler(bs2)
123+
124+
# ╔═╡ 74203e2b-b231-4a45-8d74-a3ee3299cad8
125+
cs1 + cs2
126+
127+
# ╔═╡ 6392edb7-c86c-40e5-8dc1-cd3cbbf436c2
128+
cs1 * cs2
129+
130+
# ╔═╡ 97b8e445-99b5-488e-9437-76618cf6321e
131+
zero(cs1)
132+
133+
# ╔═╡ 07f2dff4-d9de-49a2-b8f4-e29b43df5e23
134+
one(cs2)
135+
136+
# ╔═╡ 31f866f8-9ad0-439d-8c08-29a27efd3948
137+
ce1, ce2 = ConfigEnumerator([bs1, bs2]), ConfigEnumerator([bs3])
138+
139+
# ╔═╡ ee28ad87-479e-49e0-a534-6bfca71dfb96
140+
ce1 + ce2
141+
142+
# ╔═╡ f783b71b-5b7e-457a-be66-015f0e602ea2
143+
ce1 * ce2
144+
145+
# ╔═╡ 554cebaf-bbe8-4a02-905b-e4b508e0c064
146+
zero(typeof(ce1))
147+
148+
# ╔═╡ 12619536-8ea4-465b-b60d-3230bfb7b570
149+
one(typeof(ce2))
150+
151+
# ╔═╡ Cell order:
152+
# ╠═54d71646-ffad-11eb-3866-5f1c0bc5d0bf
153+
# ╠═83fc0a3c-ef51-4d5c-9c46-baa645f80fd2
154+
# ╠═1522938d-d60b-4133-8625-5a09615a7b26
155+
# ╠═05ac5610-4a1d-4433-ba0d-1d13fd8a6733
156+
# ╠═b6e916e2-7d43-4085-a3c8-5c57069e8384
157+
# ╠═03840394-3a5b-4c24-8b52-15f7e400f900
158+
# ╠═11c71de9-caa9-40c6-84cb-a0f29d873805
159+
# ╠═edfc5600-9a4a-4657-b165-8eb3e8540edf
160+
# ╟─3666f629-7a87-4b50-aaf1-cc128d598aaf
161+
# ╟─51f0f187-d14d-48ab-ab30-96e71d796959
162+
# ╟─a5b63d08-2917-4e9f-9c5c-b5f17181d2aa
163+
# ╠═0d3b2752-c800-43f3-b241-9c96084f1fc9
164+
# ╠═e779a111-ba70-4350-88bc-2b94d11741c6
165+
# ╠═677895f4-be68-4565-97ca-6f4e31c56819
166+
# ╠═e1bc2d30-919a-4675-a674-c82c534c4ccf
167+
# ╠═c560a001-8fbb-4fbb-94fc-c68cf5e67c1e
168+
# ╟─ae3c308d-40cc-4cbe-ac37-f0f2cd0eecbe
169+
# ╠═69779c4f-b939-4d0c-8548-1dc31578949c
170+
# ╠═b7f7453f-445a-40e3-9639-1c93e5b257ff
171+
# ╠═9da24765-b182-4f60-a318-d7a808e2872a
172+
# ╠═731012e5-257f-44e1-8888-16a6cbe50279
173+
# ╟─2761c355-2b10-4437-8496-a0c260bbac2c
174+
# ╠═7c584a2c-ab29-4e04-971b-06aeeb61fd0f
175+
# ╠═e5b166f4-7307-4712-8790-0eb914ff839c
176+
# ╠═50dadd8b-e73f-461b-baa9-8e1c5052e7fd
177+
# ╠═ff6ffae0-dfd4-44ba-bcfc-02435ef4221a
178+
# ╠═ab5b237c-b4aa-4cc9-91ca-b51a3d3d9d0e
179+
# ╟─ce303530-bbe8-43fc-add0-2481f78a2e9f
180+
# ╠═aefff7b7-f372-4eba-b9c9-effbb547665e
181+
# ╠═585d7ccb-5f23-4dc0-a482-076721f0be27
182+
# ╠═82a2eee5-451e-4679-a11a-74a690c13817
183+
# ╠═b26358f3-d752-4df3-9a50-526a91615796
184+
# ╠═be5b52c5-787f-4521-b14a-25f54b439d79
185+
# ╟─539503f4-68f5-4f83-918f-d21163e4df6f
186+
# ╟─a77d1d41-3964-41cb-ad1a-71db411537ee
187+
# ╠═6fd32061-0a0c-46b7-9bcb-00736d372f8c
188+
# ╠═2a800fa6-4967-4d01-aac1-548f852e7cad
189+
# ╠═74203e2b-b231-4a45-8d74-a3ee3299cad8
190+
# ╠═6392edb7-c86c-40e5-8dc1-cd3cbbf436c2
191+
# ╠═97b8e445-99b5-488e-9437-76618cf6321e
192+
# ╠═07f2dff4-d9de-49a2-b8f4-e29b43df5e23
193+
# ╠═31f866f8-9ad0-439d-8c08-29a27efd3948
194+
# ╠═ee28ad87-479e-49e0-a534-6bfca71dfb96
195+
# ╠═f783b71b-5b7e-457a-be66-015f0e602ea2
196+
# ╠═554cebaf-bbe8-4a02-905b-e4b508e0c064
197+
# ╠═12619536-8ea4-465b-b60d-3230bfb7b570

src/GraphTensorNetworks.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ using OMEinsumContractionOrders: OMEinsum
44
using Core: Argument
55
using TropicalGEMM, TropicalNumbers
66
using OMEinsum
7-
using OMEinsum: flatten
7+
using OMEinsum: flatten, timespace_complexity
8+
using LightGraphs
9+
10+
export timespace_complexity
811

912
# patches for OMEinsum
1013
OMEinsum.asarray(x, ::AbstractArray) = fill(x)
@@ -13,6 +16,7 @@ OMEinsum.dynamic_einsum(::EinCode{ixs, iy}, xs; kwargs...) where {ixs, iy} = dyn
1316
project_relative_path(xs...) = normpath(joinpath(dirname(dirname(pathof(@__MODULE__))), xs...))
1417

1518
include("arithematics.jl")
19+
include("networks.jl")
1620
include("graph_polynomials.jl")
1721
include("configurations.jl")
1822
include("graphs.jl")

src/arithematics.jl

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export is_commutative_semiring
2-
export Max2Poly, Polynomial, Tropical, CountingTropical, StaticBitVector, Mod, ConfigEnumerator, onehotv
2+
export Max2Poly, Polynomial, Tropical, CountingTropical, StaticBitVector, Mod, ConfigEnumerator, onehotv, ConfigSampler
33
export bitstringset_type, bitstringsampler_type
44

55
using Polynomials: Polynomial
@@ -63,10 +63,10 @@ function is_commutative_semiring(a::T, b::T, c::T) where T
6363
end
6464

6565
# get maximum two countings (polynomial truncated to largest two orders)
66-
struct Max2Poly{T} <: Number
66+
struct Max2Poly{T,TO} <: Number
6767
a::T
6868
b::T
69-
maxorder::Float64
69+
maxorder::TO
7070
end
7171

7272
function Base.:+(a::Max2Poly, b::Max2Poly)
@@ -88,10 +88,19 @@ function Base.:*(a::Max2Poly, b::Max2Poly)
8888
Max2Poly(a.a*b.b + a.b*b.a, a.b * b.b, maxorder)
8989
end
9090

91-
Base.zero(::Type{Max2Poly{T}}) where T = Max2Poly(zero(T), zero(T), -Inf)
92-
Base.one(::Type{Max2Poly{T}}) where T = Max2Poly(zero(T), one(T), 0.0)
93-
Base.zero(::Max2Poly{T}) where T = zero(Max2Poly{T})
94-
Base.one(::Max2Poly{T}) where T = one(Max2Poly{T})
91+
Base.zero(::Type{Max2Poly{T,TO}}) where {T,TO} = Max2Poly(zero(T), zero(T), zero(Tropical{TO}).n)
92+
Base.one(::Type{Max2Poly{T,TO}}) where {T,TO} = Max2Poly(zero(T), one(T), zero(TO))
93+
Base.zero(::Max2Poly{T,TO}) where {T,TO} = zero(Max2Poly{T,TO})
94+
Base.one(::Max2Poly{T,TO}) where {T,TO} = one(Max2Poly{T,TO})
95+
96+
Base.show(io::IO, x::Max2Poly) = show(io, MIME"text/plain"(), x)
97+
function Base.show(io::IO, ::MIME"text/plain", x::Max2Poly)
98+
if isinf(x.maxorder)
99+
print(io, 0)
100+
else
101+
printpoly(io, Polynomial([x.a, x.b], :x), offset=Int(x.maxorder-1))
102+
end
103+
end
95104

96105
struct ConfigEnumerator{N,C}
97106
data::Vector{StaticBitVector{N,C}}
@@ -150,12 +159,17 @@ function Base.:*(a::Int, y::ConfigEnumerator)
150159
a == 1 && return y
151160
error("multiplication between int and config enumerator is not defined.")
152161
end
162+
function Base.:*(a::Int, y::ConfigSampler)
163+
a == 0 && return zero(y)
164+
a == 1 && return y
165+
error("multiplication between int and config sampler is not defined.")
166+
end
153167

154168
# convert from counting type to bitstring type
155169
for (F,TP) in [(:bitstringset_type, :ConfigEnumerator), (:bitstringsampler_type, :ConfigSampler)]
156170
@eval begin
157-
function $F(::Type{T}, n::Int) where {T<:Max2Poly}
158-
Max2Poly{$F(n)}
171+
function $F(::Type{T}, n::Int) where {OT, T<:Max2Poly{C,OT} where C}
172+
Max2Poly{$F(n),OT}
159173
end
160174
function $F(::Type{T}, n::Int) where {TX, T<:Polynomial{C,TX} where C}
161175
Polynomial{$F(n),:x}
@@ -174,8 +188,8 @@ end
174188
function onehotv(::Type{Polynomial{BS,X}}, x) where {BS,X}
175189
Polynomial{BS,X}([zero(BS), onehotv(BS, x)])
176190
end
177-
function onehotv(::Type{Max2Poly{BS}}, x) where {BS}
178-
Max2Poly{BS}(zero(BS), onehotv(BS, x),1)
191+
function onehotv(::Type{Max2Poly{BS,OS}}, x) where {BS,OS}
192+
Max2Poly{BS,OS}(zero(BS), onehotv(BS, x),one(OS))
179193
end
180194
function onehotv(::Type{CountingTropical{TV,BS}}, x) where {TV,BS}
181195
CountingTropical{TV,BS}(one(TV), onehotv(BS, x))

0 commit comments

Comments
 (0)