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.

249 lines
12 KiB

8 years ago
8 years ago
5 years ago
8 years ago
  1. import stormpy
  2. import stormpy.logic
  3. from helpers.helper import get_example_path
  4. import pytest
  5. class TestSparseModel:
  6. def test_build_dtmc_from_prism_program(self):
  7. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  8. model = stormpy.build_model(program)
  9. assert model.nr_states == 13
  10. assert model.nr_transitions == 20
  11. assert model.model_type == stormpy.ModelType.DTMC
  12. assert not model.supports_parameters
  13. assert type(model) is stormpy.SparseDtmc
  14. def test_build_dtmc_from_prism_program_formulas(self):
  15. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  16. prop = "P=? [F \"one\"]"
  17. properties = stormpy.parse_properties_for_prism_program(prop, program, None)
  18. model = stormpy.build_model(program, properties)
  19. assert model.nr_states == 13
  20. assert model.nr_transitions == 20
  21. assert model.model_type == stormpy.ModelType.DTMC
  22. assert len(model.reward_models) == 0
  23. assert not model.supports_parameters
  24. assert type(model) is stormpy.SparseDtmc
  25. def test_build_dtmc_from_prism_program_reward_formulas(self):
  26. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  27. prop = "R=? [F \"done\"]"
  28. properties = stormpy.parse_properties_for_prism_program(prop, program, None)
  29. model = stormpy.build_model(program, properties)
  30. assert model.nr_states == 13
  31. assert model.nr_transitions == 20
  32. assert model.model_type == stormpy.ModelType.DTMC
  33. assert len(model.reward_models) == 1
  34. assert not model.reward_models["coin_flips"].has_state_rewards
  35. assert model.reward_models["coin_flips"].has_state_action_rewards
  36. for reward in model.reward_models["coin_flips"].state_action_rewards:
  37. assert reward == 1.0 or reward == 0.0
  38. assert not model.reward_models["coin_flips"].has_transition_rewards
  39. assert not model.supports_parameters
  40. assert type(model) is stormpy.SparseDtmc
  41. def test_reduce_to_state_based_rewards(self):
  42. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  43. prop = "R=? [F \"done\"]"
  44. properties = stormpy.parse_properties_for_prism_program(prop, program, None)
  45. model = stormpy.build_model(program, properties)
  46. model.reduce_to_state_based_rewards()
  47. assert len(model.reward_models) == 1
  48. assert model.reward_models["coin_flips"].has_state_rewards
  49. assert not model.reward_models["coin_flips"].has_state_action_rewards
  50. for reward in model.reward_models["coin_flips"].state_rewards:
  51. assert reward == 1.0 or reward == 0.0
  52. assert not model.reward_models["coin_flips"].has_transition_rewards
  53. def test_build_dtmc_from_jani_model(self):
  54. jani_model, properties = stormpy.parse_jani_model(get_example_path("dtmc", "die.jani"))
  55. model = stormpy.build_model(jani_model)
  56. assert model.nr_states == 13
  57. assert model.nr_transitions == 20
  58. assert model.model_type == stormpy.ModelType.DTMC
  59. assert not model.supports_parameters
  60. assert type(model) is stormpy.SparseDtmc
  61. def test_build_dtmc_with_undefined_constants(self):
  62. jani_model, properties = stormpy.parse_jani_model(get_example_path("dtmc", "brp.jani"))
  63. assert jani_model.has_undefined_constants
  64. assert not jani_model.undefined_constants_are_graph_preserving
  65. with pytest.raises(stormpy.StormError):
  66. model = stormpy.build_model(jani_model)
  67. def test_build_instantiated_dtmc(self):
  68. jani_model, properties = stormpy.parse_jani_model(get_example_path("dtmc", "brp.jani"))
  69. assert jani_model.has_undefined_constants
  70. assert not jani_model.undefined_constants_are_graph_preserving
  71. description = stormpy.SymbolicModelDescription(jani_model)
  72. constant_definitions = description.parse_constant_definitions("N=16, MAX=2")
  73. instantiated_jani_model = description.instantiate_constants(constant_definitions).as_jani_model()
  74. model = stormpy.build_model(instantiated_jani_model)
  75. assert model.nr_states == 677
  76. assert model.nr_transitions == 867
  77. assert model.model_type == stormpy.ModelType.DTMC
  78. assert not model.supports_parameters
  79. assert type(model) is stormpy.SparseDtmc
  80. def test_build_mdp(self):
  81. program = stormpy.parse_prism_program(get_example_path("mdp", "two_dice.nm"))
  82. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"two\" ]", program)
  83. model = stormpy.build_model(program, formulas)
  84. assert model.nr_states == 169
  85. assert model.nr_transitions == 435
  86. assert model.model_type == stormpy.ModelType.MDP
  87. assert not model.supports_parameters
  88. assert type(model) is stormpy.SparseMdp
  89. def test_build_ctmc(self):
  90. program = stormpy.parse_prism_program(get_example_path("ctmc", "polling2.sm"), True)
  91. formulas = stormpy.parse_properties_for_prism_program("P=? [ F<=3 \"target\" ]", program)
  92. model = stormpy.build_model(program)
  93. assert model.nr_states == 12
  94. assert model.nr_transitions == 22
  95. assert model.model_type == stormpy.ModelType.CTMC
  96. assert not model.supports_parameters
  97. assert type(model) is stormpy.SparseCtmc
  98. model_for_formula = stormpy.build_model(program, formulas)
  99. assert model_for_formula.nr_states == 12
  100. assert model_for_formula.nr_transitions == 21
  101. assert model_for_formula.model_type == stormpy.ModelType.CTMC
  102. assert not model_for_formula.supports_parameters
  103. assert type(model_for_formula) is stormpy.SparseCtmc
  104. def test_build_pomdp(self):
  105. program = stormpy.parse_prism_program(get_example_path("pomdp", "maze_2.prism"))
  106. formulas = stormpy.parse_properties_for_prism_program("P=? [F \"goal\"]", program)
  107. model = stormpy.build_model(program, formulas)
  108. assert model.nr_states == 15
  109. assert model.nr_observations == 8
  110. def test_build_ma(self):
  111. program = stormpy.parse_prism_program(get_example_path("ma", "simple.ma"), False, True)
  112. formulas = stormpy.parse_properties_for_prism_program("Pmax=? [ F<=2 s=2 ]", program)
  113. model = stormpy.build_model(program, formulas)
  114. assert model.nr_states == 4
  115. assert model.nr_transitions == 7
  116. assert model.model_type == stormpy.ModelType.MA
  117. assert not model.supports_parameters
  118. assert type(model) is stormpy.SparseMA
  119. def test_convert_ma_to_ctmc(self):
  120. program = stormpy.parse_prism_program(get_example_path("ma", "ctmc.ma"), True)
  121. formulas = stormpy.parse_properties_for_prism_program("P=? [ F<=3 s=2 ]", program)
  122. model = stormpy.build_model(program, formulas)
  123. assert model.nr_states == 4
  124. assert model.nr_transitions == 6
  125. assert model.model_type == stormpy.ModelType.MA
  126. assert type(model) is stormpy.SparseMA
  127. assert model.convertible_to_ctmc
  128. ctmc = model.convert_to_ctmc()
  129. assert ctmc.nr_states == 4
  130. assert ctmc.nr_transitions == 6
  131. assert ctmc.model_type == stormpy.ModelType.CTMC
  132. assert type(ctmc) is stormpy.SparseCtmc
  133. def test_initial_states(self):
  134. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  135. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"one\" ]", program)
  136. model = stormpy.build_model(program, formulas)
  137. initial_states = model.initial_states
  138. assert len(initial_states) == 1
  139. assert 0 in initial_states
  140. def test_choice_origins(self):
  141. program, _ = stormpy.parse_jani_model(get_example_path("dtmc", "die.jani"))
  142. a = stormpy.FlatSet()
  143. options = stormpy.BuilderOptions([])
  144. options.set_build_with_choice_origins()
  145. model = stormpy.build_sparse_model_with_options(program, options)
  146. a = model.choice_origins.get_edge_index_set(3)
  147. class TestSymbolicSylvanModel:
  148. def test_build_dtmc_from_prism_program(self):
  149. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  150. model = stormpy.build_symbolic_model(program)
  151. assert model.nr_states == 13
  152. assert model.nr_transitions == 20
  153. assert model.model_type == stormpy.ModelType.DTMC
  154. assert not model.supports_parameters
  155. assert type(model) is stormpy.SymbolicSylvanDtmc
  156. def test_build_dtmc_from_prism_program_formulas(self):
  157. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  158. prop = "P=? [F \"one\"]"
  159. properties = stormpy.parse_properties_for_prism_program(prop, program, None)
  160. model = stormpy.build_symbolic_model(program, properties)
  161. assert model.nr_states == 13
  162. assert model.nr_transitions == 20
  163. assert model.model_type == stormpy.ModelType.DTMC
  164. assert len(model.reward_models) == 0
  165. assert not model.supports_parameters
  166. assert type(model) is stormpy.SymbolicSylvanDtmc
  167. def test_build_dtmc_from_prism_program_reward_formulas(self):
  168. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  169. prop = "R=? [F \"done\"]"
  170. properties = stormpy.parse_properties_for_prism_program(prop, program, None)
  171. model = stormpy.build_symbolic_model(program, properties)
  172. assert model.nr_states == 13
  173. assert model.nr_transitions == 20
  174. assert model.model_type == stormpy.ModelType.DTMC
  175. assert len(model.reward_models) == 1
  176. assert not model.reward_models["coin_flips"].has_state_rewards
  177. assert model.reward_models["coin_flips"].has_state_action_rewards
  178. assert not model.reward_models["coin_flips"].has_transition_rewards
  179. assert not model.supports_parameters
  180. assert type(model) is stormpy.SymbolicSylvanDtmc
  181. def test_reduce_to_state_based_rewards(self):
  182. program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
  183. prop = "R=? [F \"done\"]"
  184. properties = stormpy.parse_properties_for_prism_program(prop, program, None)
  185. model = stormpy.build_symbolic_model(program, properties)
  186. model.reduce_to_state_based_rewards()
  187. assert len(model.reward_models) == 1
  188. assert model.reward_models["coin_flips"].has_state_rewards
  189. assert not model.reward_models["coin_flips"].has_state_action_rewards
  190. assert not model.reward_models["coin_flips"].has_transition_rewards
  191. def test_build_dtmc_from_jani_model(self):
  192. jani_model, properties = stormpy.parse_jani_model(get_example_path("dtmc", "brp.jani"))
  193. description = stormpy.SymbolicModelDescription(jani_model)
  194. constant_definitions = description.parse_constant_definitions("N=16, MAX=2")
  195. instantiated_jani_model = description.instantiate_constants(constant_definitions).as_jani_model()
  196. model = stormpy.build_symbolic_model(instantiated_jani_model)
  197. assert model.nr_states == 677
  198. assert model.nr_transitions == 867
  199. assert model.model_type == stormpy.ModelType.DTMC
  200. assert not model.supports_parameters
  201. assert type(model) is stormpy.SymbolicSylvanDtmc
  202. def test_build_mdp(self):
  203. program = stormpy.parse_prism_program(get_example_path("mdp", "two_dice.nm"))
  204. formulas = stormpy.parse_properties_for_prism_program("P=? [ F \"two\" ]", program)
  205. model = stormpy.build_symbolic_model(program, formulas)
  206. assert model.nr_states == 169
  207. assert model.nr_transitions == 435
  208. assert model.model_type == stormpy.ModelType.MDP
  209. assert not model.supports_parameters
  210. assert type(model) is stormpy.SymbolicSylvanMdp
  211. def test_build_ctmc(self):
  212. program = stormpy.parse_prism_program(get_example_path("ctmc", "polling2.sm"), True)
  213. formulas = stormpy.parse_properties_for_prism_program("P=? [ F<=3 \"target\" ]", program)
  214. model = stormpy.build_symbolic_model(program, formulas)
  215. assert model.nr_states == 12
  216. assert model.nr_transitions == 21
  217. assert model.model_type == stormpy.ModelType.CTMC
  218. assert not model.supports_parameters
  219. assert type(model) is stormpy.SymbolicSylvanCtmc
  220. def test_build_ma(self):
  221. program = stormpy.parse_prism_program(get_example_path("ma", "simple.ma"))
  222. formulas = stormpy.parse_properties_for_prism_program("P=? [ F<=2 s=2 ]", program)
  223. with pytest.raises(Exception):
  224. model = stormpy.build_symbolic_model(program, formulas)