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. import pycarl
  78. program = stormpy.parse_prism_program(get_example_path("pdtmc", "brp16_2.pm"))
  79. formulas = stormpy.parse_properties_for_prism_program("P=? [ F s=5 ]", program)
  80. model = stormpy.build_parametric_model(program, formulas)
  81. initial_state = model.initial_states[0]
  82. assert initial_state == 0
  83. matrix = model.transition_matrix
  84. # Check matrix
  85. one_pol = pycarl.Rational(1)
  86. one_pol = pycarl.FactorizedPolynomial(one_pol)
  87. one = pycarl.FactorizedRationalFunction(one_pol, one_pol)
  88. for e in matrix:
  89. assert e.value() == one or len(e.value().gather_variables()) > 0
  90. # First model checking
  91. result = stormpy.model_checking(model, formulas[0])
  92. ratFunc = result.result.at(initial_state)
  93. assert len(ratFunc.gather_variables()) > 0
  94. # Change probabilities
  95. two_pol = pycarl.Rational(2)
  96. two_pol = pycarl.FactorizedPolynomial(two_pol)
  97. new_val = pycarl.FactorizedRationalFunction(one_pol, two_pol)
  98. for e in matrix:
  99. if len(e.value().gather_variables()) > 0:
  100. e.set_value(new_val)
  101. for e in matrix:
  102. assert e.value() == new_val or e.value() == one
  103. # Second model checking
  104. result = stormpy.model_checking(model, formulas[0])
  105. ratFunc = result.result.at(initial_state)
  106. assert len(ratFunc.gather_variables()) == 0