Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
thevolatilebit committed Feb 22, 2024
1 parent 8652f3d commit 2814b14
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/ReactiveDynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const ReactionNetworkSchema = FoldedReactionNetworkType{
Set{Symbol},
FoldedObservable,
Any,
Bool
Bool,
}

Base.convert(::Type{Symbol}, ex::String) = Symbol(ex)
Expand Down
2 changes: 1 addition & 1 deletion src/compilers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function wrap_expr(fex, species_names, prm_names, varmap)

return eval(quote
function (state, transition)
$letex
return $letex
end
end)
end
Expand Down
17 changes: 11 additions & 6 deletions src/interface/agents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract type AbstractStructuredSpecies <: AbstractAlgebraicAgent end
# In general, we will probably assume that each "structured agent" type implements this field.
# Otherwise, it would be possible to implement getter and setter interface and use it from within ReaDyn.
@aagent FreeAgent struct BaseStructuredSpecies
bound_transition::Union{Nothing, ReactiveDynamics.Transition}
bound_transition::Union{Nothing,ReactiveDynamics.Transition}
end

# We use this to let the network know that the type is structured.
Expand All @@ -27,16 +27,21 @@ end
# Convenience macro to define structured species.
macro structured(network, type)
name = Docs.namify(type.args[2])

quote
$(AlgebraicAgents.aagent(BaseStructuredSpecies, AbstractStructuredSpecies, type, ReactiveDynamics))

quote
$(AlgebraicAgents.aagent(
BaseStructuredSpecies,
AbstractStructuredSpecies,
type,
ReactiveDynamics,
))
register_structured_species!($(esc(network)), $(QuoteNode(name)))
end
end

# Add a structured agent instance to an instance of a reaction network.
function add_structured_species!(problem::ReactionNetworkProblem, agent)
entangle!(getagent(problem, "structured/$(nameof(typeof(agent)))"), agent)
return entangle!(getagent(problem, "structured/$(nameof(typeof(agent)))"), agent)
end

import AlgebraicAgents
Expand All @@ -49,4 +54,4 @@ AlgebraicAgents._step!(::AbstractStructuredSpecies) = nothing
isblocked(a) = !isnothing(a.bound_transition)

# Priority with which an unbound agent will be assigned to a transition.
priority(a, transition) = 0.0
priority(a, transition) = 0.0
2 changes: 1 addition & 1 deletion src/interface/reaction_parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using MacroTools: postwalk

struct FoldedReactant
species::Union{Expr, Symbol}
species::Union{Expr,Symbol}
stoich::SampleableValues
modality::Set{Symbol}
end
Expand Down
52 changes: 36 additions & 16 deletions src/solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ function get_reqs_ongoing!(reqs, qs, state)
(state.ongoing_transitions[i][:transCycleTime] > 0) &&
(reqs[tok.index, i] += qs[i] * tok.stoich * state.dt)
if in(:rate, tok.modality) && in(tok.species, state.structured_species)
error("Modality `:rate` is not supported for structured species in transition $(trans[:transName]).")
error(
"Modality `:rate` is not supported for structured species in transition $(trans[:transName]).",
)
end
in(:nonblock, tok.modality) && (reqs[tok.index, i] += qs[i] * tok.stoich)
end
Expand Down Expand Up @@ -125,7 +127,6 @@ function get_init_satisfied(allocs, qs, state)
return qs
end


"""
Evolve transitions, spawn new transitions.
"""
Expand Down Expand Up @@ -189,12 +190,20 @@ function evolve!(state)
for (j, type) in enumerate(state.acs[:, :specName])
if type state.structured_species
if !isinteger(allocs[j, i])
error("For structured species, stoichiometry coefficient must be integer in transition $i.")
error(
"For structured species, stoichiometry coefficient must be integer in transition $i.",
)
end

all_of_type = collect(values(inners(getagent(state, "structured/$(string(type))"))))
all_of_type = collect(
values(inners(getagent(state, "structured/$(string(type))"))),
)
filter!(!isblocked, all_of_type)
sort!(all_of_type, by=a->priority(a, state.acs[i, :transName]), rev=true)
sort!(
all_of_type;
by = a -> priority(a, state.acs[i, :transName]),
rev = true,
)

ix = 1
while allocs[j, i] > 0 && ix <= length(all_of_type)
Expand Down Expand Up @@ -246,12 +255,20 @@ function evolve!(state)
for (j, type) in enumerate(state.acs[:, :specName])
if type state.structured_species
if !isinteger(allocs[j, i])
error("For structured species, stoichiometry coefficient must be integer in transition $i.")
error(
"For structured species, stoichiometry coefficient must be integer in transition $i.",
)
end

all_of_type = collect(values(inners(getagent(state, "structured/$(string(type))"))))
all_of_type = collect(
values(inners(getagent(state, "structured/$(string(type))"))),
)
filter!(!isblocked, all_of_type)
sort!(all_of_type, by=a -> priority(a, state.acs[i, :transName]), rev=true)
sort!(
all_of_type;
by = a -> priority(a, state.acs[i, :transName]),
rev = true,
)

ix = 1
while allocs[j, i] > 0 && ix <= length(all_of_type)
Expand Down Expand Up @@ -301,7 +318,7 @@ function finish!(state)
((state.t - trans_.t) < trans_.trans[:transMaxLifeTime]) &&
(trans_.state < trans_[:transCycleTime]) &&
(ix += 1; continue)

q = if trans_.state >= trans_[:transCycleTime]
rand(Distributions.Binomial(Int(trans_.q), trans_[:transProbOfSuccess]))
else
Expand All @@ -320,7 +337,7 @@ function finish!(state)
state.u[i] += q * stoich
val_reward += state[i, :specReward] * q * stoich

for _ in 1:q
for _ = 1:q
a = context_eval(state, trans_, state.wrap_fun(r.species))
entangle!(getagent(state, "structured/$(r.species.args[1])"), a)
end
Expand All @@ -340,7 +357,7 @@ function finish!(state)
tok.stoich *
(in(:rate, tok.modality) ? trans_[:transCycleTime] : 1)
if tok.species state.structured_species
for _ in 1:(trans_.q * tok.stoich)
for _ = 1:(trans_.q*tok.stoich)
trans_.bound_structured_agents[begin].bound_transition = nothing
deleteat!(trans_.bound_structured_agents, 1)
end
Expand All @@ -349,12 +366,14 @@ function finish!(state)

if in(:nonblock, tok.modality)
if in(:conserved, tok.modality)
error("Modalities `:conserved` and `:nonblock` cannot be specified at the same time.")
error(
"Modalities `:conserved` and `:nonblock` cannot be specified at the same time.",
)
end

state.u[tok.index] += trans_.q * tok.stoich
state.u[tok.index] += trans_.q * tok.stoich
if tok.species state.structured_species
for _ in 1:(trans_.q * tok.stoich)
for _ = 1:(trans_.q*tok.stoich)
trans_.nonblock_structured_agents[begin].bound_transition = nothing
deleteat!(trans_.nonblock_structured_agents, 1)
end
Expand All @@ -366,7 +385,7 @@ function finish!(state)

terminated_all[Symbol(trans_[:transHash])] =
get(terminated_all, Symbol(trans_[:transHash]), 0) + trans_.q

terminated_success[Symbol(trans_[:transHash])] =
get(terminated_success, Symbol(trans_[:transHash]), 0) + q

Expand Down Expand Up @@ -427,7 +446,8 @@ function ReactionNetworkProblem(

acs = remove_choose(acs)

structured_species_names = acs[filter(i -> acs[i, :specStructured], 1:nparts(acs, :S)), :specName]
structured_species_names =
acs[filter(i -> acs[i, :specStructured], 1:nparts(acs, :S)), :specName]

attrs, transitions, wrap_fun = compile_attrs(acs, structured_species_names)
transition_recipes = transitions
Expand Down
14 changes: 9 additions & 5 deletions src/state.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ end
# evaluate compiled numeric expression in context of (u, p, t)
function context_eval(state::ReactionNetworkProblem, transition, o)
o = o isa Function ? Base.invokelatest(o, state, transition) : o

return o isa Sampleable ? rand(o) : o
end

Expand All @@ -74,7 +74,8 @@ function Base.getindex(state::ReactionNetworkProblem, keys...)
return state.acs[keys[1], keys[2]]
else
return context_eval(
state, nothing,
state,
nothing,
(contains(string(keys[2]), "trans") ? state.transitions : state.attrs)[keys[2]][keys[1]],
)
end
Expand Down Expand Up @@ -176,7 +177,10 @@ function sample_transitions!(state::ReactionNetworkProblem)
l_line, r_line = prune_r_line(state.transition_recipes[:trans][i])

for attr in keys(state.transition_recipes)
(attr [:trans, :transPreAction, :transPostAction, :transActivated, :transHash]) && continue
(
attr
[:trans, :transPreAction, :transPostAction, :transActivated, :transHash]
) && continue
push!(
state.transitions[attr],
context_eval(state, nothing, state.transition_recipes[attr][i]),
Expand All @@ -196,10 +200,10 @@ function sample_transitions!(state::ReactionNetworkProblem)
),
)
end

push!(state.transitions[:transLHS], reactants)
push!(state.transitions[:transRHS], r_line)

foreach(
k -> push!(state.transitions[k], state.transition_recipes[k][i]),
[:transPreAction, :transPostAction, :transToSpawn, :transHash],
Expand Down
24 changes: 13 additions & 11 deletions tutorial/agents-integration/agents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ network = @ReactionNetworkSchema
# With specified intensities, generate experimental resources.
ρ1, ∅ --> R1
ρ2, ∅ --> R2

# Generate "Molecule 1" (where the integer corresponds to a "state" of, e.g., experimental triage).
ρ3, ∅ --> M1(@t(), rand(4))

# Based on properties of particular "structured agent" assigned to the transition,
# we can update the attributes of the instance of a transition (such as probability of success).

# Transition "Molecule 1" into "Molecule 2."
# Update transition probability based on properties of "M1,"
# which was assigned as a "resource" to the transition.
ρ4, R1 + M1 --> M2(@t(), rand(4)), preAction => update_prob_transition(state, transition)
ρ4,
R1 + M1 --> M2(@t(), rand(4)),
preAction => update_prob_transition(state, transition)
end

@prob_init network R1 = 10 R2 = 15
Expand All @@ -48,8 +50,8 @@ end
# We use `@structured` macro, which is a convenience wrapper around `@aagent`),
# defined in ReactiveDynamics.jl
@structured network struct M1
descriptor
time_created
descriptor::Any
time_created::Any
end

using Random
Expand All @@ -66,7 +68,7 @@ ReactiveDynamics.M1(time, descriptor) = M1("M1" * randstring(4), nothing, descri
update_prob_transition = function (state, transition)
if !isnothing(transition) && !isempty(transition.bound_structured_agents)
bound_agent = first(transition.bound_structured_agents)

transition[:transProbOfSuccess] = min(1.0, sum(bound_agent.descriptor))
end
end
Expand All @@ -77,8 +79,8 @@ end
# of ReactiveDynamics.
@register begin
@aagent BaseStructuredSpecies AbstractStructuredSpecies struct M2
descriptor
time_created
descriptor::Any
time_created::Any
end

using Random
Expand All @@ -92,10 +94,10 @@ ReactiveDynamics.register_structured_species!(network, :M2)
# Instantiate the network.
network_instance = ReactionNetworkProblem(network)

for i in 1:2
for i = 1:2
add_structured_species!(network_instance, ReactiveDynamics.M1(0.0, rand(4)))
end

# --------------------------------------------------------------------------------
# Simulate the network.
simulate(network_instance, 10)
simulate(network_instance, 10)
Loading

0 comments on commit 2814b14

Please sign in to comment.