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.

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