You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
8.1 KiB

7 years ago
7 years ago
7 years ago
  1. ****************************
  2. Getting Started
  3. ****************************
  4. Before starting with this guide, one should follow the instructions for :doc:`installation`.
  5. A Quick Tour through Stormpy
  6. ================================
  7. This guide is intended for people which have a basic understanding of probabilistic models and their verification. More details and further pointers to literature can be found on the
  8. `Storm website <http://www.stormchecker.org/>`_.
  9. While we assume some very basic programming concepts, we refrain from using more advanced concepts of python throughout the guide.
  10. We start with a selection of high-level constructs in stormpy, and go into more details afterwards. More in-depth examples can be found in the :doc:`advanced_topics`.
  11. .. seealso:: The code examples are also given in the `examples/ <https://github.com/moves-rwth/stormpy/blob/master/examples/>`_ folder. These boxes throughout the text will tell you which example contains the code discussed.
  12. We start by launching the python 3 interpreter::
  13. $ python3
  14. First we import stormpy::
  15. >>> import stormpy
  16. Building models
  17. ------------------------------------------------
  18. .. seealso:: `01-getting-started.py <https://github.com/moves-rwth/stormpy/blob/master/examples/01-getting-started.py>`_
  19. There are several ways to create a Markov chain.
  20. One of the easiest is to parse a description of such a Markov chain and to let Storm build the chain.
  21. Here, we build a Markov chain from a prism program.
  22. Stormpy comes with a small set of examples, which we use here::
  23. >>> import stormpy.examples
  24. >>> import stormpy.examples.files
  25. With this, we can now import the path of our prism file::
  26. >>> path = stormpy.examples.files.prism_dtmc_die
  27. >>> prism_program = stormpy.parse_prism_program(path)
  28. The ``prism_program`` can be translated into a Markov chain::
  29. >>> model = stormpy.build_model(prism_program)
  30. >>> print("Number of states: {}".format(model.nr_states))
  31. Number of states: 13
  32. >>> print("Number of transitions: {}".format(model.nr_transitions))
  33. Number of transitions: 20
  34. This tells us that the model has 13 states and 20 transitions.
  35. Moreover, initial states and deadlocks are indicated with a labelling function. We can see the labels present in the model by::
  36. >>> print("Labels: {}".format(model.labeling.get_labels()))
  37. Labels: ...
  38. We will investigate ways to examine the model in more detail later in :ref:`getting-started-investigating-the-model`.
  39. .. _getting-started-building-properties:
  40. Building properties
  41. --------------------------
  42. .. seealso:: `02-getting-started.py <https://github.com/moves-rwth/stormpy/blob/master/examples/02-getting-started.py>`_
  43. Storm takes properties in the prism-property format.
  44. To express that one is interested in the reachability of any state where the prism program variable ``s`` is 2, one would formulate::
  45. P=? [F s=2]
  46. Stormpy can be used to parse this. As the variables in the property refer to a program, the program has to be passed as an additional parameter::
  47. >>> formula_str = "P=? [F s=2]"
  48. >>> properties = stormpy.parse_properties(formula_str, prism_program)
  49. Notice that properties is now a list of properties containing a single element.
  50. However, if we build the model as before, then the appropriate information that the variable ``s=2`` in some states is not present.
  51. In order to label the states accordingly, we should notify Storm upon building the model that we would like to preserve given properties.
  52. Storm will then add the labels accordingly::
  53. >>> model = stormpy.build_model(prism_program, properties)
  54. >>> print("Labels in the model: {}".format(sorted(model.labeling.get_labels())))
  55. Labels in the model: ['(s = 2)', 'deadlock', 'init']
  56. Model building however now behaves slightly different: Only the properties passed are preserved, which means that model building might skip parts of the model.
  57. In particular, to check the probability of eventually reaching a state ``x`` where ``s=2``, successor states of ``x`` are not relevant::
  58. >>> print("Number of states: {}".format(model.nr_states))
  59. Number of states: 8
  60. If we consider another property, however, such as::
  61. P=? [F s=7 & d=2]
  62. then Storm is only skipping exploration of successors of the particular state ``y`` where ``s=7`` and ``d=2``. In this model, state ``y`` has a self-loop, so effectively, the whole model is explored.
  63. .. _getting-started-checking-properties:
  64. Checking properties
  65. ------------------------------------
  66. .. seealso:: `03-getting-started.py <https://github.com/moves-rwth/stormpy/blob/master/examples/03-getting-started.py>`_
  67. The last lesson taught us to construct properties and models with matching state labels.
  68. Now default checking routines are just a simple command away::
  69. >>> properties = stormpy.parse_properties(formula_str, prism_program)
  70. >>> model = stormpy.build_model(prism_program, properties)
  71. >>> result = stormpy.model_checking(model, properties[0])
  72. The result may contain information about all states.
  73. Instead, we can iterate over the results::
  74. >>> assert result.result_for_all_states
  75. >>> for x in result.get_values():
  76. ... pass # do something with x
  77. .. topic:: Results for all states
  78. Some model checking algorithms do not provide results for all states. In those cases, the result is not valid for all states, and to iterate over them, a different method is required. We will explain this later.
  79. A good way to get the result for the initial states is as follows::
  80. >>> initial_state = model.initial_states[0]
  81. >>> print(result.at(initial_state))
  82. 0.5
  83. .. _getting-started-investigating-the-model:
  84. Investigating the model
  85. -------------------------------------
  86. .. seealso:: `04-getting-started.py <https://github.com/moves-rwth/stormpy/blob/master/examples/04-getting-started.py>`_
  87. One powerful part of the Storm model checker is to quickly create the Markov chain from higher-order descriptions, as seen above::
  88. >>> path = stormpy.examples.files.prism_dtmc_die
  89. >>> prism_program = stormpy.parse_prism_program(path)
  90. >>> model = stormpy.build_model(prism_program)
  91. In this example, we will exploit this, and explore the underlying Markov chain of the model.
  92. The most basic question might be what the type of the constructed model is::
  93. >>> print(model.model_type)
  94. ModelType.DTMC
  95. We can also directly explore the underlying state space/matrix.
  96. Notice that this code can be applied to both deterministic and non-deterministic models::
  97. >>> for state in model.states:
  98. ... for action in state.actions:
  99. ... for transition in action.transitions:
  100. ... print("From state {}, with probability {}, go to state {}".format(state, transition.value(), transition.column))
  101. From state 0, with probability 0.5, go to state 1
  102. From state 0, with probability 0.5, go to state 2
  103. From state 1, with probability 0.5, go to state 3
  104. From state 1, with probability 0.5, go to state 4
  105. From state 2, with probability 0.5, go to state 5
  106. From state 2, with probability 0.5, go to state 6
  107. From state 3, with probability 0.5, go to state 1
  108. From state 3, with probability 0.5, go to state 7
  109. From state 4, with probability 0.5, go to state 8
  110. From state 4, with probability 0.5, go to state 9
  111. From state 5, with probability 0.5, go to state 10
  112. From state 5, with probability 0.5, go to state 11
  113. From state 6, with probability 0.5, go to state 2
  114. From state 6, with probability 0.5, go to state 12
  115. From state 7, with probability 1.0, go to state 7
  116. From state 8, with probability 1.0, go to state 8
  117. From state 9, with probability 1.0, go to state 9
  118. From state 10, with probability 1.0, go to state 10
  119. From state 11, with probability 1.0, go to state 11
  120. From state 12, with probability 1.0, go to state 12
  121. Let us go into some more details. For DTMCs, each state has (at most) one outgoing probability distribution.
  122. Thus::
  123. >>> for state in model.states:
  124. ... assert len(state.actions) <= 1
  125. We can also check if a state is indeed an initial state. Notice that ``model.initial_states`` contains state ids, not states.::
  126. >>> for state in model.states:
  127. ... if state.id in model.initial_states:
  128. ... pass