The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

86 lines
3.5 KiB

2 months ago
  1. import stormpy
  2. import stormpy.core
  3. import stormpy.info
  4. import pycarl
  5. import pycarl.core
  6. import stormpy.examples
  7. import stormpy.examples.files
  8. import stormpy.pomdp
  9. import stormpy._config as config
  10. def example_parametric_models_01():
  11. # Check support for parameters
  12. if not config.storm_with_pars:
  13. print("Support parameters is missing. Try building storm-pars.")
  14. return
  15. import stormpy.pars
  16. from pycarl.formula import FormulaType, Relation
  17. if stormpy.info.storm_ratfunc_use_cln():
  18. import pycarl.cln.formula
  19. else:
  20. import pycarl.gmp.formula
  21. # Prevent curious side effects from earlier runs (for tests only)
  22. pycarl.clear_pools()
  23. # ###
  24. # # How to apply an unknown FSC to obtain a pMC from a POMDP
  25. # path = stormpy.examples.files.prism_pomdp_maze
  26. # prism_program = stormpy.parse_prism_program(path)
  27. #
  28. # formula_str = "P=? [!\"bad\" U \"goal\"]"
  29. # properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program)
  30. # # construct the POMDP
  31. # pomdp = stormpy.build_model(prism_program, properties)
  32. # # make its representation canonic.
  33. # pomdp = stormpy.pomdp.make_canonic(pomdp)
  34. # # make the POMDP simple. This step is optional but often beneficial
  35. # pomdp = stormpy.pomdp.make_simple(pomdp)
  36. # # construct the memory for the FSC
  37. # # in this case, a selective counter with two states
  38. # memory_builder = stormpy.pomdp.PomdpMemoryBuilder()
  39. # memory = memory_builder.build(stormpy.pomdp.PomdpMemoryPattern.selective_counter, 2)
  40. # # apply the memory onto the POMDP to get the cartesian product
  41. # pomdp = stormpy.pomdp.unfold_memory(pomdp, memory)
  42. # # apply the memory onto the POMDP to get the cartesian product
  43. # pmc = stormpy.pomdp.apply_unknown_fsc(pomdp, stormpy.pomdp.PomdpFscApplicationMode.simple_linear)
  44. ####
  45. # How to apply an unknown FSC to obtain a pMC from a pPOMDP
  46. path = stormpy.examples.files.prism_par_pomdp_maze
  47. prism_program = stormpy.parse_prism_program(path)
  48. formula_str = "P=? [!\"bad\" U \"goal\"]"
  49. properties = stormpy.parse_properties_for_prism_program(formula_str, prism_program)
  50. # construct the pPOMDP
  51. options = stormpy.BuilderOptions([p.raw_formula for p in properties])
  52. options.set_build_state_valuations()
  53. options.set_build_choice_labels()
  54. pomdp = stormpy.build_sparse_parametric_model_with_options(prism_program, options)
  55. # make its representation canonic.
  56. pomdp = stormpy.pomdp.make_canonic(pomdp)
  57. # construct the memory for the FSC
  58. # in this case, a selective counter with two states
  59. memory_builder = stormpy.pomdp.PomdpMemoryBuilder()
  60. memory = memory_builder.build(stormpy.pomdp.PomdpMemoryPattern.selective_counter, 3)
  61. # apply the memory onto the POMDP to get the cartesian product
  62. pomdp = stormpy.pomdp.unfold_memory(pomdp, memory, add_memory_labels=True, keep_state_valuations=True)
  63. # make the POMDP simple. This step is optional but often beneficial
  64. pomdp = stormpy.pomdp.make_simple(pomdp, keep_state_valuations=True)
  65. # apply the unknown FSC to obtain a pmc from the POMDP
  66. pmc = stormpy.pomdp.apply_unknown_fsc(pomdp, stormpy.pomdp.PomdpFscApplicationMode.simple_linear)
  67. export_pmc = False # Set to True to export the pMC as drn.
  68. if export_pmc:
  69. export_options = stormpy.core.DirectEncodingOptions()
  70. export_options.allow_placeholders = False
  71. stormpy.export_to_drn(pmc, "test.out", export_options)
  72. if __name__ == '__main__':
  73. example_parametric_models_01()