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.

187 lines
8.1 KiB

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 Markov chains::
  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 in :ref:`getting-started-investigating-the-model`.
  39. Building properties
  40. --------------------------
  41. .. seealso:: `02-getting-started.py <https://github.com/moves-rwth/stormpy/blob/master/examples/02-getting-started.py>`_
  42. Storm takes properties in the prism-property format.
  43. To express that one is interested in the reachability of any state where the prism program variable s is 2, one would formulate::
  44. P=? [F s=2]
  45. 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::
  46. >>> formula_str = "P=? [F s=2]"
  47. >>> properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program)
  48. Notice that properties is now a list of properties containing a single element.
  49. However, if we build the model as before, then the appropriate information that the variable s=2 in some states is not present.
  50. In order to label the states accordingly, we should notify Storm upon building the model that we would like to preserve given properties.
  51. Storm will then add the labels accordingly::
  52. >>> model = stormpy.build_model(prism_program, properties)
  53. >>> print("Labels in the model: {}".format(sorted(model.labeling.get_labels())))
  54. Labels in the model: ['(s = 2)', 'deadlock', 'init']
  55. Model building however now behaves slightly different: Only the properties passed are preserved, which means that model building might skip parts of the model.
  56. In particular, to check the probability of eventually reaching a state x where s=2, successor states of x are not relevant::
  57. >>> print("Number of states: {}".format(model.nr_states))
  58. Number of states: 8
  59. If we consider another property, however, such as::
  60. P=? [F s=7 & d=2]
  61. 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.
  62. .. _getting-started-checking-properties:
  63. Checking properties
  64. ------------------------------------
  65. .. seealso:: `03-getting-started.py <https://github.com/moves-rwth/stormpy/blob/master/examples/03-getting-started.py>`_
  66. The last lesson taught us to construct properties and models with matching state labels.
  67. Now default checking routines are just a simple command away::
  68. >>> properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program)
  69. >>> model = stormpy.build_model(prism_program, properties)
  70. >>> result = stormpy.model_checking(model, properties[0])
  71. The result may contain information about all states.
  72. Instead, we can iterate over the results::
  73. >>> assert result.result_for_all_states
  74. >>> for x in result.get_values():
  75. ... pass # do something with x
  76. .. topic:: Results for all states
  77. 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.
  78. A good way to get the result for the initial states is as follows::
  79. >>> initial_state = model.initial_states[0]
  80. >>> print(result.at(initial_state))
  81. 0.5
  82. .. _getting-started-investigating-the-model:
  83. Investigating the model
  84. -------------------------------------
  85. .. seealso:: `04-getting-started.py <https://github.com/moves-rwth/stormpy/blob/master/examples/04-getting-started.py>`_
  86. One powerful part of the Storm model checker is to quickly create the Markov chain from higher-order descriptions, as seen above::
  87. >>> path = stormpy.examples.files.prism_dtmc_die
  88. >>> prism_program = stormpy.parse_prism_program(path)
  89. >>> model = stormpy.build_model(prism_program)
  90. In this example, we will exploit this, and explore the underlying Markov chain of the model.
  91. The most basic question might be what the type of the constructed model is::
  92. >>> print(model.model_type)
  93. ModelType.DTMC
  94. We can also directly explore the underlying state space/matrix.
  95. Notice that this code can be applied to both deterministic and non-deterministic models::
  96. >>> for state in model.states:
  97. ... for action in state.actions:
  98. ... for transition in action.transitions:
  99. ... print("From state {}, with probability {}, go to state {}".format(state, transition.value(), transition.column))
  100. From state 0, with probability 0.5, go to state 1
  101. From state 0, with probability 0.5, go to state 2
  102. From state 1, with probability 0.5, go to state 3
  103. From state 1, with probability 0.5, go to state 4
  104. From state 2, with probability 0.5, go to state 5
  105. From state 2, with probability 0.5, go to state 6
  106. From state 3, with probability 0.5, go to state 1
  107. From state 3, with probability 0.5, go to state 7
  108. From state 4, with probability 0.5, go to state 8
  109. From state 4, with probability 0.5, go to state 9
  110. From state 5, with probability 0.5, go to state 10
  111. From state 5, with probability 0.5, go to state 11
  112. From state 6, with probability 0.5, go to state 2
  113. From state 6, with probability 0.5, go to state 12
  114. From state 7, with probability 1.0, go to state 7
  115. From state 8, with probability 1.0, go to state 8
  116. From state 9, with probability 1.0, go to state 9
  117. From state 10, with probability 1.0, go to state 10
  118. From state 11, with probability 1.0, go to state 11
  119. From state 12, with probability 1.0, go to state 12
  120. Let us go into some more details. For DTMCs, each state has (at most) one outgoing probability distribution.
  121. Thus::
  122. >>> for state in model.states:
  123. ... assert len(state.actions) <= 1
  124. We can also check if a state is indeed an initial state. Notice that model.initial_states contains state ids, not states.::
  125. >>> for state in model.states:
  126. ... if state.id in model.initial_states:
  127. ... pass