updated advancedSimulation

This commit is contained in:
JuliusHerrmann 2021-07-15 04:54:47 +02:00
parent f1bce8dc4e
commit 3961ee0030
4 changed files with 40 additions and 10 deletions

View File

@ -10,7 +10,7 @@
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": ["I R 1.0 R S 0.7 I S I I 0.8"]
"args": ["I R 1.0 R S 0.7 I S I I 0.8", "S I R", "0.5 0.5 0"]
}
]
}

Binary file not shown.

21
python/graphUtils.py Normal file
View File

@ -0,0 +1,21 @@
def edgelistToDot(name, inp):
output = "strict graph \"" + name + "\" {\n"
for (x,y) in inp:
output += f" {x} -- {y};\n"
output += "}"
return output
def dotToEdgelist(graph):
outStr = []
graph = graph.split("\n")
name = graph[0].split(" ")[2]
name = name[1:len(name) - 1]
for i in range(len(graph) - 1):
if(i == 0):
continue
nodes = graph[i].split("--")
node1 = int(nodes[0])
node2 = int(nodes[1][0:len(nodes[1]) - 1])
outStr.append((node1, node2,))
return (name, outStr)

View File

@ -1,11 +1,12 @@
import numpy as np
import sys
import graphUtils
statesComp = ["S", "I", "R"]
rulesComp = [("I", "R", 1.0), # spontaneous rule I -> R with rate 1.0
("R", "S", 0.7), # spontaneous rule R -> S with rate 0.7
(("I","S"),("I","I"), 0.8)] # contact rule I+S -> I+I with rate 0.4
allStates = []
simulation = []
class Rule:
def __init__(self, ruleParts, probability):
@ -65,7 +66,6 @@ def stringToRule(Input):
while(len(Input) > 0):
newString = parseState(Input)
rulePartsBuffer.append(newString[0])
allStates.append(newString[0])
# check if we are at the end
if(newString[1][0].isdigit()):
@ -80,25 +80,34 @@ def stringToRule(Input):
output.append(r.getOutput())
return output
def generateStatesList():
def stringToStates(inp):
inp = inp.split(" ")
out = []
for s in allStates:
if(not(s in out)):
for s in inp:
out.append(s)
return out
def stringToDistr(inp):
inp = inp.split(" ")
out = []
for i in inp:
out.append(float(i))
return out
# parse the input wohooooo
rules = stringToRule(sys.argv[1])
states = generateStatesList()
states = stringToStates(sys.argv[2])
initial_distribution = stringToDistr(sys.argv[3])
simulation = []
graph_as_edgelist = [(0, 4), (0, 1), (1, 5), (1, 2), (2, 6), (2, 3), (3, 7), (4, 8), (4, 5), (5, 9), (5, 6), (6, 10), (6, 7), (7, 11), (8, 12), (8, 9), (9, 13), (9, 10), (10, 14), (10, 11), (11, 15), (12, 13), (13, 14), (14, 15)]
print(graphUtils.edgelistToDot("testGraph", graph_as_edgelist))
print(graphUtils.dotToEdgelist("strict graph 'testGraph' {\n0 -- 4;\n0 -- 1;\n1 -- 5;\n}")[0])
horizon = 20.0 # wie lange wird simuliert
initial_distribution = [0.5, 0.5, 0.0] # gleiche Reihenfolge wie states, musss zu rules passen und normalisiert werden
# initial_distribution = [0.5, 0.5, 0.0] # gleiche Reihenfolge wie states, musss zu rules passen und normalisiert werden
timepoint_num = 101
def get_next_state(current_labels):
fastes_firing_time = 10000000.0 #dummy