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.

58 lines
1.8 KiB

  1. import stormpy.core
  2. class Simulator:
  3. """
  4. Base class for simulators.
  5. """
  6. def __init__(self, seed=None):
  7. self._seed = seed
  8. def step(self, action=None):
  9. raise NotImplementedError("Abstract superclass")
  10. def restart(self):
  11. raise NotImplementedError("Abstract superclass")
  12. class SparseSimulator(Simulator):
  13. """
  14. Simulator on top of sparse models.
  15. """
  16. def __init__(self, model, seed=None):
  17. super().__init__(seed)
  18. self._model = model
  19. self._engine = stormpy.core._DiscreteTimeSparseModelSimulatorDouble(model)
  20. if seed is not None:
  21. self._engine.set_seed(seed)
  22. def step(self, action=None):
  23. if action is None:
  24. if self._model.is_nondeterministic_model:
  25. raise RuntimeError("Must specify an action in nondeterministic models")
  26. check = self._engine.step(0)
  27. assert(check)
  28. else:
  29. if action >= self._model.get_nondeterministic_choices():
  30. raise RuntimeError(f"Only {self._model.get_nondeterministic_choices()} actions available")
  31. check = self._engine.step(action)
  32. assert(check)
  33. return self._engine.get_current_state()
  34. def restart(self):
  35. self._engine.reset_to_initial_state()
  36. def create_simulator(model, seed = None):
  37. """
  38. Factory method for creating a simulator.
  39. :param model: Some form of model
  40. :param seed: A seed for reproducibility. If None (default), the seed is internally generated.
  41. :return: A simulator that can simulate on top of this model
  42. """
  43. if isinstance(model, stormpy.storage._ModelBase):
  44. if model.is_sparse_model:
  45. return SparseSimulator(model, seed)
  46. else:
  47. raise NotImplementedError("Currently, we only support simulators for sparse models.")