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.

98 lines
4.1 KiB

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