Ejemplo n.º 1
0
    def test_parameters(self):
        program = stormpy.parse_prism_program(
            get_example_path("pdtmc", "brp16_2.pm"))
        formulas = stormpy.parse_properties_for_prism_program(
            "P=? [F s=5]", program)
        model = stormpy.build_parametric_model(program, formulas)
        model_parameters = model.collect_probability_parameters()
        reward_parameters = model.collect_reward_parameters()
        all_parameters = model.collect_all_parameters()
        assert len(model_parameters) == 2
        assert len(reward_parameters) == 0
        assert len(all_parameters) == 2

        program_reward = stormpy.parse_prism_program(
            get_example_path("pdtmc", "brp_rewards16_2.pm"))
        formulas_reward = stormpy.parse_properties_for_prism_program(
            "Rmin=? [ F \"target\" ]", program_reward)
        model = stormpy.build_parametric_model(program_reward, formulas_reward)
        model_parameters = model.collect_probability_parameters()
        reward_parameters = model.collect_reward_parameters()
        all_parameters = model.collect_all_parameters()
        assert len(model_parameters) == 2
        assert len(reward_parameters) == 2
        assert len(all_parameters) == 4

        model = stormpy.build_symbolic_parametric_model(program, formulas)
        assert len(model.get_parameters()) == 4

        model = stormpy.build_symbolic_parametric_model(
            program_reward, formulas_reward)
        assert len(model.get_parameters()) == 4
Ejemplo n.º 2
0
    def test_parametric_bisimulation(self):
        program = stormpy.parse_prism_program(
            get_example_path("pdtmc", "brp16_2.pm"))
        assert program.nr_modules == 5
        assert program.model_type == stormpy.PrismModelType.DTMC
        assert program.has_undefined_constants
        assert program.undefined_constants_are_graph_preserving
        prop = "P=? [F s=5]"
        properties = stormpy.parse_properties_for_prism_program(prop, program)
        model = stormpy.build_parametric_model(program, properties)
        assert model.nr_states == 613
        assert model.nr_transitions == 803
        assert model.model_type == stormpy.ModelType.DTMC
        assert model.has_parameters
        initial_state = model.initial_states[0]
        assert initial_state == 0

        result = stormpy.model_checking(model, properties[0])
        ratFunc = result.at(initial_state)

        model_bisim = stormpy.perform_bisimulation(
            model, properties, stormpy.BisimulationType.STRONG)
        assert model_bisim.nr_states == 324
        assert model_bisim.nr_transitions == 452
        assert model_bisim.model_type == stormpy.ModelType.DTMC
        assert model_bisim.has_parameters

        result_bisim = stormpy.model_checking(model_bisim, properties[0])
        initial_state_bisim = model_bisim.initial_states[0]
        assert initial_state_bisim == 316
        ratFunc_bisim = result_bisim.at(initial_state_bisim)
        assert ratFunc == ratFunc_bisim
Ejemplo n.º 3
0
    def test_change_parametric_matrix_modelchecking(self):
        import stormpy.logic

        program = stormpy.parse_prism_program(
            get_example_path("pdtmc", "brp16_2.pm"))
        formulas = stormpy.parse_properties_for_prism_program(
            "P=? [ F s=5 ]", program)
        model = stormpy.build_parametric_model(program, formulas)
        initial_state = model.initial_states[0]
        assert initial_state == 0
        matrix = model.transition_matrix
        # Check matrix
        one_pol = stormpy.RationalRF(1)
        one_pol = stormpy.FactorizedPolynomial(one_pol)
        one = stormpy.FactorizedRationalFunction(one_pol, one_pol)
        for e in matrix:
            assert e.value() == one or len(e.value().gather_variables()) > 0
        # First model checking
        result = stormpy.model_checking(model, formulas[0])
        ratFunc = result.at(initial_state)
        assert len(ratFunc.gather_variables()) > 0

        # Change probabilities
        two_pol = stormpy.RationalRF(2)
        two_pol = stormpy.FactorizedPolynomial(two_pol)
        new_val = stormpy.FactorizedRationalFunction(one_pol, two_pol)
        for e in matrix:
            if len(e.value().gather_variables()) > 0:
                e.set_value(new_val)
        for e in matrix:
            assert e.value() == new_val or e.value() == one
        # Second model checking
        result = stormpy.model_checking(model, formulas[0])
        ratFunc = result.at(initial_state)
        assert len(ratFunc.gather_variables()) == 0
Ejemplo n.º 4
0
 def test_pla(self):
     program = stormpy.parse_prism_program(
         get_example_path("pdtmc", "brp16_2.pm"))
     prop = "P<=0.84 [F s=5 ]"
     formulas = stormpy.parse_properties_for_prism_program(prop, program)
     model = stormpy.build_parametric_model(program, formulas)
     assert model.nr_states == 613
     assert model.nr_transitions == 803
     assert model.model_type == stormpy.ModelType.DTMC
     assert model.has_parameters
     env = stormpy.Environment()
     checker = stormpy.pars.create_region_checker(env, model,
                                                  formulas[0].raw_formula)
     parameters = model.collect_probability_parameters()
     assert len(parameters) == 2
     region = stormpy.pars.ParameterRegion.create_from_string(
         "0.7<=pL<=0.9,0.75<=pK<=0.95",
         parameters,
         splitting_threshold=None)
     result = checker.check_region(env, region)
     assert result == stormpy.pars.RegionResult.ALLSAT
     region = stormpy.pars.ParameterRegion.create_from_string(
         "0.4<=pL<=0.65,0.75<=pK<=0.95",
         parameters,
         splitting_threshold=None)
     result = checker.check_region(
         env, region, stormpy.pars.RegionResultHypothesis.UNKNOWN,
         stormpy.pars.RegionResult.UNKNOWN, True)
     assert result == stormpy.pars.RegionResult.EXISTSBOTH
     region = stormpy.pars.ParameterRegion.create_from_string(
         "0.1<=pL<=0.73,0.2<=pK<=0.715",
         parameters,
         splitting_threshold=None)
     result = checker.check_region(env, region)
     assert result == stormpy.pars.RegionResult.ALLVIOLATED
Ejemplo n.º 5
0
def example_parametric_models_01():
    # Check support for parameters
    if not config.storm_with_pars:
        print("Support parameters is missing. Try building storm-pars.")
        return

    import stormpy.pars
    path = stormpy.examples.files.prism_pdtmc_die
    prism_program = stormpy.parse_prism_program(path)

    formula_str = "P=? [F s=7 & d=3]"
    properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program)
    model = stormpy.build_parametric_model(prism_program, properties)
    print("Model supports parameters: {}".format(model.supports_parameters))
    parameters = model.collect_probability_parameters()
    assert len(parameters) == 2

    instantiator = stormpy.pars.PDtmcInstantiator(model)
    point = dict()
    for x in parameters:
        print(x.name)
        point[x] = stormpy.RationalRF(0.0)
    instantiated_model = instantiator.instantiate(point)
    result = stormpy.model_checking(instantiated_model, properties[0])
    print(result)
Ejemplo n.º 6
0
def example_parametric_models_02():
    # Check support for parameters
    if not config.storm_with_pars:
        print("Support parameters is missing. Try building storm-pars.")
        return

    import stormpy.pars
    from pycarl.formula import FormulaType, Relation
    if stormpy.info.storm_ratfunc_use_cln():
        import pycarl.cln.formula
    else:
        import pycarl.gmp.formula

    path = stormpy.examples.files.prism_pdtmc_die
    prism_program = stormpy.parse_prism_program(path)

    formula_str = "P=? [F s=7 & d=2]"
    properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program)
    model = stormpy.build_parametric_model(prism_program, properties)

    initial_state = model.initial_states[0]
    result = stormpy.model_checking(model, properties[0])
    print("Result: {}".format(result.at(initial_state)))

    collector = stormpy.ConstraintCollector(model)
    print("Well formed constraints:")
    for formula in collector.wellformed_constraints:
        print(formula.get_constraint())
    print("Graph preserving constraints:")
    for formula in collector.graph_preserving_constraints:
        print(formula.get_constraint())
Ejemplo n.º 7
0
 def test_mdp_simplification(self):
     program = stormpy.parse_prism_program(get_example_path("pmdp", "two_dice.nm"))
     formulas = stormpy.parse_properties_for_prism_program("Pmin=? [ F \"two\" ]", program)
     formula = formulas[0].raw_formula
     model = stormpy.build_parametric_model(program, formulas)
     assert model.nr_states == 169
     assert model.nr_transitions == 435
     model, formula = stormpy.pars.simplify_model(model, formula)
     assert model.nr_states == 17
     assert model.nr_transitions == 50
Ejemplo n.º 8
0
def build_model(file, hybrid=False, sylvan_threads=1, sylvan_memory=4096):
    """
    Build model from file and apply bisimulation.
    :param file: File.
    :param hybrid: If true, the model is built symbolically with BDDs and in the end converted to a sparse model.
                   If false, the model is built as a sparse model from the beginning.
    :param sylvan_threads: Number of threads to use in Sylvan library.
    :param sylvan_memory: Memory available to Sylvan.
    :return: Tuple (sparse model, prism program, property, time (s) for building , time (s) for bisimulation).
    """
    logging.debug("Build ({}) model for file {}".format("symbolic" if hybrid else "sparse", file))
    # Build model
    build_start = time.time()
    program = stormpy.parse_prism_program(file)
    prop = "R=? [F \"stable\"]"
    properties = stormpy.parse_properties(prop, program)
    assert (len(properties) == 1)

    program, properties = stormpy.preprocess_prism_program(program, properties, "")
    program = program.as_prism_program()

    if hybrid:
        # Set number of Sylvan threads to use
        stormpy.set_settings(["--sylvan:threads", str(sylvan_threads)])
        # Set memory for Sylvan
        stormpy.set_settings(["--sylvan:maxmem", str(sylvan_memory)])
        model = stormpy.build_symbolic_parametric_model(program, properties)
    else:
        model = stormpy.build_parametric_model(program, properties)
    logging.info("Built ({}) model with {} states and {} transitions.".format("symbolic" if hybrid else "sparse", model.nr_states, model.nr_transitions))
    build_end = time.time()
    time_build = build_end - build_start
    logging.info("Building model took {}s".format(time_build))

    # Bisimulation
    if hybrid:
        # Extract bisimulation quotient as sparse model
        stormpy.set_settings(["--bisimulation:quot", "sparse"])
        model = stormpy.perform_symbolic_bisimulation(model, properties)
    else:
        model = stormpy.perform_bisimulation(model, properties, stormpy.BisimulationType.STRONG)
    assert type(model) is stormpy.SparseParametricDtmc
    logging.info("Model after bisimulation: {} states and {} transitions.".format(model.nr_states, model.nr_transitions))
    time_bisim = time.time() - build_end
    logging.info("Computing bisimulation quotient took {}s".format(time_bisim))

    prop = properties[0]

    # Simplify model (by eliminating constant transitions)
    # Disabled for the moment as this leads to significantly more transitions in Herman
    if False:
        model, formula = stormpy.pars.simplify_model(model, prop.raw_formula)
        logging.info("Model after simplification: {} states and {} transitions.".format(model.nr_states, model.nr_transitions))

    return model, program, prop, time_build, time_bisim
Ejemplo n.º 9
0
 def test_build_parametric_mdp(self):
     program = stormpy.parse_prism_program(
         get_example_path("pmdp", "two_dice.nm"))
     formulas = stormpy.parse_properties_for_prism_program(
         "P=? [ F \"two\" ]", program)
     model = stormpy.build_parametric_model(program, formulas)
     assert model.nr_states == 169
     assert model.nr_transitions == 435
     assert model.model_type == stormpy.ModelType.MDP
     assert model.supports_parameters
     assert type(model) is stormpy.SparseParametricMdp
Ejemplo n.º 10
0
 def test_dtmc_simplification(self):
     program = stormpy.parse_prism_program(get_example_path("pdtmc", "brp16_2.pm"))
     prop = "P<=0.84 [F s=5 ]"
     formulas = stormpy.parse_properties_for_prism_program(prop, program)
     formula = formulas[0].raw_formula
     model = stormpy.build_parametric_model(program, formulas)
     assert model.nr_states == 613
     assert model.nr_transitions == 803
     model, formula = stormpy.pars.simplify_model(model, formula)
     assert model.nr_states == 193
     assert model.nr_transitions == 383
Ejemplo n.º 11
0
 def test_build_dtmc_supporting_parameters(self):
     program = stormpy.parse_prism_program(
         get_example_path("dtmc", "die.pm"))
     formulas = stormpy.parse_properties_for_prism_program(
         "P=? [ F \"one\" ]", program)
     model = stormpy.build_parametric_model(program, formulas)
     assert model.nr_states == 13
     assert model.nr_transitions == 20
     assert model.model_type == stormpy.ModelType.DTMC
     assert model.supports_parameters
     assert not model.has_parameters
     assert type(model) is stormpy.SparseParametricDtmc
Ejemplo n.º 12
0
 def test_build_parametric_dtmc(self):
     program = stormpy.parse_prism_program(
         get_example_path("pdtmc", "brp16_2.pm"))
     formulas = stormpy.parse_properties_for_prism_program(
         "P=? [ F s=5 ]", program)
     model = stormpy.build_parametric_model(program, formulas)
     assert model.nr_states == 613
     assert model.nr_transitions == 803
     assert model.model_type == stormpy.ModelType.DTMC
     assert model.supports_parameters
     assert model.has_parameters
     assert type(model) is stormpy.SparseParametricDtmc
Ejemplo n.º 13
0
def example_parametric_models_03():
    path = stormpy.examples.files.prism_dtmc_brp
    prism_program = stormpy.parse_prism_program(path)

    formula_str = "P=? [F \"target\"]"
    properties = stormpy.parse_properties_for_prism_program(
        formula_str, prism_program)
    model = stormpy.build_parametric_model(prism_program, properties)

    print(model.model_type)
    start = time.time()
    t = 0
    for state in model.states:
        if state.id in model.initial_states:
            print(state)
        for action in state.actions:
            for transition in action.transitions:
                if transition.value().constant_part() == 1:
                    t += 1
                    #print("From state {}, with probability {}, go to state {}".format(state, transition.value(), transition.column))
    print(time.time() - start)
    print(t)

    t2 = 0
    start = time.time()
    for row_group in range(model.nr_states):
        for row in range(
                model.transition_matrix.get_row_group_start(row_group),
                model.transition_matrix.get_row_group_end(row_group)):
            for entry in model.transition_matrix.get_row(row):
                if entry.value().constant_part() == 1:
                    t2 += 1
    print(time.time() - start)
    print(t2)

    states_and_transitions = []
    for row_group in range(model.nr_states):
        states_and_transitions.append([])
        for row in range(
                model.transition_matrix.get_row_group_start(row_group),
                model.transition_matrix.get_row_group_end(row_group)):
            for entry in model.transition_matrix.get_row(row):
                states_and_transitions[-1].append(
                    (entry.value(), entry.column))
    t3 = 0
    start = time.time()
    for s in states_and_transitions:
        for (v, c) in s:
            if v.constant_part() == 1:
                t3 += 1
    print(time.time() - start)
    print(t3)
Ejemplo n.º 14
0
 def test_derivatives(self):
     program = stormpy.parse_prism_program(get_example_path("pdtmc", "brp16_2.pm"))
     prop = "P<=0.84 [F s=5 ]"
     formulas = stormpy.parse_properties_for_prism_program(prop, program)
     model = stormpy.build_parametric_model(program, formulas)
     assert model.nr_states == 613
     assert model.nr_transitions == 803
     assert model.model_type == stormpy.ModelType.DTMC
     assert model.has_parameters
     parameters = model.collect_probability_parameters()
     assert len(parameters) == 2
     derivatives = stormpy.pars.gather_derivatives(model, list(parameters)[0])
     assert len(derivatives) == 0
Ejemplo n.º 15
0
 def test_parametric_model_checking_sparse(self):
     program = stormpy.parse_prism_program(get_example_path("pdtmc", "brp16_2.pm"))
     prop = "P=? [F s=5]"
     formulas = stormpy.parse_properties_for_prism_program(prop, program)
     model = stormpy.build_parametric_model(program, formulas)
     assert model.nr_states == 613
     assert model.nr_transitions == 803
     assert model.model_type == stormpy.ModelType.DTMC
     assert model.has_parameters
     initial_state = model.initial_states[0]
     assert initial_state == 0
     result = stormpy.model_checking(model, formulas[0])
     func = result.at(initial_state)
     one = stormpy.FactorizedPolynomial(stormpy.RationalRF(1))
     assert func.denominator == one
Ejemplo n.º 16
0
 def test_build_parametric_dtmc_preprocess(self):
     program = stormpy.parse_prism_program(
         get_example_path("pdtmc", "herman5.pm"))
     formulas = stormpy.parse_properties_for_prism_program(
         "R=? [ F \"stable\" ]", program)
     trans_program, trans_formulas = stormpy.preprocess_symbolic_input(
         program, formulas, "")
     trans_prism = trans_program.as_prism_program()
     model = stormpy.build_parametric_model(trans_prism, trans_formulas)
     assert model.nr_states == 33
     assert model.nr_transitions == 266
     assert model.model_type == stormpy.ModelType.DTMC
     assert model.supports_parameters
     assert model.has_parameters
     assert type(model) is stormpy.SparseParametricDtmc
Ejemplo n.º 17
0
 def test_parametric_transitions(self):
     program = stormpy.parse_prism_program(
         get_example_path("pmdp", "two_dice.nm"))
     model = stormpy.build_parametric_model(program)
     assert model.states[1].id == 1
     one = stormpy.FactorizedPolynomial(stormpy.RationalRF(1))
     i = 0
     for state in model.states:
         assert state.id == i
         i += 1
         j = 0
         for action in state.actions:
             assert j == 0 or j == 1
             j += 1
             for transition in action.transitions:
                 assert transition.value().denominator == one
Ejemplo n.º 18
0
 def test_label_parametric(self):
     program = stormpy.parse_prism_program(
         get_example_path("pdtmc", "brp16_2.pm"))
     formulas = stormpy.parse_properties_for_prism_program(
         "P=? [ F s=5 ]", program)
     model = stormpy.build_parametric_model(program, formulas)
     labels = model.labeling.get_labels()
     assert len(labels) == 3
     assert "init" in labels
     assert "(s = 5)" in labels
     assert "init" in model.labels_state(0)
     assert "(s = 5)" in model.labels_state(28)
     assert "(s = 5)" in model.labels_state(611)
     initial_states = model.initial_states
     assert len(initial_states) == 1
     assert 0 in initial_states
Ejemplo n.º 19
0
 def test_dynasty(self):
     program = stormpy.parse_prism_program(
         "/home/simon/School/DP/synthesis/workspace/simon/die/sketch.templ")
     prop = "P>=1/6 [F s=7 & d=1]"
     formulas = stormpy.parse_properties_for_prism_program(prop, program)
     model = stormpy.build_parametric_model(program, formulas)
     assert model.has_parameters
     env = stormpy.Environment()
     checker = stormpy.pars.MdpParameterLiftingModelChecker()
     checker.specify(env, model, formulas[0].raw_formula, True)
     parameters = model.collect_probability_parameters()
     assert len(parameters) == 1
     region = stormpy.pars.ParameterRegion.create_from_string(
         "0.1<=p0<=0.62", parameters)
     minr = checker.get_bound(env, region, False)
     maxr = checker.get_bound(env, region, True)
     result = checker.check_region(env, region)
     assert True
Ejemplo n.º 20
0
 def test_pla_manual(self):
     program = stormpy.parse_prism_program(
         get_example_path("pdtmc", "brp16_2.pm"))
     prop = "P=? [F s=5 ]"
     formulas = stormpy.parse_properties_for_prism_program(prop, program)
     model = stormpy.build_parametric_model(program, formulas)
     assert model.has_parameters
     env = stormpy.Environment()
     checker = stormpy.pars.DtmcParameterLiftingModelChecker()
     checker.specify(env, model, formulas[0].raw_formula)
     parameters = model.collect_probability_parameters()
     assert len(parameters) == 2
     region = stormpy.pars.ParameterRegion.create_from_string(
         "0.7<=pL<=0.9,0.75<=pK<=0.95", parameters)
     result = checker.get_bound(env, region, True)
     assert math.isclose(float(result.constant_part()),
                         0.8369631383670559,
                         rel_tol=1e-6)
Ejemplo n.º 21
0
 def test_pla_region_valuation(self):
     program = stormpy.parse_prism_program(
         get_example_path("pdtmc", "brp16_2.pm"))
     prop = "P<=0.84 [F s=5 ]"
     formulas = stormpy.parse_properties_for_prism_program(prop, program)
     model = stormpy.build_parametric_model(program, formulas)
     assert model.nr_states == 613
     assert model.nr_transitions == 803
     assert model.model_type == stormpy.ModelType.DTMC
     assert model.has_parameters
     env = stormpy.Environment()
     checker = stormpy.pars.create_region_checker(env, model,
                                                  formulas[0].raw_formula)
     parameters = model.collect_probability_parameters()
     assert len(parameters) == 2
     for par in parameters:
         if par.name == "pL":
             pL = par
         elif par.name == "pK":
             pK = par
         else:
             assert False
     region_valuation = dict()
     region_valuation[pL] = (stormpy.RationalRF(0.7),
                             stormpy.RationalRF(0.9))
     region_valuation[pK] = (stormpy.RationalRF(0.75),
                             stormpy.RationalRF(0.95))
     region = stormpy.pars.ParameterRegion(region_valuation)
     result = checker.check_region(env, region)
     assert result == stormpy.pars.RegionResult.ALLSAT
     region_valuation[pL] = (stormpy.RationalRF(0.4),
                             stormpy.RationalRF(0.65))
     region = stormpy.pars.ParameterRegion(region_valuation)
     result = checker.check_region(
         env, region, stormpy.pars.RegionResultHypothesis.UNKNOWN,
         stormpy.pars.RegionResult.UNKNOWN, True)
     assert result == stormpy.pars.RegionResult.EXISTSBOTH
     region_valuation[pK] = (stormpy.RationalRF(0.2),
                             stormpy.RationalRF(0.715))
     region_valuation[pL] = (stormpy.RationalRF(0.1),
                             stormpy.RationalRF(0.73))
     region = stormpy.pars.ParameterRegion(region_valuation)
     result = checker.check_region(env, region)
     assert result == stormpy.pars.RegionResult.ALLVIOLATED
Ejemplo n.º 22
0
    def test_instantiate_dtmc(self):
        program = stormpy.parse_prism_program(
            get_example_path("pdtmc", "brp16_2.pm"))
        formulas = stormpy.parse_properties_for_prism_program(
            "P=? [ F s=5 ]", program)
        model = stormpy.build_parametric_model(program, formulas)
        parameters = model.collect_probability_parameters()
        assert len(parameters) == 2
        instantiator = stormpy.pars.ModelInstantiator(model)

        point = {p: stormpy.RationalRF("0.4") for p in parameters}
        instantiated_model = instantiator.instantiate(point)
        assert instantiated_model.nr_states == model.nr_states
        assert not instantiated_model.has_parameters
        assert "0.4" in str(instantiated_model.transition_matrix[1])

        point = {p: stormpy.RationalRF("0.5") for p in parameters}
        instantiated_model2 = instantiator.instantiate(point)
        assert "0.5" in str(instantiated_model2.transition_matrix[1])
Ejemplo n.º 23
0
 def test_region(self):
     program = stormpy.parse_prism_program(get_example_path("pdtmc", "brp16_2.pm"))
     prop = "P<=0.84 [F s=5 ]"
     formulas = stormpy.parse_properties_for_prism_program(prop, program)
     model = stormpy.build_parametric_model(program, formulas)
     parameters = model.collect_probability_parameters()
     assert len(parameters) == 2
     region = stormpy.pars.ParameterRegion.create_from_string("0.7<=pL<=0.9,0.75<=pK<=0.95", parameters)
     assert region.area == stormpy.RationalRF(1) / stormpy.RationalRF(25)
     for par in parameters:
         if par.name == "pL":
             pL = par
         elif par.name == "pK":
             pK = par
         else:
             assert False
     dec = stormpy.RationalRF(100)
     region_valuation = {pL: (stormpy.RationalRF(70) / dec, stormpy.RationalRF(90) / dec), pK: (stormpy.RationalRF(75) / dec, stormpy.RationalRF(95) / dec)}
     region = stormpy.pars.ParameterRegion(region_valuation)
     assert region.area == stormpy.RationalRF(1) / stormpy.RationalRF(25)
    def test_pdtmc_exact_instantiation_checker(self):
        program = stormpy.parse_prism_program(
            get_example_path("pdtmc", "herman5.pm"))
        formulas = stormpy.parse_properties_for_prism_program(
            "R=? [F \"stable\"]", program)
        model = stormpy.build_parametric_model(program, formulas)

        parameters = model.collect_probability_parameters()
        inst_checker = stormpy.pars.PDtmcExactInstantiationChecker(model)
        inst_checker.specify_formula(
            stormpy.ParametricCheckTask(formulas[0].raw_formula, True))
        inst_checker.set_graph_preserving(True)
        env = stormpy.Environment()

        point = {p: stormpy.RationalRF("1/2") for p in parameters}
        result = inst_checker.check(env, point)
        assert isinstance(result, stormpy.ExplicitExactQuantitativeCheckResult)
        res = result.at(model.initial_states[0])
        assert isinstance(res, stormpy.Rational)
        assert res == stormpy.Rational("29/15")
Ejemplo n.º 25
0
 def test_pla_bounds(self):
     program = stormpy.parse_prism_program(
         get_example_path("pdtmc", "brp16_2.pm"))
     prop = "P=? [F s=5 ]"
     formulas = stormpy.parse_properties_for_prism_program(prop, program)
     model = stormpy.build_parametric_model(program, formulas)
     assert model.has_parameters
     env = stormpy.Environment()
     checker = stormpy.pars.create_region_checker(env, model,
                                                  formulas[0].raw_formula)
     parameters = model.collect_probability_parameters()
     assert len(parameters) == 2
     region = stormpy.pars.ParameterRegion("0.7<=pL<=0.9,0.75<=pK<=0.95",
                                           parameters)
     result = checker.get_bound(env, region, True)
     assert math.isclose(float(result.constant_part()),
                         0.8369631383670559,
                         rel_tol=1e-6)
     result_vec = checker.get_bound_all_states(env, region, True)
     result = result_vec.at(model.initial_states[0])
     assert math.isclose(result, 0.8369631383670559, rel_tol=1e-6)
Ejemplo n.º 26
0
 def test_constraints_collector(self):
     from pycarl.formula import FormulaType, Relation
     if stormpy.info.storm_ratfunc_use_cln():
         import pycarl.cln.formula
     else:
         import pycarl.gmp.formula
     program = stormpy.parse_prism_program(get_example_path("pdtmc", "brp16_2.pm"))
     prop = "P=? [F s=5]"
     formulas = stormpy.parse_properties_for_prism_program(prop, program)
     model = stormpy.build_parametric_model(program, formulas)
     collector = stormpy.ConstraintCollector(model)
     constraints_well_formed = collector.wellformed_constraints
     for formula in constraints_well_formed:
         assert formula.type == FormulaType.CONSTRAINT
         constraint = formula.get_constraint()
         assert constraint.relation == Relation.LEQ
     constraints_graph_preserving = collector.graph_preserving_constraints
     for formula in constraints_graph_preserving:
         assert formula.type == FormulaType.CONSTRAINT
         constraint = formula.get_constraint()
         assert constraint.relation == Relation.NEQ
Ejemplo n.º 27
0
 def test_pla_state_bounds(self):
     program = stormpy.parse_prism_program(
         get_example_path("pdtmc", "brp16_2.pm"))
     prop = "P=? [F s=5 ]"
     formulas = stormpy.parse_properties_for_prism_program(prop, program)
     model = stormpy.build_parametric_model(program, formulas)
     assert model.has_parameters
     env = stormpy.Environment()
     checker = stormpy.pars.DtmcParameterLiftingModelChecker()
     checker.specify(env,
                     model,
                     formulas[0].raw_formula,
                     allow_model_simplification=False)
     parameters = model.collect_probability_parameters()
     assert len(parameters) == 2
     region = stormpy.pars.ParameterRegion.create_from_string(
         "0.7<=pL<=0.9,0.75<=pK<=0.95", parameters)
     result_vec = checker.get_bound_all_states(env, region, True)
     assert len(result_vec.get_values()) == model.nr_states
     assert math.isclose(result_vec.at(model.initial_states[0]),
                         0.836963056082918,
                         rel_tol=1e-6)
Ejemplo n.º 28
0
def example_parametric_models_04():
    # Check support for parameters
    if not config.storm_with_pars:
        print("Support parameters is missing. Try building storm-pars.")
        return

    import stormpy.pars
    path = stormpy.examples.files.prism_pdtmc_die
    prism_program = stormpy.parse_prism_program(path)

    formula_str = "P=? [F s=7 & d=2]"
    properties = stormpy.parse_properties(formula_str, prism_program)
    model = stormpy.build_parametric_model(prism_program, properties)

    # Modify
    i = 0
    for state in model.states:
        if state.id in model.initial_states:
            print(state)
        for action in state.actions:
            for transition in action.transitions:
                if len(transition.value().gather_variables()) > 0:

                    new_var = pycarl.Variable("p{}".format(i))
                    i += 1
                    new_pol = stormpy.Polynomial(new_var)
                    pol_in_right_format = stormpy.FactorizedPolynomial(new_pol, transition.value().numerator.cache())

                    new_factorized_ratfunc = stormpy.FactorizedRationalFunction(pol_in_right_format)
                    transition.set_value(new_factorized_ratfunc)

    # Display
    for state in model.states:
        if state.id in model.initial_states:
            print(state)
        for action in state.actions:
            for transition in action.transitions:
                print("From state {}, with probability {}, go to state {}".format(state, transition.value(), transition.column))
Ejemplo n.º 29
0
def example_parametric_models_01():
    # Check support for parameters
    if not config.storm_with_pars:
        print("Support parameters is missing. Try building storm-pars.")
        return

    import stormpy.pars
    from pycarl.formula import FormulaType, Relation
    if stormpy.info.storm_ratfunc_use_cln():
        import pycarl.cln.formula
    else:
        import pycarl.gmp.formula

    # Prevent curious side effects from earlier runs (for tests only)
    pycarl.clear_pools()
    ###
    # How to apply an unknown FSC to obtain a pMC from a POMDP
    path = stormpy.examples.files.prism_pomdp_maze
    prism_program = stormpy.parse_prism_program(path)

    formula_str = "P=? [\"goal\"]"
    properties = stormpy.parse_properties_for_prism_program(
        formula_str, prism_program)
    # construct the POMDP
    pomdp = stormpy.build_model(prism_program, properties)
    # make its representation canonic.
    pomdp = stormpy.pomdp.make_canonic(pomdp)
    # make the POMDP simple. This step is optional but often beneficial
    pomdp = stormpy.pomdp.make_simple(pomdp)
    # construct the memory for the FSC
    # in this case, a selective counter with two states
    memory_builder = stormpy.pomdp.PomdpMemoryBuilder()
    memory = memory_builder.build(
        stormpy.pomdp.PomdpMemoryPattern.selective_counter, 2)
    # apply the memory onto the POMDP to get the cartesian product
    pomdp = stormpy.pomdp.unfold_memory(pomdp, memory)
    # apply the memory onto the POMDP to get the cartesian product
    pmc = stormpy.pomdp.apply_unknown_fsc(
        pomdp, stormpy.pomdp.PomdpFscApplicationMode.simple_linear)

    ####
    # How to apply an unknown FSC to obtain a pMC from a pPOMDP
    path = stormpy.examples.files.prism_par_pomdp_maze
    prism_program = stormpy.parse_prism_program(path)

    formula_str = "P=? [\"goal\"]"
    properties = stormpy.parse_properties_for_prism_program(
        formula_str, prism_program)
    # construct the pPOMDP
    pomdp = stormpy.build_parametric_model(prism_program, properties)
    # make its representation canonic.
    pomdp = stormpy.pomdp.make_canonic(pomdp)
    # make the POMDP simple. This step is optional but often beneficial
    pomdp = stormpy.pomdp.make_simple(pomdp)
    # construct the memory for the FSC
    # in this case, a selective counter with two states
    memory_builder = stormpy.pomdp.PomdpMemoryBuilder()
    memory = memory_builder.build(
        stormpy.pomdp.PomdpMemoryPattern.selective_counter, 2)
    # apply the memory onto the POMDP to get the cartesian product
    pomdp = stormpy.pomdp.unfold_memory(pomdp, memory)
    # apply the unknown FSC to obtain a pmc from the POMDP
    pmc = stormpy.pomdp.apply_unknown_fsc(
        pomdp, stormpy.pomdp.PomdpFscApplicationMode.simple_linear)

    export_pmc = False  # Set to True to export the pMC as drn.
    if export_pmc:
        export_options = stormpy.core.DirectEncodingOptions()
        export_options.allow_placeholders = False
        stormpy.export_parametric_to_drn(pmc, "test.out", export_options)
Ejemplo n.º 30
0
def test2():
    t0 = time.time()
    #path = "collision_partial_obs_2d_upd_hobs_20_small.prism"

    path, interval_path, formula_str, threshold = input_files()

    prism_program = stormpy.parse_prism_program(path)

    #formula_str = "P=? [!\"bad\" U \"goal\"]"
    #formula_str = "P=? [F \"goal\"]"
    #interval_path="collision_partial_obs_2d_upd_hobs_20_small.intervals"
    opts = stormpy.DirectEncodingParserOptions()
    opts.build_choice_labels = True
    properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program)
    # construct the pPOMDP
    import inspect
    print(inspect.getfullargspec(stormpy.build_parametric_model))
    pomdp = stormpy.build_parametric_model(prism_program, properties)

    pomdp_parameters = pomdp.collect_probability_parameters()
    stormpy.export_parametric_to_drn(pomdp, "pomdp_ex")

    # make its representation canonic.
    pomdp = stormpy.pomdp.make_canonic(pomdp)
    #stormpy.export_parametric_to_drn(pomdp, "export_pomdp.drn")

    # construct the memory for the FSC
    # in this case, a selective counter with two states
    memory_builder = stormpy.pomdp.PomdpMemoryBuilder()
    memval=1
    memory = memory_builder.build(stormpy.pomdp.PomdpMemoryPattern.selective_counter, memval)
    # apply the memory onto the POMDP to get the cartesian product
    pomdp = stormpy.pomdp.unfold_memory(pomdp, memory)
    print("Number of pomdp states before simple:",pomdp.nr_states)
    print("Number of transitions: {}".format(pomdp.nr_transitions))

    # make the POMDP simple. This step is optional but often beneficial
    pomdp = stormpy.pomdp.make_simple(pomdp)
    print("Number of pomdp states after simple:",pomdp.nr_states)
    print("Number of transitions: {}".format(pomdp.nr_transitions))

    # apply the unknown FSC to obtain a pmc from the POMDP
    pmc = stormpy.pomdp.apply_unknown_fsc(pomdp, stormpy.pomdp.PomdpFscApplicationMode.simple_linear)
    print("Number of pomdp states after simple",pmc.nr_states)
    print("Number of transitions: {}".format(pmc.nr_transitions))
    print(pmc)
    print("applied pmc")
    path_pmc = "export_" + str(memval) + "_mem_" + path
    print(path_pmc)
    stormpy.export_parametric_to_drn(pmc, path_pmc)
    print("built model")
    #print(model.initial_states)
    fsc_parameters = pmc.collect_probability_parameters() - pomdp.collect_probability_parameters()
    print("number of pomdp parameters:",len(fsc_parameters))
    #print(fsc_parameters)
    #print(pomdp_parameters)
    #intervals2, polyhedrons = interval_parser.parse_input(interval_path)
    intervals, polyhedrons,items = interval_parser.parse_model_interval(pmc,pomdp_parameters,interval_path)
    #for item in intervals:
        #print(item,"printing in the main")
    #for item in items:
    #    print(item,"printing in the main")
    for p in polyhedrons:
        #print(p)
        p.compute_vertices()


    properties = stormpy.parse_properties(formula_str)
    print("Building model from {}".format(path))
    #parameters = model.collect_probability_parameters()
    print(pmc.nr_states)

    prob0E, prob1A = stormpy.prob01max_states(pmc, properties[0].raw_formula.subformula)
    #print(prob0E)
    #threshold = 0.90
    direction = "below"  # can be "below" or "above"
    options = QcqpOptions(mu=1e4, maxiter=10000, graph_epsilon=1e-2, silent=False)

    # result = solver.run(reward_model_name ,model_rew, parameters_rew, rew0, rew_threshold, direction, options)
    solver = QcqpSolver_affine_simple_fun()
    result = solver.run(pmc,  fsc_parameters, pomdp_parameters,properties, prob0E, prob1A, threshold, direction, options,intervals,items,True)
    print("number of iterations={}".format(solver.iterations))
    print("solver time={}".format(solver.solver_timer))

    #compute the policy against the robust interval
    #interval_path="collision_partial_obs_2d_upd_hobs_20_small.intervals"
    intervals, polyhedrons,items = interval_parser.parse_model_interval(pmc,pomdp_parameters,interval_path)
    regiondict = dict()
    for x in pomdp_parameters:
        for item in items:
            if item.name == x.name:
                regiondict[x] = (stormpy.RationalRF(item.lowerbound), stormpy.RationalRF(item.upperbound))
    region = stormpy.pars.ParameterRegion(regiondict)
    instantiator = stormpy.pars.PartialPDtmcInstantiator(pmc)
    instantiated_model = instantiator.instantiate(solver.solver_params)

    env = stormpy.Environment()
    env.solver_environment.set_linear_equation_solver_type(stormpy.EquationSolverType.eigen)
    env.solver_environment.native_solver_environment.method = stormpy.NativeLinearEquationSolverMethod.optimistic_value_iteration
    env.solver_environment.native_solver_environment.precision = stormpy.Rational('0.01')
    start_check = time.time()

    region_checker = stormpy.pars.create_region_checker(env, instantiated_model, properties[0].raw_formula,
                                                                        allow_model_simplification=False)

    print("region check")
    result = region_checker.get_bound_all_states(env, region, maximise=False)
    end_check = time.time()
    print("model checking ans:")
    ansval=result.at(pmc.initial_states[0])
    print(ansval)
    tend = time.time()
    print("Total time: ", str(tend - t0))