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.

209 lines
9.0 KiB

  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.
  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. In order to do this, we import stormpy::
  13. >>> import stormpy
  14. >>> import stormpy.core
  15. Building models
  16. ------------------------------------------------
  17. .. seealso:: `01-getting-started.py <https://github.com/moves-rwth/stormpy/blob/master/examples/01-getting-started.py>`_
  18. There are several ways to create a Markov chain.
  19. One of the easiest is to parse a description of such a Markov chain and to let storm build the chain.
  20. Here, we build a Markov chain from a prism program.
  21. Stormpy comes with a small set of examples, which we use here::
  22. >>> import stormpy.examples
  23. >>> import stormpy.examples.files
  24. With this, we can now import the path of our prism file::
  25. >>> path = stormpy.examples.files.prism_dtmc_die
  26. >>> prism_program = stormpy.parse_prism_program(path)
  27. The `prism_program` can be translated into Markov chains::
  28. >>> model = stormpy.build_model(prism_program)
  29. >>> print("Number of states: {}".format(model.nr_states))
  30. Number of states: 13
  31. >>> print("Number of transitions: {}".format(model.nr_transitions))
  32. Number of transitions: 20
  33. This tells us that the model has 13 states and 20 transitions.
  34. Moreover, initial states and deadlocks are indicated with a labelling function. We can see the labels present in the model by::
  35. >>> print("Labels: {}".format(model.labeling.get_labels()))
  36. Labels: ...
  37. We will investigate ways to examine the model in more detail in :ref:`getting-started-investigating-the-model`
  38. Building properties
  39. --------------------------
  40. .. seealso:: `02-getting-started.py <https://github.com/moves-rwth/stormpy/blob/master/examples/02-getting-started.py>`_
  41. Storm takes properties in the prism-property format.
  42. To express that one is interested in the reachability of any state where the prism program variable s is 2, one would formulate::
  43. P=? [F s=2]
  44. 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::
  45. >>> formula_str = "P=? [F s=2]"
  46. >>> properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program)
  47. Notice that properties is now a list of properties containing a single element.
  48. However, if we build the model as before, then the appropriate information that the variable s=2 in some states is not present.
  49. In order to label the states accordingly, we should notify storm upon building the model that we would like to preserve given properties.
  50. Storm will then add the labels accordingly::
  51. >>> model = stormpy.build_model(prism_program, properties)
  52. >>> print("Labels in the model: {}".format(sorted(model.labeling.get_labels())))
  53. Labels in the model: ['(s = 2)', 'deadlock', 'init']
  54. Model building however now behaves slightly different: Only the properties passed are preserved, which means that model building might skip parts of the model.
  55. In particular, to check the probability of eventually reaching a state x where s=2, successor states of x are not relevant::
  56. >>> print("Number of states: {}".format(model.nr_states))
  57. Number of states: 8
  58. If we consider another property, however, such as::
  59. P=? [F s=7 & d=2]
  60. 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.
  61. Checking properties
  62. ------------------------------------
  63. .. seealso:: `03-getting-started.py <https://github.com/moves-rwth/stormpy/blob/master/examples/03-getting-started.py>`_
  64. The last lesson taught us to construct properties and models with matching state labels.
  65. Now default checking routines are just a simple command away::
  66. >>> properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program)
  67. >>> model = stormpy.build_model(prism_program, properties)
  68. >>> result = stormpy.model_checking(model, properties[0])
  69. The result may contain information about all states.
  70. Instead, we can iterate over the results::
  71. >>> assert result.result_for_all_states
  72. >>> for x in result.get_values():
  73. ... pass # do something with x
  74. .. topic:: Results for all states
  75. 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.
  76. A good way to get the result for the initial states is as follows::
  77. >>> initial_state = model.initial_states[0]
  78. >>> print(result.at(initial_state))
  79. 0.5
  80. Instantiating parametric models
  81. ------------------------------------
  82. .. seealso:: `04-getting-started.py <https://github.com/moves-rwth/stormpy/blob/master/examples/04-getting-started.py>`_
  83. Input formats such as prism allow to specify programs with open constants. We refer to these open constants as parameters.
  84. If the constants only influence the probabilities or rates, but not the topology of the underlying model, we can build these models as parametric models::
  85. >>> model = stormpy.build_parametric_model(prism_program, properties)
  86. >>> parameters = model.collect_probability_parameters()
  87. >>> for x in parameters:
  88. ... print(x)
  89. In order to obtain a standard DTMC, MDP or other Markov model, we need to instantiate these models by means of a model instantiator::
  90. >>> import stormpy.pars
  91. >>> instantiator = stormpy.pars.PDtmcInstantiator(model)
  92. Before we obtain an instantiated model, we need to map parameters to values: We build such a dictionary as follows::
  93. >>> point = dict()
  94. >>> for x in parameters:
  95. ... print(x.name)
  96. ... point[x] = 0.4
  97. >>> instantiated_model = instantiator.instantiate(point)
  98. >>> result = stormpy.model_checking(instantiated_model, properties[0])
  99. Checking parametric models
  100. ------------------------------------
  101. .. seealso:: ``05-getting-started.py``
  102. .. _getting-started-investigating-the-model:
  103. Investigating the model
  104. -------------------------------------
  105. .. seealso:: `06-getting-started.py <https://github.com/moves-rwth/stormpy/blob/master/examples/06-getting-started.py>`_
  106. One powerful part of the storm model checker is to quickly create the Markov chain from higher-order descriptions, as seen above::
  107. >>> path = stormpy.examples.files.prism_dtmc_die
  108. >>> prism_program = stormpy.parse_prism_program(path)
  109. >>> model = stormpy.build_model(prism_program)
  110. In this example, we will exploit this, and explore the underlying Markov chain of the model.
  111. The most basic question might be what the type of the constructed model is::
  112. >>> print(model.model_type)
  113. ModelType.DTMC
  114. We can also directly explore the underlying matrix.
  115. Notice that this code can be applied to both deterministic and non-deterministic models::
  116. >>> for state in model.states:
  117. ... for action in state.actions:
  118. ... for transition in action.transitions:
  119. ... print("From state {}, with probability {}, go to state {}".format(state, transition.value(), transition.column))
  120. From state 0, with probability 0.5, go to state 1
  121. From state 0, with probability 0.5, go to state 2
  122. From state 1, with probability 0.5, go to state 3
  123. From state 1, with probability 0.5, go to state 4
  124. From state 2, with probability 0.5, go to state 5
  125. From state 2, with probability 0.5, go to state 6
  126. From state 3, with probability 0.5, go to state 1
  127. From state 3, with probability 0.5, go to state 7
  128. From state 4, with probability 0.5, go to state 8
  129. From state 4, with probability 0.5, go to state 9
  130. From state 5, with probability 0.5, go to state 10
  131. From state 5, with probability 0.5, go to state 11
  132. From state 6, with probability 0.5, go to state 2
  133. From state 6, with probability 0.5, go to state 12
  134. From state 7, with probability 1.0, go to state 7
  135. From state 8, with probability 1.0, go to state 8
  136. From state 9, with probability 1.0, go to state 9
  137. From state 10, with probability 1.0, go to state 10
  138. From state 11, with probability 1.0, go to state 11
  139. From state 12, with probability 1.0, go to state 12
  140. Let us go into some more details. For DTMCs, each state has (at most) one outgoing probability distribution.
  141. Thus::
  142. >>> for state in model.states:
  143. ... assert len(state.actions) <= 1