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.

135 lines
6.0 KiB

  1. #ifndef STORM_MODELS_ABSTRACTDETERMINISTICMODEL_H_
  2. #define STORM_MODELS_ABSTRACTDETERMINISTICMODEL_H_
  3. #include "AbstractModel.h"
  4. #include <memory>
  5. #include <sstream>
  6. namespace storm {
  7. namespace models {
  8. /*!
  9. * @brief Base class for all deterministic model classes.
  10. *
  11. * This is base class defines a common interface for all deterministic models.
  12. */
  13. template<class T>
  14. class AbstractDeterministicModel: public AbstractModel<T> {
  15. public:
  16. /*! Constructs an abstract determinstic model from the given parameters.
  17. * All values are copied.
  18. * @param transitionMatrix The matrix representing the transitions in the model.
  19. * @param stateLabeling The labeling that assigns a set of atomic
  20. * propositions to each state.
  21. * @param stateRewardVector The reward values associated with the states.
  22. * @param transitionRewardMatrix The reward values associated with the transitions of the model.
  23. */
  24. AbstractDeterministicModel(storm::storage::SparseMatrix<T> const& transitionMatrix, storm::models::AtomicPropositionsLabeling const& stateLabeling,
  25. boost::optional<std::vector<T>> const& optionalStateRewardVector, boost::optional<storm::storage::SparseMatrix<T>> const& optionalTransitionRewardMatrix,
  26. boost::optional<std::vector<std::list<uint_fast64_t>>> const& optionalChoiceLabeling)
  27. : AbstractModel<T>(transitionMatrix, stateLabeling, optionalStateRewardVector, optionalTransitionRewardMatrix, optionalChoiceLabeling) {
  28. }
  29. /*! Constructs an abstract determinstic model from the given parameters.
  30. * Moves all references.
  31. * @param transitionMatrix The matrix representing the transitions in the model.
  32. * @param stateLabeling The labeling that assigns a set of atomic
  33. * propositions to each state.
  34. * @param stateRewardVector The reward values associated with the states.
  35. * @param transitionRewardMatrix The reward values associated with the transitions of the model.
  36. */
  37. AbstractDeterministicModel(storm::storage::SparseMatrix<T>&& transitionMatrix, storm::models::AtomicPropositionsLabeling&& stateLabeling,
  38. boost::optional<std::vector<T>>&& optionalStateRewardVector, boost::optional<storm::storage::SparseMatrix<T>>&& optionalTransitionRewardMatrix,
  39. boost::optional<std::vector<std::list<uint_fast64_t>>>&& optionalChoiceLabeling)
  40. // The std::move call must be repeated here because otherwise this calls the copy constructor of the Base Class
  41. : AbstractModel<T>(std::move(transitionMatrix), std::move(stateLabeling), std::move(optionalStateRewardVector), std::move(optionalTransitionRewardMatrix),
  42. std::move(optionalChoiceLabeling)) {
  43. // Intentionally left empty.
  44. }
  45. /*!
  46. * Destructor.
  47. */
  48. virtual ~AbstractDeterministicModel() {
  49. // Intentionally left empty.
  50. }
  51. /*!
  52. * Copy Constructor.
  53. */
  54. AbstractDeterministicModel(AbstractDeterministicModel const& other) : AbstractModel<T>(other) {
  55. // Intentionally left empty.
  56. }
  57. /*!
  58. * Move Constructor.
  59. */
  60. AbstractDeterministicModel(AbstractDeterministicModel && other) : AbstractModel<T>(std::move(other)) {
  61. // Intentionally left empty.
  62. }
  63. virtual typename storm::storage::SparseMatrix<T>::Rows getRows(uint_fast64_t state) const override {
  64. return this->transitionMatrix.getRows(state, state);
  65. }
  66. virtual typename storm::storage::SparseMatrix<T>::ConstRowIterator rowIteratorBegin(uint_fast64_t state) const override {
  67. return this->transitionMatrix.begin(state);
  68. }
  69. virtual typename storm::storage::SparseMatrix<T>::ConstRowIterator rowIteratorEnd(uint_fast64_t state) const override {
  70. return this->transitionMatrix.end(state);
  71. }
  72. /*!
  73. * Calculates a hash over all values contained in this Model.
  74. * @return size_t A Hash Value
  75. */
  76. virtual std::size_t getHash() const override {
  77. return AbstractModel<T>::getHash();
  78. }
  79. virtual void writeDotToStream(std::ostream& outStream, bool includeLabeling = true, storm::storage::BitVector const* subsystem = nullptr, std::vector<T> const* firstValue = nullptr, std::vector<T> const* secondValue = nullptr, std::vector<uint_fast64_t> const* stateColoring = nullptr, std::vector<std::string> const* colors = nullptr, std::vector<uint_fast64_t>* scheduler = nullptr, bool finalizeOutput = true) const override {
  80. AbstractModel<T>::writeDotToStream(outStream, includeLabeling, subsystem, firstValue, secondValue, stateColoring, colors, scheduler, false);
  81. // Simply iterate over all transitions and draw the arrows with probability information attached.
  82. auto rowIt = this->transitionMatrix.begin();
  83. for (uint_fast64_t i = 0; i < this->transitionMatrix.getRowCount(); ++i, ++rowIt) {
  84. for (auto transitionIt = rowIt.begin(), transitionIte = rowIt.end(); transitionIt != transitionIte; ++transitionIt) {
  85. if (transitionIt.value() != storm::utility::constGetZero<T>()) {
  86. if (subsystem == nullptr || subsystem->get(transitionIt.column())) {
  87. outStream << "\t" << i << " -> " << transitionIt.column() << " [ label= \"" << transitionIt.value() << "\" ];" << std::endl;
  88. }
  89. }
  90. }
  91. }
  92. if (finalizeOutput) {
  93. outStream << "}" << std::endl;
  94. }
  95. }
  96. /*!
  97. * Assigns this model a new set of choiceLabels, giving each state a label with the stateId
  98. * @return void
  99. */
  100. virtual void setStateIdBasedChoiceLabeling() override {
  101. std::vector<std::list<uint_fast64_t>> newChoiceLabeling;
  102. size_t stateCount = this->getNumberOfStates();
  103. newChoiceLabeling.resize(stateCount);
  104. for (size_t state = 0; state < stateCount; ++state) {
  105. newChoiceLabeling.at(state).push_back(state);
  106. }
  107. this->choiceLabeling.reset(newChoiceLabeling);
  108. }
  109. };
  110. } // namespace models
  111. } // namespace storm
  112. #endif /* STORM_MODELS_ABSTRACTDETERMINISTICMODEL_H_ */