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.

141 lines
6.1 KiB

8 years ago
8 years ago
  1. import stormpy
  2. from helpers.helper import get_example_path
  3. import math
  4. class TestMatrix:
  5. def test_matrix(self):
  6. model = stormpy.build_sparse_model_from_explicit(get_example_path("dtmc", "die.tra"),
  7. get_example_path("dtmc", "die.lab"))
  8. matrix = model.transition_matrix
  9. assert type(matrix) is stormpy.storage.SparseMatrix
  10. assert matrix.nr_rows == model.nr_states
  11. assert matrix.nr_columns == model.nr_states
  12. assert matrix.nr_entries == 20
  13. assert matrix.nr_entries == model.nr_transitions
  14. for e in matrix:
  15. assert e.value() == 0.5 or e.value() == 0 or (e.value() == 1 and e.column > 6)
  16. def test_backward_matrix(self):
  17. model = stormpy.build_sparse_model_from_explicit(get_example_path("dtmc", "die.tra"),
  18. get_example_path("dtmc", "die.lab"))
  19. matrix = model.backward_transition_matrix
  20. assert type(matrix) is stormpy.storage.SparseMatrix
  21. assert matrix.nr_rows == model.nr_states
  22. assert matrix.nr_columns == model.nr_states
  23. assert matrix.nr_entries == 20
  24. assert matrix.nr_entries == model.nr_transitions
  25. for e in matrix:
  26. assert e.value() == 0.5 or e.value() == 0 or (e.value() == 1 and e.column > 6)
  27. def test_change_matrix(self):
  28. model = stormpy.build_sparse_model_from_explicit(get_example_path("dtmc", "die.tra"),
  29. get_example_path("dtmc", "die.lab"))
  30. matrix = model.transition_matrix
  31. for e in matrix:
  32. assert e.value() == 0.5 or e.value() == 0 or e.value() == 1
  33. i = 0
  34. for e in matrix:
  35. e.set_value(i)
  36. i += 0.1
  37. i = 0
  38. for e in matrix:
  39. assert e.value() == i
  40. i += 0.1
  41. def test_change_matrix_modelchecking(self):
  42. import stormpy.logic
  43. model = stormpy.build_sparse_model_from_explicit(get_example_path("dtmc", "die.tra"),
  44. get_example_path("dtmc", "die.lab"))
  45. matrix = model.transition_matrix
  46. # Check matrix
  47. for e in matrix:
  48. assert e.value() == 0.5 or e.value() == 0 or e.value() == 1
  49. # First model checking
  50. formulas = stormpy.parse_properties("P=? [ F \"one\" ]")
  51. result = stormpy.model_checking(model, formulas[0])
  52. resValue = result.at(model.initial_states[0])
  53. assert math.isclose(resValue, 0.16666666666666663)
  54. # Change probabilities
  55. i = 0
  56. for e in matrix:
  57. if e.value() == 0.5:
  58. if i % 2 == 0:
  59. e.set_value(0.3)
  60. else:
  61. e.set_value(0.7)
  62. i += 1
  63. for e in matrix:
  64. assert e.value() == 0.3 or e.value() == 0.7 or e.value() == 1 or e.value() == 0
  65. # Second model checking
  66. result = stormpy.model_checking(model, formulas[0])
  67. resValue = result.at(model.initial_states[0])
  68. assert math.isclose(resValue, 0.06923076923076932)
  69. # Change probabilities again
  70. for state in model.states:
  71. for action in state.actions:
  72. for transition in action.transitions:
  73. if transition.value() == 0.3:
  74. transition.set_value(0.8)
  75. elif transition.value() == 0.7:
  76. transition.set_value(0.2)
  77. # Third model checking
  78. result = stormpy.model_checking(model, formulas[0])
  79. resValue = result.at(model.initial_states[0])
  80. assert math.isclose(resValue, 0.3555555555555556)
  81. def test_change_parametric_matrix_modelchecking(self):
  82. import stormpy.logic
  83. program = stormpy.parse_prism_program(get_example_path("pdtmc", "brp16_2.pm"))
  84. formulas = stormpy.parse_properties_for_prism_program("P=? [ F s=5 ]", program)
  85. model = stormpy.build_parametric_model(program, formulas)
  86. initial_state = model.initial_states[0]
  87. assert initial_state == 0
  88. matrix = model.transition_matrix
  89. # Check matrix
  90. one_pol = stormpy.RationalRF(1)
  91. one_pol = stormpy.FactorizedPolynomial(one_pol)
  92. one = stormpy.FactorizedRationalFunction(one_pol, one_pol)
  93. for e in matrix:
  94. assert e.value() == one or len(e.value().gather_variables()) > 0
  95. # First model checking
  96. result = stormpy.model_checking(model, formulas[0])
  97. ratFunc = result.at(initial_state)
  98. assert len(ratFunc.gather_variables()) > 0
  99. # Change probabilities
  100. two_pol = stormpy.RationalRF(2)
  101. two_pol = stormpy.FactorizedPolynomial(two_pol)
  102. new_val = stormpy.FactorizedRationalFunction(one_pol, two_pol)
  103. for e in matrix:
  104. if len(e.value().gather_variables()) > 0:
  105. e.set_value(new_val)
  106. for e in matrix:
  107. assert e.value() == new_val or e.value() == one
  108. # Second model checking
  109. result = stormpy.model_checking(model, formulas[0])
  110. ratFunc = result.at(initial_state)
  111. assert len(ratFunc.gather_variables()) == 0
  112. def test_submatrix(self):
  113. model = stormpy.build_sparse_model_from_explicit(get_example_path("dtmc", "die.tra"),
  114. get_example_path("dtmc", "die.lab"))
  115. matrix = model.transition_matrix
  116. assert matrix.nr_rows == 13
  117. assert matrix.nr_columns == 13
  118. assert matrix.nr_entries == 20
  119. assert matrix.nr_entries == model.nr_transitions
  120. for e in matrix:
  121. assert e.value() == 0.5 or e.value() == 0 or (e.value() == 1 and e.column > 6)
  122. row_constraint = stormpy.BitVector(13, [0, 1, 3, 4, 7, 8, 9])
  123. submatrix = matrix.submatrix(row_constraint, row_constraint)
  124. assert submatrix.nr_rows == 7
  125. assert submatrix.nr_columns == 7
  126. assert submatrix.nr_entries == 10
  127. for e in submatrix:
  128. assert e.value() == 0.5 or e.value() == 0 or (e.value() == 1 and e.column > 3)