Skip to content

Commit

Permalink
fixes initialization, stepping function
Browse files Browse the repository at this point in the history
- fix support for initial values specified in acs
- fix support for custom tsteps
- fix req/alloc

Signed-off-by: Bíma, Jan <[email protected]>
  • Loading branch information
thevolatilebit committed Sep 16, 2023
1 parent 6afa807 commit e5ade9f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/interface/create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ end

function expand_rate(rate)
rate = if !(isexpr(rate, :macrocall) && (macroname(rate) == :per_step))
:(rand(Poisson(max($rate, 0))))
:(rand(Poisson(max(state.dt * $rate, 0))))
else
rate.args[3]
end
Expand Down
2 changes: 1 addition & 1 deletion src/interface/update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ Add a jump process (with specified Poisson intensity per unit time step) to a mo
macro jump(acsex, inex, acex)
return push_to_acs!(
acsex,
Expr(:&&, Expr(:call, :rand, :(Poisson(max(@solverarg(:tstep) * $inex, 0)))), acex),
Expr(:&&, Expr(:call, :rand, :(Poisson(max(state.dt * $inex, 0)))), acex),
)
end

Expand Down
16 changes: 7 additions & 9 deletions src/solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ function get_init_satisfied(allocs, qs, state)
(reqs[tok.index, i] += tok.stoich)
end
end
@show 2 reqs
for i in eachindex(allocs)
allocs[i] = reqs[i] == 0.0 ? Inf : floor(allocs[i] / reqs[i])
end
@show allocs
foreach(i -> qs[i] = min(qs[i], minimum(allocs[:, i])), 1:size(reqs, 2))
foreach(i -> allocs[:, i] .= reqs[:, i] * qs[i], 1:size(reqs, 2))

Expand All @@ -133,7 +135,7 @@ function evolve!(state)
qs .= ceil.(Ref(Int), qs)
@show qs
for i = 1:nparts(state, :T)
new_instances = state.dt * qs[i] + state[i, :transToSpawn]
new_instances = qs[i] + state[i, :transToSpawn]
capacity =
state[i, :transCapacity] -
count(t -> t[:transHash] == state[i, :transHash], state.ongoing_transitions)
Expand Down Expand Up @@ -165,7 +167,7 @@ function evolve!(state)
for i = 1:nparts(state, :T)
qs[i] != 0 && push!(
state.ongoing_transitions,
Transition(state[i, :transName] * "_@$(state.t)", get_sampled_transition(state, i), state.t, qs[i], 0.0),
Transition(string(state[i, :transName]) * "_@$(state.t)", get_sampled_transition(state, i), state.t, qs[i], 0.0),
)
end

Expand Down Expand Up @@ -258,11 +260,13 @@ function finish!(state)
(in(:rate, tok.modality) ? trans_[:transCycleTime] : 1)
)
end

q = if trans_.state >= trans_[:transCycleTime]
rand(Distributions.Binomial(Int(trans_.q), trans_[:transProbOfSuccess]))
else
0
end

foreach(
tok -> (state.u[tok.index] += q * tok.stoich;
val_reward += state[tok.index, :specReward] * q * tok.stoich),
Expand Down Expand Up @@ -378,26 +382,20 @@ function ReactiveNetwork(
end

function AlgebraicAgents.step!(state::ReactiveNetwork)
#du = copy(state.u)
free_blocked_species!(state)
#du .= state.u
update_observables(state)
sample_transitions!(state)
evolve!(state)
finish!(state)
#update_u!(state, u)
event_action!(state)

push!(
state.log,
(:valuation, state.t, state.u' * [state[i, :specValuation] for i = 1:nparts(state, :S)]),
)

#update_u!(state, du)
save!(state)

#state.u .= du
state.t += state.dt
return state.t += state.dt
end

function AlgebraicAgents._projected_to(state::ReactiveNetwork)
Expand Down
2 changes: 1 addition & 1 deletion src/state.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ end
## query the state

t(state::ReactiveNetwork) = state.t
solverarg(state::ReactiveNetwork, arg) = state.solverargs[arg]
solverarg(state::ReactiveNetwork, arg) = state.p[arg]
take(state::ReactiveNetwork, pcs::Symbol) = state.observables[pcs].sampled
log(state::ReactiveNetwork, msg) = (println(msg); push!(state.log, (:log, msg)))
state(state::ReactiveNetwork) = state
Expand Down
5 changes: 4 additions & 1 deletion tutorial/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ end

@prob_init acs X = 10 Y = 20
@prob_params acs
@prob_meta acs tspan = 250 dt = 0.1
@prob_meta acs tspan = 250 dt = 0.11


# sol = ReactiveDynamics.solve(prob)
Expand All @@ -23,6 +23,9 @@ for i in 1:30
AlgebraicAgents.step!(prob)
end
prob.history_u



using Plots

@plot sol plot_type = summary
Expand Down

0 comments on commit e5ade9f

Please sign in to comment.