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.

100 lines
3.4 KiB

  1. import stormpy
  2. import numpy as np
  3. # Knuth's model of a fair die using only fair coins
  4. def example_building_mdps_01():
  5. nr_states = 13
  6. nr_choices = 14
  7. # Transition matrix with custom row grouping: nondeterministic choice over the actions available in states
  8. builder = stormpy.SparseMatrixBuilder(rows=0, columns=0, entries=0, force_dimensions=False,
  9. has_custom_row_grouping=True, row_groups=0)
  10. # New row group, for actions of state 0
  11. builder.new_row_group(0)
  12. builder.add_next_value(0, 1, 0.5)
  13. builder.add_next_value(0, 2, 0.5)
  14. builder.add_next_value(1, 1, 0.2)
  15. builder.add_next_value(1, 2, 0.8)
  16. # State 1
  17. builder.new_row_group(2)
  18. builder.add_next_value(2, 3, 0.5)
  19. builder.add_next_value(2, 4, 0.5)
  20. # State 2
  21. builder.new_row_group(3)
  22. builder.add_next_value(3, 5, 0.5)
  23. builder.add_next_value(3, 6, 0.5)
  24. # State 3
  25. builder.new_row_group(4)
  26. builder.add_next_value(4, 7, 0.5)
  27. builder.add_next_value(4, 1, 0.5)
  28. # State 4
  29. builder.new_row_group(5)
  30. builder.add_next_value(5, 8, 0.5)
  31. builder.add_next_value(5, 9, 0.5)
  32. # State 5
  33. builder.new_row_group(6)
  34. builder.add_next_value(6, 10, 0.5)
  35. builder.add_next_value(6, 11, 0.5)
  36. # State 6
  37. builder.new_row_group(7)
  38. builder.add_next_value(7, 2, 0.5)
  39. builder.add_next_value(7, 12, 0.5)
  40. # Add transitions for the final states
  41. for s in range(8, 14):
  42. builder.new_row_group(s)
  43. builder.add_next_value(s, s - 1, 1)
  44. transition_matrix = builder.build()
  45. # State labeling
  46. state_labeling = stormpy.storage.StateLabeling(nr_states)
  47. # Add labels
  48. labels = {'init', 'one', 'two', 'three', 'four', 'five', 'six', 'done', 'deadlock'}
  49. for label in labels:
  50. state_labeling.add_label(label)
  51. # Set labeling of states
  52. state_labeling.add_label_to_state('init', 0)
  53. state_labeling.add_label_to_state('one', 7)
  54. state_labeling.add_label_to_state('two', 8)
  55. state_labeling.add_label_to_state('three', 9)
  56. state_labeling.add_label_to_state('four', 10)
  57. state_labeling.add_label_to_state('five', 11)
  58. state_labeling.add_label_to_state('six', 12)
  59. # Set label 'done' for multiple states
  60. state_labeling.set_states('done', stormpy.BitVector(nr_states, [7, 8, 9, 10, 11, 12]))
  61. # Choice labeling
  62. choice_labeling = stormpy.storage.ChoiceLabeling(nr_choices)
  63. choice_labels = {'a', 'b'}
  64. # Add labels
  65. for label in choice_labels:
  66. choice_labeling.add_label(label)
  67. # Set labels
  68. choice_labeling.add_label_to_choice('a', 0)
  69. choice_labeling.add_label_to_choice('b', 1)
  70. print(choice_labeling)
  71. # Reward models
  72. reward_models = {}
  73. # Create a vector representing the state-action rewards
  74. 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]
  75. reward_models['coin_flips'] = stormpy.SparseRewardModel(optional_state_action_reward_vector=action_reward)
  76. # Collect components
  77. components = stormpy.SparseModelComponents(transition_matrix=transition_matrix, state_labeling=state_labeling,
  78. reward_models=reward_models, rate_transitions=False)
  79. components.choice_labeling = choice_labeling
  80. # Build the model
  81. mdp = stormpy.storage.SparseMdp(components)
  82. print(mdp)
  83. if __name__ == '__main__':
  84. example_building_mdps_01()