Browse Source

Updated to Petriv1 with stochastic differential equations (#10)

pull/11/head v0.3.0
Micah Halter 9 months ago
committed by GitHub
parent
commit
cc1a938553
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .gitignore
  2. 4
      Project.toml
  3. 1
      docs/Project.toml
  4. 1
      examples/covid/Project.toml
  5. 11
      examples/covid/_generate_figs.jl
  6. 11
      examples/covid/covid.jl
  7. 17
      examples/covid/epidemiology.jl
  8. 9
      examples/predation/lotka-volterra.jl
  9. 14
      src/AlgebraicPetri.jl

1
.gitignore

@ -16,6 +16,7 @@ deps/src/
# Build artifacts for creating documentation generated by the Documenter package
docs/build/
docs/site/
docs/src/examples
# File generated by Pkg, the package manager, based on a corresponding Project.toml
# It records a fixed state of all packages used by the project. As such, it should not be

4
Project.toml

@ -2,7 +2,7 @@ name = "AlgebraicPetri"
uuid = "4f99eebe-17bf-4e98-b6a1-2c4f205a959b"
license = "MIT"
authors = ["Micah Halter <micah@mehalter.com>"]
version = "0.2.0"
version = "0.3.0"
[deps]
AutoHashEquals = "15f4f7f2-30c1-5605-9d31-71845cf9641f"
@ -12,7 +12,7 @@ Petri = "4259d249-1051-49fa-8328-3f8ab9391c33"
[compat]
AutoHashEquals = "0.2.0"
Catlab = "0.7.0"
Petri = "0.4.0"
Petri = "1.0.0"
julia = "1.0"
[extras]

1
docs/Project.toml

@ -6,3 +6,4 @@ Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Petri = "4259d249-1051-49fa-8328-3f8ab9391c33"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0"

1
examples/covid/Project.toml

@ -4,3 +4,4 @@ Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Petri = "4259d249-1051-49fa-8328-3f8ab9391c33"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0"

11
examples/covid/_generate_figs.jl

@ -1,6 +1,7 @@
# +
include("epidemiology.jl")
using OrdinaryDiffEq
using Catlab.Graphics.Graphviz: run_graphviz
# +
@ -46,7 +47,7 @@ save_graph(Graph(p_sir), "sir_graph.svg")
u0 = [10.0, 1, 0]
p = [0.4, 0.4]
# create and solve ODE problem
prob = ODEProblem(vectorfields(p_sir),u0,(0.0,7.5),p)
prob = ODEProblem(p_sir,u0,(0.0,7.5),p)
sol = OrdinaryDiffEq.solve(prob,Tsit5())
# visualize the solution
splot(sol, "sir_soln.svg")
@ -58,7 +59,7 @@ save_graph(Graph(p_seir), "seir_graph.svg")
u0 = [10.0, 1, 0, 0]
p = [0.9, 0.2, 0.5]
# create and solve ODE problem
prob = ODEProblem(vectorfields(p_seir),u0,(0.0,15.0),p)
prob = ODEProblem(p_seir,u0,(0.0,15.0),p)
sol = OrdinaryDiffEq.solve(prob,Tsit5())
# visualize the solution
splot(sol, "seir_soln.svg")
@ -70,7 +71,7 @@ save_graph(Graph(p_seird), "seird_graph.svg")
u0 = [10.0, 1, 0, 0, 0]
p = [0.9, 0.2, 0.5, 0.1]
# create and solve ODE problem
prob = ODEProblem(vectorfields(p_seird),u0,(0.0,15.0),p)
prob = ODEProblem(p_seird,u0,(0.0,15.0),p)
sol = OrdinaryDiffEq.solve(prob,Tsit5())
# visualize the solution
splot(sol, "seird_soln.svg")
@ -100,7 +101,7 @@ u0[2] = 1
# Define transition rates
params = seirdparams(3, 5)
# Generate and solve resulting ODE
prob = ODEProblem(vectorfields(p_seird_3),u0,tspan,params)
prob = ODEProblem(p_seird_3,u0,tspan,params)
sol = OrdinaryDiffEq.solve(prob,Tsit5())
splotchannels(sol, "seird")
@ -109,7 +110,7 @@ function drawsol(f, a, b, p, dir)
println("INFO: Processing $dir")
dynparams = waveparams(f, a/sum(u0), b/sum(u0),p)
mkpath(dir)
prob = ODEProblem(vectorfields(p_seird_3),u0,tspan, dynparams)
prob = ODEProblem(p_seird_3,u0,tspan, dynparams)
sol = OrdinaryDiffEq.solve(prob,Tsit5(), saveat=1:1:tspan[2])
splotchannels(sol, dir)
savefig(plot(sol.t, f(a,b,p), ylim=(0,12), legend=false, linewidth=5), "$dir/transmissability.svg")

11
examples/covid/covid.jl

@ -15,7 +15,6 @@ using Catlab.Programs
using Catlab.CategoricalAlgebra.ShapeDiagrams
using Catlab.WiringDiagrams
using Catlab.Graphics
using Catlab.Graphics.Graphviz: Graph
import Catlab.Theories: id
@ -95,8 +94,8 @@ params = seirdparams(3, 5);
# Generate, solve, and visualize resulting ODE
prob = ODEProblem(vectorfields(p_seird_3),u0,tspan,params);
sol = OrdinaryDiffEq.solve(prob,Tsit5());
prob = ODEProblem(p_seird_3,u0,tspan,params);
sol = solve(prob,Tsit5());
plot(sol)
@ -126,7 +125,7 @@ a,b,p = 10, 1, 1/6
dynparams = waveparams(asymptotic, a/sum(u0), b/sum(u0),p)
prob = ODEProblem(vectorfields(p_seird_3),u0,tspan, dynparams)
sol = OrdinaryDiffEq.solve(prob,Tsit5(), saveat=1:1:tspan[2])
prob = ODEProblem(p_seird_3,u0,tspan, dynparams)
sol = solve(prob,Tsit5(), saveat=1:1:tspan[2])
plot(sol)
plot(sol)

17
examples/covid/epidemiology.jl

@ -5,13 +5,12 @@
using AlgebraicPetri.Epidemiology
using Petri
using OrdinaryDiffEq
using StochasticDiffEq
using Plots
using Catlab.Theories
using Catlab.CategoricalAlgebra.ShapeDiagrams
using Catlab.Graphics
using Catlab.Graphics.Graphviz: Graph
display_wd(ex) = to_graphviz(ex, orientation=LeftToRight, labels=true);
@ -34,8 +33,8 @@ Graph(p_sir)
u0 = [10.0, 1, 0];
p = [0.4, 0.4];
prob = ODEProblem(vectorfields(p_sir),u0,(0.0,7.5),p);
sol = OrdinaryDiffEq.solve(prob,Tsit5());
prob,cb = SDEProblem(p_sir,u0,(0.0,7.5),p);
sol = solve(prob,SRA1(),callback=cb)
plot(sol)
@ -61,8 +60,8 @@ Graph(p_seir)
u0 = [10.0, 1, 0, 0];
p = [0.9, 0.2, 0.5];
prob = ODEProblem(vectorfields(p_seir),u0,(0.0,15.0),p);
sol = OrdinaryDiffEq.solve(prob,Tsit5());
prob,cb = SDEProblem(p_seir,u0,(0.0,15.0),p);
sol = solve(prob,SRA1(),callback=cb)
plot(sol)
@ -86,7 +85,7 @@ Graph(p_seird)
u0 = [10.0, 1, 0, 0, 0];
p = [0.9, 0.2, 0.5, 0.1];
prob = ODEProblem(vectorfields(p_seird),u0,(0.0,15.0),p);
sol = OrdinaryDiffEq.solve(prob,Tsit5());
prob,cb = SDEProblem(p_seird,u0,(0.0,15.0),p);
sol = solve(prob,SRA1(),callback=cb)
plot(sol)
plot(sol)

9
examples/predation/lotka-volterra.jl

@ -14,7 +14,6 @@ using Catlab.Programs
using Catlab.CategoricalAlgebra.ShapeDiagrams
using Catlab.WiringDiagrams
using Catlab.Graphics
using Catlab.Graphics.Graphviz: Graph
display_wd(ex) = to_graphviz(ex, orientation=LeftToRight, labels=true);
@ -58,8 +57,8 @@ Graph(lotka_petri)
u0 = [40, 2];
p = [4.0, 1.0, 2.0];
prob = ODEProblem(vectorfields(lotka_petri),u0,(0.0,8.0),p);
sol = OrdinaryDiffEq.solve(prob,Tsit5(),abstol=1e-6);
prob = ODEProblem(lotka_petri,u0,(0.0,8.0),p);
sol = solve(prob,Tsit5(),abstol=1e-6);
plot(sol)
# There is also a second syntax that is easier to write for programmers
@ -108,6 +107,6 @@ Graph(dual_lv_petri)
u0 = [40, 2, 1];
p = [4.0, 1.0, 2.0, 2.5, 1.5];
prob = ODEProblem(vectorfields(dual_lv_petri),u0,(0.0,8.0),p);
sol = OrdinaryDiffEq.solve(prob,Tsit5(),abstol=1e-6);
prob = ODEProblem(dual_lv_petri,u0,(0.0,8.0),p);
sol = solve(prob,Tsit5(),abstol=1e-6);
plot(sol)

14
src/AlgebraicPetri.jl

@ -104,8 +104,6 @@ function (::Type{PetriCospan})(l::AbstractVector, m::Petri.Model, r::AbstractVec
id(PetriFunctor), m)
end
NullPetri(n::Int) = Petri.Model(collect(1:n), Vector{Tuple{Dict{Int, Number}, Dict{Int, Number}}}())
@instance BiproductCategory(PetriCospanOb, PetriCospan) begin
dom(f::PetriCospan) = PetriCospanOb(dom(left(f)).n)
codom(f::PetriCospan) = PetriCospanOb(dom(right(f)).n)
@ -126,7 +124,7 @@ NullPetri(n::Int) = Petri.Model(collect(1:n), Vector{Tuple{Dict{Int, Number}, Di
id(X::PetriCospanOb) = PetriCospan(
Cospan(id(FinOrd(X.n)), id(FinOrd(X.n))),
id(PetriFunctor),
NullPetri(X.n))
EmptyPetri(X.n))
otimes(X::PetriCospanOb, Y::PetriCospanOb) = PetriCospanOb(X.n + Y.n)
@ -153,28 +151,28 @@ NullPetri(n::Int) = Petri.Model(collect(1:n), Vector{Tuple{Dict{Int, Number}, Di
Cospan(
id(FinOrd(Z.n)),
FinOrdFunction(vcat(X.n+1:Z.n, 1:X.n), Z.n, Z.n)
), id(PetriFunctor), NullPetri(Z.n))
), id(PetriFunctor), EmptyPetri(Z.n))
end
mcopy(X::PetriCospanOb) = PetriCospan(
Cospan(
id(FinOrd(X.n)),
FinOrdFunction(vcat(1:X.n,1:X.n), 2*X.n, X.n)
), id(PetriFunctor), NullPetri(X.n))
), id(PetriFunctor), EmptyPetri(X.n))
mmerge(X::PetriCospanOb) = PetriCospan(
Cospan(
FinOrdFunction(vcat(1:X.n,1:X.n), 2*X.n, X.n),
id(FinOrd(X.n))
), id(PetriFunctor), NullPetri(X.n))
), id(PetriFunctor), EmptyPetri(X.n))
create(X::PetriCospanOb) = PetriCospan(
Cospan(FinOrdFunction(Int[], 0, X.n), id(FinOrd(X.n))),
id(PetriFunctor), NullPetri(X.n))
id(PetriFunctor), EmptyPetri(X.n))
delete(X::PetriCospanOb) = PetriCospan(
Cospan(id(FinOrd(X.n)), FinOrdFunction(Int[], 0, X.n)),
id(PetriFunctor), NullPetri(X.n))
id(PetriFunctor), EmptyPetri(X.n))
pair(f::PetriCospan, g::PetriCospan) = compose(mcopy(dom(f)), otimes(f, g))
copair(f::PetriCospan, g::PetriCospan) = compose(otimes(f, g), mmerge(codom(f)))

Loading…
Cancel
Save