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.

99 lines
3.3 KiB

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