示例#1
0
    def test_create_AP_labels(self):
        model = Model(set(), collections.Counter(), dict(), set())

        complex_parser = Parser("rate_complex")
        complex_1 = complex_parser.parse("K(S{i},T{a}).B{o}::cyt").data.children[0]
        complex_2 = complex_parser.parse("K(S{a},T{a}).B{o}::cyt").data.children[0]
        complex_3 = complex_parser.parse("K(S{a},T{i}).B{o}::cyt").data.children[0]

        complex_abstract = complex_parser.parse("K(S{a}).B{_}::cyt").data.children[0]

        ordering = (complex_1, complex_2, complex_3)

        APs = [Core.Formula.AtomicProposition(complex_abstract, " >= ", "3"),
               Core.Formula.AtomicProposition(complex_1, " < ", 2)]

        s1 = State(np.array((1, 2, 2)))
        s2 = State(np.array((5, 1, 1)))
        s3 = State(np.array((2, 4, 3)))
        s4 = State(np.array((1, 4, 3)))

        states_encoding = {s1: 1, s2: 2, s3: 3, s4: 4}

        result_AP_lables = {APs[0]: 'property_0', APs[1]: 'property_1'}
        result_state_labels = {1: {'property_0', 'property_1'},
                               3: {'property_0', 'init'},
                               4: {'property_0', 'property_1'}}

        ts = TS.TransitionSystem.TransitionSystem(ordering)
        ts.states_encoding = states_encoding
        ts.init = 3

        state_labels, AP_lables = model.create_AP_labels(APs, ts, 0)
        self.assertEqual(state_labels, result_state_labels)
        self.assertEqual(AP_lables, result_AP_lables)
示例#2
0
def load_TS_from_json(json_file: str) -> TransitionSystem:
    """
    Loads given JSON and interprets it as a TransitionSystem.

    :param json_file: given TS in JSON
    :return: resulting TransitionSystem
    """
    complex_parser = Parser("rate_complex")
    with open(json_file) as json_file:
        data = json.load(json_file)

        ordering = SortedList(
            map(lambda agent: complex_parser.parse(agent).data.children[0],
                data['ordering']))
        ts = TransitionSystem(ordering)
        ts.states_encoding = {
            State(np.array(eval(data['nodes'][node_id]))): int(node_id)
            for node_id in data['nodes']
        }
        ts.edges = {edge_from_dict(edge) for edge in data['edges']}
        ts.init = data['initial']

        ts.unprocessed = {
            State(np.array(eval(state)))
            for state in data.get('unprocessed', list())
        }
        ts.processed = ts.states_encoding.keys() - ts.unprocessed
        return ts
示例#3
0
 def test_vectorize(self):
     ordering = (self.c2, self.c3)
     self.assertEqual(self.rate_1.vectorize(ordering, dict()),
                      [State(np.array([1, 1]))])
     ordering = (self.c2, self.c3, self.c4, self.c5, self.c6, self.c7)
     self.assertEqual(self.rate_2.vectorize(ordering, dict()), [
         State(np.array([0, 0, 1, 1, 0, 0])),
         State(np.array([1, 1, 0, 0, 0, 0]))
     ])
示例#4
0
    def setUp(self):
        self.s1 = State(np.array((1, 2, 3)))
        self.s2 = State(np.array((5, 4, 3)))
        self.s3 = State(np.array((5, 4, 3, 2)))
        self.s4 = State(np.array((2, 2, 2, 1)))
        self.s5 = State(np.array((7, 6, 5, 3)))
        self.s6 = State(np.array((1, 0, 0, 1, 0)))
        self.s_inf = State(np.array((np.inf, np.inf, np.inf)))

        complex_parser = Parsing.ParseBCSL.Parser("rate_complex")

        self.complex_1 = complex_parser.parse("K(S{i},T{a}).B{o}::cyt").data.children[0]
        self.complex_2 = complex_parser.parse("K(S{a},T{a}).B{o}::cyt").data.children[0]
        self.complex_3 = complex_parser.parse("K(S{a},T{i}).B{o}::cyt").data.children[0]
示例#5
0
    def change_hell(self, bound):
        """
        Changes hell from inf to bound + 1.

        TODO: maybe we could get rid of inf completely, but it is more clear for
        debugging purposes

        :param bound: given allowed bound
        """
        for key, value in self.states_encoding.items():
            if key.is_inf:
                del self.states_encoding[key]
                hell = State(np.array([bound + 1] * len(key)))
                hell.is_inf = True
                self.states_encoding[hell] = value
                break
示例#6
0
 def test_change_hell(self):
     ordering = (self.c1, self.c2, self.c3, self.c4)
     ts = TransitionSystem(ordering)
     ts.states_encoding = {self.s1: 1, self.s2: 2, self.s3: 0, self.hell: 3}
     ts.change_hell(4)
     new_hell = State(np.array([5, 5, 5]))
     new_encoding = {self.s1: 1, self.s2: 2, self.s3: 0, new_hell: 3}
     self.assertEqual(ts.states_encoding, new_encoding)
示例#7
0
    def to_vector(self, ordering: SortedList) -> State:
        """
        Convert the Side to a State accoring to given ordering.

        :param ordering: sequence of complex agents
        :return: State representing vector
        """
        vector = np.zeros(len(ordering), dtype=int)
        multiset = self.to_counter()
        for agent in list(multiset):
            vector[ordering.index(agent)] = multiset[agent]
        return State(vector)
示例#8
0
class TestState(unittest.TestCase):
    def setUp(self):
        self.s1 = State(np.array((1, 2, 3)))
        self.s2 = State(np.array((5, 4, 3)))
        self.s3 = State(np.array((5, 4, 3, 2)))
        self.s4 = State(np.array((2, 2, 2, 1)))
        self.s5 = State(np.array((7, 6, 5, 3)))
        self.s6 = State(np.array((1, 0, 0, 1, 0)))
        self.s_inf = State(np.array((np.inf, np.inf, np.inf)))

        complex_parser = Parsing.ParseBCSL.Parser("rate_complex")

        self.complex_1 = complex_parser.parse("K(S{i},T{a}).B{o}::cyt").data.children[0]
        self.complex_2 = complex_parser.parse("K(S{a},T{a}).B{o}::cyt").data.children[0]
        self.complex_3 = complex_parser.parse("K(S{a},T{i}).B{o}::cyt").data.children[0]

    def test_sub(self):
        self.assertEqual(self.s1 - self.s2, State(np.array((-4, -2, 0))))
        self.assertEqual(self.s3 - self.s4, State(np.array((3, 2, 1, 1))))
        with self.assertRaises(ValueError):
            self.s2 - self.s3

    def test_check_negative(self):
        self.assertFalse((self.s1 - self.s2).check_negative())
        self.assertTrue((self.s3 - self.s4).check_negative())

    def test_add_with_bound(self):
        bound = 5
        self.assertEqual(self.s1.add_with_bound(self.s2, bound), self.s_inf)
        bound = 8
        self.assertEqual(self.s3.add_with_bound(self.s4, bound), self.s5)

    def test_to_ODE_string(self):
        self.assertEqual(self.s6.to_ODE_string(), "y[0] + y[3]")

    def test_reorder(self):
        order = np.array([2, 0, 1])
        self.assertEqual(self.s1.reorder(order), State(np.array((3, 1, 2))))

    def test_check_AP(self):
        ordering = (self.complex_1, self.complex_2, self.complex_3)
        ap = AtomicProposition(self.complex_2, "<=", 2)
        self.assertTrue(self.s1.check_AP(ap, ordering))
        ap = AtomicProposition(self.complex_2, ">", 2)
        self.assertFalse(self.s1.check_AP(ap, ordering))

    def test_to_PRISM_string(self):
        self.assertEqual(self.s1.to_PRISM_string(), "(VAR_0=1) & (VAR_1=2) & (VAR_2=3)")
        self.assertEqual(self.s1.to_PRISM_string(True), "(VAR_0'=1) & (VAR_1'=2) & (VAR_2'=3)")
示例#9
0
    def setUp(self):
        # agents

        self.a1 = AtomicAgent("T", "i")
        self.a2 = AtomicAgent("S", "i")
        self.a3 = AtomicAgent("T", "a")
        self.a4 = AtomicAgent("S", "a")

        self.s1 = StructureAgent("X", {self.a4})
        self.s2 = StructureAgent("X", {self.a2})
        self.s3 = StructureAgent("K", {self.a3})
        self.s4 = StructureAgent("K", {self.a1})
        self.s5 = StructureAgent("X", set())
        self.s6 = StructureAgent("K", set())

        self.c1 = Complex([self.s6], "cyt")  # K()::cyt
        self.c2 = Complex([self.s3], "cyt")  # K(T{a})::cyt
        self.c3 = Complex([self.s4], "cyt")  # K(T{i})::cyt
        self.c4 = Complex([self.s4, self.s1], "cyt")  # K(T{i}).X(S{a})::cyt
        self.c5 = Complex([self.s4, self.s2], "cyt")  # K(T{i}).X(S{i})::cyt
        self.c6 = Complex([self.s3, self.s1], "cyt")  # K(T{a}).X(S{a})::cyt
        self.c7 = Complex([self.s3, self.s2], "cyt")  # K(T{a}).X(S{i})::cyt

        # rates

        self.parser = Parser("rate")
        rate_expr = "3.0*[K()::cyt]/2.0*v_1"

        self.rate_1 = Core.Rate.Rate(self.parser.parse(rate_expr).data)

        rate_expr = "3.0*[K(T{i}).X()::cyt] + [K()::cyt]"

        self.rate_2 = Core.Rate.Rate(self.parser.parse(rate_expr).data)

        # states

        self.state_1 = State(np.array([2, 3]))
        self.state_2 = State(np.array([2, 0, 3, 1, 6, 2]))
示例#10
0
    def setUp(self):
        self.s1 = StructureAgent("X", set())
        self.s2 = StructureAgent("Y", set())
        self.s3 = StructureAgent("Z", set())

        self.c1 = Complex([self.s1], "rep")
        self.c2 = Complex([self.s2], "rep")
        self.c3 = Complex([self.s3], "rep")

        ordering = (self.c1, self.c2, self.c3)
        params = {"k1": 0.05, "k2": 0.1}

        self.rate_parser = Parser("rate")
        rate_expr = "1/(1+([X()::rep])**2)"
        rate_1 = Rate(self.rate_parser.parse(rate_expr).data)
        rate_1.vectorize(ordering, params)

        rate_expr = "k1*[X()::rep]"
        rate_2 = Rate(self.rate_parser.parse(rate_expr).data)
        rate_2.vectorize(ordering, params)

        rate_expr = "k2*[Z()::rep]"
        rate_3 = Rate(self.rate_parser.parse(rate_expr).data)
        rate_3.vectorize(ordering, params)

        init = State(np.array([2.0, 1.0, 1.0]))

        vector_reactions = {
            VectorReaction(State(np.array([0.0, 0.0, 0.0])),
                           State(np.array([0.0, 1.0, 0.0])), rate_1),
            VectorReaction(State(np.array([1.0, 0.0, 0.0])),
                           State(np.array([0.0, 0.0, 0.0])), rate_2),
            VectorReaction(State(np.array([0.0, 0.0, 1.0])),
                           State(np.array([1.0, 0.0, 0.0])), None)
        }

        self.vm_1 = VectorModel(vector_reactions, init, ordering, None)

        vector_reactions = {
            VectorReaction(State(np.array([0.0, 0.0, 0.0])),
                           State(np.array([0.0, 1.0, 0.0])), rate_1),
            VectorReaction(State(np.array([1.0, 0.0, 0.0])),
                           State(np.array([0.0, 0.0, 0.0])), rate_2),
            VectorReaction(State(np.array([0.0, 0.0, 1.0])),
                           State(np.array([1.0, 0.0, 0.0])), rate_3)
        }

        self.vm_2 = VectorModel(vector_reactions, init, ordering, None)

        # test abstract model

        self.model_parser = Parser("model")

        self.model_abstract = \
            """#! rules
            => X()::rep @ k2*[T{_}::rep]
            T{a}::rep => T{i}::rep @ k1*[T{_}::rep]

            #! inits
            10 T{a}::rep

            #! definitions
            k1 = 0.05
            k2 = 0.12
            """

        # test transition system generating

        a1 = AtomicAgent("B", "a")
        a2 = AtomicAgent("S", "u")
        a3 = AtomicAgent("S", "p")
        a4 = AtomicAgent("T", "i")

        s1 = StructureAgent("K", {a3, a4})
        s2 = StructureAgent("K", {a2, a4})

        cx1 = Complex([a1], "cyt")
        cx2 = Complex([s1], "cyt")
        cx3 = Complex([s2], "cyt")
        cx4 = Complex([s1, a1], "cyt")
        cx5 = Complex([s2, a1], "cyt")

        ordering = (cx5, cx4, cx3, cx2, cx1)
        # (K(S{u},T{i}).B{a}::cyt, K(S{p},T{i}).B{a}::cyt, K(S{u},T{i})::cyt, K(S{p},T{i})::cyt, B{a}::cyt)

        self.model_TS = \
            """#! rules
            => K(S{u},T{i})::cyt @ omega
            K(S{u})::cyt => K(S{p})::cyt @ alpha*[K(S{u})::cyt]
            K(S{p})::cyt + B{a}::cyt => K(S{p}).B{a}::cyt @ beta*[K(S{p})::cyt]*[B{a}::cyt]
            B{_}::cyt => @ gamma*[B{_}::cyt]
            K(S{u},T{i}).B{a}::cyt => @ 5

            #! inits
            1 B{a}::cyt

            #! definitions
            alpha = 10
            beta = 5
            gamma = 2
            omega = 3
            """

        alpha = 10
        beta = 5
        gamma = 2
        omega = 3

        self.test_ts = TransitionSystem(ordering)

        states = [
            State(np.array((0.0, 0.0, 0.0, 0.0, 1.0))),
            State(np.array((0.0, 0.0, 0.0, 0.0, 0.0))),
            State(np.array((0.0, 0.0, 1.0, 0.0, 0.0))),
            State(np.array((0.0, 0.0, 1.0, 0.0, 1.0))),
            State(np.array((np.inf, np.inf, np.inf, np.inf, np.inf))),
            State(np.array((0.0, 0.0, 0.0, 1.0, 1.0))),
            State(np.array((0.0, 0.0, 1.0, 1.0, 1.0))),
            State(np.array((0.0, 1.0, 0.0, 0.0, 0.0))),
            State(np.array((0.0, 0.0, 0.0, 1.0, 0.0))),
            State(np.array((0.0, 0.0, 1.0, 1.0, 0.0))),
            State(np.array((0.0, 1.0, 1.0, 0.0, 0.0))),
            State(np.array((0.0, 1.0, 0.0, 1.0, 0.0))),
            State(np.array((0.0, 1.0, 1.0, 1.0, 0.0)))
        ]

        # in edges we have probabilities, not rates, so we must normalise
        go = gamma + omega  # 5
        goa = gamma + omega + alpha  # 15
        goab = gamma + omega + alpha + beta  # 20
        gob = gamma + omega + beta  # 10
        oa = omega + alpha  # 13

        self.test_ts.processed = set(states)

        self.test_ts.edges = {
            Edge(states[0], states[1], gamma / go),
            Edge(states[0], states[3], omega / go),
            Edge(states[1], states[2], omega / omega),
            Edge(states[2], states[4], omega / oa),
            Edge(states[2], states[8], alpha / oa),
            Edge(states[3], states[2], gamma / goa),
            Edge(states[3], states[4], omega / goa),
            Edge(states[3], states[5], alpha / goa),
            Edge(states[4], states[4], 1),
            Edge(states[5], states[6], omega / gob),
            Edge(states[5], states[7], beta / gob),
            Edge(states[5], states[8], gamma / gob),
            Edge(states[6], states[4], oa / goab),
            Edge(states[6], states[9], gamma / goab),
            Edge(states[6], states[10], beta / goab),
            Edge(states[7], states[10], omega / omega),
            Edge(states[8], states[9], gamma / gamma),
            Edge(states[9], states[4], 1),
            Edge(states[10], states[4], omega / oa),
            Edge(states[10], states[11], alpha / oa),
            Edge(states[11], states[12], omega / omega),
            Edge(states[12], states[4], 1)
        }

        self.test_ts.encode(states[0])

        # bigger TS

        self.model_bigger_TS = \
            """#! rules
            => 2 K(S{u},T{i})::cyt @ omega
            K(S{u})::cyt => K(S{p})::cyt @ alpha*[K(S{u})::cyt]
            K(S{p})::cyt + B{a}::cyt => K(S{p}).B{a}::cyt @ beta*[K(S{p})::cyt]*[B{a}::cyt]
            B{_}::cyt => @ gamma*[B{_}::cyt]
            K(S{u},T{i}).B{a}::cyt => @ 5

            #! inits
            6 B{a}::cyt

            #! definitions
            alpha = 10
            beta = 5
            gamma = 2
            omega = 3
            """

        # even bigger TS

        self.model_even_bigger_TS = \
            """#! rules
            => K(S{u},T{i})::cyt @ omega
            K(S{u})::cyt => K(S{p})::cyt @ alpha*[K(S{u})::cyt]
            K(S{p})::cyt + B{a}::cyt => K(S{p}).B{a}::cyt @ beta*[K(S{p})::cyt]*[B{a}::cyt]
            B{_}::cyt => @ gamma*[B{_}::cyt]
            K(S{u},T{i}).B{a}::cyt => @ 5

            #! inits
            10 B{a}::cyt

            #! definitions
            alpha = 10
            beta = 5
            gamma = 2
            omega = 3
            """

        self.model_parametrised = \
            """#! rules
            => K(S{u},T{i})::cyt @ omega
            K(S{u})::cyt => K(S{p})::cyt @ alpha*[K(S{u})::cyt]
            K(S{p})::cyt + B{a}::cyt => K(S{p}).B{a}::cyt @ beta*[K(S{p})::cyt]*[B{a}::cyt]
            B{_}::cyt => @ gamma*[B{_}::cyt]
            K(S{u},T{i}).B{a}::cyt => @ 5

            #! inits
            1 B{a}::cyt

            #! definitions
            alpha = 10
            beta = 5
            //gamma = 2
            omega = 3
            """

        self.model_with_sinks = \
            """#! rules
示例#11
0
    def setUp(self):
        # agents

        self.s1 = StructureAgent("X", set())
        self.s2 = StructureAgent("Y", set())
        self.s3 = StructureAgent("Z", set())

        self.c1 = Complex([self.s1], "rep")
        self.c2 = Complex([self.s2], "rep")
        self.c3 = Complex([self.s3], "rep")

        #  rules

        sequence_1 = (self.s1,)
        mid_1 = 1
        compartments_1 = ["rep"]
        complexes_1 = [(0, 0)]
        pairs_1 = [(0, None)]
        rate_1 = Rate("k1*[X()::rep]")

        self.r1 = Rule(sequence_1, mid_1, compartments_1, complexes_1, pairs_1, rate_1)

        sequence_2 = (self.s3, self.s1)
        mid_2 = 1
        compartments_2 = ["rep"] * 2
        complexes_2 = [(0, 0), (1, 1)]
        pairs_2 = [(0, 1)]

        self.r2 = Rule(sequence_2, mid_2, compartments_2, complexes_2, pairs_2, None)

        sequence_3 = (self.s2,)
        mid_3 = 0
        compartments_3 = ["rep"]
        complexes_3 = [(0, 0)]
        pairs_3 = [(None, 0)]
        rate_3 = Rate("1.0/(1.0+([X()::rep])**4.0)")

        self.r3 = Rule(sequence_3, mid_3, compartments_3, complexes_3, pairs_3, rate_3)

        # inits

        self.inits = collections.Counter({self.c1: 2, self.c2: 1})

        # defs

        self.defs = {'k1': 0.05, 'k2': 0.12}

        self.model = Model({self.r1, self.r2, self.r3}, self.inits, self.defs, set())
        # model

        self.model_str_1 = """
        #! rules
        X()::rep => @ k1*[X()::rep]
        Z()::rep => X()::rep
        => Y()::rep @ 1/(1+([X()::rep])**4)

        #! inits
        2 X()::rep
        Y()::rep
        
        #! definitions
        k1 = 0.05
        k2 = 0.12   
        """

        self.model_parser = Parser("model")

        self.model_str_2 = """
        #! rules
        X(K{i})::rep => X(K{p})::rep @ k1*[X()::rep]
        X(T{a})::rep => X(T{o})::rep @ k2*[Z()::rep]
        => Y(P{f})::rep @ 1/(1+([X()::rep])**4)

        #! inits
        2 X(K{c}, T{e}).X(K{c}, T{j})::rep
        Y(P{g}, N{l})::rep

        #! definitions
        k1 = 0.05
        k2 = 0.12
        """

        # vectors

        ordering = (self.c1, self.c2, self.c3)

        self.rate_parser = Parser("rate")
        rate_expr = "1/(1+([X()::rep])**4)"
        rate_1 = Rate(self.rate_parser.parse(rate_expr).data)
        rate_1.vectorize(ordering, dict())

        rate_expr = "k1*[X()::rep]"
        rate_2 = Rate(self.rate_parser.parse(rate_expr).data)
        rate_2.vectorize(ordering, {"k1": 0.05})

        init = State(np.array([2, 1, 0]))

        vector_reactions = {VectorReaction(State(np.array([0, 0, 0])), State(np.array([0, 1, 0])), rate_1),
                            VectorReaction(State(np.array([1, 0, 0])), State(np.array([0, 0, 0])), rate_2),
                            VectorReaction(State(np.array([0, 0, 1])), State(np.array([1, 0, 0])), None)}

        self.vm_1 = VectorModel(vector_reactions, init, ordering, None)

        # wrong models

        self.model_wrong_1 = \
            """#! rules
            X(K{i})::rep => X(K{p})::rep @ k1*[X()::rep]
            X(T{a})::rep => X(T{o}):;rep @ k2*[Z()::rep]
            => Y(P{f})::rep @ 1/(1+([X()::rep])**4)

            #! inits
            2 X(K{c}, T{e}).X(K{c}, T{j})::rep
            Y(P{g}, N{l})::rep

            #! definitions
            k1 = 0.05
            k2 = 0.12
            """

        self.model_wrong_2 = \
            """#! rules
            X(K{i})::rep => X(K{p})::rep @ k1*[X()::rep]
            X(T{a})::rep = X(T{o})::rep @ k2*[Z()::rep]
            => Y(P{f})::rep @ 1/(1+([X()::rep])**4)

            #! inits
            2 X(K{c}, T{e}).X(K{c}, T{j})::rep
            Y(P{g}, N{l})::rep

            #! definitions
            k1 = 0.05
            k2 = 0.12
            """

        self.model_with_comments = """
            #! rules
            // commenting
            X(K{i})::rep => X(K{p})::rep @ k1*[X()::rep] // also here
            X(T{a})::rep => X(T{o})::rep @ k2*[Z()::rep]
            => Y(P{f})::rep @ 1/(1+([X()::rep])**4) // ** means power (^)

            #! inits
            // here
            2 X(K{c}, T{e}).X(K{c}, T{j})::rep
            Y(P{g}, N{l})::rep // comment just 1 item

            #! definitions
            // and
            k1 = 0.05 // also
            k2 = 0.12
            """
        
        self.model_with_complexes = """
            #! rules
            // commenting
            X(T{a}):XX::rep => X(T{o}):XX::rep @ k2*[X().X()::rep]
            K{i}:X():XYZ::rep => K{p}:X():XYZ::rep @ k1*[X().Y().Z()::rep] // also here
            => P{f}:XP::rep @ 1/(1+([X().P{_}::rep])**4) // ** means power (^)

            #! inits
            // here
            2 X(K{c}, T{e}).X(K{c}, T{j})::rep
            Y(P{g}, N{l})::rep // comment just 1 item

            #! definitions
            // and
            k1 = 0.05 // also
            k2 = 0.12

            #! complexes
            XYZ = X().Y().Z() // a big complex
            XX = X().X()
            XP = X().P{_}
            """

        self.model_without_complexes = """
            #! rules
            // commenting
            X(T{a}).X()::rep => X(T{o}).X()::rep @ k2*[X().X()::rep]
            X(K{i}).Y().Z()::rep => X(K{p}).Y().Z()::rep @ k1*[X().Y().Z()::rep] // also here
            => X().P{f}::rep @ 1/(1+([X().P{_}::rep])**4) // ** means power (^)

            #! inits
            // here
            2 X(K{c}, T{e}).X(K{c}, T{j})::rep
            Y(P{g}, N{l})::rep // comment just 1 item

            #! definitions
            // and
            k1 = 0.05 // also
            k2 = 0.12
            """

        self.model_with_variable = """
            #! rules
            // commenting
            T{a}:X():?::rep => T{o}:X():?::rep @ k2*[X().X()::rep] ; ? = { XX, XY }
            K{i}:X():XY::rep => K{p}:X():XY::rep @ k1*[X().Y().Z().X()::rep] // also here

            #! inits
            // here
            2 X(K{c}, T{e}).X(K{c}, T{j})::rep

            #! definitions
            // and
            k1 = 0.05 // also
            k2 = 0.12

            #! complexes
            XX = X().X()
            XY = X().Y()
            """

        self.model_without_variable = """
            #! rules
            // commenting
            X(K{i}).Y()::rep => X(K{p}).Y()::rep @ k1*[X().Y().Z().X()::rep]
            X(T{a}).X()::rep => X(T{o}).X()::rep @ k2*[X().X()::rep]
            X(T{a}).Y()::rep => X(T{o}).Y()::rep @ k2*[X().X()::rep]

            #! inits
            // here
            2 X(K{c}, T{e}).X(K{c}, T{j})::rep

            #! definitions
            // and
            k1 = 0.05 // also
            k2 = 0.12
            """

        self.model_with_redundant = """
            #! rules
            K(S{u}).B()::cyt => K(S{p})::cyt + B()::cyt + D(A{_})::cell @ 3*[K().B()::cyt]/2*v_1
            K().B()::cyt => K()::cyt + B()::cyt + D(A{_})::cell @ 3*[K().B()::cyt]/2*v_1
            K().K()::cyt => K()::cyt + K()::cyt
            K(S{i}).K()::cyt => K(S{a})::cyt + K()::cyt
            K(S{i}, T{p}).K()::cyt => K(S{a}, T{p})::cyt + K()::cyt

            #! inits
            2 X(K{c}, T{e}).X(K{c}, T{j})::rep

            #! definitions
            v_1 = 0.05
            k2 = 0.12
            """

        self.model_without_redundant = """
            #! rules
            K().B()::cyt => K()::cyt + B()::cyt + D(A{_})::cell @ 3*[K().B()::cyt]/2*v_1
            K().K()::cyt => K()::cyt + K()::cyt

            #! inits
            2 X(K{c}, T{e}).X(K{c}, T{j})::rep

            #! definitions
            v_1 = 0.05
            k2 = 0.12
            """

        self.model_with_context = """
            #! rules
            K(S{i}).B(T{a})::cyt => K(S{i})::cyt + B(T{a})::cyt @ 3*[K(S{i}).B(T{a})::cyt]/2*v_1
            A{p}.K(S{i},T{i})::cyt => A{i}::cyt + K(S{a},T{a})::cyt
            K(S{i},T{i})::cyt => K(S{a},T{i})::cyt

            #! inits
            2 K(S{i}).B(T{a})::cyt
            1 A{p}.K(S{i},T{i})::cyt

            #! definitions
            v_1 = 0.05
            k2 = 0.12
            """

        self.model_without_context = """
            #! rules
            K().B()::cyt => K()::cyt + B()::cyt @ 3*[K().B()::cyt]/2*v_1
            A{_}.K()::cyt => A{_}::cyt + K()::cyt

            #! inits
            2 K().B()::cyt
            1 A{_}.K()::cyt

            #! definitions
            v_1 = 0.05
            k2 = 0.12
            """

        self.model_reachable = """
            #! rules
            K(S{i}).B()::cyt => K(S{a})::cyt + B()::cyt @ 3*[K(S{i}).B()::cyt]/2*v_1
            K(S{a})::cyt + A{i}::cyt => K(S{a}).A{i}::cyt
            K().A{i}::cyt => K().A{a}::cyt

            #! inits
            2 K(S{i}).B()::cyt
            1 A{i}::cyt

            #! definitions
            v_1 = 0.05
            k2 = 0.12
            """

        self.model_nonreachable = """
            #! rules
            K(S{i}).B()::cyt => K(S{a})::cyt + B()::cyt @ 3*[K(S{i}).B()::cyt]/2*v_1
            K(S{a})::cyt + A{i}::cyt => K(S{a}).A{i}::cyt

            #! inits
            2 K(S{i}).B()::cyt
            1 A{i}::cyt

            #! definitions
            v_1 = 0.05
            k2 = 0.12
            """
        
        self.model_parametrised = """
            #! rules
            // commenting
            X(K{i})::rep => X(K{p})::rep @ k1*[X()::rep] // also here
            X(T{a})::rep => X(T{o})::rep @ k2*[Z()::rep]
            => Y(P{f})::rep @ 1/(v_3+([X()::rep])**4) // ** means power (^)

            #! inits
            2 X(K{c}, T{e}).X(K{c}, T{j})::rep
            Y(P{g}, N{l})::rep // comment just 1 item

            #! definitions
            k1 = 0.05
            """

        self.miyoshi = """
示例#12
0
 def test_reorder(self):
     order = np.array([2, 0, 1])
     self.assertEqual(self.s1.reorder(order), State(np.array((3, 1, 2))))
示例#13
0
 def test_sub(self):
     self.assertEqual(self.s1 - self.s2, State(np.array((-4, -2, 0))))
     self.assertEqual(self.s3 - self.s4, State(np.array((3, 2, 1, 1))))
     with self.assertRaises(ValueError):
         self.s2 - self.s3
示例#14
0
    def setUp(self):
        self.str1 = StructureAgent("X", set())
        self.str2 = StructureAgent("Y", set())
        self.str3 = StructureAgent("Z", set())
        self.str4 = StructureAgent("W", set())

        self.c1 = Complex([self.str1], "rep")
        self.c2 = Complex([self.str2], "rep")
        self.c3 = Complex([self.str3], "rep")
        self.c4 = Complex([self.str4], "rep")

        ordering = (self.c1, self.c2, self.c3)
        ordering_wrong = (self.c1, self.c2, self.c3, self.c4),
        ordering_reordered = (self.c3, self.c1, self.c2)

        self.s1 = State(np.array((1, 2, 3)))
        self.s2 = State(np.array((1, 2, 4)))
        self.s3 = State(np.array((1, 2, 5)))
        self.s4 = State(np.array((4, 2, 3)))
        self.hell = State(np.array((np.inf, np.inf, np.inf)))
        self.hell.is_inf = True

        self.s1_reordered = State(np.array((3, 1, 2)))
        self.s2_reordered = State(np.array((4, 1, 2)))
        self.s3_reordered = State(np.array((5, 1, 2)))

        self.ts = TransitionSystem(tuple())
        self.ts.states_encoding = {self.s1: 1, self.s2: 2}

        self.edge_1 = Edge(0, 1, 0.5)

        # equality of TS

        self.ts_eq_1 = TransitionSystem(ordering)
        self.ts_eq_1.states_encoding = {self.s1: 0, self.s2: 1, self.s3: 2}
        self.ts_eq_1.edges = {Edge(0, 1, 0.3), Edge(0, 2, 0.7), Edge(1, 2, 1)}

        self.ts_eq_2 = TransitionSystem(ordering)
        self.ts_eq_2.states_encoding = {self.s1: 1, self.s2: 2, self.s3: 0}
        self.ts_eq_2.edges = {Edge(1, 2, 0.3), Edge(1, 0, 0.7), Edge(2, 0, 1)}

        self.ts_eq_3 = TransitionSystem(ordering_reordered)
        self.ts_eq_3.states_encoding = {self.s1_reordered: 1, self.s2_reordered: 2, self.s3_reordered: 0}
        self.ts_eq_3.edges = {Edge(1, 2, 0.3), Edge(1, 0, 0.7), Edge(2, 0, 1)}

        # inequality of TS

        self.ts_neq_1 = TransitionSystem(ordering)
        self.ts_neq_1.states_encoding = {self.s1: 0, self.s2: 1, self.s3: 2}
        self.ts_neq_1.edges = {Edge(0, 1, 0.3), Edge(0, 2, 0.7), Edge(1, 2, 1)}

        self.ts_neq_2 = TransitionSystem(ordering)
        self.ts_neq_2.states_encoding = {self.s1: 1, self.s2: 2, self.s3: 0}
        self.ts_neq_2.edges = {Edge(1, 2, 0.3), Edge(1, 0, 0.7), Edge(0, 2, 1)}

        self.ts_neq_3 = TransitionSystem(ordering_wrong)
        self.ts_neq_3.states_encoding = {self.s1: 1, self.s2: 2, self.s3: 0}
        self.ts_neq_3.edges = {Edge(1, 2, 0.3), Edge(1, 0, 0.7), Edge(0, 2, 1)}

        # test bigger TS

        ordering = (self.c1, self.c2, self.c3, self.c4)

        self.ts_bigger = TransitionSystem(ordering)
        self.ts_bigger.states_encoding = {self.s1: 1, self.s2: 2, self.s3: 0, self.s4: 3}
        self.ts_bigger.edges = {Edge(1, 2, 0.8), Edge(0, 1, 0.5), Edge(1, 0, 0.3),
                                Edge(3, 1, 0.9), Edge(3, 2, 0.1), Edge(1, 3, 0.2)}
示例#15
0
    def setUp(self):
        self.model_parser = Parser("model")

        """
        Model 1 - Transition system of die model
        Analysis of a PRISM example model from the Knuth-Yao
        source: storm website
        """
        self.str1 = StructureAgent("S", set())
        self.str2 = StructureAgent("D", set())

        self.c1 = Complex([self.str1], "rep")
        self.c2 = Complex([self.str2], "rep")

        ordering = (self.c1, self.c2)

        self.s1 = State(np.array((0, 0)))
        self.s2 = State(np.array((1, 0)))
        self.s3 = State(np.array((2, 0)))
        self.s4 = State(np.array((3, 0)))
        self.s5 = State(np.array((4, 0)))
        self.s6 = State(np.array((5, 0)))
        self.s7 = State(np.array((6, 0)))
        self.s8 = State(np.array((7, 1)))
        self.s9 = State(np.array((7, 2)))
        self.s10 = State(np.array((7, 3)))
        self.s11 = State(np.array((7, 4)))
        self.s12 = State(np.array((7, 5)))
        self.s13 = State(np.array((7, 6)))

        self.die_ts = TransitionSystem(ordering)
        self.die_ts.init = 0
        self.die_ts.states_encoding = {self.s1: 0, self.s2: 1, self.s3: 2, self.s4: 3, self.s5: 4,
                                       self.s6: 5, self.s7: 6, self.s8: 7, self.s9: 8, self.s10: 9,
                                       self.s11: 10, self.s12: 11, self.s13: 12}
        self.die_ts.edges = {Edge(0, 1, 0.5), Edge(0, 2, 0.5), Edge(1, 3, 0.5), Edge(1, 4, 0.5), Edge(2, 5, 0.5),
                             Edge(2, 6, 0.5), Edge(3, 1, 0.5), Edge(3, 7, 0.5), Edge(4, 8, 0.5), Edge(4, 9, 0.5),
                             Edge(5, 10, 0.5), Edge(5, 11, 0.5), Edge(6, 2, 0.5), Edge(6, 12, 0.5), Edge(7, 7, 1),
                             Edge(8, 8, 1), Edge(9, 9, 1), Edge(10, 10, 1), Edge(11, 11, 1), Edge(12, 12, 1)}

        # die parametric TS
        self.die_ts_parametric = TransitionSystem(ordering)
        self.die_ts_parametric.init = 0
        self.die_ts_parametric.states_encoding = {self.s1: 0, self.s2: 1, self.s3: 2, self.s4: 3, self.s5: 4,
                                                  self.s6: 5, self.s7: 6, self.s8: 7, self.s9: 8, self.s10: 9,
                                                  self.s11: 10, self.s12: 11, self.s13: 12}
        self.die_ts_parametric.edges = {Edge(0, 1, "p"), Edge(0, 2, "(1-p)"), Edge(1, 3, "p"), Edge(1, 4, "(1-p)"),
                                        Edge(2, 5, "p"),
                                        Edge(2, 6, "(1-p)"), Edge(3, 1, "p"), Edge(3, 7, "(1-p)"), Edge(4, 8, "p"),
                                        Edge(4, 9, "(1-p)"),
                                        Edge(5, 10, "p"), Edge(5, 11, "(1-p)"), Edge(6, 2, "p"), Edge(6, 12, "(1-p)"),
                                        Edge(7, 7, 1),
                                        Edge(8, 8, 1), Edge(9, 9, 1), Edge(10, 10, 1), Edge(11, 11, 1), Edge(12, 12, 1)}

        self.labels = {0: {'init'}, 7: {'one', 'done'},
                       9: {'done'}, 8: {'done'}, 10: {'done'}, 11: {'done'}, 12: {'done'}}

        # PCTL formulas for model checking
        self.die_pctl_prism = "P=? [F VAR_0=7&VAR_1=1]"  # 0.1666666667
        self.die_pctl_explicit = "P=? [F \"one\"]"  # 0.1666666667
        self.die_pctl_parametric = "P=? [F VAR_0=7&VAR_1=1]"
        self.die_pctl1 = "P=? [F VAR_0=7&VAR_1=1 || F VAR_0=7&VAR_1<4]"  # 0.3333333333 not used
        self.die_pctl2 = "P<=0.15 [F VAR_0=7&VAR_1=1]"  # false not used
        self.result = 0.166666667