Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agents refactor #14

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright © 2022 Merck & Co., Inc., Rahway, NJ, USA and its affiliates. All rights reserved.
Copyright © 2023 Merck & Co., Inc., Rahway, NJ, USA and its affiliates. All rights reserved.

Jan Bima (MSD), Otto Ritter (MSD), Sean L. Wu (Merck)

Expand Down
36 changes: 3 additions & 33 deletions LICENSES_THIRD_PARTY
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
ReactiveDynamics.jl depends on third-party Julia packages which may be distributed under different licenses. We have listed all of these third party packages and their licenses below. For the most up-to-date information, see `Project.toml`.
ReactiveDynamics.jl depends on third-party Julia packages, which may be distributed under various licenses. For the most recent list of these packages, refer to `Project.toml` and consult the license terms of the individual packages.

You must agree to the terms of these licenses, in addition to the ReactiveDynamics source code license, in order to use this software.
You must agree to the terms of these licenses, in addition to the ReactiveDynamics.jl source code license, in order to use this software.

--------------------------------------------------
Third party software listed by License type
--------------------------------------------------

MIT License (or adaptations) (https://www.opensource.org/licenses/MIT)
* The Julia Language - https://github.com/JuliaLang/julia/blob/master/LICENSE.md
* BenchmarkTools.jl - https://github.com/JuliaCI/BenchmarkTools.jl
* CSV - https://github.com/JuliaData/CSV.jl
* Catlab - https://github.com/AlgebraicJulia/Catlab.jl
* ComponentArrays.jl - https://github.com/jonniedie/ComponentArrays.jl
* Crayons - https://github.com/KristofferC/Crayons.jl
* DataFrames.jl - https://github.com/JuliaData/DataFrames.jl
* DiffEqBase.jl - https://github.com/SciML/DiffEqBase.jl
* DifferentialEquations.jl - https://github.com/SciML/DifferentialEquations.jl
* Distributions.jl - https://github.com/JuliaStats/Distributions.jl
* Documenter - https://github.com/JuliaDocs/Documenter.jl
* DocumenterMarkdown - https://github.com/JuliaDocs/DocumenterMarkdown.jl
* GeneratedExpressions.jl - https://github.com/Merck/GeneratedExpressions.jl
* IJulia - https://github.com/JuliaLang/IJulia.jl
* JLD2 - https://github.com/JuliaIO/JLD2.jl
* JSON.jl - https://github.com/JuliaIO/JSON.jl
* MacroTools.jl - https://github.com/FluxML/MacroTools.jl
* NLopt - https://github.com/JuliaOpt/NLopt.jl
* OrdinaryDiffEq - https://github.com/SciML/OrdinaryDiffEq.jl
* Plots - https://github.com/JuliaPlots/Plots.jl
* Pluto.jl - https://github.com/fonsp/Pluto.jl
* Reexport - https://github.com/simonster/Reexport.jl
* SafeTestsets - https://github.com/YingboMa/SafeTestsets.jl
* Statistics.jl - https://github.com/JuliaStats/Statistics.jl
* StatsFuns.jl - https://github.com/JuliaStats/StatsFuns.jl
* Symbolics.jl - https://github.com/JuliaSymbolics/Symbolics.jl
* TOML.jl - https://github.com/JuliaLang/TOML.jl
* Tables.jl - https://github.com/JuliaData/Tables.jl

The Unlicense (https://opensource.org/licenses/unlicense)
* PlutoUI.jl - https://github.com/JuliaPluto/PlutoUI.jl
* The Julia Language - https://github.com/JuliaLang/julia/blob/master/LICENSE.md
49 changes: 19 additions & 30 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,63 +1,52 @@
name = "ReactiveDynamics"
uuid = "c7456e7d-545a-4b79-91ea-6e93d96dd4d4"
version = "0.2.7"
version = "0.2.8"

[deps]
ACSets = "227ef7b5-1206-438b-ac65-934d6da304b8"
AlgebraicAgents = "f6eb0ae3-10fa-40e6-88dd-9006ba45093a"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
DocumenterMarkdown = "997ab1e6-3595-5248-9280-8efb232c3433"
GeneratedExpressions = "84d730a5-1eb9-4187-a799-27dd07f33a14"
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
NLopt = "76087f3c-5699-56af-9a33-bf431cd00edd"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Pluto = "c3e4b0f8-55cb-11ea-2926-15256bba5781"
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
julia = "1.9"
DifferentialEquations = "7.9"
StatsFuns = "1.3"
Catlab = "0.14"
DataFrames = "1.6"
PlutoUI = "0.7"
Statistics = "1.9"
DocumenterMarkdown = "0.2"
ACSets = "0.2.6"
CSV = "0.10"
ComponentArrays = "0.14"
JLD2 = "0.4"
Crayons = "4.1"
DataFrames = "1.6"
Distributions = "0.25"
GeneratedExpressions = "0.1"
DiffEqBase = "6.128"
JSON = "0.21"
NLopt = "1.0"
OrdinaryDiffEq = "6.55"
Symbolics = "5.5"
IJulia = "1.24"
SafeTestsets = "0.1"
CSV = "0.10"
JLD2 = "0.4"
JSON = "0.21"
MacroTools = "0.5"
Plots = "1.39"
Pluto = "0.19"
PlutoUI = "0.7"
Reexport = "1.2"
SafeTestsets = "0.1"
Statistics = "1.9"
StatsFuns = "1.3"
Symbolics = "5.5"
TOML = "1.0"
MacroTools = "0.5"
Crayons = "4.1"
Documenter = "0.27"
Tables = "1.10"
Distributions = "0.25"
Pluto = "0.19"
julia = "1.9"
4 changes: 2 additions & 2 deletions docs/build/index.html

Large diffs are not rendered by default.

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
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Follow the SIR model's reactions:
using ReactiveDynamics

# model dynamics
sir_acs = @ReactionNetwork begin
sir_acs = @ReactionNetworkSchema begin
α*S*I, S+I --> 2I, name=>I2R
β*I, I --> R, name=>R2S
end
Expand Down Expand Up @@ -196,7 +196,7 @@ To harness the capabilities of **GeneratedExpressions.jl**, let us first declare
end

# generate submodel dynamics
push!(rd_models, @ReactionNetwork begin
push!(rd_models, @ReactionNetworkSchema begin
M[$i][$m, $n], state[$m] + {demand[$i][$m, $n, $l]*resource[$l], l=1:$r, dlm=+} --> state[$n] +
{production[$i][$m, $n, $l]*resource[$l], l=1:$r, dlm=+}, cycle_time=>cycle_times[$i][$m, $n], probability_of_success=>$m*$n/(n[$i])^2
end m=1:ReactiveDynamics.ns[$i] n=1:ReactiveDynamics.ns[$i]
Expand Down Expand Up @@ -292,7 +292,7 @@ end
Next we set up a simple dynamics and supply initial parameters.

```julia
acs = @ReactionNetwork begin
acs = @ReactionNetworkSchema begin
function_to_learn(A, B, C, params), A --> B+C
1., B --> C
2., C --> B
Expand Down
132 changes: 67 additions & 65 deletions src/ReactiveDynamics.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module ReactiveDynamics

using Catlab, Catlab.CategoricalAlgebra, Catlab.Present
using ACSets
using Reexport
using MacroTools
using NLopt
using ComponentArrays

@reexport using GeneratedExpressions
Expand All @@ -28,60 +27,64 @@ Base.@kwdef mutable struct FoldedObservable
on::Vector{SampleableValues} = SampleableValues[]
end

@present TheoryReactionNetwork(FreeSchema) begin
(S, T)::Ob # species, transitions

(
SymbolicAttributeT,
DescriptiveAttributeT,
SampleableAttributeT,
ModalityAttributeT,
PcsOptT,
PrmAttributeT,
)::AttrType

specName::Attr(S, SymbolicAttributeT)
specModality::Attr(S, ModalityAttributeT)
specInitVal::Attr(S, SampleableAttributeT)
specInitUncertainty::Attr(S, SampleableAttributeT)
(specCost, specReward, specValuation)::Attr(S, SampleableAttributeT)

trans::Attr(T, SampleableAttributeT)
transPriority::Attr(T, SampleableAttributeT)
transRate::Attr(T, SampleableAttributeT)
transCycleTime::Attr(T, SampleableAttributeT)
transProbOfSuccess::Attr(T, SampleableAttributeT)
transCapacity::Attr(T, SampleableAttributeT)
transMaxLifeTime::Attr(T, SampleableAttributeT)
transPostAction::Attr(T, SampleableAttributeT)
transMultiplier::Attr(T, SampleableAttributeT)
transName::Attr(T, DescriptiveAttributeT)

E::Ob # events
(eventTrigger, eventAction)::Attr(E, SampleableAttributeT)

obs::Ob # processes (observables)
obsName::Attr(obs, SymbolicAttributeT)
obsOpts::Attr(obs, PcsOptT)

(P, M)::Ob # model params, solver args

prmName::Attr(P, SymbolicAttributeT)
prmVal::Attr(P, PrmAttributeT)

metaKeyword::Attr(M, SymbolicAttributeT)
metaVal::Attr(M, SampleableAttributeT)
end
TheoryReactionNetwork = BasicSchema(
[:S, :T, :E, :obs, :P, :M], # species, transitions, events, processes (observables), model params, solver args
[], # no homs
[
:SymbolicAttributeT,
:DescriptiveAttributeT,
:SampleableAttributeT,
:ModalityAttributeT,
:PcsOptT,
:PrmAttributeT,
:BoolAttributeT,
], # AttrTypes
[
# species
(:specName, :S, :SymbolicAttributeT),
(:specModality, :S, :ModalityAttributeT),
(:specInitVal, :S, :SampleableAttributeT),
(:specInitUncertainty, :S, :SampleableAttributeT),
(:specCost, :S, :SampleableAttributeT),
(:specReward, :S, :SampleableAttributeT),
(:specValuation, :S, :SampleableAttributeT),
(:specStructured, :S, :BoolAttributeT),
# transitions
(:trans, :T, :SampleableAttributeT),
(:transPriority, :T, :SampleableAttributeT),
(:transRate, :T, :SampleableAttributeT),
(:transCycleTime, :T, :SampleableAttributeT),
(:transProbOfSuccess, :T, :SampleableAttributeT),
(:transCapacity, :T, :SampleableAttributeT),
(:transMaxLifeTime, :T, :SampleableAttributeT),
(:transPreAction, :T, :SampleableAttributeT),
(:transPostAction, :T, :SampleableAttributeT),
(:transMultiplier, :T, :SampleableAttributeT),
(:transName, :T, :DescriptiveAttributeT),
# events
(:eventTrigger, :E, :SampleableAttributeT),
(:eventAction, :E, :SampleableAttributeT),
# observables
(:obsName, :obs, :SymbolicAttributeT),
(:obsOpts, :obs, :PcsOptT),
# params, args
(:prmName, :P, :SymbolicAttributeT),
(:prmVal, :P, :PrmAttributeT),
(:metaKeyword, :M, :SymbolicAttributeT),
(:metaVal, :M, :SampleableAttributeT),
],
)

@acset_type FoldedReactionNetworkType(TheoryReactionNetwork)

const ReactionNetwork = FoldedReactionNetworkType{
const ReactionNetworkSchema = FoldedReactionNetworkType{
Symbol,
Union{String,Symbol,Missing},
SampleableValues,
Set{Symbol},
FoldedObservable,
Any,
Bool,
}

Base.convert(::Type{Symbol}, ex::String) = Symbol(ex)
Expand All @@ -94,12 +97,14 @@ Base.convert(::Type{Union{String,Symbol,Missing}}, ex::String) =
end

Base.convert(::Type{SampleableValues}, ex::String) = MacroTools.striplines(Meta.parse(ex))

Base.convert(::Type{Set{Symbol}}, ex::String) = eval(Meta.parse(ex))
Base.convert(::Type{FoldedObservable}, ex::String) = eval(Meta.parse(ex))

prettynames = Dict(
:transRate => [:rate],
:specInitUncertainty => [:uncertainty, :stoch, :stochasticity],
:transPreAction => [:preAction, :action, :pre],
:transPostAction => [:postAction, :post],
:transName => [:name, :interpretation],
:transPriority => [:priority],
Expand All @@ -117,6 +122,7 @@ defargs = Dict(
:transCycleTime => 0.0,
:transMaxLifeTime => Inf,
:transMultiplier => 1,
:transPreAction => :(),
:transPostAction => :(),
:transName => missing,
),
Expand All @@ -126,47 +132,43 @@ defargs = Dict(
:specCost => 0.0,
:specReward => 0.0,
:specValuation => 0.0,
:specStructured => false,
),
:P => Dict{Symbol,Any}(:prmVal => missing),
:M => Dict{Symbol,Any}(:metaVal => missing),
)

compilable_attrs =
filter(attr -> eltype(attr) == SampleableValues, propertynames(ReactionNetwork()))
filter(attr -> eltype(attr) == SampleableValues, propertynames(ReactionNetworkSchema()))

species_modalities = [:nonblock, :conserved, :rate]

function assign_defaults!(acs::ReactionNetwork)
function assign_defaults!(acs::ReactionNetworkSchema)
for (_, v_) in defargs, (k, v) in v_
for i = 1:length(subpart(acs, k))
isnothing(acs[i, k]) && (subpart(acs, k)[i] = v)
for i in dom_parts(acs, k)
isnothing(acs[i, k]) && (acs[i, k] = v)
end
end

foreach(
i ->
!isnothing(acs[i, :specModality]) ||
(subpart(acs, :specModality)[i] = Set{Symbol}()),
1:nparts(acs, :S),
i -> !isnothing(acs[i, :specModality]) || (acs[i, :specModality] = Set{Symbol}()),
parts(acs, :S),
)
k = [:specCost, :specReward, :specValuation]
foreach(
k -> foreach(
i -> !isnothing(acs[i, k]) || (subpart(acs, k)[i] = 0.0),
1:nparts(acs, :S),
),
k -> foreach(i -> !isnothing(acs[i, k]) || (acs[i, k] = 0.0), parts(acs, :S)),
k,
)

return acs
end

function ReactionNetwork(transitions, reactants, obs, events)
return merge_acs!(ReactionNetwork(), transitions, reactants, obs, events)
function ReactionNetworkSchema(transitions, reactants, obs, events)
return merge_acs!(ReactionNetworkSchema(), transitions, reactants, obs, events)
end

function ReactionNetwork(transitions, reactants, obs)
return merge_acs!(ReactionNetwork(), transitions, reactants, obs, [])
function ReactionNetworkSchema(transitions, reactants, obs)
return merge_acs!(ReactionNetworkSchema(), transitions, reactants, obs, [])
end

function add_obs!(acs, obs)
Expand Down Expand Up @@ -195,7 +197,7 @@ function add_obs!(acs, obs)
return acs
end

function merge_acs!(acs::ReactionNetwork, transitions, reactants, obs, events)
function merge_acs!(acs::ReactionNetworkSchema, transitions, reactants, obs, events)
foreach(
t -> add_part!(acs, :T; trans = t[1][2], transRate = t[1][1], t[2]...),
transitions,
Expand All @@ -220,7 +222,7 @@ include.(readdir(joinpath(@__DIR__, "interface"); join = true))
include.(readdir(joinpath(@__DIR__, "utils"); join = true))
include.(readdir(joinpath(@__DIR__, "operators"); join = true))
include("solvers.jl")
include("optim.jl")
#include("optim.jl")
include("loadsave.jl")

end
Loading
Loading