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.

774 lines
34 KiB

2 months ago
  1. import stormpy
  2. import stormpy.logic
  3. from helpers.helper import get_example_path
  4. from configurations import numpy_avail
  5. class TestSparseModelComponents:
  6. def test_init_default(self):
  7. components = stormpy.SparseModelComponents()
  8. assert components.state_labeling.get_labels() == set()
  9. assert components.reward_models == {}
  10. assert components.transition_matrix.nr_rows == 0
  11. assert components.transition_matrix.nr_columns == 0
  12. assert components.markovian_states is None
  13. assert components.player1_matrix is None
  14. assert not components.rate_transitions
  15. def test_build_dtmc(self):
  16. nr_states = 13
  17. nr_choices = 13
  18. # transition_matrix
  19. builder = stormpy.SparseMatrixBuilder(rows=0, columns=0, entries=0, force_dimensions=False,
  20. has_custom_row_grouping=False, row_groups=0)
  21. # Add transitions
  22. builder.add_next_value(0, 1, 0.5)
  23. builder.add_next_value(0, 2, 0.5)
  24. builder.add_next_value(1, 3, 0.5)
  25. builder.add_next_value(1, 4, 0.5)
  26. builder.add_next_value(2, 5, 0.5)
  27. builder.add_next_value(2, 6, 0.5)
  28. builder.add_next_value(3, 7, 0.5)
  29. builder.add_next_value(3, 1, 0.5)
  30. builder.add_next_value(4, 8, 0.5)
  31. builder.add_next_value(4, 9, 0.5)
  32. builder.add_next_value(5, 10, 0.5)
  33. builder.add_next_value(5, 11, 0.5)
  34. builder.add_next_value(6, 2, 0.5)
  35. builder.add_next_value(6, 12, 0.5)
  36. for s in range(7, 13):
  37. builder.add_next_value(s, s, 1)
  38. # Build transition matrix, update number of rows and columns
  39. transition_matrix = builder.build(nr_states, nr_states)
  40. # state labeling
  41. state_labeling = stormpy.storage.StateLabeling(nr_states)
  42. state_labels = {'init', 'one', 'two', 'three', 'four', 'five', 'six', 'done', 'deadlock'}
  43. for label in state_labels:
  44. state_labeling.add_label(label)
  45. # Add label to one state
  46. state_labeling.add_label_to_state('init', 0)
  47. state_labeling.add_label_to_state('one', 7)
  48. state_labeling.add_label_to_state('two', 8)
  49. state_labeling.add_label_to_state('three', 9)
  50. state_labeling.add_label_to_state('four', 10)
  51. state_labeling.add_label_to_state('five', 11)
  52. state_labeling.add_label_to_state('six', 12)
  53. state_labeling.set_states('done', stormpy.BitVector(nr_states, [7, 8, 9, 10, 11, 12]))
  54. # reward_models
  55. reward_models = {}
  56. # Create a vector representing the state-action rewards
  57. action_reward = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  58. reward_models['coin_flips'] = stormpy.SparseRewardModel(optional_state_action_reward_vector=action_reward)
  59. # state valuations
  60. manager = stormpy.ExpressionManager()
  61. var_s = manager.create_integer_variable(name='s')
  62. var_d = manager.create_integer_variable(name='d')
  63. v_builder = stormpy.StateValuationsBuilder()
  64. v_builder.add_variable(var_s)
  65. v_builder.add_variable(var_d)
  66. for s in range(7):
  67. v_builder.add_state(state=s, integer_values=[s, 0])
  68. for s in range(7, 13):
  69. v_builder.add_state(state=s, integer_values=[7, s - 6])
  70. state_valuations = v_builder.build(13)
  71. # choice origins
  72. prism_program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  73. index_to_identifier_mapping = [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8]
  74. id_to_command_set_mapping = [stormpy.FlatSet() for _ in range(9)]
  75. for i in range(1, 8): # 0: no origin
  76. id_to_command_set_mapping[i].insert(i - 1)
  77. id_to_command_set_mapping[8].insert(7)
  78. choice_origins = stormpy.PrismChoiceOrigins(prism_program, index_to_identifier_mapping,
  79. id_to_command_set_mapping)
  80. # Construct components
  81. components = stormpy.SparseModelComponents(transition_matrix=transition_matrix, state_labeling=state_labeling,
  82. reward_models=reward_models)
  83. components.choice_origins = choice_origins
  84. components.state_valuations = state_valuations
  85. # Build DTMC
  86. dtmc = stormpy.storage.SparseDtmc(components)
  87. assert type(dtmc) is stormpy.SparseDtmc
  88. assert not dtmc.supports_parameters
  89. # Test transition matrix
  90. assert dtmc.nr_choices == nr_choices
  91. assert dtmc.nr_states == nr_states
  92. assert dtmc.nr_transitions == 20
  93. assert dtmc.transition_matrix.nr_entries == dtmc.nr_transitions
  94. for e in dtmc.transition_matrix:
  95. assert e.value() == 0.5 or e.value() == 0 or (e.value() == 1 and e.column > 6)
  96. for state in dtmc.states:
  97. assert len(state.actions) <= 1
  98. # Test state labeling
  99. assert dtmc.labeling.get_labels() == {'init', 'deadlock', 'done', 'one', 'two', 'three', 'four', 'five', 'six'}
  100. # Test reward_models
  101. assert len(dtmc.reward_models) == 1
  102. assert not dtmc.reward_models["coin_flips"].has_state_rewards
  103. assert dtmc.reward_models["coin_flips"].has_state_action_rewards
  104. for reward in dtmc.reward_models["coin_flips"].state_action_rewards:
  105. assert reward == 1.0 or reward == 0.0
  106. assert not dtmc.reward_models["coin_flips"].has_transition_rewards
  107. # Test choice labeling
  108. assert not dtmc.has_choice_labeling()
  109. # Test state_valuations
  110. assert dtmc.has_state_valuations()
  111. assert dtmc.state_valuations
  112. value_s = [None] * nr_states
  113. value_d = [None] * nr_states
  114. for s in range(0, dtmc.nr_states):
  115. value_s[s] = dtmc.state_valuations.get_integer_value(s, var_s)
  116. value_d[s] = dtmc.state_valuations.get_integer_value(s, var_d)
  117. assert value_s == [0, 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7]
  118. assert value_d == [0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6]
  119. # Test choice origins
  120. assert dtmc.has_choice_origins()
  121. assert dtmc.choice_origins is components.choice_origins
  122. assert dtmc.choice_origins.get_number_of_identifiers() == 9
  123. def test_build_mdp(self):
  124. nr_states = 13
  125. nr_choices = 14
  126. # Build transition matrix
  127. builder = stormpy.SparseMatrixBuilder(rows=0, columns=0, entries=0, force_dimensions=False,
  128. has_custom_row_grouping=True, row_groups=0)
  129. # Row group, state 0
  130. builder.new_row_group(0)
  131. builder.add_next_value(0, 1, 0.5)
  132. builder.add_next_value(0, 2, 0.5)
  133. builder.add_next_value(1, 1, 0.2)
  134. builder.add_next_value(1, 2, 0.8)
  135. # Row group, state 1
  136. builder.new_row_group(2)
  137. builder.add_next_value(2, 3, 0.5)
  138. builder.add_next_value(2, 4, 0.5)
  139. # Row group, state 2
  140. builder.new_row_group(3)
  141. builder.add_next_value(3, 5, 0.5)
  142. builder.add_next_value(3, 6, 0.5)
  143. # Row group, state 3
  144. builder.new_row_group(4)
  145. builder.add_next_value(4, 7, 0.5)
  146. builder.add_next_value(4, 1, 0.5)
  147. # Row group, state 4
  148. builder.new_row_group(5)
  149. builder.add_next_value(5, 8, 0.5)
  150. builder.add_next_value(5, 9, 0.5)
  151. # Row group, state 5
  152. builder.new_row_group(6)
  153. builder.add_next_value(6, 10, 0.5)
  154. builder.add_next_value(6, 11, 0.5)
  155. # Row group, state 6
  156. builder.new_row_group(7)
  157. builder.add_next_value(7, 2, 0.5)
  158. builder.add_next_value(7, 12, 0.5)
  159. # final states
  160. for s in range(8, 14):
  161. builder.new_row_group(s)
  162. builder.add_next_value(s, s - 1, 1)
  163. transition_matrix = builder.build(nr_choices, nr_states)
  164. # state labeling
  165. state_labeling = stormpy.storage.StateLabeling(nr_states)
  166. labels = {'init', 'one', 'two', 'three', 'four', 'five', 'six', 'done', 'deadlock'}
  167. for label in labels:
  168. state_labeling.add_label(label)
  169. state_labeling.add_label_to_state('init', 0)
  170. state_labeling.add_label_to_state('one', 7)
  171. state_labeling.add_label_to_state('two', 8)
  172. state_labeling.add_label_to_state('three', 9)
  173. state_labeling.add_label_to_state('four', 10)
  174. state_labeling.add_label_to_state('five', 11)
  175. state_labeling.add_label_to_state('six', 12)
  176. state_labeling.set_states('done', stormpy.BitVector(nr_states, [7, 8, 9, 10, 11, 12]))
  177. # reward models
  178. reward_models = {}
  179. # Vector representing the state-action rewards
  180. action_reward = [0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  181. reward_models['coin_flips'] = stormpy.SparseRewardModel(optional_state_action_reward_vector=action_reward)
  182. # choice labeling
  183. choice_labeling = stormpy.storage.ChoiceLabeling(nr_choices)
  184. choice_labels = {'a', 'b'}
  185. for label in choice_labels:
  186. choice_labeling.add_label(label)
  187. choice_labeling.add_label_to_choice('a', 0)
  188. choice_labeling.add_label_to_choice('b', 1)
  189. # state valuations
  190. manager = stormpy.ExpressionManager()
  191. var_s = manager.create_integer_variable(name='s')
  192. var_d = manager.create_integer_variable(name='d')
  193. v_builder = stormpy.StateValuationsBuilder()
  194. v_builder.add_variable(var_s)
  195. v_builder.add_variable(var_d)
  196. for s in range(7):
  197. # values: vector [value for s, value for d]
  198. v_builder.add_state(state=s, boolean_values=[], integer_values=[s, 0], rational_values=[])
  199. for s in range(7, 13):
  200. v_builder.add_state(state=s, boolean_values=[], integer_values=[7, s - 6], rational_values=[])
  201. state_valuations = v_builder.build(13)
  202. # choice origins
  203. prism_program = stormpy.parse_prism_program(get_example_path("mdp", "die_c1.nm"))
  204. index_to_identifier_mapping = [1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 9, 9, 9]
  205. id_to_command_set_mapping = [stormpy.FlatSet() for _ in range(10)]
  206. for i in range(1, 9):
  207. # 0: no origin
  208. id_to_command_set_mapping[i].insert(i - 1)
  209. id_to_command_set_mapping[9].insert(8)
  210. choice_origins = stormpy.PrismChoiceOrigins(prism_program, index_to_identifier_mapping,
  211. id_to_command_set_mapping)
  212. # Construct Components
  213. components = stormpy.SparseModelComponents(transition_matrix=transition_matrix, state_labeling=state_labeling,
  214. reward_models=reward_models, rate_transitions=False)
  215. components.state_valuations = state_valuations
  216. components.choice_labeling = choice_labeling
  217. components.choice_origins = choice_origins
  218. # Build MDP
  219. mdp = stormpy.storage.SparseMdp(components)
  220. assert type(mdp) is stormpy.SparseMdp
  221. assert not mdp.supports_parameters
  222. # Test transition matrix
  223. assert mdp.nr_choices == nr_choices
  224. assert mdp.nr_states == nr_states
  225. assert mdp.nr_transitions == 22
  226. assert mdp.transition_matrix.nr_entries == mdp.nr_transitions
  227. for e in mdp.transition_matrix:
  228. assert e.value() == 0.5 or e.value() == 0 or e.value() == 0.2 or e.value() == 0.8 or (
  229. e.value() == 1 and e.column > 6)
  230. for state in mdp.states:
  231. assert len(state.actions) <= 2
  232. # Test state labeling
  233. assert mdp.labeling.get_labels() == {'init', 'deadlock', 'done', 'one', 'two', 'three', 'four', 'five', 'six'}
  234. # Test reward models
  235. assert len(mdp.reward_models) == 1
  236. assert not mdp.reward_models["coin_flips"].has_state_rewards
  237. assert mdp.reward_models["coin_flips"].has_state_action_rewards
  238. for reward in mdp.reward_models["coin_flips"].state_action_rewards:
  239. assert reward == 1.0 or reward == 0.0
  240. assert not mdp.reward_models["coin_flips"].has_transition_rewards
  241. # Test choice labeling
  242. assert mdp.has_choice_labeling()
  243. assert mdp.choice_labeling.get_labels() == {'a', 'b'}
  244. # Test state valuations
  245. assert mdp.has_state_valuations()
  246. assert mdp.state_valuations
  247. value_s = [None] * nr_states
  248. value_d = [None] * nr_states
  249. for s in range(0, mdp.nr_states):
  250. value_s[s] = mdp.state_valuations.get_integer_value(s, var_s)
  251. value_d[s] = mdp.state_valuations.get_integer_value(s, var_d)
  252. assert value_s == [0, 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7]
  253. assert value_d == [0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6]
  254. # Test choice origins
  255. assert mdp.has_choice_origins()
  256. assert mdp.choice_origins is components.choice_origins
  257. assert mdp.choice_origins.get_number_of_identifiers() == 10
  258. @numpy_avail
  259. def test_build_ctmc(self):
  260. import numpy as np
  261. nr_states = 12
  262. nr_choices = 12
  263. # Build transition_matrix
  264. transitions = np.array([
  265. [0, 0.5, 0.5, 200, 0, 0, 0, 0, 0, 0, 0, 0],
  266. [0, 0, 0, 0, 0.5, 200, 0, 0, 0, 0, 0, 0],
  267. [0, 0, 0, 0, 0.5, 0, 200, 0, 0, 0, 0, 0],
  268. [200, 0, 0, 0, 0, 0, 0.5, 0.5, 0, 0, 0, 0],
  269. [0, 0, 0, 0, 0, 0, 0, 0, 200, 0, 0, 0],
  270. [0, 0, 0, 1, 0, 0, 0, 0, 0.5, 0, 0, 0],
  271. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 200, 0],
  272. [0, 200, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0],
  273. [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
  274. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 200],
  275. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5],
  276. [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype='float64')
  277. transition_matrix = stormpy.build_sparse_matrix(transitions)
  278. # state labeling
  279. state_labeling = stormpy.storage.StateLabeling(nr_states)
  280. # Add labels
  281. state_labels = {'init', 'deadlock', 'target'}
  282. for label in state_labels:
  283. state_labeling.add_label(label)
  284. # Add labels to states
  285. state_labeling.add_label_to_state('init', 0)
  286. state_labeling.set_states('target', stormpy.BitVector(nr_states, [5, 8]))
  287. # reward models
  288. reward_models = {}
  289. # vector representing state-action rewards
  290. action_reward = [0.0, 0.0, 0.0, 0.0, 0.0, 2 / 3, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]
  291. reward_models['served'] = stormpy.SparseRewardModel(optional_state_action_reward_vector=action_reward)
  292. # vector representing state rewards
  293. state_reward = [0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0]
  294. reward_models['waiting'] = stormpy.SparseRewardModel(optional_state_reward_vector=state_reward)
  295. # choice labeling
  296. choice_labeling = stormpy.storage.ChoiceLabeling(nr_choices)
  297. choice_labels = {'loop1a', 'loop1b', 'serve1', 'loop2a', 'loop2b', 'serve2'}
  298. # Add labels
  299. for label in choice_labels:
  300. choice_labeling.add_label(label)
  301. choice_labeling.set_choices('loop1a', stormpy.BitVector(nr_choices, [0, 2]))
  302. choice_labeling.set_choices('loop1b', stormpy.BitVector(nr_choices, [1, 4]))
  303. choice_labeling.set_choices('serve1', stormpy.BitVector(nr_choices, [5, 8]))
  304. choice_labeling.set_choices('loop2a', stormpy.BitVector(nr_choices, [3, 7]))
  305. choice_labeling.set_choices('loop2b', stormpy.BitVector(nr_choices, [6, 9]))
  306. choice_labeling.set_choices('serve2', stormpy.BitVector(nr_choices, [10, 11]))
  307. # state exit rates
  308. exit_rates = [201.0, 200.5, 200.5, 201.0, 200.0, 1.5, 200.5, 200.5, 1.0, 200.0, 1.5, 1.0]
  309. # state valuations
  310. manager = stormpy.ExpressionManager()
  311. var_s = manager.create_integer_variable(name='s')
  312. var_a = manager.create_integer_variable(name='a')
  313. var_s1 = manager.create_integer_variable(name='s1')
  314. var_s2 = manager.create_integer_variable(name='s2')
  315. v_builder = stormpy.StateValuationsBuilder()
  316. v_builder.add_variable(var_s)
  317. v_builder.add_variable(var_a)
  318. v_builder.add_variable(var_s1)
  319. v_builder.add_variable(var_s2)
  320. v_builder.add_state(state=0, boolean_values=[], integer_values=[1, 0, 0, 0], rational_values=[])
  321. v_builder.add_state(state=1, boolean_values=[], integer_values=[1, 0, 1, 0], rational_values=[])
  322. v_builder.add_state(state=2, boolean_values=[], integer_values=[1, 0, 0, 1], rational_values=[])
  323. v_builder.add_state(state=3, boolean_values=[], integer_values=[2, 0, 0, 0], rational_values=[])
  324. v_builder.add_state(state=4, boolean_values=[], integer_values=[1, 0, 1, 1], rational_values=[])
  325. v_builder.add_state(state=5, boolean_values=[], integer_values=[1, 1, 1, 0], rational_values=[])
  326. v_builder.add_state(state=6, boolean_values=[], integer_values=[2, 0, 0, 1], rational_values=[])
  327. v_builder.add_state(state=7, boolean_values=[], integer_values=[2, 0, 1, 0], rational_values=[])
  328. v_builder.add_state(state=8, boolean_values=[], integer_values=[1, 1, 1, 1], rational_values=[])
  329. v_builder.add_state(state=9, boolean_values=[], integer_values=[2, 0, 1, 1], rational_values=[])
  330. v_builder.add_state(state=10, boolean_values=[], integer_values=[2, 1, 0, 1], rational_values=[])
  331. v_builder.add_state(state=11, boolean_values=[], integer_values=[2, 1, 1, 1], rational_values=[])
  332. state_valuations = v_builder.build(nr_states)
  333. # set rate_transitions to True: the transition values are interpreted as rates
  334. components = stormpy.SparseModelComponents(transition_matrix=transition_matrix, state_labeling=state_labeling,
  335. reward_models=reward_models, rate_transitions=True)
  336. components.choice_labeling = choice_labeling
  337. components.exit_rates = exit_rates
  338. components.state_valuations = state_valuations
  339. # Build CTMC
  340. ctmc = stormpy.storage.SparseCtmc(components)
  341. assert type(ctmc) is stormpy.SparseCtmc
  342. assert not ctmc.supports_parameters
  343. # Test transition matrix
  344. assert ctmc.nr_choices == nr_choices
  345. assert ctmc.nr_states == nr_states
  346. assert ctmc.nr_transitions == 22
  347. assert ctmc.transition_matrix.nr_columns == nr_states
  348. assert ctmc.transition_matrix.nr_rows == nr_choices
  349. for e in ctmc.transition_matrix:
  350. assert e.value() == 0.5 or e.value() == 0 or e.value() == 200 or e.value() == 1.0
  351. for state in ctmc.states:
  352. assert len(state.actions) <= 1
  353. # Test state labeling
  354. assert ctmc.labeling.get_labels() == {'target', 'init', 'deadlock'}
  355. # Test reward models
  356. assert len(ctmc.reward_models) == 2
  357. assert not ctmc.reward_models["served"].has_state_rewards
  358. assert ctmc.reward_models["served"].has_state_action_rewards
  359. assert ctmc.reward_models["served"].state_action_rewards == [0.0, 0.0, 0.0, 0.0, 0.0, 0.6666666666666666, 0.0,
  360. 0.0, 1.0, 0.0, 0.0, 0.0]
  361. assert not ctmc.reward_models["served"].has_transition_rewards
  362. assert ctmc.reward_models["waiting"].has_state_rewards
  363. assert not ctmc.reward_models["waiting"].has_state_action_rewards
  364. assert ctmc.reward_models["waiting"].state_rewards == [0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0]
  365. assert not ctmc.reward_models["waiting"].has_transition_rewards
  366. # Test choice labeling
  367. assert ctmc.has_choice_labeling()
  368. assert ctmc.choice_labeling.get_labels() == {'loop1a', 'loop1b', 'serve1', 'loop2a', 'loop2b', 'serve2'}
  369. # Test state valuations
  370. assert ctmc.has_state_valuations()
  371. assert ctmc.state_valuations
  372. value_s = [None] * nr_states
  373. value_a = [None] * nr_states
  374. value_s1 = [None] * nr_states
  375. value_s2 = [None] * nr_states
  376. for s in range(0, ctmc.nr_states):
  377. value_s[s] = ctmc.state_valuations.get_integer_value(s, var_s)
  378. value_a[s] = ctmc.state_valuations.get_integer_value(s, var_a)
  379. value_s1[s] = ctmc.state_valuations.get_integer_value(s, var_s1)
  380. value_s2[s] = ctmc.state_valuations.get_integer_value(s, var_s2)
  381. assert value_s == [1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2]
  382. assert value_a == [0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1]
  383. assert value_s1 == [0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1]
  384. assert value_s2 == [0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1]
  385. # Test choice origins
  386. assert not ctmc.has_choice_origins()
  387. # Test exit_rates
  388. assert ctmc.exit_rates == [201.0, 200.5, 200.5, 201.0, 200.0, 1.5, 200.5, 200.5, 1.0, 200.0, 1.5, 1.0]
  389. def test_build_ma(self):
  390. nr_states = 5
  391. nr_choices = 10
  392. # Build transition matrix
  393. builder = stormpy.SparseMatrixBuilder(rows=0, columns=0, entries=0, force_dimensions=False,
  394. has_custom_row_grouping=True, row_groups=0)
  395. # Row group, state 0
  396. builder.new_row_group(0)
  397. # Add Transition for (state) 0 to target states
  398. builder.add_next_value(0, 2, 1)
  399. builder.add_next_value(1, 2, 1)
  400. builder.add_next_value(2, 0, 0.8)
  401. builder.add_next_value(2, 1, 0.2)
  402. # Row group, state 1
  403. builder.new_row_group(3)
  404. # New Transition (state) 1 to target state
  405. builder.add_next_value(3, 3, 1)
  406. # Row group, state 2
  407. builder.new_row_group(4)
  408. # New Transition (state) 1 to target state
  409. builder.add_next_value(4, 0, 0.9)
  410. builder.add_next_value(4, 4, 0.1)
  411. # Row group, state 3
  412. builder.new_row_group(5)
  413. # New Transition (state) 1 to target state
  414. builder.add_next_value(5, 4, 1)
  415. builder.add_next_value(6, 3, 1)
  416. # Row group, state 4
  417. builder.new_row_group(7)
  418. # New Transition (state) 1 to target state
  419. builder.add_next_value(7, 3, 0.5)
  420. builder.add_next_value(7, 4, 0.5)
  421. builder.add_next_value(8, 3, 1)
  422. builder.add_next_value(9, 4, 1)
  423. transition_matrix = builder.build(nr_choices, nr_states)
  424. # state labeling
  425. state_labeling = stormpy.storage.StateLabeling(nr_states)
  426. # Add labels
  427. state_labels = {'init', 'deadlock'}
  428. for label in state_labels:
  429. state_labeling.add_label(label)
  430. # Add label to states
  431. state_labeling.add_label_to_state('init', 0)
  432. # state valuations
  433. manager = stormpy.ExpressionManager()
  434. var_s = manager.create_integer_variable(name='s')
  435. v_builder = stormpy.StateValuationsBuilder()
  436. v_builder.add_variable(var_s)
  437. v_builder.add_state(state=0, boolean_values=[], integer_values=[0], rational_values=[])
  438. v_builder.add_state(state=1, boolean_values=[], integer_values=[2], rational_values=[])
  439. v_builder.add_state(state=2, boolean_values=[], integer_values=[1], rational_values=[])
  440. v_builder.add_state(state=3, boolean_values=[], integer_values=[4], rational_values=[])
  441. v_builder.add_state(state=4, boolean_values=[], integer_values=[3], rational_values=[])
  442. state_valuations = v_builder.build(nr_states)
  443. # choice origins:
  444. prism_program = stormpy.parse_prism_program(get_example_path("ma", "hybrid_states.ma"))
  445. index_to_identifier_mapping = [1, 2, 3, 4, 5, 6, 7, 8, 9,
  446. 10]
  447. id_to_command_set_mapping = [stormpy.FlatSet() for _ in range(11)]
  448. id_to_command_set_mapping[1].insert(2)
  449. id_to_command_set_mapping[2].insert(1)
  450. id_to_command_set_mapping[3].insert(0)
  451. id_to_command_set_mapping[4].insert(4)
  452. id_to_command_set_mapping[5].insert(3)
  453. id_to_command_set_mapping[6].insert(9)
  454. id_to_command_set_mapping[7].insert(8)
  455. id_to_command_set_mapping[8].insert(7)
  456. id_to_command_set_mapping[9].insert(6)
  457. id_to_command_set_mapping[10].insert(5)
  458. choice_origins = stormpy.PrismChoiceOrigins(prism_program, index_to_identifier_mapping,
  459. id_to_command_set_mapping)
  460. exit_rates = [3.0, 12.0, 10.0, 3.0, 4.0]
  461. markovian_states = stormpy.BitVector(5, [0, 1, 2, 3, 4])
  462. # Build components, set rate_transitions to False
  463. components = stormpy.SparseModelComponents(transition_matrix=transition_matrix, state_labeling=state_labeling,
  464. rate_transitions=False, markovian_states=markovian_states)
  465. components.state_valuations = state_valuations
  466. components.choice_origins = choice_origins
  467. components.exit_rates = exit_rates
  468. # Build MA
  469. ma = stormpy.storage.SparseMA(components)
  470. assert type(ma) is stormpy.SparseMA
  471. assert not ma.supports_parameters
  472. # Test transition matrix
  473. assert ma.nr_choices == nr_choices
  474. assert ma.nr_states == nr_states
  475. assert ma.nr_transitions == 13
  476. assert ma.transition_matrix.nr_columns == nr_states
  477. assert ma.transition_matrix.nr_rows == nr_choices
  478. # Check row groups
  479. assert ma.transition_matrix.get_row_group_start(0) == 0
  480. assert ma.transition_matrix.get_row_group_end(0) == 3
  481. assert ma.transition_matrix.get_row_group_start(1) == 3
  482. assert ma.transition_matrix.get_row_group_end(1) == 4
  483. assert ma.transition_matrix.get_row_group_start(2) == 4
  484. assert ma.transition_matrix.get_row_group_end(2) == 5
  485. assert ma.transition_matrix.get_row_group_start(3) == 5
  486. assert ma.transition_matrix.get_row_group_end(3) == 7
  487. assert ma.transition_matrix.get_row_group_start(4) == 7
  488. assert ma.transition_matrix.get_row_group_end(4) == 10
  489. for e in ma.transition_matrix:
  490. assert e.value() == 1.0 or e.value() == 0 or e.value() == 0.8 or e.value() == 0.2 or e.value() == 0.1 or e.value() == 0.5 or e.value() == 0.9
  491. for state in ma.states:
  492. assert len(state.actions) <= 3
  493. # Test state labeling
  494. assert ma.labeling.get_labels() == {'deadlock', 'init'}
  495. # Test reward models
  496. assert len(ma.reward_models) == 0
  497. # Test choice labeling
  498. assert not ma.has_choice_labeling()
  499. # Test state valuations
  500. assert ma.has_state_valuations()
  501. value_s = [None] * nr_states
  502. for s in range(0, ma.nr_states):
  503. value_s[s] = ma.state_valuations.get_integer_value(s, var_s)
  504. assert value_s == [0, 2, 1, 4, 3]
  505. # Test choice origins
  506. assert ma.has_choice_origins()
  507. assert ma.choice_origins.get_number_of_identifiers() == 11
  508. # Test exit rates
  509. assert ma.exit_rates == [3.0, 12.0, 10.0, 3.0, 4.0]
  510. # Test Markovian states
  511. assert ma.markovian_states == stormpy.BitVector(5, [0, 1, 2, 3, 4])
  512. @numpy_avail
  513. def test_build_pomdp(self):
  514. import numpy as np
  515. nr_states = 10
  516. nr_choices = 34
  517. # Build transition matrix
  518. builder = stormpy.SparseMatrixBuilder(rows=0, columns=0, entries=0, force_dimensions=False,
  519. has_custom_row_grouping=True, row_groups=0)
  520. transitions = np.array([
  521. [0, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0],
  522. [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
  523. [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  524. [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
  525. [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  526. [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
  527. [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
  528. [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
  529. [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  530. [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
  531. [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
  532. [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
  533. [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
  534. [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  535. [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  536. [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
  537. [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
  538. [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
  539. [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
  540. [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
  541. [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
  542. [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
  543. [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
  544. [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
  545. [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
  546. [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
  547. [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
  548. [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
  549. [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  550. [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
  551. [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
  552. [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
  553. [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
  554. [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])
  555. transition_matrix = stormpy.build_sparse_matrix(transitions,
  556. row_group_indices=[0, 1, 5, 9, 13, 17, 21, 25, 29, 33])
  557. # state labeling
  558. state_labeling = stormpy.storage.StateLabeling(nr_states)
  559. labels = {'deadlock', 'goal', 'init'}
  560. for label in labels:
  561. state_labeling.add_label(label)
  562. state_labeling.add_label_to_state('init', 0)
  563. state_labeling.add_label_to_state('goal', 9)
  564. # reward models
  565. reward_models = {}
  566. # Vector representing state-action rewards
  567. action_reward = [0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
  568. 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0]
  569. reward_models[''] = stormpy.SparseRewardModel(optional_state_action_reward_vector=action_reward)
  570. # choice labeling
  571. choice_labeling = stormpy.storage.ChoiceLabeling(nr_choices)
  572. choice_labels = {'south', 'north', 'west', 'east', 'done'}
  573. for label in choice_labels:
  574. choice_labeling.add_label(label)
  575. choice_labeling.set_choices('south', stormpy.BitVector(nr_choices, [4, 8, 12, 16, 20, 24, 28, 32]))
  576. choice_labeling.set_choices('north', stormpy.BitVector(nr_choices, [3, 7, 11, 15, 19, 23, 27, 31]))
  577. choice_labeling.set_choices('west', stormpy.BitVector(nr_choices, [2, 6, 10, 14, 18, 22, 26, 30]))
  578. choice_labeling.set_choices('east', stormpy.BitVector(nr_choices, [1, 5, 9, 13, 17, 21, 25, 29]))
  579. choice_labeling.set_choices('done', stormpy.BitVector(nr_choices, [33]))
  580. # state valuations
  581. manager = stormpy.ExpressionManager()
  582. var_x = manager.create_integer_variable(name='x')
  583. var_y = manager.create_integer_variable(name='y')
  584. var_o = manager.create_integer_variable(name='o')
  585. v_builder = stormpy.StateValuationsBuilder()
  586. v_builder.add_variable(var_x)
  587. v_builder.add_variable(var_y)
  588. v_builder.add_variable(var_o)
  589. v_builder.add_state(state=0, boolean_values=[], integer_values=[0, 0, 0], rational_values=[])
  590. v_builder.add_state(state=1, boolean_values=[], integer_values=[0, 0, 1], rational_values=[])
  591. v_builder.add_state(state=2, boolean_values=[], integer_values=[0, 1, 1], rational_values=[])
  592. v_builder.add_state(state=3, boolean_values=[], integer_values=[0, 2, 1], rational_values=[])
  593. v_builder.add_state(state=4, boolean_values=[], integer_values=[1, 0, 1], rational_values=[])
  594. v_builder.add_state(state=5, boolean_values=[], integer_values=[1, 1, 1], rational_values=[])
  595. v_builder.add_state(state=6, boolean_values=[], integer_values=[1, 2, 1], rational_values=[])
  596. v_builder.add_state(state=7, boolean_values=[], integer_values=[2, 1, 1], rational_values=[])
  597. v_builder.add_state(state=8, boolean_values=[], integer_values=[2, 2, 1], rational_values=[])
  598. v_builder.add_state(state=9, boolean_values=[], integer_values=[2, 0, 2], rational_values=[])
  599. state_valuations = v_builder.build(nr_states)
  600. observations = [1, 0, 0, 0, 0, 0, 0, 0, 0, 2]
  601. # Build components, set rate_transitions to False
  602. components = stormpy.SparseModelComponents(transition_matrix=transition_matrix, state_labeling=state_labeling,
  603. reward_models=reward_models, rate_transitions=False)
  604. components.state_valuations = state_valuations
  605. components.choice_labeling = choice_labeling
  606. # components.choice_origins=choice_origins
  607. components.observability_classes = observations
  608. # Build POMDP
  609. pomdp = stormpy.storage.SparsePomdp(components)
  610. assert type(pomdp) is stormpy.SparsePomdp
  611. assert not pomdp.supports_parameters
  612. # Test transition matrix
  613. assert pomdp.nr_choices == nr_choices
  614. assert pomdp.nr_states == nr_states
  615. assert pomdp.nr_transitions == 41
  616. for e in pomdp.transition_matrix:
  617. assert e.value() == 1 or e.value() == 0 or e.value() == 0.125
  618. for state in pomdp.states:
  619. assert len(state.actions) <= 4
  620. # Test state labeling
  621. assert pomdp.labeling.get_labels() == {'init', 'goal', 'deadlock'}
  622. # Test reward models
  623. assert len(pomdp.reward_models) == 1
  624. assert not pomdp.reward_models[''].has_state_rewards
  625. assert pomdp.reward_models[''].has_state_action_rewards
  626. for reward in pomdp.reward_models[''].state_action_rewards:
  627. assert reward == 1.0 or reward == 0.0
  628. assert not pomdp.reward_models[''].has_transition_rewards
  629. # Test choice labeling
  630. assert pomdp.has_choice_labeling()
  631. assert pomdp.choice_labeling.get_labels() == {'east', 'west', 'north', 'south', 'done'}
  632. # Test state valuations
  633. assert pomdp.has_state_valuations()
  634. assert pomdp.state_valuations
  635. value_x = [None] * nr_states
  636. value_y = [None] * nr_states
  637. value_o = [None] * nr_states
  638. for s in range(0, pomdp.nr_states):
  639. value_x[s] = pomdp.state_valuations.get_integer_value(s, var_x)
  640. value_y[s] = pomdp.state_valuations.get_integer_value(s, var_y)
  641. value_o[s] = pomdp.state_valuations.get_integer_value(s, var_o)
  642. assert value_x == [0, 0, 0, 0, 1, 1, 1, 2, 2, 2]
  643. assert value_y == [0, 0, 1, 2, 0, 1, 2, 1, 2, 0]
  644. assert value_o == [0, 1, 1, 1, 1, 1, 1, 1, 1, 2]
  645. # Test choice origins
  646. assert not pomdp.has_choice_origins()
  647. assert pomdp.observations == [1, 0, 0, 0, 0, 0, 0, 0, 0, 2]