Skip to content

Commit 6d2aebc

Browse files
committed
Add QN creation and sampling
1 parent 81a91a0 commit 6d2aebc

File tree

6 files changed

+123
-2
lines changed

6 files changed

+123
-2
lines changed

Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ authors = ["Reuben Gardos Reid <[email protected]>"]
44
version = "0.1.0"
55

66
[deps]
7+
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
78
DynamicalSystems = "61744808-ddfa-5f27-97ff-6e42cc95d634"
89
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
10+
HerbCore = "2b23ba43-8213-43cb-b5ea-38c12b45bd45"
11+
HerbGrammar = "4ef9e186-2fe5-4b24-8de7-9f7291f24af7"
12+
HerbSearch = "3008d8e8-f9aa-438a-92ed-26e9c7b4829f"
913
MetaGraphsNext = "fa8bd995-216d-47f1-8a91-f3b68fbeb377"
1014
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1115
SoleLogics = "b002da8f-3cb3-4d91-bbe3-2953433912b5"

src/GraphDynamicalSystems.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ using SoleLogics
44
using MetaGraphsNext
55
using DynamicalSystems
66

7-
include("BooleanNetworks.jl")
8-
7+
include("boolean_networks.jl")
98
export BooleanNetworks
109

10+
include("qualitative_networks.jl")
11+
export QualitativeNetworks
12+
1113
# """
1214
# gds(bn::MetaGraph)
1315

src/qualitative_networks.jl

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
module QualitativeNetworks
2+
3+
using AbstractTrees: Leaves
4+
using HerbCore: AbstractGrammar, AbstractRuleNode, RuleNode, get_rule
5+
using HerbGrammar: @csgrammar, add_rule!
6+
using HerbSearch
7+
using MetaGraphsNext: MetaGraph, SimpleDiGraph, add_edge!
8+
9+
const base_qn_grammar = @csgrammar begin
10+
Vals = Pos() | Neg()
11+
Val =
12+
(Val + Val) |
13+
(Val - Val) |
14+
Val / Val |
15+
Avg(Vals) |
16+
Min(Val, Val) |
17+
Max(Val, Val) |
18+
Ceil(Val) |
19+
Floor(Val)
20+
end
21+
22+
const default_qn_constants = [2]
23+
24+
function build_qn_grammar(
25+
entity_names::AbstractVector{Symbol},
26+
constants::AbstractVector{Int},
27+
)
28+
g = deepcopy(base_qn_grammar)
29+
30+
for e in entity_names
31+
add_rule!(g, :(Val = $e))
32+
end
33+
34+
for c in constants
35+
add_rule!(g, :(Val = $c))
36+
end
37+
38+
return g
39+
end
40+
41+
function update_functions_to_network(
42+
update_functions::AbstractDict{Symbol,<:AbstractRuleNode},
43+
grammar::AbstractGrammar,
44+
)
45+
network = MetaGraph(
46+
SimpleDiGraph();
47+
label_type = Symbol,
48+
vertex_data_type = AbstractRuleNode,
49+
graph_data = grammar,
50+
)
51+
52+
for (e, f) in update_functions
53+
network[e] = f
54+
end
55+
56+
for (e1, f) in update_functions
57+
leaves = get_rule.(collect(Leaves(f)))
58+
input_variables = grammar.rules[leaves]
59+
for e2 in input_variables
60+
add_edge!(network, e1, e2)
61+
end
62+
end
63+
64+
return network
65+
end
66+
67+
function sample_qualitative_network(entities::AbstractVector{Symbol}, max_eq_depth::Int)
68+
g = build_qn_grammar(entities, default_qn_constants)
69+
update_fns = Dict([e => rand(RuleNode, g, :Val, max_eq_depth) for e in entities])
70+
graph = update_functions_to_network(update_fns, g)
71+
72+
return graph
73+
end
74+
75+
function sample_qualitative_network(size::Int, max_eq_depth::Int)
76+
entities = [Symbol(e) for e = 1:size]
77+
sample_qualitative_network(entities, max_eq_depth)
78+
end
79+
80+
mutable struct QualitativeNetwork
81+
graph::MetaGraph
82+
state::AbstractVector{Int}
83+
end
84+
85+
const QN = QualitativeNetwork
86+
87+
function qn_step!(model::QN) end
88+
89+
extract_state(model::QN) = model.state
90+
extract_parameters(model::QN) = model.graph
91+
92+
function reset_model!(model::QN, u, _)
93+
model.state .= u
94+
end
95+
96+
end

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[deps]
22
DynamicalSystems = "61744808-ddfa-5f27-97ff-6e42cc95d634"
3+
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
34
MetaGraphsNext = "fa8bd995-216d-47f1-8a91-f3b68fbeb377"
45
SoleLogics = "b002da8f-3cb3-4d91-bbe3-2953433912b5"
56
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/test-qn.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Graphs: ne, nv
2+
3+
@testset "QN Grammar Creation" begin
4+
entities = [:a, :b, :c]
5+
constants = [i for i = 1:10]
6+
g = QualitativeNetworks.build_qn_grammar(entities, constants)
7+
8+
@test issubset(Set(entities), Set(g.rules))
9+
@test issubset(Set(constants), Set(g.rules))
10+
end
11+
12+
@testset "QN Sampling" begin
13+
size = 3
14+
max_eq_depth = 3
15+
qn = QualitativeNetworks.sample_qualitative_network(size, max_eq_depth)
16+
@test nv(qn.graph) == size
17+
@test ne(qn.graph) > 0
18+
end

0 commit comments

Comments
 (0)