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.

96 lines
4.1 KiB

  1. import stormpy
  2. from helpers.helper import get_example_path
  3. class TestMatrix:
  4. def test_sparse_matrix(self):
  5. model = stormpy.parse_explicit_model(get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab"))
  6. matrix = model.transition_matrix
  7. assert type(matrix) is stormpy.storage.SparseMatrix
  8. assert matrix.nr_rows == model.nr_states
  9. assert matrix.nr_columns == model.nr_states
  10. assert matrix.nr_entries == 27 #model.nr_transitions
  11. for e in matrix:
  12. assert e.value() == 0.5 or e.value() == 0 or (e.value() == 1 and e.column > 6)
  13. def test_change_sparse_matrix(self):
  14. model = stormpy.parse_explicit_model(get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab"))
  15. matrix = model.transition_matrix
  16. for e in matrix:
  17. assert e.value() == 0.5 or e.value() == 0 or e.value() == 1
  18. i = 0
  19. for e in matrix:
  20. e.set_value(i)
  21. i += 0.1
  22. i = 0
  23. for e in matrix:
  24. assert e.value() == i
  25. i += 0.1
  26. def test_change_sparse_matrix_modelchecking(self):
  27. import stormpy.logic
  28. model = stormpy.parse_explicit_model(get_example_path("dtmc", "die.tra"), get_example_path("dtmc", "die.lab"))
  29. matrix = model.transition_matrix
  30. # Check matrix
  31. for e in matrix:
  32. assert e.value() == 0.5 or e.value() == 0 or e.value() == 1
  33. # First model checking
  34. formulas = stormpy.parse_formulas("P=? [ F \"one\" ]")
  35. result = stormpy.model_checking(model, formulas[0])
  36. assert result == 0.16666666666666663
  37. # Change probabilities
  38. i = 0
  39. for e in matrix:
  40. if e.value() == 0.5:
  41. if i % 2 == 0:
  42. e.set_value(0.3)
  43. else:
  44. e.set_value(0.7)
  45. i += 1
  46. for e in matrix:
  47. assert e.value() == 0.3 or e.value() == 0.7 or e.value() == 1 or e.value() == 0
  48. # Second model checking
  49. result = stormpy.model_checking(model, formulas[0])
  50. assert result == 0.06923076923076932
  51. # Change probabilities again
  52. for state in stormpy.state.State(0, model):
  53. for action in state.actions():
  54. for transition in action.transitions():
  55. if transition.value() == 0.3:
  56. transition.set_value(0.8)
  57. elif transition.value() == 0.7:
  58. transition.set_value(0.2)
  59. # Third model checking
  60. result = stormpy.model_checking(model, formulas[0])
  61. assert result == 0.3555555555555556 or result == 0.3555555555555557
  62. def test_change_parametric_sparse_matrix_modelchecking(self):
  63. import stormpy.logic
  64. import pycarl
  65. program = stormpy.parse_prism_program(get_example_path("pdtmc", "brp16_2.pm"))
  66. formulas = stormpy.parse_formulas_for_prism_program("P=? [ F s=5 ]", program)
  67. model = stormpy.build_parametric_model(program, formulas[0])
  68. matrix = model.transition_matrix
  69. # Check matrix
  70. one_pol = pycarl.Rational(1)
  71. one_pol = pycarl.FactorizedPolynomial(one_pol)
  72. one = pycarl.FactorizedRationalFunction(one_pol, one_pol)
  73. for e in matrix:
  74. assert e.value() == one or len(e.value().gather_variables()) > 0
  75. # First model checking
  76. result = stormpy.model_checking(model, formulas[0])
  77. assert len(result.result_function.gather_variables()) > 0
  78. # Change probabilities
  79. two_pol = pycarl.Rational(2)
  80. two_pol = pycarl.FactorizedPolynomial(two_pol)
  81. new_val = pycarl.FactorizedRationalFunction(one_pol, two_pol)
  82. for e in matrix:
  83. if len(e.value().gather_variables()) > 0:
  84. e.set_value(new_val)
  85. for e in matrix:
  86. assert e.value() == new_val or e.value() == one
  87. # Second model checking
  88. result = stormpy.model_checking(model, formulas[0])
  89. assert len(result.result_function.gather_variables()) == 0