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.

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