Exemplo n.º 1
0
def composition_test():
    # Test deterministic transitions
    states1 = {"c1", "c2"}
    transitions1 = {
        ("c1", "c2", 1),
        ("c2", "c1", 0),
    }
    init1 = "c1"

    states2 = {"c1", "c2", "c3"}
    transitions2 = {
        ("c1", "c2"),
        ("c2", "c3"),
        ("c3", "c3"),
    }
    init2 = "c1"

    states3 = {"c4", "c5"}
    transitions3 = {
        ("c4", "c5", 1),
        ("c5", "c4", 0),
    }
    init3 = "c4"

    ts1 = _construct_wks(states1, transitions1, init1)
    ts2 = _construct_wks(states2, transitions2, init2)
    ts3 = _construct_wks(states3, transitions3, init3)

    ts = synchronous_parallel([ts1, ts2, ts3])
    assert isinstance(ts, WKS)

    assert len(ts.states) == len(states1) * len(states2) * len(states3)
    assert len(ts.states.initial) == 1
    init = list(ts.states.initial)[0]
    init_expected = (init1, init2, init3)

    visited = {init}
    visited_expected = {init_expected}
    Q = [init]
    Q_expected = [init_expected]

    while len(Q) > 0:
        v = Q.pop(0)
        v_expected = Q_expected.pop(0)
        transitions = ts.transitions.find(v)
        transitions1 = ts1.transitions.find(v_expected[0])
        transitions2 = ts2.transitions.find(v_expected[1])
        transitions3 = ts3.transitions.find(v_expected[2])
        assert len(transitions) == 1
        assert (transitions[0][2]["cost"] == transitions1[0][2]["cost"] +
                transitions3[0][2]["cost"])
        if transitions[0][1] not in visited:
            visited.add(transitions[0][1])
            visited_expected.add(
                (transitions1[0][1], transitions2[0][1], transitions3[0][1]))
            Q.append(transitions[0][1])
            Q_expected.append(
                (transitions1[0][1], transitions2[0][1], transitions3[0][1]))

    assert len(Q_expected) == 0
    def prob_TL(self, phi):
        model_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                  "models")
        prism_file_path = os.path.join(model_path, "pedestrian.nm")
        path_MC = os.path.join(model_path, "model_MC.nm")
        env_MC = os.path.join(model_path, "env_MC.nm")
        # Print self markov chain:
        # print(self.MC)
        # Writing prism files:
        stormpy_int.to_prism_file(self.MC, path_MC)
        stormpy_int.to_prism_file(self.true_env_MC, env_MC)
        composed = synchronous_parallel([self.MC, self.true_env_MC])
        # print(composed.transitions)
        result = stormpy_int.model_checking(composed, phi, prism_file_path)
        # Returns a tulip transys:
        # MC_ts = stormpy_int.to_tulip_transys(path_MC)
        result = stormpy_int.model_checking(
            self.MC, phi, prism_file_path
        )  # Since there is no moving obstacle, try checking only the pedestrian obstacle

        # for state in self.MC.states:
        #    print(self.MC.states[state]["ap"])
        #    self.MC.states[state]["ap"] |= {"good"}

        # Debugging: print states of result:
        # print(self.MC)
        # print(result)
        # print(" ")

        # for state in self.MC.states:
        #     print("  State {}, with labels {}, Pr = {}".format(state, self.MC.states[state], result[(state, None)]))
        # for state in self.MC.states:
        #   print("  State {}, with labels {}, Pr = {}".format(state, self.MC.states[state]["ap"], result[state]))
        return result  # Implement this soon: Storm py
Exemplo n.º 3
0
def mvp_test():
    states_a = {"c8", "c4", "c9"}
    transitions_a = {
        ("c8", "c8", 1),
        ("c8", "c4", 0),
        ("c4", "c4", 1),
        ("c4", "c9", 0),
        ("c9", "c9", 0),
    }
    init_a = "c8"

    states_h = {"c3", "c4", "c5", "c6"}
    transitions_h = {
        ("c3", "c4"),
        ("c4", "c5"),
        ("c5", "c6"),
        ("c6", "c6"),
    }
    init_h = "c3"

    states_l = {"green", "red"}
    transitions_l = {
        ("green", "red"),
        ("red", "red"),
    }
    init_l = "green"

    ts_a = _construct_wks(states_a, transitions_a, init_a, "a")
    ts_h = _construct_wks(states_h, transitions_h, init_h, "h")
    ts_l = _construct_wks(states_l, transitions_l, init_l)

    ts = synchronous_parallel([ts_a, ts_h, ts_l])

    # To define the transition !(h4 & a4), we define 2 sets:
    #   * ap_without_h4 contains all the atomic propositions except 'h4'
    #   * ap_without_a4 contains all the atomic propositions except 'a4'
    # The subset of 2^{AP} corresponding to !(h4 & a4) is the union of
    # PowerSet(ap_without_h4) and PowerSet(ap_without_a4).
    fa1 = _construct_wfa(ts.atomic_propositions, [["a4"], ["h4"]])

    # To define the transition !(red & (a8 | a4)), we define 2 sets:
    #   * ap_without_red contains all the atomic propositions except 'red'
    #   * ap_without_a4a8 contains all the atomic propositions except 'a4' and 'a8'
    fa2 = _construct_wfa(ts.atomic_propositions, [["red"], ["a4", "a8"]])

    # Define the prioritized safety specification
    spec = PrioritizedSpecification()
    spec.add_rule(fa1, priority=1, level=0)
    spec.add_rule(fa2, priority=1, level=1)

    # Solve the minimum violation planning problem
    (cost, state_path, product_path, wpa) = solve_mvp(ts, "a9", spec)
    assert cost == [0, 2, 1]
    assert state_path == [
        ("c8", "c3", "green"),
        ("c8", "c4", "red"),
        ("c4", "c5", "red"),
        ("c9", "c6", "red"),
    ]
    assert product_path == [
        ("null", ("q0", "q0")),
        (("c8", "c3", "green"), ("q0", "q0")),
        (("c8", "c4", "red"), ("q0", "q0")),
        (("c4", "c5", "red"), ("q0", "q0")),
        (("c9", "c6", "red"), ("q0", "q0")),
    ]
def compose_test():
    # Build models from prism files
    mh = stormpy_int.to_tulip_transys(mh_path)
    light = stormpy_int.to_tulip_transys(light_path)

    # Compose models
    composed = synchronous_parallel([mh, light])

    # Check properties
    formula = 'P=? [ "green" U "h6" ]'

    # Model checking
    result = stormpy_int.model_checking(composed, formula, out_model_path)
    os.remove(out_model_path)

    assert len(composed.states) == 14
    expected_result = [
        {
            "labels": {"h6", "green"},
            "result": 1.0
        },
        {
            "labels": {"red", "h6"},
            "result": 1.0
        },
        {
            "labels": {"h2", "green"},
            "result": 0.42122366709344317
        },
        {
            "labels": {"red", "h2"},
            "result": 0.0
        },
        {
            "labels": {"h4", "green"},
            "result": 0.7256235827664395
        },
        {
            "labels": {"red", "h4"},
            "result": 0.0
        },
        {
            "labels": {"h5", "green"},
            "result": 0.9523809523809521
        },
        {
            "labels": {"red", "h5"},
            "result": 0.0
        },
        {
            "labels": {"green", "h1"},
            "result": 0.3209323177854804
        },
        {
            "labels": {"red", "h1"},
            "result": 0.0
        },
        {
            "labels": {"h0", "green"},
            "result": 0.2445198611698898
        },
        {
            "labels": {"red", "h0"},
            "result": 0.0
        },
        {
            "labels": {"green", "h3"},
            "result": 0.5528560630601442
        },
        {
            "labels": {"red", "h3"},
            "result": 0.0
        },
    ]

    _check_result(composed, result, expected_result)
def synthesis_test():
    # Build models from prism files
    ma = stormpy_int.to_tulip_transys(ma_path)
    mh = stormpy_int.to_tulip_transys(mh_path)
    light = stormpy_int.to_tulip_transys(light_path)

    # Compose models
    composed = synchronous_parallel([ma, mh, light])

    # Check properties
    safety = '!("h4" & "a4") & !("red" & ("a8" | "a4"))'
    reach = '"a9"'
    formula = "Pmax=? [ ({}) U ({}) ]".format(safety, reach)

    # Construct policy
    (result, policy) = stormpy_int.model_checking(composed, formula,
                                                  out_model_path, True)
    os.remove(out_model_path)

    assert abs(result[list(composed.states.initial)[0]] -
               0.7429340826573935) < 1e-6

    # Get the MC induced by applying policy_opt on model
    induced_mc = apply_policy(composed, policy)

    # Model checking
    result = stormpy_int.model_checking(induced_mc, formula, out_model_path)
    os.remove(out_model_path)

    expected_result = [
        {
            "labels": {"green", "h5", "a8"},
            "result": 0.745341614906832
        },
        {
            "labels": {"h5", "red", "a8"},
            "result": 0.0
        },
        {
            "labels": {"green", "h0", "a8"},
            "result": 0.7429340826573935
        },
        {
            "labels": {"h0", "red", "a8"},
            "result": 0.0
        },
        {
            "labels": {"green", "h1", "a8"},
            "result": 0.7287988161717747
        },
        {
            "labels": {"h1", "a8", "red"},
            "result": 0.0
        },
        {
            "labels": {"green", "a8", "h4"},
            "result": 0.6059687926071806
        },
        {
            "labels": {"red", "a8", "h4"},
            "result": 0.0
        },
        {
            "labels": {"green", "a8", "h6"},
            "result": 0.745341614906832
        },
        {
            "labels": {"red", "a8", "h6"},
            "result": 0.0
        },
        {
            "labels": {"green", "a8", "h3"},
            "result": 0.3861264940442798
        },
        {
            "labels": {"red", "a8", "h3"},
            "result": 0.0
        },
        {
            "labels": {"green", "h2", "a8"},
            "result": 0.6458232194557872
        },
        {
            "labels": {"h2", "a8", "red"},
            "result": 0.0
        },
        {
            "labels": {"green", "h5", "a4"},
            "result": 0.9523809523809523
        },
        {
            "labels": {"h5", "red", "a4"},
            "result": 0.0
        },
        {
            "labels": {"green", "h0", "a4"},
            "result": 0.9520897806888625
        },
        {
            "labels": {"h0", "red", "a4"},
            "result": 0.0
        },
        {
            "labels": {"green", "h1", "a4"},
            "result": 0.9501789664595234
        },
        {
            "labels": {"h1", "a4", "red"},
            "result": 0.0
        },
        {
            "labels": {"green", "a4", "h4"},
            "result": 0.0
        },
        {
            "labels": {"red", "a4", "h4"},
            "result": 0.0
        },
        {
            "labels": {"green", "a4", "h6"},
            "result": 0.9523809523809521
        },
        {
            "labels": {"red", "a4", "h6"},
            "result": 0.0
        },
        {
            "labels": {"green", "a4", "h3"},
            "result": 0.8264462809917354
        },
        {
            "labels": {"red", "a4", "h3"},
            "result": 0.0
        },
        {
            "labels": {"green", "h2", "a4"},
            "result": 0.9357284338501467
        },
        {
            "labels": {"h2", "a4", "red"},
            "result": 0.0
        },
        {
            "labels": {"green", "h5", "a9"},
            "result": 1.0
        },
        {
            "labels": {"h5", "a9", "red"},
            "result": 1.0
        },
        {
            "labels": {"green", "h0", "a9"},
            "result": 1.0
        },
        {
            "labels": {"h0", "a9", "red"},
            "result": 1.0
        },
        {
            "labels": {"green", "a9", "h1"},
            "result": 1.0
        },
        {
            "labels": {"a9", "red", "h1"},
            "result": 1.0
        },
        {
            "labels": {"green", "a9", "h4"},
            "result": 1.0
        },
        {
            "labels": {"a9", "red", "h4"},
            "result": 1.0
        },
        {
            "labels": {"green", "a9", "h6"},
            "result": 1.0
        },
        {
            "labels": {"a9", "h6", "red"},
            "result": 1.0
        },
        {
            "labels": {"green", "a9", "h3"},
            "result": 1.0
        },
        {
            "labels": {"a9", "h3", "red"},
            "result": 1.0
        },
        {
            "labels": {"green", "a9", "h2"},
            "result": 1.0
        },
        {
            "labels": {"a9", "h2", "red"},
            "result": 1.0
        },
    ]

    _check_result(composed, result, expected_result)
def compose_test():
    # Build models from prism files
    mh = stormpy_int.to_tulip_transys(mh_path)
    light = stormpy_int.to_tulip_transys(light_path)

    # Compose models
    composed = synchronous_parallel([mh, light])

    # Check properties
    formula = 'P=? [ "green" U "h6" ]'

    # Model checking
    result = stormpy_int.model_checking(composed, formula, out_model_path)
    os.remove(out_model_path)

    assert len(composed.states) == 14
    expected_result = [
        {
            "labels": {"h6", "green"},
            "result": 1.0
        },
        {
            "labels": {"red", "h6"},
            "result": 1.0
        },
        {
            "labels": {"h2", "green"},
            "result": 0.421_223_667_093_443_17
        },
        {
            "labels": {"red", "h2"},
            "result": 0.0
        },
        {
            "labels": {"h4", "green"},
            "result": 0.725_623_582_766_439_5
        },
        {
            "labels": {"red", "h4"},
            "result": 0.0
        },
        {
            "labels": {"h5", "green"},
            "result": 0.952_380_952_380_952_1
        },
        {
            "labels": {"red", "h5"},
            "result": 0.0
        },
        {
            "labels": {"green", "h1"},
            "result": 0.320_932_317_785_480_4
        },
        {
            "labels": {"red", "h1"},
            "result": 0.0
        },
        {
            "labels": {"h0", "green"},
            "result": 0.244_519_861_169_889_8
        },
        {
            "labels": {"red", "h0"},
            "result": 0.0
        },
        {
            "labels": {"green", "h3"},
            "result": 0.552_856_063_060_144_2
        },
        {
            "labels": {"red", "h3"},
            "result": 0.0
        },
    ]

    _check_result(composed, result, expected_result)
def synthesis_test():
    # Build models from prism files
    ma = stormpy_int.to_tulip_transys(ma_path)
    mh = stormpy_int.to_tulip_transys(mh_path)
    light = stormpy_int.to_tulip_transys(light_path)

    # Compose models
    composed = synchronous_parallel([ma, mh, light])

    # Check properties
    safety = '!("h4" & "a4") & !("red" & ("a8" | "a4"))'
    reach = '"a9"'
    formula = "Pmax=? [ ({}) U ({}) ]".format(safety, reach)

    # Construct policy
    (result, policy) = stormpy_int.model_checking(composed, formula,
                                                  out_model_path, True)
    os.remove(out_model_path)

    assert abs(result[list(composed.states.initial)[0]] -
               0.742_934_082_657_393_5) < 1e-6

    # Get the MC induced by applying policy_opt on model
    induced_mc = apply_policy(composed, policy)

    # Model checking
    result = stormpy_int.model_checking(induced_mc, formula, out_model_path)
    os.remove(out_model_path)

    expected_result = [
        {
            "labels": {"green", "h5", "a8"},
            "result": 0.745_341_614_906_832
        },
        {
            "labels": {"h5", "red", "a8"},
            "result": 0.0
        },
        {
            "labels": {"green", "h0", "a8"},
            "result": 0.742_934_082_657_393_5
        },
        {
            "labels": {"h0", "red", "a8"},
            "result": 0.0
        },
        {
            "labels": {"green", "h1", "a8"},
            "result": 0.728_798_816_171_774_7
        },
        {
            "labels": {"h1", "a8", "red"},
            "result": 0.0
        },
        {
            "labels": {"green", "a8", "h4"},
            "result": 0.605_968_792_607_180_6
        },
        {
            "labels": {"red", "a8", "h4"},
            "result": 0.0
        },
        {
            "labels": {"green", "a8", "h6"},
            "result": 0.745_341_614_906_832
        },
        {
            "labels": {"red", "a8", "h6"},
            "result": 0.0
        },
        {
            "labels": {"green", "a8", "h3"},
            "result": 0.386_126_494_044_279_8
        },
        {
            "labels": {"red", "a8", "h3"},
            "result": 0.0
        },
        {
            "labels": {"green", "h2", "a8"},
            "result": 0.645_823_219_455_787_2
        },
        {
            "labels": {"h2", "a8", "red"},
            "result": 0.0
        },
        {
            "labels": {"green", "h5", "a4"},
            "result": 0.952_380_952_380_952_3
        },
        {
            "labels": {"h5", "red", "a4"},
            "result": 0.0
        },
        {
            "labels": {"green", "h0", "a4"},
            "result": 0.952_089_780_688_862_5
        },
        {
            "labels": {"h0", "red", "a4"},
            "result": 0.0
        },
        {
            "labels": {"green", "h1", "a4"},
            "result": 0.950_178_966_459_523_4
        },
        {
            "labels": {"h1", "a4", "red"},
            "result": 0.0
        },
        {
            "labels": {"green", "a4", "h4"},
            "result": 0.0
        },
        {
            "labels": {"red", "a4", "h4"},
            "result": 0.0
        },
        {
            "labels": {"green", "a4", "h6"},
            "result": 0.952_380_952_380_952_1
        },
        {
            "labels": {"red", "a4", "h6"},
            "result": 0.0
        },
        {
            "labels": {"green", "a4", "h3"},
            "result": 0.826_446_280_991_735_4
        },
        {
            "labels": {"red", "a4", "h3"},
            "result": 0.0
        },
        {
            "labels": {"green", "h2", "a4"},
            "result": 0.935_728_433_850_146_7
        },
        {
            "labels": {"h2", "a4", "red"},
            "result": 0.0
        },
        {
            "labels": {"green", "h5", "a9"},
            "result": 1.0
        },
        {
            "labels": {"h5", "a9", "red"},
            "result": 1.0
        },
        {
            "labels": {"green", "h0", "a9"},
            "result": 1.0
        },
        {
            "labels": {"h0", "a9", "red"},
            "result": 1.0
        },
        {
            "labels": {"green", "a9", "h1"},
            "result": 1.0
        },
        {
            "labels": {"a9", "red", "h1"},
            "result": 1.0
        },
        {
            "labels": {"green", "a9", "h4"},
            "result": 1.0
        },
        {
            "labels": {"a9", "red", "h4"},
            "result": 1.0
        },
        {
            "labels": {"green", "a9", "h6"},
            "result": 1.0
        },
        {
            "labels": {"a9", "h6", "red"},
            "result": 1.0
        },
        {
            "labels": {"green", "a9", "h3"},
            "result": 1.0
        },
        {
            "labels": {"a9", "h3", "red"},
            "result": 1.0
        },
        {
            "labels": {"green", "a9", "h2"},
            "result": 1.0
        },
        {
            "labels": {"a9", "h2", "red"},
            "result": 1.0
        },
    ]

    _check_result(composed, result, expected_result)
import os
from tulip.interfaces import stormpy as stormpy_int
from tulip.transys.compositions import synchronous_parallel

model_path = os.path.join("/home/ubuntu/eeci/c1-probabilistic", "models")
ma_path = os.path.join(model_path, "ma.nm")
mh_path = os.path.join(model_path, "mh.pm")
light_path = os.path.join(model_path, "light.pm")

ma = stormpy_int.to_tulip_transys(ma_path)
mh = stormpy_int.to_tulip_transys(mh_path)
light = stormpy_int.to_tulip_transys(light_path)

composed = synchronous_parallel([mh, light])

formula = 'P=? [ "green" U "h6" ]'

out_model_path = os.path.join(model_path, "out_composed_model.nm")
result = stormpy_int.model_checking(composed, formula, out_model_path)

for state in composed.states:
    print("  State {}, with labels {}, Pr = {}".format(state, composed.states[state]["ap"], result[state]))