-
Notifications
You must be signed in to change notification settings - Fork 20
/
AlgebraicPetri.jl
297 lines (246 loc) · 10.6 KB
/
AlgebraicPetri.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
""" Computing in the category of finite sets and Petri cospans
"""
module AlgebraicPetri
export TheoryPetriNet, PetriNet, OpenPetriNetOb, AbstractPetriNet, ns, nt, ni, no,
add_species!, add_transition!, add_transitions!,
add_input!, add_inputs!, add_output!, add_outputs!, inputs, outputs,
TransitionMatrices, vectorfield,
TheoryLabelledPetriNet, LabelledPetriNet, AbstractLabelledPetriNet, sname, tname,
TheoryReactionNet, ReactionNet, AbstractReactionNet, concentration, concentrations, rate, rates,
TheoryLabelledReactionNet, LabelledReactionNet, AbstractLabelledReactionNet,
Open, OpenPetriNet, OpenLabelledPetriNet, OpenReactionNet, OpenLabelledReactionNet,
OpenPetriNetOb, OpenLabelledPetriNetOb, OpenReactionNetOb, OpenLabelledReactionNetOb
using Catlab
using Catlab.CategoricalAlgebra
using Catlab.CategoricalAlgebra.FinSets
using Catlab.Present
using Catlab.Theories
using Petri
using LabelledArrays
using LinearAlgebra: mul!
import Petri: Model, Graph, vectorfield
vectorify(n::Vector) = n
vectorify(n::Tuple) = length(n) == 1 ? [n] : n
vectorify(n) = [n]
state_dict(n) = Dict(s=>i for (i, s) in enumerate(n))
# Petri Nets
############
@present TheoryPetriNet(FreeSchema) begin
T::Ob
S::Ob
I::Ob
O::Ob
it::Hom(I,T)
is::Hom(I,S)
ot::Hom(O,T)
os::Hom(O,S)
end
const AbstractPetriNet = AbstractACSetType(TheoryPetriNet)
const PetriNet = CSetType(TheoryPetriNet,index=[:it,:is,:ot,:os])
const OpenPetriNetOb, OpenPetriNet = OpenCSetTypes(PetriNet,:S)
Open(p::AbstractPetriNet) = OpenPetriNet(p, map(x->FinFunction([x], ns(p)), 1:ns(p))...)
Open(p::AbstractPetriNet, legs...) = OpenPetriNet(p, map(l->FinFunction(l, ns(p)), legs)...)
Open(n, p::AbstractPetriNet, m) = Open(p, n, m)
# PetriNet([:S, :I, :R], :infection=>((1, 2), 3))
PetriNet(n::Int, ts::Vararg{Union{Pair,Tuple}}) = begin
p = PetriNet()
add_species!(p, n)
add_transitions!(p, length(ts))
for (i,(ins,outs)) in enumerate(ts)
ins = vectorify(ins)
outs = vectorify(outs)
add_inputs!(p, length(ins), repeat([i], length(ins)), collect(ins))
add_outputs!(p, length(outs), repeat([i], length(outs)), collect(outs))
end
p
end
ns(p::AbstractPetriNet) = nparts(p,:S)
nt(p::AbstractPetriNet) = nparts(p,:T)
ni(p::AbstractPetriNet) = nparts(p,:I)
no(p::AbstractPetriNet) = nparts(p,:O)
add_species!(p::AbstractPetriNet;kw...) = add_part!(p,:S;kw...)
add_species!(p::AbstractPetriNet,n;kw...) = add_parts!(p,:S,n;kw...)
add_transition!(p::AbstractPetriNet;kw...) = add_part!(p,:T;kw...)
add_transitions!(p::AbstractPetriNet,n;kw...) = add_parts!(p,:T,n;kw...)
add_input!(p::AbstractPetriNet,t,s;kw...) = add_part!(p,:I;it=t,is=s,kw...)
add_inputs!(p::AbstractPetriNet,n,t,s;kw...) = add_parts!(p,:I,n;it=t,is=s,kw...)
add_output!(p::AbstractPetriNet,t,s;kw...) = add_part!(p,:O;ot=t,os=s,kw...)
add_outputs!(p::AbstractPetriNet,n,t,s;kw...) = add_parts!(p,:O,n;ot=t,os=s,kw...)
sname(p::AbstractPetriNet,s) = (1:ns(p))[s]
tname(p::AbstractPetriNet,t) = (1:nt(p))[t]
# Note: although indexing makes this pretty fast, it is often faster to bulk-convert
# the PetriNet net into a transition matrix, if you are working with all of the transitions
inputs(p::AbstractPetriNet,t) = subpart(p,incident(p,t,:it),:is)
outputs(p::AbstractPetriNet,t) = subpart(p,incident(p,t,:ot),:os)
struct TransitionMatrices
input::Matrix{Int}
output::Matrix{Int}
TransitionMatrices(p::AbstractPetriNet) = begin
input, output = zeros(Int,(nt(p),ns(p))), zeros(Int,(nt(p),ns(p)))
for i in 1:ni(p)
input[subpart(p,i,:it),subpart(p,i,:is)] += 1
end
for o in 1:no(p)
output[subpart(p,o,:ot),subpart(p,o,:os)] += 1
end
new(input,output)
end
end
PetriNet(tm::TransitionMatrices) = begin
(m,n) = size(tm.input)
p = PetriNet()
add_species!(p,n)
add_transitions!(p,m)
for i in 1:m
for j in 1:n
add_inputs!(p,tm.input[i,j],i,j)
add_outputs!(p,tm.output[i,j],i,j)
end
end
p
end
valueat(x::Number, u, t) = x
valueat(f::Function, u, t) = try f(u,t) catch e f(t) end
vectorfield(pn::AbstractPetriNet) = begin
tm = TransitionMatrices(pn)
dt = tm.output - tm.input
log_rates = zeros(nt(pn))
f(du,u,p,t) = begin
u_m = [u[sname(pn, i)] for i in 1:ns(pn)]
p_m = [p[tname(pn, i)] for i in 1:nt(pn)]
for i in 1:nt(pn)
log_rates[i] = valueat(p_m[i],u,t) * prod(u_m[j] ^ tm.input[i,j] for j in 1:ns(pn))
end
for j in 1:ns(pn)
du[sname(pn, j)] = sum(log_rates[i] * dt[i,j] for i in 1:nt(pn))
end
return du
end
return f
end
@present TheoryLabelledPetriNet <: TheoryPetriNet begin
Name::Data
tname::Attr(T, Name)
sname::Attr(S, Name)
end
const AbstractLabelledPetriNet = AbstractACSetType(TheoryLabelledPetriNet)
const LabelledPetriNetUntyped = ACSetType(TheoryLabelledPetriNet, index=[:it,:is,:ot,:os])
const LabelledPetriNet = LabelledPetriNetUntyped{Symbol}
const OpenLabelledPetriNetObUntyped, OpenLabelledPetriNetUntyped = OpenACSetTypes(LabelledPetriNetUntyped,:S)
const OpenLabelledPetriNetOb, OpenLabelledPetriNet = OpenLabelledPetriNetObUntyped{Symbol}, OpenLabelledPetriNetUntyped{Symbol}
Open(p::AbstractLabelledPetriNet) = OpenLabelledPetriNet(p, map(x->FinFunction([x], ns(p)), 1:ns(p))...)
Open(p::AbstractLabelledPetriNet, legs...) = begin
s_idx = Dict(sname(p, s)=>s for s in 1:ns(p))
OpenLabelledPetriNet(p, map(l->FinFunction(map(i->s_idx[i], l), ns(p)),legs)...)
end
Open(n, p::AbstractLabelledPetriNet, m) = Open(p, n, m)
LabelledPetriNet(n, ts::Vararg{Union{Pair,Tuple}}) = begin
p = LabelledPetriNet()
n = vectorify(n)
state_idx = state_dict(n)
add_species!(p, length(n), sname=n)
for (name,(ins,outs)) in ts
i = add_transition!(p, tname=name)
ins = vectorify(ins)
outs = vectorify(outs)
add_inputs!(p, length(ins), repeat([i], length(ins)), map(x->state_idx[x], collect(ins)))
add_outputs!(p, length(outs), repeat([i], length(outs)), map(x->state_idx[x], collect(outs)))
end
p
end
# Reaction Nets
###############
@present TheoryReactionNet <: TheoryPetriNet begin
Rate::Data
Concentration::Data
rate::Attr(T, Rate)
concentration::Attr(S, Concentration)
end
const AbstractReactionNet = AbstractACSetType(TheoryReactionNet)
const ReactionNet = ACSetType(TheoryReactionNet, index=[:it,:is,:ot,:os])
const OpenReactionNetOb, OpenReactionNet = OpenACSetTypes(ReactionNet,:S)
Open(p::AbstractReactionNet{R,C}) where {R,C} = OpenReactionNet{R,C}(p, map(x->FinFunction([x], ns(p)), 1:ns(p))...)
Open(p::AbstractReactionNet{R,C}, legs...) where {R,C} = OpenReactionNet{R,C}(p, map(l->FinFunction(l, ns(p)), legs)...)
Open(n, p::AbstractReactionNet, m) = Open(p, n, m)
ReactionNet{R,C}(n, ts::Vararg{Union{Pair,Tuple}}) where {R,C} = begin
p = ReactionNet{R,C}()
add_species!(p, length(n), concentration=n)
for (i, (rate,(ins,outs))) in enumerate(ts)
i = add_transition!(p, rate=rate)
ins = vectorify(ins)
outs = vectorify(outs)
add_inputs!(p, length(ins), repeat([i], length(ins)), collect(ins))
add_outputs!(p, length(outs), repeat([i], length(outs)), collect(outs))
end
p
end
concentration(p::AbstractReactionNet,s) = subpart(p,s,:concentration)
rate(p::AbstractReactionNet,t) = subpart(p,t,:rate)
concentrations(p::AbstractReactionNet) = map(s->concentration(p, s), 1:ns(p))
rates(p::AbstractReactionNet) = map(t->rate(p, t), 1:nt(p))
@present TheoryLabelledReactionNet <: TheoryReactionNet begin
Name::Data
tname::Attr(T, Name)
sname::Attr(S, Name)
end
const AbstractLabelledReactionNet = AbstractACSetType(TheoryLabelledReactionNet)
const LabelledReactionNetUntyped = ACSetType(TheoryLabelledReactionNet, index=[:it,:is,:ot,:os])
const LabelledReactionNet{R,C} = LabelledReactionNetUntyped{R,C,Symbol}
const OpenLabelledReactionNetObUntyped, OpenLabelledReactionNetUntyped = OpenACSetTypes(LabelledReactionNetUntyped,:S)
const OpenLabelledReactionNetOb{R,C} = OpenLabelledReactionNetObUntyped{R,C,Symbol}
const OpenLabelledReactionNet{R,C} = OpenLabelledReactionNetUntyped{R,C,Symbol}
Open(p::AbstractLabelledReactionNet{R,C}) where {R,C} = OpenLabelledReactionNet{R,C}(p, map(x->FinFunction([x], ns(p)), 1:ns(p))...)
Open(p::AbstractLabelledReactionNet{R,C}, legs...) where {R,C} = begin
s_idx = Dict(sname(p, s)=>s for s in 1:ns(p))
OpenLabelledReactionNet{R,C}(p, map(l->FinFunction(map(i->s_idx[i], l), ns(p)), legs)...)
end
Open(n, p::AbstractLabelledReactionNet, m) = Open(p, n, m)
# Ex. LabelledReactionNet{Number, Int}((:S=>990, :I=>10, :R=>0), (:inf, .3/1000)=>((:S, :I)=>(:I,:I)), (:rec, .2)=>(:I=>:R))
LabelledReactionNet{R,C}(n, ts::Vararg{Union{Pair,Tuple}}) where {R,C} = begin
p = LabelledReactionNet{R,C}()
n = vectorify(n)
states = map(first, collect(n))
concentrations = map(last, collect(n))
state_idx = state_dict(states)
add_species!(p, length(states), concentration=concentrations, sname=states)
for (i, ((name,rate),(ins,outs))) in enumerate(ts)
i = add_transition!(p,rate=rate, tname=name)
ins = vectorify(ins)
outs = vectorify(outs)
add_inputs!(p, length(ins), repeat([i], length(ins)), map(x->state_idx[x], collect(ins)))
add_outputs!(p, length(outs), repeat([i], length(outs)), map(x->state_idx[x], collect(outs)))
end
p
end
sname(p::Union{AbstractLabelledPetriNet, AbstractLabelledReactionNet},s) = subpart(p,s,:sname)
tname(p::Union{AbstractLabelledPetriNet, AbstractLabelledReactionNet},t) = subpart(p,t,:tname)
# Interoperability with Petri.jl
Petri.Model(p::AbstractPetriNet) = begin
ts = TransitionMatrices(p)
t_in = map(i->Dict(k=>v for (k,v) in enumerate(ts.input[i,:]) if v != 0), 1:nt(p))
t_out = map(i->Dict(k=>v for (k,v) in enumerate(ts.output[i,:]) if v != 0), 1:nt(p))
Δ = Dict(i=>t for (i,t) in enumerate(zip(t_in, t_out)))
return Petri.Model(ns(p), Δ)
end
Petri.Model(p::Union{AbstractLabelledPetriNet, AbstractLabelledReactionNet}) = begin
snames = [sname(p, s) for s in 1:ns(p)]
tnames = [tname(p, t) for t in 1:nt(p)]
ts = TransitionMatrices(p)
t_in = map(i->LVector(;[(snames[k]=>v) for (k,v) in enumerate(ts.input[i,:]) if v != 0]...), 1:nt(p))
t_out = map(i->LVector(;[(snames[k]=>v) for (k,v) in enumerate(ts.output[i,:]) if v != 0]...), 1:nt(p))
Δ = LVector(;[(tnames[i]=>t) for (i,t) in enumerate(zip(t_in, t_out))]...)
return Petri.Model(collect(values(snames)), Δ)
end
concentration(p::AbstractLabelledReactionNet,s) = subpart(p,s,:concentration)
rate(p::AbstractLabelledReactionNet,t) = subpart(p,t,:rate)
concentrations(p::AbstractLabelledReactionNet) = begin
snames = [sname(p, s) for s in 1:ns(p)]
LVector(;[(snames[s]=>concentration(p, s)) for s in 1:ns(p)]...)
end
rates(p::AbstractLabelledReactionNet) = begin
tnames = [tname(p, s) for s in 1:nt(p)]
LVector(;[(tnames[t]=>rate(p, t)) for t in 1:nt(p)]...)
end
include("visualization.jl")
include("Epidemiology.jl")
end