Esempio n. 1
0
def example_building_mas_01():
    if not numpy_found:
        print("Numpy not available")
        return

    # Building the transition matrix using numpy
    transitions = np.array([
        [0, 1, 0, 0, 0],
        [0.8, 0, 0.2, 0, 0],
        [0.9, 0, 0, 0.1, 0],
        [0, 0, 0, 0, 1],
        [0, 0, 0, 1, 0],
        [0, 0, 0, 0, 1]
    ], dtype='float64')

    # Build matrix and define indices of row groups (ascending order)
    transition_matrix = stormpy.build_sparse_matrix(transitions, [0, 2, 3, 4, 5])
    print(transition_matrix)

    # StateLabeling
    state_labeling = stormpy.storage.StateLabeling(5)
    # Add labels
    state_labels = {'init', 'deadlock'}
    # Set labeling of states
    for label in state_labels:
        state_labeling.add_label(label)
    state_labeling.add_label_to_state('init', 0)

    # Choice labeling
    choice_labeling = stormpy.storage.ChoiceLabeling(6)
    # Add labels
    choice_labels = {'alpha', 'beta'}
    # Set labeling of choices
    for label in choice_labels:
        choice_labeling.add_label(label)
    choice_labeling.add_label_to_choice('alpha', 0)
    choice_labeling.add_label_to_choice('beta', 1)

    exit_rates = [0.0, 10.0, 12.0, 1.0, 1.0]

    markovian_states = stormpy.BitVector(5, [1, 2, 3, 4])

    # Collect components
    components = stormpy.SparseModelComponents(transition_matrix=transition_matrix, state_labeling=state_labeling,
                                               markovian_states=markovian_states)
    components.choice_labeling = choice_labeling
    components.exit_rates = exit_rates

    # Build the model
    ma = stormpy.storage.SparseMA(components)
    print(ma)
Esempio n. 2
0
    def test_matrix_from_numpy(self):
        import numpy as np
        array = np.array([[0, 2], [3, 4], [0.1, 24], [-0.3, -4]],
                         dtype='float64')

        matrix = stormpy.build_sparse_matrix(array)

        # 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])
Esempio n. 3
0
def example_building_ctmcs_01():
    if not numpy_found:
        print("Numpy not available")
        return

    # Building the transition matrix using numpy
    transitions = np.array([
        [0, 1.5, 0, 0],
        [3, 0, 1.5, 0],
        [0, 3, 0, 1.5],
        [0, 0, 3, 0], ], dtype='float64')

    # Default row groups: [0,1,2,3]
    transition_matrix = stormpy.build_sparse_matrix(transitions)
    print(transition_matrix)

    # State labeling
    state_labeling = stormpy.storage.StateLabeling(4)
    state_labels = {'empty', 'init', 'deadlock', 'full'}
    for label in state_labels:
        state_labeling.add_label(label)

    # Adding label to states
    state_labeling.add_label_to_state('init', 0)
    state_labeling.add_label_to_state('empty', 0)
    state_labeling.add_label_to_state('full', 3)

    # Exit rate for each state
    exit_rates = [1.5, 4.5, 4.5, 3.0]

    # Collect components
    # rate_transitions = True, because the transition values are interpreted as rates
    components = stormpy.SparseModelComponents(transition_matrix=transition_matrix, state_labeling=state_labeling, rate_transitions=True)
    components.exit_rates = exit_rates

    # Build the model
    ctmc = stormpy.storage.SparseCtmc(components)

    print(ctmc)
Esempio n. 4
0
    def test_matrix_from_numpy_row_grouping(self):
        import numpy as np
        array = np.array([[0, 2], [3, 4], [0.1, 24], [-0.3, -4]],
                         dtype='float64')

        matrix = stormpy.build_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
    def test_build_pomdp(self):
        import numpy as np
        nr_states = 10
        nr_choices = 34

        # Build transition matrix
        builder = stormpy.SparseMatrixBuilder(rows=0,
                                              columns=0,
                                              entries=0,
                                              force_dimensions=False,
                                              has_custom_row_grouping=True,
                                              row_groups=0)

        transitions = np.array(
            [[0, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0],
             [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
             [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])

        transition_matrix = stormpy.build_sparse_matrix(
            transitions,
            row_group_indices=[0, 1, 5, 9, 13, 17, 21, 25, 29, 33])

        # state labeling
        state_labeling = stormpy.storage.StateLabeling(nr_states)
        labels = {'deadlock', 'goal', 'init'}
        for label in labels:
            state_labeling.add_label(label)
        state_labeling.add_label_to_state('init', 0)
        state_labeling.add_label_to_state('goal', 9)

        # reward models
        reward_models = {}
        # Vector representing state-action rewards
        action_reward = [
            0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
            1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
            1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0
        ]
        reward_models[''] = stormpy.SparseRewardModel(
            optional_state_action_reward_vector=action_reward)

        # choice labeling
        choice_labeling = stormpy.storage.ChoiceLabeling(nr_choices)
        choice_labels = {'south', 'north', 'west', 'east', 'done'}
        for label in choice_labels:
            choice_labeling.add_label(label)
        choice_labeling.set_choices(
            'south',
            stormpy.BitVector(nr_choices, [4, 8, 12, 16, 20, 24, 28, 32]))
        choice_labeling.set_choices(
            'north',
            stormpy.BitVector(nr_choices, [3, 7, 11, 15, 19, 23, 27, 31]))
        choice_labeling.set_choices(
            'west',
            stormpy.BitVector(nr_choices, [2, 6, 10, 14, 18, 22, 26, 30]))
        choice_labeling.set_choices(
            'east', stormpy.BitVector(nr_choices,
                                      [1, 5, 9, 13, 17, 21, 25, 29]))
        choice_labeling.set_choices('done',
                                    stormpy.BitVector(nr_choices, [33]))

        # state valuations
        manager = stormpy.ExpressionManager()
        var_x = manager.create_integer_variable(name='x')
        var_y = manager.create_integer_variable(name='y')
        var_o = manager.create_integer_variable(name='o')
        v_builder = stormpy.StateValuationsBuilder()

        v_builder.add_variable(var_x)
        v_builder.add_variable(var_y)
        v_builder.add_variable(var_o)

        v_builder.add_state(state=0,
                            boolean_values=[],
                            integer_values=[0, 0, 0],
                            rational_values=[])
        v_builder.add_state(state=1,
                            boolean_values=[],
                            integer_values=[0, 0, 1],
                            rational_values=[])
        v_builder.add_state(state=2,
                            boolean_values=[],
                            integer_values=[0, 1, 1],
                            rational_values=[])
        v_builder.add_state(state=3,
                            boolean_values=[],
                            integer_values=[0, 2, 1],
                            rational_values=[])
        v_builder.add_state(state=4,
                            boolean_values=[],
                            integer_values=[1, 0, 1],
                            rational_values=[])
        v_builder.add_state(state=5,
                            boolean_values=[],
                            integer_values=[1, 1, 1],
                            rational_values=[])
        v_builder.add_state(state=6,
                            boolean_values=[],
                            integer_values=[1, 2, 1],
                            rational_values=[])
        v_builder.add_state(state=7,
                            boolean_values=[],
                            integer_values=[2, 1, 1],
                            rational_values=[])
        v_builder.add_state(state=8,
                            boolean_values=[],
                            integer_values=[2, 2, 1],
                            rational_values=[])
        v_builder.add_state(state=9,
                            boolean_values=[],
                            integer_values=[2, 0, 2],
                            rational_values=[])

        state_valuations = v_builder.build(nr_states)

        observations = [1, 0, 0, 0, 0, 0, 0, 0, 0, 2]

        # Build components, set rate_transitions to False
        components = stormpy.SparseModelComponents(
            transition_matrix=transition_matrix,
            state_labeling=state_labeling,
            reward_models=reward_models,
            rate_transitions=False)
        components.state_valuations = state_valuations
        components.choice_labeling = choice_labeling
        # components.choice_origins=choice_origins
        components.observability_classes = observations

        # Build POMDP
        pomdp = stormpy.storage.SparsePomdp(components)
        assert type(pomdp) is stormpy.SparsePomdp
        assert not pomdp.supports_parameters

        # Test transition matrix
        assert pomdp.nr_choices == nr_choices
        assert pomdp.nr_states == nr_states
        assert pomdp.nr_transitions == 41
        for e in pomdp.transition_matrix:
            assert e.value() == 1 or e.value() == 0 or e.value() == 0.125
        for state in pomdp.states:
            assert len(state.actions) <= 4

        # Test state labeling
        assert pomdp.labeling.get_labels() == {'init', 'goal', 'deadlock'}

        # Test reward models
        assert len(pomdp.reward_models) == 1
        assert not pomdp.reward_models[''].has_state_rewards
        assert pomdp.reward_models[''].has_state_action_rewards
        for reward in pomdp.reward_models[''].state_action_rewards:
            assert reward == 1.0 or reward == 0.0
        assert not pomdp.reward_models[''].has_transition_rewards

        # Test choice labeling
        assert pomdp.has_choice_labeling()
        assert pomdp.choice_labeling.get_labels() == {
            'east', 'west', 'north', 'south', 'done'
        }

        # Test state valuations
        assert pomdp.has_state_valuations()
        assert pomdp.state_valuations
        value_x = [None] * nr_states
        value_y = [None] * nr_states
        value_o = [None] * nr_states
        for s in range(0, pomdp.nr_states):
            value_x[s] = pomdp.state_valuations.get_integer_value(s, var_x)
            value_y[s] = pomdp.state_valuations.get_integer_value(s, var_y)
            value_o[s] = pomdp.state_valuations.get_integer_value(s, var_o)
        assert value_x == [0, 0, 0, 0, 1, 1, 1, 2, 2, 2]
        assert value_y == [0, 0, 1, 2, 0, 1, 2, 1, 2, 0]
        assert value_o == [0, 1, 1, 1, 1, 1, 1, 1, 1, 2]

        # Test choice origins
        assert not pomdp.has_choice_origins()

        assert pomdp.observations == [1, 0, 0, 0, 0, 0, 0, 0, 0, 2]
    def test_build_ctmc(self):
        import numpy as np

        nr_states = 12
        nr_choices = 12

        # Build transition_matrix
        transitions = np.array([[0, 0.5, 0.5, 200, 0, 0, 0, 0, 0, 0, 0, 0],
                                [0, 0, 0, 0, 0.5, 200, 0, 0, 0, 0, 0, 0],
                                [0, 0, 0, 0, 0.5, 0, 200, 0, 0, 0, 0, 0],
                                [200, 0, 0, 0, 0, 0, 0.5, 0.5, 0, 0, 0, 0],
                                [0, 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0],
                                [0, 0, 0, 1, 0, 0, 0, 0, 0.5, 0, 0, 0],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 200, 0],
                                [0, 200, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0],
                                [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 200],
                                [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5],
                                [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
                               dtype='float64')

        transition_matrix = stormpy.build_sparse_matrix(transitions)

        # state labeling
        state_labeling = stormpy.storage.StateLabeling(nr_states)
        # Add labels
        state_labels = {'init', 'deadlock', 'target'}
        for label in state_labels:
            state_labeling.add_label(label)

        # Add labels to states
        state_labeling.add_label_to_state('init', 0)
        state_labeling.set_states('target',
                                  stormpy.BitVector(nr_states, [5, 8]))

        # reward models
        reward_models = {}
        # vector representing state-action rewards
        action_reward = [
            0.0, 0.0, 0.0, 0.0, 0.0, 2 / 3, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0
        ]
        reward_models['served'] = stormpy.SparseRewardModel(
            optional_state_action_reward_vector=action_reward)

        # vector representing state rewards
        state_reward = [
            0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0
        ]
        reward_models['waiting'] = stormpy.SparseRewardModel(
            optional_state_reward_vector=state_reward)

        # choice labeling
        choice_labeling = stormpy.storage.ChoiceLabeling(nr_choices)
        choice_labels = {
            'loop1a', 'loop1b', 'serve1', 'loop2a', 'loop2b', 'serve2'
        }
        # Add labels
        for label in choice_labels:
            choice_labeling.add_label(label)

        choice_labeling.set_choices('loop1a',
                                    stormpy.BitVector(nr_choices, [0, 2]))
        choice_labeling.set_choices('loop1b',
                                    stormpy.BitVector(nr_choices, [1, 4]))
        choice_labeling.set_choices('serve1',
                                    stormpy.BitVector(nr_choices, [5, 8]))
        choice_labeling.set_choices('loop2a',
                                    stormpy.BitVector(nr_choices, [3, 7]))
        choice_labeling.set_choices('loop2b',
                                    stormpy.BitVector(nr_choices, [6, 9]))
        choice_labeling.set_choices('serve2',
                                    stormpy.BitVector(nr_choices, [10, 11]))

        # state exit rates
        exit_rates = [
            201.0, 200.5, 200.5, 201.0, 200.0, 1.5, 200.5, 200.5, 1.0, 200.0,
            1.5, 1.0
        ]

        # state valuations
        manager = stormpy.ExpressionManager()
        var_s = manager.create_integer_variable(name='s')
        var_a = manager.create_integer_variable(name='a')
        var_s1 = manager.create_integer_variable(name='s1')
        var_s2 = manager.create_integer_variable(name='s2')
        v_builder = stormpy.StateValuationsBuilder()
        v_builder.add_variable(var_s)
        v_builder.add_variable(var_a)
        v_builder.add_variable(var_s1)
        v_builder.add_variable(var_s2)

        v_builder.add_state(state=0,
                            boolean_values=[],
                            integer_values=[1, 0, 0, 0],
                            rational_values=[])
        v_builder.add_state(state=1,
                            boolean_values=[],
                            integer_values=[1, 0, 1, 0],
                            rational_values=[])
        v_builder.add_state(state=2,
                            boolean_values=[],
                            integer_values=[1, 0, 0, 1],
                            rational_values=[])
        v_builder.add_state(state=3,
                            boolean_values=[],
                            integer_values=[2, 0, 0, 0],
                            rational_values=[])
        v_builder.add_state(state=4,
                            boolean_values=[],
                            integer_values=[1, 0, 1, 1],
                            rational_values=[])
        v_builder.add_state(state=5,
                            boolean_values=[],
                            integer_values=[1, 1, 1, 0],
                            rational_values=[])
        v_builder.add_state(state=6,
                            boolean_values=[],
                            integer_values=[2, 0, 0, 1],
                            rational_values=[])
        v_builder.add_state(state=7,
                            boolean_values=[],
                            integer_values=[2, 0, 1, 0],
                            rational_values=[])
        v_builder.add_state(state=8,
                            boolean_values=[],
                            integer_values=[1, 1, 1, 1],
                            rational_values=[])
        v_builder.add_state(state=9,
                            boolean_values=[],
                            integer_values=[2, 0, 1, 1],
                            rational_values=[])
        v_builder.add_state(state=10,
                            boolean_values=[],
                            integer_values=[2, 1, 0, 1],
                            rational_values=[])
        v_builder.add_state(state=11,
                            boolean_values=[],
                            integer_values=[2, 1, 1, 1],
                            rational_values=[])

        state_valuations = v_builder.build(nr_states)

        # set rate_transitions to True: the transition values are interpreted as rates
        components = stormpy.SparseModelComponents(
            transition_matrix=transition_matrix,
            state_labeling=state_labeling,
            reward_models=reward_models,
            rate_transitions=True)
        components.choice_labeling = choice_labeling
        components.exit_rates = exit_rates
        components.state_valuations = state_valuations

        # Build CTMC
        ctmc = stormpy.storage.SparseCtmc(components)
        assert type(ctmc) is stormpy.SparseCtmc
        assert not ctmc.supports_parameters

        # Test transition matrix
        assert ctmc.nr_choices == nr_choices
        assert ctmc.nr_states == nr_states
        assert ctmc.nr_transitions == 22
        assert ctmc.transition_matrix.nr_columns == nr_states
        assert ctmc.transition_matrix.nr_rows == nr_choices
        for e in ctmc.transition_matrix:
            assert e.value() == 0.5 or e.value() == 0 or e.value(
            ) == 200 or e.value() == 1.0
        for state in ctmc.states:
            assert len(state.actions) <= 1

        # Test state labeling
        assert ctmc.labeling.get_labels() == {'target', 'init', 'deadlock'}

        # Test reward models
        assert len(ctmc.reward_models) == 2
        assert not ctmc.reward_models["served"].has_state_rewards
        assert ctmc.reward_models["served"].has_state_action_rewards
        assert ctmc.reward_models["served"].state_action_rewards == [
            0.0, 0.0, 0.0, 0.0, 0.0, 0.6666666666666666, 0.0, 0.0, 1.0, 0.0,
            0.0, 0.0
        ]
        assert not ctmc.reward_models["served"].has_transition_rewards

        assert ctmc.reward_models["waiting"].has_state_rewards
        assert not ctmc.reward_models["waiting"].has_state_action_rewards
        assert ctmc.reward_models["waiting"].state_rewards == [
            0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0
        ]
        assert not ctmc.reward_models["waiting"].has_transition_rewards

        # Test choice labeling
        assert ctmc.has_choice_labeling()
        assert ctmc.choice_labeling.get_labels() == {
            'loop1a', 'loop1b', 'serve1', 'loop2a', 'loop2b', 'serve2'
        }

        # Test state valuations
        assert ctmc.has_state_valuations()
        assert ctmc.state_valuations
        value_s = [None] * nr_states
        value_a = [None] * nr_states
        value_s1 = [None] * nr_states
        value_s2 = [None] * nr_states
        for s in range(0, ctmc.nr_states):
            value_s[s] = ctmc.state_valuations.get_integer_value(s, var_s)
            value_a[s] = ctmc.state_valuations.get_integer_value(s, var_a)
            value_s1[s] = ctmc.state_valuations.get_integer_value(s, var_s1)
            value_s2[s] = ctmc.state_valuations.get_integer_value(s, var_s2)
        assert value_s == [1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2]
        assert value_a == [0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1]
        assert value_s1 == [0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1]
        assert value_s2 == [0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1]

        # Test choice origins
        assert not ctmc.has_choice_origins()

        # Test exit_rates
        assert ctmc.exit_rates == [
            201.0, 200.5, 200.5, 201.0, 200.0, 1.5, 200.5, 200.5, 1.0, 200.0,
            1.5, 1.0
        ]