Skip to content

Commit

Permalink
Add tutorial w/ CEED integration
Browse files Browse the repository at this point in the history
  • Loading branch information
thevolatilebit committed Apr 23, 2024
1 parent 4791a81 commit c99d4ce
Show file tree
Hide file tree
Showing 12 changed files with 361 additions and 44 deletions.
2 changes: 1 addition & 1 deletion docs/build/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@push acs 1.0 X ⟶ Y
@prob_init acs X=1 Y=2 XY=α
@prob_params acs γ=1 α=4
@solve_and_plot acs</code></pre></div></section></article><h2 id="Modify-a-model"><a class="docs-heading-anchor" href="#Modify-a-model">Modify a model</a><a id="Modify-a-model-1"></a><a class="docs-heading-anchor-permalink" href="#Modify-a-model" title="Permalink"></a></h2><p>We list common transition attributes:</p><table><tr><th style="text-align: left">attribute</th><th style="text-align: left">interpretation</th></tr><tr><td style="text-align: left"><code>transPriority</code></td><td style="text-align: left">priority of a transition (influences resource allocation)</td></tr><tr><td style="text-align: left"><code>transProbOfSuccess</code></td><td style="text-align: left">probability that a transition terminates successfully</td></tr><tr><td style="text-align: left"><code>transCapacity</code></td><td style="text-align: left">maximum number of concurrent instances of the transition</td></tr><tr><td style="text-align: left"><code>transCycleTime</code></td><td style="text-align: left">duration of a transition&#39;s instance (adjusted by resource allocation)</td></tr><tr><td style="text-align: left"><code>transMaxLifeTime</code></td><td style="text-align: left">maximal duration of a transition&#39;s instance</td></tr><tr><td style="text-align: left"><code>transPostAction</code></td><td style="text-align: left">action to be executed once a transition&#39;s instance terminates</td></tr><tr><td style="text-align: left"><code>transName</code></td><td style="text-align: left">name of a transition</td></tr></table><p>We list common species attributes:</p><table><tr><th style="text-align: left">attribute</th><th style="text-align: left">interpretation</th></tr><tr><td style="text-align: left"><code>specInitUncertainty</code></td><td style="text-align: left">uncertainty about variable&#39;s initial state (modelled as Gaussian standard deviation)</td></tr><tr><td style="text-align: left"><code>specInitVal</code></td><td style="text-align: left">initial value of a variable</td></tr></table><p>Moreover, it is possible to specify the semantics of the &quot;rate&quot; term. By default, at each time step <code>n ~ Poisson(rate * dt)</code> instances of a given transition will be spawned. If you want to specify the rate in terms of a cycle time, you may want to use <code>@ct(cycle_time)</code>, e.g., <code>@ct(ex), A --&gt; B, ...</code>. This is a shorthand for <code>1/ex, A --&gt; B, ...</code>.</p><p>For deterministic &quot;rates&quot;, use <code>@per_step(ex)</code>. Here, <code>ex</code> evaluates to a deterministic number (ceiled to the nearest integer) of a transition&#39;s instances to spawn per a single integrator&#39;s step. However, note that in this case, the number doesn&#39;t scale with the step length! Moreover</p><article class="docstring"><header><a class="docstring-binding" id="ReactiveDynamics.@add_species" href="#ReactiveDynamics.@add_species"><code>ReactiveDynamics.@add_species</code></a><span class="docstring-category">Macro</span></header><section><div><p>Add new species to a model.</p><p><strong>Examples</strong></p><pre><code class="language-julia hljs">@add_species acs S I R</code></pre></div></section></article><article class="docstring"><header><a class="docstring-binding" id="ReactiveDynamics.@aka" href="#ReactiveDynamics.@aka"><code>ReactiveDynamics.@aka</code></a><span class="docstring-category">Macro</span></header><section><div><p>Alias object name in an acs.</p><p><strong>Default names</strong></p><table><tr><th style="text-align: left">name</th><th style="text-align: left">short name</th></tr><tr><td style="text-align: left">species</td><td style="text-align: left">S</td></tr><tr><td style="text-align: left">transition</td><td style="text-align: left">T</td></tr><tr><td style="text-align: left">action</td><td style="text-align: left">A</td></tr><tr><td style="text-align: left">event</td><td style="text-align: left">E</td></tr><tr><td style="text-align: left">param</td><td style="text-align: left">P</td></tr><tr><td style="text-align: left">meta</td><td style="text-align: left">M</td></tr></table><p><strong>Examples</strong></p><pre><code class="language-julia hljs">@aka acs species=resource transition=reaction</code></pre></div></section></article><article class="docstring"><header><a class="docstring-binding" id="ReactiveDynamics.@mode" href="#ReactiveDynamics.@mode"><code>ReactiveDynamics.@mode</code></a><span class="docstring-category">Macro</span></header><section><div><p>Set species modality.</p><p><strong>Supported modalities</strong></p><ul><li>nonblock</li><li>conserved</li><li>rate</li></ul><p><strong>Examples</strong></p><pre><code class="language-julia hljs">@mode acs (r&quot;proj\w+&quot;, r&quot;experimental\w+&quot;) conserved
@solve_and_plot acs</code></pre></div></section></article><h2 id="Modify-a-model"><a class="docs-heading-anchor" href="#Modify-a-model">Modify a model</a><a id="Modify-a-model-1"></a><a class="docs-heading-anchor-permalink" href="#Modify-a-model" title="Permalink"></a></h2><p>We list common transition attributes:</p><table><tr><th style="text-align: left">attribute</th><th style="text-align: left">interpretation</th></tr><tr><td style="text-align: left"><code>transPriority</code></td><td style="text-align: left">priority of a transition (influences resource allocation)</td></tr><tr><td style="text-align: left"><code>transProbOfSuccess</code></td><td style="text-align: left">probability that a transition terminates successfully</td></tr><tr><td style="text-align: left"><code>transCapacity</code></td><td style="text-align: left">maximum number of concurrent instances of the transition</td></tr><tr><td style="text-align: left"><code>transCycleTime</code></td><td style="text-align: left">duration of a transition&#39;s instance (adjusted by resource allocation)</td></tr><tr><td style="text-align: left"><code>transMaxLifeTime</code></td><td style="text-align: left">maximal duration of a transition&#39;s instance</td></tr><tr><td style="text-align: left"><code>transPostAction</code></td><td style="text-align: left">action to be executed once a transition&#39;s instance terminates</td></tr><tr><td style="text-align: left"><code>transName</code></td><td style="text-align: left">name of a transition</td></tr></table><p>We list common species attributes:</p><table><tr><th style="text-align: left">attribute</th><th style="text-align: left">interpretation</th></tr><tr><td style="text-align: left"><code>specInitUncertainty</code></td><td style="text-align: left">uncertainty about variable&#39;s initial state (modelled as Gaussian standard deviation)</td></tr><tr><td style="text-align: left"><code>specInitVal</code></td><td style="text-align: left">initial value of a variable</td></tr></table><p>Moreover, it is possible to specify the semantics of the &quot;rate&quot; term. By default, at each time step <code>n ~ Poisson(rate * dt)</code> instances of a given transition will be spawned. If you want to specify the rate in terms of a cycle time, you may want to use <code>@ct(cycle_time)</code>, e.g., <code>@ct(ex), A --&gt; B, ...</code>. This is a shorthand for <code>1/ex, A --&gt; B, ...</code>.</p><p>For deterministic &quot;rates&quot;, use <code>@deterministic(ex)</code>. Here, <code>ex</code> evaluates to a deterministic number (ceiled to the nearest integer) of a transition&#39;s instances to spawn per a single integrator&#39;s step. However, note that in this case, the number doesn&#39;t scale with the step length! Moreover</p><article class="docstring"><header><a class="docstring-binding" id="ReactiveDynamics.@add_species" href="#ReactiveDynamics.@add_species"><code>ReactiveDynamics.@add_species</code></a><span class="docstring-category">Macro</span></header><section><div><p>Add new species to a model.</p><p><strong>Examples</strong></p><pre><code class="language-julia hljs">@add_species acs S I R</code></pre></div></section></article><article class="docstring"><header><a class="docstring-binding" id="ReactiveDynamics.@aka" href="#ReactiveDynamics.@aka"><code>ReactiveDynamics.@aka</code></a><span class="docstring-category">Macro</span></header><section><div><p>Alias object name in an acs.</p><p><strong>Default names</strong></p><table><tr><th style="text-align: left">name</th><th style="text-align: left">short name</th></tr><tr><td style="text-align: left">species</td><td style="text-align: left">S</td></tr><tr><td style="text-align: left">transition</td><td style="text-align: left">T</td></tr><tr><td style="text-align: left">action</td><td style="text-align: left">A</td></tr><tr><td style="text-align: left">event</td><td style="text-align: left">E</td></tr><tr><td style="text-align: left">param</td><td style="text-align: left">P</td></tr><tr><td style="text-align: left">meta</td><td style="text-align: left">M</td></tr></table><p><strong>Examples</strong></p><pre><code class="language-julia hljs">@aka acs species=resource transition=reaction</code></pre></div></section></article><article class="docstring"><header><a class="docstring-binding" id="ReactiveDynamics.@mode" href="#ReactiveDynamics.@mode"><code>ReactiveDynamics.@mode</code></a><span class="docstring-category">Macro</span></header><section><div><p>Set species modality.</p><p><strong>Supported modalities</strong></p><ul><li>nonblock</li><li>conserved</li><li>rate</li></ul><p><strong>Examples</strong></p><pre><code class="language-julia hljs">@mode acs (r&quot;proj\w+&quot;, r&quot;experimental\w+&quot;) conserved
@mode acs (S, I) conserved
@mode acs S conserved</code></pre></div></section></article><article class="docstring"><header><a class="docstring-binding" id="ReactiveDynamics.@name_transition" href="#ReactiveDynamics.@name_transition"><code>ReactiveDynamics.@name_transition</code></a><span class="docstring-category">Macro</span></header><section><div><p>Set name of a transition in the model.</p><p><strong>Examples</strong></p><pre><code class="language-julia hljs">@name_transition acs 1=&quot;name&quot;
@name_transition acs name=&quot;transition_name&quot;
Expand Down
2 changes: 1 addition & 1 deletion docs/build/search_index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ We list common species attributes:

Moreover, it is possible to specify the semantics of the "rate" term. By default, at each time step `n ~ Poisson(rate * dt)` instances of a given transition will be spawned. If you want to specify the rate in terms of a cycle time, you may want to use `@ct(cycle_time)`, e.g., `@ct(ex), A --> B, ...`. This is a shorthand for `1/ex, A --> B, ...`.

For deterministic "rates", use `@per_step(ex)`. Here, `ex` evaluates to a deterministic number (ceiled to the nearest integer) of a transition's instances to spawn per a single integrator's step. However, note that in this case, the number doesn't scale with the step length! Moreover
For deterministic "rates", use `@deterministic(ex)`. Here, `ex` evaluates to a deterministic number (ceiled to the nearest integer) of a transition's instances to spawn per a single integrator's step. However, note that in this case, the number doesn't scale with the step length! Moreover

```@docs
@add_species
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveDynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Base.convert(::Type{FoldedObservable}, ex::String) = eval(Meta.parse(ex))
prettynames = Dict(
:transRate => [:rate],
:specInitUncertainty => [:uncertainty, :stoch, :stochasticity],
:transPreAction => [:preAction, :pre],
:transPreAction => [:preAction, :action, :pre],
:transPostAction => [:postAction, :post],
:transName => [:name, :interpretation],
:transPriority => [:priority],
Expand Down
17 changes: 6 additions & 11 deletions src/compilers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ function recursively_expand_dots_in_ex!(ex, vars)
return ex
end

reserved_names =
[:t, :state, :obs, :resample, :solverarg, :take, :log, :periodic, :set_params]
reserved_names = [:t, :obs, :resample, :solverarg, :take, :log, :periodic, :set_params]

function escape_ref(ex, species)
return if ex isa Symbol
Expand All @@ -92,20 +91,16 @@ function wrap_expr(fex, species_names, prm_names, varmap)
let
end
)

# expression walking (MacroTools): visit each expression, subsitute with the body's return value
fex = prewalk(fex) do x
# here we convert the query metalanguage: @t() -> time(state) etc.
if isexpr(x, :macrocall) && (macroname(x) reserved_names)
Expr(:call, macroname(x), :state, x.args[3:end]...)
else
x
end
end

fex = prewalk(fex) do x
# here we convert the query metalanguage: @t() -> time(state) etc.
if isexpr(x, :macrocall) && (macroname(x) == :transition)
elseif isexpr(x, :macrocall) && (macroname(x) == :transition)
:transition
elseif isexpr(x, :macrocall) && (macroname(x) == :state)
:state
else
x
end
Expand Down Expand Up @@ -148,7 +143,7 @@ function skip_compile(attr)
end

function compile_attrs(acs::ReactionNetworkSchema, structured_token)
species_names = setdiff(collect(acs[:, :specName]), structured_token)
species_names = collect(acs[:, :specName])#setdiff(collect(acs[:, :specName]), structured_token)

prm_names = collect(acs[:, :prmName])
varmap = Dict([name => :(state.u[$i]) for (i, name) in enumerate(species_names)])
Expand Down
9 changes: 6 additions & 3 deletions src/interface/create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function recursively_expand_actions!(evs, condex, event)
end

function expand_rate(rate)
rate = if !(isexpr(rate, :macrocall) && (macroname(rate) == :per_step))
rate = if !(isexpr(rate, :macrocall) && (macroname(rate) == :deterministic))
:(rand(Poisson(max(state.dt * $rate, 0))))
else
rate.args[3]
Expand Down Expand Up @@ -271,7 +271,7 @@ function prune_reaction_line!(pcs, reactants, line)
return line
end

function recursively_find_reactants!(reactants, pcs, ex::SampleableValues)
function recursively_find_reactants!(reactants, pcs, ex)
if typeof(ex) != Expr || isexpr(ex, :.) || (ex.head == :escape)
if (ex == 0 || in(ex, empty_set))
return :∅
Expand All @@ -293,8 +293,11 @@ function recursively_find_reactants!(reactants, pcs, ex::SampleableValues)
isexpr(ex.args[i], :tuple) ? ex.args[i].args[2] : ex.args[i],
)
end
elseif isexpr(ex, :macrocall) && macroname(ex) [:structured, :move]
return ex
elseif isexpr(ex, :macrocall)
recursively_find_reactants!(reactants, pcs, ex.args[3])
pass_value = ex.args[3] isa QuoteNode ? ex.args[3].value : ex.args[3]
recursively_find_reactants!(reactants, pcs, pass_value)
elseif isexpr(ex, :call)
push!(reactants, ex.args[1])
else
Expand Down
44 changes: 33 additions & 11 deletions src/solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,31 @@ end

function structured_rhs(expr::Expr, state, transition)
if isexpr(expr, :macrocall) && macroname(expr) == :structured
expr = quote
token = $(expr.args[end-1])
species = $(expr.args[end])
if length(expr.args) == 3
expr = quote
return $(expr.args[end])
end
# write docs
token = context_eval(state, transition, state.wrap_fun(expr))

return token, species
end
entangle!(getagent(state, "structured"), token)

token, species = context_eval(state, transition, state.wrap_fun(expr))
set_species!(token, Symbol(species))
return token, get_species(token)
else
expr = quote
token = $(expr.args[end-1])
species = $(expr.args[end])

entangle!(getagent(state, "structured"), token)
return token, species
end
# write docs
token, species = context_eval(state, transition, state.wrap_fun(expr))
set_species!(token, Symbol(species))

return token, get_species(token)
entangle!(getagent(state, "structured"), token)

return token, get_species(token)
end
elseif isexpr(expr, :macrocall) && macroname(expr) == :move
expr = quote
species_from = $(expr.args[end-1])
Expand Down Expand Up @@ -528,6 +540,8 @@ function ReactionNetworkProblem(
structured_token_names =
acs[filter(i -> acs[i, :specStructured], 1:nparts(acs, :S)), :specName]

println(acs[:, :specName])
println(structured_token_names)
attrs, transitions, wrap_fun = compile_attrs(acs, structured_token_names)
transition_recipes = transitions
u0_init = zeros(nparts(acs, :S))
Expand Down Expand Up @@ -583,7 +597,7 @@ function ReactionNetworkProblem(

entangle!(network, FreeAgent("structured"))

save!(network)
# save!(network)

return network
end
Expand Down Expand Up @@ -612,6 +626,11 @@ function update_u_structured!(state)
end

function AlgebraicAgents._step!(state::ReactionNetworkProblem)
update_u_structured!(state)
if isempty(state.sol)
save!(state)
end

free_blocked_species!(state)
update_u_structured!(state)
update_observables(state)
Expand All @@ -632,8 +651,11 @@ function AlgebraicAgents._step!(state::ReactionNetworkProblem)
),
)

state.t += state.dt

save!(state)
return state.t += state.dt

return state.t
end

function AlgebraicAgents._projected_to(state::ReactionNetworkProblem)
Expand Down
7 changes: 7 additions & 0 deletions tutorial/agents-integration/Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
[deps]
AlgebraicAgents = "f6eb0ae3-10fa-40e6-88dd-9006ba45093a"
CEEDesigns = "e939450b-799e-4198-a5f5-3f2f7fb1c671"
Copulas = "ae264745-0b69-425e-9d9d-cf662c5eec93"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
MCTS = "e12ccd36-dcad-5f33-8774-9175229e7b33"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
POMDPTools = "7588e00f-9cae-40de-98dc-e0c70c48cdd7"
POMDPs = "a93abf59-7444-517b-a68a-c42f96afdd7d"
PlotGraphviz = "78a92bc3-407c-4e2f-aae5-75bb47a6fe36"
ReactiveDynamics = "c7456e7d-545a-4b79-91ea-6e93d96dd4d4"
ScientificTypes = "321657f4-b219-11e9-178b-2701a2544e81"
SimpleWeightedGraphs = "47aef6b3-ad0c-573a-a1e2-d07658019622"
Loading

0 comments on commit c99d4ce

Please sign in to comment.