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.

66 lines
2.4 KiB

4 years ago
  1. import stormpy
  2. import numpy as np
  3. def example_building_dtmcs_01():
  4. # Use the SparseMatrixBuilder for constructing the transition matrix
  5. builder = stormpy.SparseMatrixBuilder(rows = 0, columns = 0, entries = 0, force_dimensions = False, has_custom_row_grouping = False)
  6. # New Transition from state 0 to target state 1 with probability 0.5
  7. builder.add_next_value(row = 0, column = 1, value = 0.5)
  8. builder.add_next_value(0, 2, 0.5)
  9. builder.add_next_value(1, 3, 0.5)
  10. builder.add_next_value(1, 4, 0.5)
  11. builder.add_next_value(2, 5, 0.5)
  12. builder.add_next_value(2, 6, 0.5)
  13. builder.add_next_value(3, 7, 0.5)
  14. builder.add_next_value(3, 1, 0.5)
  15. builder.add_next_value(4, 8, 0.5)
  16. builder.add_next_value(4, 9, 0.5)
  17. builder.add_next_value(5, 10, 0.5)
  18. builder.add_next_value(5, 11, 0.5)
  19. builder.add_next_value(6, 2, 0.5)
  20. builder.add_next_value(6, 12, 0.5)
  21. # Add transitions for the final states
  22. for s in range(7,13):
  23. builder.add_next_value(s, s, 1)
  24. # Build matrix
  25. transition_matrix = builder.build(13, 13)
  26. # State labeling
  27. state_labeling = stormpy.storage.StateLabeling(13)
  28. # Add labels
  29. labels = {'init','one', 'two', 'three', 'four', 'five', 'six', 'done', 'deadlock'}
  30. for label in labels:
  31. state_labeling.add_label(label)
  32. # Add label to state
  33. state_labeling.add_label_to_state('init', 0)
  34. state_labeling.add_label_to_state('one', 7)
  35. state_labeling.add_label_to_state('two', 8)
  36. state_labeling.add_label_to_state('three', 9)
  37. state_labeling.add_label_to_state('four', 10)
  38. state_labeling.add_label_to_state('five', 11)
  39. state_labeling.add_label_to_state('six', 12)
  40. # Add label 'done' to multiple states
  41. state_labeling.set_states('done', stormpy.BitVector(13, [7, 8, 9, 10, 11, 12]))
  42. # Reward models:
  43. reward_models = {}
  44. # Create a vector representing the state-action rewards
  45. 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]
  46. reward_models['coin_flips'] = stormpy.SparseRewardModel(optional_state_action_reward_vector = action_reward)
  47. components = stormpy.SparseModelComponents(transition_matrix=transition_matrix, state_labeling=state_labeling, reward_models=reward_models)
  48. dtmc = stormpy.storage.SparseDtmc(components)
  49. print(dtmc)
  50. if __name__ == '__main__':
  51. example_building_dtmcs_01()