Exemple #1
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
    def test_parametric_matrix_from_numpy_row_grouping(self):
        import numpy as np
        one_pol = stormpy.RationalRF(1)
        one_pol = stormpy.FactorizedPolynomial(one_pol)
        first_val = stormpy.FactorizedRationalFunction(one_pol, one_pol)
        two_pol = stormpy.RationalRF(2)
        two_pol = stormpy.FactorizedPolynomial(two_pol)
        sec_val = stormpy.FactorizedRationalFunction(two_pol, two_pol)
        third_val = stormpy.FactorizedRationalFunction(one_pol, two_pol)

        array = np.array([[sec_val, first_val], [first_val, sec_val],
                          [sec_val, sec_val], [third_val, third_val]])

        matrix = stormpy.build_parametric_sparse_matrix(
            array, row_group_indices=[1, 3])

        # Check matrix dimension
        assert matrix.nr_rows == array.shape[0]
        assert matrix.nr_columns == array.shape[1]
        assert matrix.nr_entries == 8

        # Check matrix values
        for r in range(array.shape[1]):
            row = matrix.get_row(r)
            for e in row:
                assert (e.value() == array[r, e.column])

        # Check row groups
        assert matrix.get_row_group_start(0) == 1
        assert matrix.get_row_group_end(0) == 3

        assert matrix.get_row_group_start(1) == 3
        assert matrix.get_row_group_end(1) == 4
Exemple #3
0
 def test_pycarl(self):
     import stormpy
     rational = stormpy.Rational(0.25)
     assert str(rational) == "1/4"
     pol1 = stormpy.FactorizedPolynomial(32)
     pol2 = stormpy.FactorizedPolynomial(2)
     rat = stormpy.FactorizedRationalFunction(pol1, pol2)
     assert str(rat) == "16"
    def test_parametric_matrix_builder(self):
        builder = stormpy.ParametricSparseMatrixBuilder(force_dimensions=True)
        matrix = builder.build()
        assert matrix.nr_columns == 0
        assert matrix.nr_rows == 0
        assert matrix.nr_entries == 0

        builder_5x5 = stormpy.ParametricSparseMatrixBuilder(
            5, 5, force_dimensions=False)

        one_pol = stormpy.RationalRF(1)
        one_pol = stormpy.FactorizedPolynomial(one_pol)
        first_val = stormpy.FactorizedRationalFunction(one_pol)

        two_pol = stormpy.RationalRF(2)
        two_pol = stormpy.FactorizedPolynomial(two_pol)
        sec_val = stormpy.FactorizedRationalFunction(two_pol)

        builder_5x5.add_next_value(0, 0, first_val)
        builder_5x5.add_next_value(0, 1, first_val)
        builder_5x5.add_next_value(2, 2, sec_val)
        builder_5x5.add_next_value(2, 3, sec_val)

        assert builder_5x5.get_last_column() == 3
        assert builder_5x5.get_last_row() == 2

        builder_5x5.add_next_value(3, 2, sec_val)
        builder_5x5.add_next_value(3, 4, sec_val)
        builder_5x5.add_next_value(4, 3, sec_val)

        matrix_5x5 = builder_5x5.build()

        assert matrix_5x5.nr_columns == 5
        assert matrix_5x5.nr_rows == 5
        assert matrix_5x5.nr_entries == 7

        for e in matrix_5x5:
            assert (e.value() == first_val
                    and e.column < 2) or (e.value() == sec_val
                                          and e.column > 1)
    def test_parametric_matrix_replace_columns(self):
        builder = stormpy.ParametricSparseMatrixBuilder(3,
                                                        4,
                                                        force_dimensions=False)

        one_pol = stormpy.RationalRF(1)
        one_pol = stormpy.FactorizedPolynomial(one_pol)
        first_val = stormpy.FactorizedRationalFunction(one_pol, one_pol)
        two_pol = stormpy.RationalRF(2)
        two_pol = stormpy.FactorizedPolynomial(two_pol)
        sec_val = stormpy.FactorizedRationalFunction(two_pol, two_pol)
        third_val = stormpy.FactorizedRationalFunction(one_pol, two_pol)

        builder.add_next_value(0, 1, first_val)
        builder.add_next_value(0, 2, sec_val)
        builder.add_next_value(0, 3, third_val)

        builder.add_next_value(1, 1, first_val)
        builder.add_next_value(1, 2, sec_val)
        builder.add_next_value(1, 3, third_val)

        builder.add_next_value(2, 1, first_val)
        builder.add_next_value(2, 2, sec_val)
        builder.add_next_value(2, 3, third_val)

        # replace rows
        builder.replace_columns([3, 2], 2)
        matrix = builder.build()

        assert matrix.nr_entries == 9

        # Check if columns where replaced
        for e in matrix:
            assert (e.value() == first_val and e.column == 1) or (
                e.value() == third_val
                and e.column == 2) or (e.value() == sec_val and e.column == 3)
Exemple #6
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
Exemple #7
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
Exemple #8
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))