Skip to content

Commit d2f9aba

Browse files
solve
1 parent 809a2df commit d2f9aba

File tree

6 files changed

+60
-91
lines changed

6 files changed

+60
-91
lines changed

Project.toml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,12 @@ Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
99
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
1010
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
1111
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
12-
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
13-
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
1412
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
15-
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
16-
DocumenterMarkdown = "997ab1e6-3595-5248-9280-8efb232c3433"
1713
GeneratedExpressions = "84d730a5-1eb9-4187-a799-27dd07f33a14"
1814
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
1915
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
2016
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
2117
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
22-
NLopt = "76087f3c-5699-56af-9a33-bf431cd00edd"
23-
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
2418
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
2519
Pluto = "c3e4b0f8-55cb-11ea-2926-15256bba5781"
2620
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
@@ -32,26 +26,19 @@ StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
3226
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
3327
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
3428
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
35-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3629

3730
[compat]
3831
CSV = "0.10"
3932
Catlab = "0.14"
4033
ComponentArrays = "0.14"
4134
Crayons = "4.1"
4235
DataFrames = "1.6"
43-
DiffEqBase = "6.128"
44-
DifferentialEquations = "7.9"
4536
Distributions = "0.25"
46-
Documenter = "0.27"
47-
DocumenterMarkdown = "0.2"
4837
GeneratedExpressions = "0.1"
4938
IJulia = "1.24"
5039
JLD2 = "0.4"
5140
JSON = "0.21"
5241
MacroTools = "0.5"
53-
NLopt = "1.0"
54-
OrdinaryDiffEq = "6.55"
5542
Plots = "1.39"
5643
Pluto = "0.19"
5744
PlutoUI = "0.7"

src/ReactiveDynamics.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module ReactiveDynamics
33
using Catlab, Catlab.CategoricalAlgebra, Catlab.Present
44
using Reexport
55
using MacroTools
6-
using NLopt
76
using ComponentArrays
87

98
@reexport using GeneratedExpressions

src/solvers.jl

Lines changed: 46 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# assortment of SciML-compatible problem solvers
2-
3-
export DiscreteProblem
4-
5-
using DiffEqBase, DifferentialEquations
61
using Distributions
72
using Random
83

@@ -37,7 +32,7 @@ function get_reqs_ongoing!(reqs, qs, state)
3732
for tok in state.ongoing_transitions[i][:transLHS]
3833
in(:rate, tok.modality) &&
3934
(state.ongoing_transitions[i][:transCycleTime] > 0) &&
40-
(reqs[tok.index, i] += qs[i] * tok.stoich * state.solverargs[:tstep])
35+
(reqs[tok.index, i] += qs[i] * tok.stoich * state.dt)
4136
in(:nonblock, tok.modality) && (reqs[tok.index, i] += qs[i] * tok.stoich)
4237
end
4338
end
@@ -124,9 +119,8 @@ end
124119
"""
125120
Evolve transitions, spawn new transitions.
126121
"""
127-
function evolve!(u, state)
128-
update_u!(state, u)
129-
actual_allocs = zero(u)
122+
function evolve!(state)
123+
actual_allocs = zero(state.u)
130124

131125
## schedule new transitions
132126
reqs = zeros(nparts(state, :S), nparts(state, :T))
@@ -137,8 +131,9 @@ function evolve!(u, state)
137131
1:nparts(state, :T),
138132
)
139133
qs .= ceil.(Ref(Int), qs)
134+
@show qs
140135
for i = 1:nparts(state, :T)
141-
new_instances = state.solverargs[:tstep] * qs[i] + state[i, :transToSpawn]
136+
new_instances = state.dt * qs[i] + state[i, :transToSpawn]
142137
capacity =
143138
state[i, :transCapacity] -
144139
count(t -> t[:transHash] == state[i, :transHash], state.ongoing_transitions)
@@ -148,10 +143,13 @@ function evolve!(u, state)
148143
end
149144

150145
reqs = get_reqs_init!(reqs, qs, state)
146+
@show reqs
151147
allocs =
152-
get_allocs!(reqs, u, state, state[:, :transPriority], state.solverargs[:strategy])
148+
get_allocs!(reqs, state.u, state, state[:, :transPriority], state.p[:strategy])
149+
@show allocs
153150
qs .= get_init_satisfied(allocs, qs, state)
154-
151+
@show qs
152+
println("====")
155153
push!(
156154
state.log,
157155
(
@@ -160,7 +158,7 @@ function evolve!(u, state)
160158
[(hash, q) for (hash, q) in zip(state[:, :transHash], qs)]...,
161159
),
162160
)
163-
u .-= sum(allocs; dims = 2)
161+
state.u .-= sum(allocs; dims = 2)
164162
actual_allocs .+= sum(allocs; dims = 2)
165163

166164
# add spawned transitions to the heap
@@ -171,18 +169,17 @@ function evolve!(u, state)
171169
)
172170
end
173171

174-
update_u!(state, u)
175172
## evolve ongoing transitions
176173
reqs = zeros(nparts(state, :S), length(state.ongoing_transitions))
177174
qs = map(t -> t.q, state.ongoing_transitions)
178175

179176
get_reqs_ongoing!(reqs, qs, state)
180177
allocs = get_allocs!(
181178
reqs,
182-
u,
179+
state.u,
183180
state,
184181
map(t -> t[:transPriority], state.ongoing_transitions),
185-
state.solverargs[:strategy],
182+
state.p[:strategy],
186183
)
187184
qs .= get_frac_satisfied(allocs, reqs, state)
188185
push!(
@@ -196,11 +193,11 @@ function evolve!(u, state)
196193
]...,
197194
),
198195
)
199-
u .-= sum(allocs; dims = 2)
196+
state.u .-= sum(allocs; dims = 2)
200197
actual_allocs .+= sum(allocs; dims = 2)
201198

202199
foreach(
203-
i -> state.ongoing_transitions[i].state += qs[i] * state.solverargs[:tstep],
200+
i -> state.ongoing_transitions[i].state += qs[i] * state.dt,
204201
eachindex(state.ongoing_transitions),
205202
)
206203

@@ -229,8 +226,7 @@ function event_action!(state)
229226
end
230227

231228
# collect terminated transitions
232-
function finish!(u, state)
233-
update_u!(state, u)
229+
function finish!(state)
234230
val_reward = 0
235231
terminated_all = Dict{Symbol,Float64}()
236232
terminated_success = Dict{Symbol,Float64}()
@@ -256,7 +252,7 @@ function finish!(u, state)
256252
end
257253
for tok in trans_[:transLHS]
258254
in(:conserved, tok.modality) && (
259-
u[tok.index] +=
255+
state.u[tok.index] +=
260256
trans_.q *
261257
tok.stoich *
262258
(in(:rate, tok.modality) ? trans_[:transCycleTime] : 1)
@@ -268,12 +264,11 @@ function finish!(u, state)
268264
0
269265
end
270266
foreach(
271-
tok -> (u[tok.index] += q * tok.stoich;
267+
tok -> (state.u[tok.index] += q * tok.stoich;
272268
val_reward += state[tok.index, :specReward] * q * tok.stoich),
273269
toks_rhs,
274270
)
275271

276-
update_u!(state, u)
277272
context_eval(state, trans_.trans[:transPostAction])
278273
terminated_all[trans_[:transHash]] =
279274
get(terminated_all, trans_[:transHash], 0) + trans_.q
@@ -289,7 +284,7 @@ function finish!(u, state)
289284
push!(state.log, (:terminated_success, state.t, terminated_success...))
290285
push!(state.log, (:valuation_reward, state.t, val_reward))
291286

292-
return u
287+
return state.u
293288
end
294289

295290
function free_blocked_species!(state)
@@ -306,15 +301,15 @@ function get_tcontrol(tspan, args)
306301
tunit = get(args, :tunit, oneunit(tspan))
307302
tspan = tspan / tunit
308303

309-
tstep = get(args, :tstep, haskey(args, :tstops) ? tspan / args[:tstops] : tunit) / tunit
304+
dt = get(args, :dt, haskey(args, :tstops) ? tspan / args[:tstops] : tunit) / tunit
310305

311-
return ((0.0, tspan), tstep)
306+
return ((0.0, tspan), dt)
312307
end
313308

314309
function ReactiveNetwork(
315310
acs::ReactionNetwork,
316311
u0 = Dict(),
317-
p = DiffEqBase.NullParameters();
312+
p = Dict();
318313
name = "reactive_network",
319314
kwargs...,
320315
)
@@ -325,39 +320,21 @@ function ReactiveNetwork(
325320
])
326321
merge!(keywords, Dict(collect(kwargs)))
327322
merge!(keywords, Dict(:strategy => get(keywords, :alloc_strategy, :weighted)))
323+
328324
keywords[:tspan], keywords[:tstep] = get_tcontrol(keywords[:tspan], keywords)
329325

330326
acs = remove_choose(acs)
331327
attrs, transitions, wrap_fun = compile_attrs(acs)
332-
333-
init_u!(state)
334-
save!(state)
328+
transition_recipes = transitions
329+
u0_init = zeros(nparts(acs, :S))
335330

336-
u0_init = zeros(nparts(state, :S))
337-
338-
u0 isa Dict && foreach(
339-
i ->
340-
if !isnothing(acs[i, :specName]) && haskey(u0, acs[i, :specName])
341-
u0_init[i] = u0[acs[i, :specName]]
342-
end,
343-
1:nparts(state, :S),
344-
)
345-
346-
p_ = p == DiffEqBase.NullParameters() ? Dict() : Dict(k => v for (k, v) in p)
347-
prob = remake(
348-
prob;
349-
u0 = prob.u0,
350-
tspan = keywords[:tspan],
351-
dt = get(keywords, :tstep, 1),
352-
p = merge(
353-
prob.p,
354-
p_,
355-
Dict(
356-
:tstep => get(keywords, :tstep, 1),
357-
:strategy => get(keywords, :alloc_strategy, :weighted),
358-
),
359-
),
360-
)
331+
for i in 1:nparts(acs, :S)
332+
if !isnothing(acs[i, :specName]) && haskey(u0, acs[i, :specName])
333+
u0_init[i] = u0[acs[i, :specName]]
334+
else
335+
u0_init[i] = acs[i, :specInitVal]
336+
end
337+
end
361338

362339
ongoing_transitions = Transition[]
363340
log = NamedTuple[]
@@ -369,21 +346,19 @@ function ReactiveNetwork(
369346
) [:transLHS, :transRHS, :transToSpawn, :transHash]
370347
transitions = Dict{Symbol,Vector}(a => [] for a in transitions_attrs)
371348

372-
return ReactiveNetwork(
349+
network = ReactiveNetwork(
373350
name,
374351
acs,
375352
attrs,
376353
transition_recipes,
377354
u0_init,
378355
merge(
379-
prob.p,
380-
p_,
356+
p,
381357
Dict(
382358
:tstep => get(keywords, :tstep, 1),
383359
:strategy => get(keywords, :alloc_strategy, :weighted),
384360
),
385361
),
386-
t,
387362
keywords[:tspan][1],
388363
keywords[:tspan],
389364
get(keywords, :tstep, 1),
@@ -396,31 +371,32 @@ function ReactiveNetwork(
396371
Vector{Float64}[],
397372
Float64[],
398373
)
374+
375+
save!(network)
376+
377+
return network
399378
end
400379

401380
function AlgebraicAgents.step!(state::ReactiveNetwork)
402-
du = copy(state.u)
381+
#du = copy(state.u)
403382
free_blocked_species!(state)
404-
du .= state.u
383+
#du .= state.u
405384
update_observables(state)
406385
sample_transitions!(state)
407-
evolve!(du, state)
408-
finish!(du, state)
409-
update_u!(state, du)
386+
evolve!(state)
387+
finish!(state)
388+
#update_u!(state, u)
410389
event_action!(state)
411390

412-
du .= state.u
413391
push!(
414392
state.log,
415-
(:valuation, t, du' * [state[i, :specValuation] for i = 1:nparts(state, :S)]),
393+
(:valuation, state.t, state.u' * [state[i, :specValuation] for i = 1:nparts(state, :S)]),
416394
)
417395

418-
t = (state.t += state.solverargs[:tstep])
419-
update_u!(state, du)
396+
#update_u!(state, du)
420397
save!(state)
421-
sync_p!(p, state)
422398

423-
state.u .= du
399+
#state.u .= du
424400
state.t += state.dt
425401
end
426402

src/state.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using DiffEqBase: NullParameters
2-
31
using AlgebraicAgents
42

53
struct UnfoldedReactant
@@ -77,7 +75,7 @@ function init_u!(state::ReactiveNetwork)
7775
state.u = u)
7876
end
7977
function save!(state::ReactiveNetwork)
80-
return (push!(state.history_u, state.u); push!(state.history_t, state.t))
78+
return (push!(state.history_u, copy(state.u)); push!(state.history_t, state.t))
8179
end
8280

8381
function compile_observables(acs::ReactionNetwork)
File renamed without changes.

tutorial/basics.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
using ReactiveDynamics
1+
using ReactiveDynamics, AlgebraicAgents
22

33
# acs = @ReactionNetwork begin
44
# 1.0, X ⟺ Y
55
# end
66

77
acs = @ReactionNetwork begin
8-
1.0, X Y, name => "transition1"
8+
1.0, X --> Y, name => "transition1"
99
end
1010

1111
@prob_init acs X = 10 Y = 20
1212
@prob_params acs
1313
@prob_meta acs tspan = 250 dt = 0.1
1414

15-
prob = @problematize acs
1615

1716
# sol = ReactiveDynamics.solve(prob)
1817

19-
sol = @solve prob
18+
#sol = @solve prob
19+
20+
prob = @agentize acs
2021

22+
for i in 1:30
23+
AlgebraicAgents.step!(prob)
24+
end
25+
prob.history_u
2126
using Plots
2227

2328
@plot sol plot_type = summary
29+
30+
31+
prob = @problematize acs
32+
@solve prob

0 commit comments

Comments
 (0)