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.

273 lines
9.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. import stormpy
  2. import numpy as np
  3. class TestMatrixBuilder:
  4. def test_matrix_builder(self):
  5. builder = stormpy.SparseMatrixBuilder(force_dimensions=True)
  6. matrix = builder.build()
  7. assert matrix.nr_columns == 0
  8. assert matrix.nr_rows == 0
  9. assert matrix.nr_entries == 0
  10. builder_5x5 = stormpy.SparseMatrixBuilder(5, 5, force_dimensions=False)
  11. builder_5x5.add_next_value(0, 0, 0)
  12. builder_5x5.add_next_value(0, 1, 0.1)
  13. builder_5x5.add_next_value(2, 2, 22)
  14. builder_5x5.add_next_value(2, 3, 23)
  15. assert builder_5x5.get_last_column() == 3
  16. assert builder_5x5.get_last_row() == 2
  17. builder_5x5.add_next_value(3, 2, 32)
  18. builder_5x5.add_next_value(3, 4, 34)
  19. builder_5x5.add_next_value(4, 3, 43)
  20. matrix_5x5 = builder_5x5.build()
  21. assert matrix_5x5.nr_columns == 5
  22. assert matrix_5x5.nr_rows == 5
  23. assert matrix_5x5.nr_entries == 7
  24. for e in matrix_5x5:
  25. assert (e.value() == 0.1 and e.column == 1) or e.value() == 0 or (e.value() > 20 and e.column > 1)
  26. def test_parametric_matrix_builder(self):
  27. builder = stormpy.ParametricSparseMatrixBuilder(force_dimensions=True)
  28. matrix = builder.build()
  29. assert matrix.nr_columns == 0
  30. assert matrix.nr_rows == 0
  31. assert matrix.nr_entries == 0
  32. builder_5x5 = stormpy.ParametricSparseMatrixBuilder(5, 5, force_dimensions=False)
  33. one_pol = stormpy.RationalRF(1)
  34. one_pol = stormpy.FactorizedPolynomial(one_pol)
  35. first_val = stormpy.FactorizedRationalFunction(one_pol)
  36. two_pol = stormpy.RationalRF(2)
  37. two_pol = stormpy.FactorizedPolynomial(two_pol)
  38. sec_val = stormpy.FactorizedRationalFunction(two_pol)
  39. builder_5x5.add_next_value(0, 0, first_val)
  40. builder_5x5.add_next_value(0, 1, first_val)
  41. builder_5x5.add_next_value(2, 2, sec_val)
  42. builder_5x5.add_next_value(2, 3, sec_val)
  43. assert builder_5x5.get_last_column() == 3
  44. assert builder_5x5.get_last_row() == 2
  45. builder_5x5.add_next_value(3, 2, sec_val)
  46. builder_5x5.add_next_value(3, 4, sec_val)
  47. builder_5x5.add_next_value(4, 3, sec_val)
  48. matrix_5x5 = builder_5x5.build()
  49. assert matrix_5x5.nr_columns == 5
  50. assert matrix_5x5.nr_rows == 5
  51. assert matrix_5x5.nr_entries == 7
  52. for e in matrix_5x5:
  53. assert (e.value() == first_val and e.column < 2) or (e.value() == sec_val and e.column > 1)
  54. def test_matrix_replace_columns(self):
  55. builder = stormpy.SparseMatrixBuilder(3, 4, force_dimensions=False)
  56. builder.add_next_value(0, 0, 0)
  57. builder.add_next_value(0, 1, 1)
  58. builder.add_next_value(0, 2, 2)
  59. builder.add_next_value(0, 3, 3)
  60. builder.add_next_value(1, 1, 1)
  61. builder.add_next_value(1, 2, 2)
  62. builder.add_next_value(1, 3, 3)
  63. builder.add_next_value(2, 1, 1)
  64. builder.add_next_value(2, 2, 2)
  65. builder.add_next_value(2, 3, 3)
  66. # replace rows
  67. builder.replace_columns([3, 2, 1], 1)
  68. matrix = builder.build()
  69. assert matrix.nr_entries == 10
  70. # Check if columns where replaced
  71. for e in matrix:
  72. assert (e.value() == 0 and e.column == 0) or (e.value() == 3 and e.column == 1) or (
  73. e.value() == 2 and e.column == 2) or (e.value() == 1 and e.column == 3)
  74. def test_parametric_matrix_replace_columns(self):
  75. builder = stormpy.ParametricSparseMatrixBuilder(3, 4, force_dimensions=False)
  76. one_pol = stormpy.RationalRF(1)
  77. one_pol = stormpy.FactorizedPolynomial(one_pol)
  78. first_val = stormpy.FactorizedRationalFunction(one_pol, one_pol)
  79. two_pol = stormpy.RationalRF(2)
  80. two_pol = stormpy.FactorizedPolynomial(two_pol)
  81. sec_val = stormpy.FactorizedRationalFunction(two_pol, two_pol)
  82. third_val = stormpy.FactorizedRationalFunction(one_pol, two_pol)
  83. builder.add_next_value(0, 1, first_val)
  84. builder.add_next_value(0, 2, sec_val)
  85. builder.add_next_value(0, 3, third_val)
  86. builder.add_next_value(1, 1, first_val)
  87. builder.add_next_value(1, 2, sec_val)
  88. builder.add_next_value(1, 3, third_val)
  89. builder.add_next_value(2, 1, first_val)
  90. builder.add_next_value(2, 2, sec_val)
  91. builder.add_next_value(2, 3, third_val)
  92. # replace rows
  93. builder.replace_columns([3, 2], 2)
  94. matrix = builder.build()
  95. assert matrix.nr_entries == 9
  96. # Check if columns where replaced
  97. for e in matrix:
  98. assert (e.value() == first_val and e.column == 1) or (e.value() == third_val and e.column == 2) or (
  99. e.value() == sec_val and e.column == 3)
  100. def test_matrix_builder_row_grouping(self):
  101. num_rows = 5
  102. builder = stormpy.SparseMatrixBuilder(num_rows, 6, has_custom_row_grouping=True, row_groups=2)
  103. builder.new_row_group(1)
  104. assert builder.get_current_row_group_count() == 1
  105. builder.new_row_group(4)
  106. assert builder.get_current_row_group_count() == 2
  107. matrix = builder.build()
  108. assert matrix.get_row_group_start(0) == 1
  109. assert matrix.get_row_group_end(0) == 4
  110. assert matrix.get_row_group_start(1) == 4
  111. assert matrix.get_row_group_end(1) == 5
  112. def test_parametric_matrix_builder_row_grouping(self):
  113. num_rows = 5
  114. builder = stormpy.ParametricSparseMatrixBuilder(num_rows, 6, has_custom_row_grouping=True, row_groups=2)
  115. builder.new_row_group(1)
  116. assert builder.get_current_row_group_count() == 1
  117. builder.new_row_group(4)
  118. assert builder.get_current_row_group_count() == 2
  119. matrix = builder.build()
  120. assert matrix.get_row_group_start(0) == 1
  121. assert matrix.get_row_group_end(0) == 4
  122. assert matrix.get_row_group_start(1) == 4
  123. assert matrix.get_row_group_end(1) == 5
  124. def test_matrix_from_numpy(self):
  125. array = np.array([[0, 2],
  126. [3, 4],
  127. [0.1, 24],
  128. [-0.3, -4]], dtype='float64')
  129. matrix = stormpy.build_sparse_matrix(array)
  130. # Check matrix dimension
  131. assert matrix.nr_rows == array.shape[0]
  132. assert matrix.nr_columns == array.shape[1]
  133. assert matrix.nr_entries == 8
  134. # Check matrix values
  135. for r in range(array.shape[1]):
  136. row = matrix.get_row(r)
  137. for e in row:
  138. assert (e.value() == array[r, e.column])
  139. def test_parametric_matrix_from_numpy(self):
  140. one_pol = stormpy.RationalRF(1)
  141. one_pol = stormpy.FactorizedPolynomial(one_pol)
  142. first_val = stormpy.FactorizedRationalFunction(one_pol, one_pol)
  143. two_pol = stormpy.RationalRF(2)
  144. two_pol = stormpy.FactorizedPolynomial(two_pol)
  145. sec_val = stormpy.FactorizedRationalFunction(two_pol, two_pol)
  146. third_val = stormpy.FactorizedRationalFunction(one_pol, two_pol)
  147. array = np.array([[sec_val, first_val],
  148. [first_val, sec_val],
  149. [sec_val, sec_val],
  150. [third_val, third_val]])
  151. matrix = stormpy.build_parametric_sparse_matrix(array)
  152. # Check matrix dimension
  153. assert matrix.nr_rows == array.shape[0]
  154. assert matrix.nr_columns == array.shape[1]
  155. assert matrix.nr_entries == 8
  156. # Check matrix values
  157. for r in range(array.shape[1]):
  158. row = matrix.get_row(r)
  159. for e in row:
  160. assert (e.value() == array[r, e.column])
  161. def test_matrix_from_numpy_row_grouping(self):
  162. array = np.array([[0, 2],
  163. [3, 4],
  164. [0.1, 24],
  165. [-0.3, -4]], dtype='float64')
  166. matrix = stormpy.build_sparse_matrix(array, row_group_indices=[1, 3])
  167. # Check matrix dimension
  168. assert matrix.nr_rows == array.shape[0]
  169. assert matrix.nr_columns == array.shape[1]
  170. assert matrix.nr_entries == 8
  171. # Check matrix values
  172. for r in range(array.shape[1]):
  173. row = matrix.get_row(r)
  174. for e in row:
  175. assert (e.value() == array[r, e.column])
  176. # Check row groups
  177. assert matrix.get_row_group_start(0) == 1
  178. assert matrix.get_row_group_end(0) == 3
  179. assert matrix.get_row_group_start(1) == 3
  180. assert matrix.get_row_group_end(1) == 4
  181. def test_parametric_matrix_from_numpy_row_grouping(self):
  182. one_pol = stormpy.RationalRF(1)
  183. one_pol = stormpy.FactorizedPolynomial(one_pol)
  184. first_val = stormpy.FactorizedRationalFunction(one_pol, one_pol)
  185. two_pol = stormpy.RationalRF(2)
  186. two_pol = stormpy.FactorizedPolynomial(two_pol)
  187. sec_val = stormpy.FactorizedRationalFunction(two_pol, two_pol)
  188. third_val = stormpy.FactorizedRationalFunction(one_pol, two_pol)
  189. array = np.array([[sec_val, first_val],
  190. [first_val, sec_val],
  191. [sec_val, sec_val],
  192. [third_val, third_val]])
  193. matrix = stormpy.build_parametric_sparse_matrix(array, row_group_indices=[1, 3])
  194. # Check matrix dimension
  195. assert matrix.nr_rows == array.shape[0]
  196. assert matrix.nr_columns == array.shape[1]
  197. assert matrix.nr_entries == 8
  198. # Check matrix values
  199. for r in range(array.shape[1]):
  200. row = matrix.get_row(r)
  201. for e in row:
  202. assert (e.value() == array[r, e.column])
  203. # Check row groups
  204. assert matrix.get_row_group_start(0) == 1
  205. assert matrix.get_row_group_end(0) == 3
  206. assert matrix.get_row_group_start(1) == 3
  207. assert matrix.get_row_group_end(1) == 4