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.

106 lines
4.4 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_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_properties("P=? [ F \"one\" ]")
  36. result = stormpy.model_checking(model, formulas[0])
  37. resValue = result.at(model.initial_states[0])
  38. assert math.isclose(resValue, 0.16666666666666663)
  39. # Change probabilities
  40. i = 0
  41. for e in matrix:
  42. if e.value() == 0.5:
  43. if i % 2 == 0:
  44. e.set_value(0.3)
  45. else:
  46. e.set_value(0.7)
  47. i += 1
  48. for e in matrix:
  49. assert e.value() == 0.3 or e.value() == 0.7 or e.value() == 1 or e.value() == 0
  50. # Second model checking
  51. result = stormpy.model_checking(model, formulas[0])
  52. resValue = result.at(model.initial_states[0])
  53. assert math.isclose(resValue, 0.06923076923076932)
  54. # Change probabilities again
  55. for state in stormpy.state.State(0, model):
  56. for action in state.actions():
  57. for transition in action.transitions():
  58. if transition.value() == 0.3:
  59. transition.set_value(0.8)
  60. elif transition.value() == 0.7:
  61. transition.set_value(0.2)
  62. # Third model checking
  63. result = stormpy.model_checking(model, formulas[0])
  64. resValue = result.at(model.initial_states[0])
  65. assert math.isclose(resValue, 0.3555555555555556)
  66. def test_change_parametric_sparse_matrix_modelchecking(self):
  67. import stormpy.logic
  68. import pycarl
  69. program = stormpy.parse_prism_program(get_example_path("pdtmc", "brp16_2.pm"))
  70. formulas = stormpy.parse_properties_for_prism_program("P=? [ F s=5 ]", program)
  71. model = stormpy.build_parametric_model(program, formulas)
  72. initial_state = model.initial_states[0]
  73. assert initial_state == 0
  74. matrix = model.transition_matrix
  75. # Check matrix
  76. one_pol = pycarl.Rational(1)
  77. one_pol = pycarl.FactorizedPolynomial(one_pol)
  78. one = pycarl.FactorizedRationalFunction(one_pol, one_pol)
  79. for e in matrix:
  80. assert e.value() == one or len(e.value().gather_variables()) > 0
  81. # First model checking
  82. result = stormpy.model_checking(model, formulas[0])
  83. ratFunc = result.result.at(initial_state)
  84. assert len(ratFunc.gather_variables()) > 0
  85. # Change probabilities
  86. two_pol = pycarl.Rational(2)
  87. two_pol = pycarl.FactorizedPolynomial(two_pol)
  88. new_val = pycarl.FactorizedRationalFunction(one_pol, two_pol)
  89. for e in matrix:
  90. if len(e.value().gather_variables()) > 0:
  91. e.set_value(new_val)
  92. for e in matrix:
  93. assert e.value() == new_val or e.value() == one
  94. # Second model checking
  95. result = stormpy.model_checking(model, formulas[0])
  96. ratFunc = result.result.at(initial_state)
  97. assert len(ratFunc.gather_variables()) == 0