Skip to content

Commit

Permalink
julia struct based ABM
Browse files Browse the repository at this point in the history
  • Loading branch information
Kris Brown committed May 14, 2024
1 parent 22086e0 commit 4dfa651
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ Manifest.toml
.DS_Store
.vscode
.ipynb_checkpoints

cache/

figures/SIRSDiscretetrajectory.png
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ version = "0.0.1"
AlgebraicPetri = "4f99eebe-17bf-4e98-b6a1-2c4f205a959b"
AlgebraicRewriting = "725a01d3-f174-5bbd-84e1-b9417bad95d9"
Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Fleck = "5bb9b785-358c-4fee-af0f-b94a146244a8"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
41 changes: 41 additions & 0 deletions docs/literate/game_of_life.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# # Petri Net rewriting
#
# First we want to load our package with `using`

using AlgebraicABMs
using Catlab, AlgebraicPetri # for declaring model building blocks
using Distributions # for defining hazard rates

# ## Schema
#
# We create a regular grid

@present SchLife(FreeSchema) begin
Cell::Ob
(N,E,W,S)::Hom(Cell,Cell)
Life::Ob
live::Hom(Life,Cell)
end

# We define a network of cells

@present SchLifeGraph <: SchSymmetricGraph begin
Life::Ob
live::Hom(Life,V)
end

@acset_type Life(SchLifeGraph) <: AbstractSymmetricGraph

function living_neighbors(n::Int; alive=false)
X = Life(1)
alive && add_part!(X, :Live, live=1)
for _ in 1:n
v = add_part!(X, :V)
add_part!(X, :Life, live=v)
add_edge!(X, v, 1)
end
X
end

living_neighbors(2)

8 changes: 4 additions & 4 deletions docs/literate/petri_example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ to_graphviz(sir_pn)

# A "state of the world" in this model is just a finite set of Susceptible, Infected, and Recovered people. Thus we can specify a model with 3 integers.

init = PetriNetCSet(sir_pn, S=100, I=5) # Initial state
init = PetriNetCSet(sir_pn, S=100, I=5); # Initial state


# We declare parameters to specify the random waiting times
Expand All @@ -46,15 +46,15 @@ clockdists[:wane] = (t) -> Exponential(wane)

## the Weibull clock (non-Markov)
α, θ = weibullpar(30, 5)
clockdists[:rec] = (t) -> Weibull(α, θ)
clockdists[:rec] = (t) -> Weibull(α, θ);

# We make a reporting function that extracts information from each time step.
count(acs::ACSet) =
NamedTuple(Dict([o => nparts(acs, o) for o in ob(acset_schema(acs))]))
NamedTuple(Dict([o => nparts(acs, o) for o in ob(acset_schema(acs))]));


# We simulate a model
sirout = run!(sir_pn, clockdists, init; save=count, maxevent=20)
sirout = run!(sir_pn, clockdists, init; save=count, maxevent=2000)
X = first.(sirout)
SIR = [getindex.(last.(sirout), x) for x in 1:3]
f = Figure();
Expand Down
5 changes: 4 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
CurrentModule = AlgebraicABMs
```

`AlgebraicABMs.jl` is a Julia library for creating agent-based models.
`AlgebraicABMs.jl` is a Julia library for creating agent-based models. We ultimate want to provide capabilities on par with software like AnyLogic, NetLogo, and Agents.jl - all while offering a mostly declarative interface such that:
- The model's logic is transparent (rather than hidden away in complicated interactions of code blocks)
- Models can be built compositionally (we can naturally 'glue' models together at a high level, without worrying about implementation details and edge-cases)
- Models can be _migrated_ at a high level, whether interpersonally (collaboration with others who have a different ontology/vocabulary) or intrapersonally (one updates one's own model of the of world and wishes to reuse one's old model under new assumptions, without having to manually refactor code and dig into implementation details).
Binary file removed figures/SIRSDiscretetrajectory.png
Binary file not shown.
Loading

0 comments on commit 4dfa651

Please sign in to comment.