195 lines
6.0 KiB

  1. /*
  2. * Ctmc.h
  3. *
  4. * Created on: 14.11.2012
  5. * Author: Christian Dehnert
  6. */
  7. #ifndef STORM_MODELS_CTMC_H_
  8. #define STORM_MODELS_CTMC_H_
  9. #include <ostream>
  10. #include <iostream>
  11. #include <memory>
  12. #include <cstdlib>
  13. #include "AtomicPropositionsLabeling.h"
  14. #include "GraphTransitions.h"
  15. #include "src/storage/SparseMatrix.h"
  16. #include "src/exceptions/InvalidArgumentException.h"
  17. #include "src/models/AbstractModel.h"
  18. namespace storm {
  19. namespace models {
  20. /*!
  21. * This class represents a discrete-time Markov chain (DTMC) whose states are
  22. * labeled with atomic propositions.
  23. */
  24. template <class T>
  25. class Ctmc : public storm::models::AbstractModel {
  26. public:
  27. //! Constructor
  28. /*!
  29. * Constructs a CTMC object from the given transition rate matrix and
  30. * the given labeling of the states.
  31. * @param rateMatrix The transition rate function of the
  32. * CTMC given by a matrix.
  33. * @param stateLabeling The labeling that assigns a set of atomic
  34. * propositions to each state.
  35. */
  36. Ctmc(std::shared_ptr<storm::storage::SparseMatrix<T>> rateMatrix,
  37. std::shared_ptr<storm::models::AtomicPropositionsLabeling> stateLabeling,
  38. std::shared_ptr<std::vector<T>> stateRewards = nullptr,
  39. std::shared_ptr<storm::storage::SparseMatrix<T>> transitionRewardMatrix = nullptr)
  40. : rateMatrix(rateMatrix), stateLabeling(stateLabeling),
  41. stateRewards(stateRewards), transitionRewardMatrix(transitionRewardMatrix),
  42. backwardTransitions(nullptr) {
  43. }
  44. //! Copy Constructor
  45. /*!
  46. * Copy Constructor. Performs a deep copy of the given CTMC.
  47. * @param ctmc A reference to the CTMC that is to be copied.
  48. */
  49. Ctmc(const Ctmc<T> &ctmc) : rateMatrix(ctmc.rateMatrix),
  50. stateLabeling(ctmc.stateLabeling), stateRewards(ctmc.stateRewards),
  51. transitionRewardMatrix(ctmc.transitionRewardMatrix) {
  52. if (ctmc.backwardTransitions != nullptr) {
  53. this->backwardTransitions = new storm::models::GraphTransitions<T>(*ctmc.backwardTransitions);
  54. }
  55. }
  56. //! Destructor
  57. /*!
  58. * Destructor. Frees the matrix and labeling associated with this CTMC.
  59. */
  60. ~Ctmc() {
  61. if (this->backwardTransitions != nullptr) {
  62. delete this->backwardTransitions;
  63. }
  64. }
  65. /*!
  66. * Returns the state space size of the CTMC.
  67. * @return The size of the state space of the CTMC.
  68. */
  69. uint_fast64_t getNumberOfStates() const {
  70. return this->rateMatrix->getRowCount();
  71. }
  72. /*!
  73. * Returns the number of (non-zero) transitions of the CTMC.
  74. * @return The number of (non-zero) transitions of the CTMC.
  75. */
  76. uint_fast64_t getNumberOfTransitions() const {
  77. return this->rateMatrix->getNonZeroEntryCount();
  78. }
  79. /*!
  80. * Returns a bit vector in which exactly those bits are set to true that
  81. * correspond to a state labeled with the given atomic proposition.
  82. * @param ap The atomic proposition for which to get the bit vector.
  83. * @return A bit vector in which exactly those bits are set to true that
  84. * correspond to a state labeled with the given atomic proposition.
  85. */
  86. storm::storage::BitVector* getLabeledStates(std::string ap) const {
  87. return this->stateLabeling->getAtomicProposition(ap);
  88. }
  89. /*!
  90. * Returns a pointer to the matrix representing the transition probability
  91. * function.
  92. * @return A pointer to the matrix representing the transition probability
  93. * function.
  94. */
  95. std::shared_ptr<storm::storage::SparseMatrix<T>> getTransitionRateMatrix() const {
  96. return this->rateMatrix;
  97. }
  98. /*!
  99. * Returns a pointer to the matrix representing the transition rewards.
  100. * @return A pointer to the matrix representing the transition rewards.
  101. */
  102. std::shared_ptr<storm::storage::SparseMatrix<T>> getTransitionRewardMatrix() const {
  103. return this->transitionRewardMatrix;
  104. }
  105. /*!
  106. * Returns a pointer to the vector representing the state rewards.
  107. * @return A pointer to the vector representing the state rewards.
  108. */
  109. std::shared_ptr<std::vector<T>> getStateRewards() const {
  110. return this->stateRewards;
  111. }
  112. /*!
  113. *
  114. */
  115. std::set<std::string> const getPropositionsForState(uint_fast64_t const &state) const {
  116. return stateLabeling->getPropositionsForState(state);
  117. }
  118. /*!
  119. * Retrieves a reference to the backwards transition relation.
  120. * @return A reference to the backwards transition relation.
  121. */
  122. storm::models::GraphTransitions<T>& getBackwardTransitions() {
  123. if (this->backwardTransitions == nullptr) {
  124. this->backwardTransitions = new storm::models::GraphTransitions<T>(this->probabilityMatrix, false);
  125. }
  126. return *this->backwardTransitions;
  127. }
  128. /*!
  129. * Prints information about the model to the specified stream.
  130. * @param out The stream the information is to be printed to.
  131. */
  132. void printModelInformationToStream(std::ostream& out) const {
  133. out << "-------------------------------------------------------------- "
  134. << std::endl;
  135. out << "Model type: \t\tCTMC" << std::endl;
  136. out << "States: \t\t" << this->getNumberOfStates() << std::endl;
  137. out << "Transitions: \t\t" << this->getNumberOfTransitions() << std::endl;
  138. this->stateLabeling->printAtomicPropositionsInformationToStream(out);
  139. out << "Size in memory: \t"
  140. << (this->rateMatrix->getSizeInMemory() +
  141. this->stateLabeling->getSizeInMemory() +
  142. sizeof(*this))/1024 << " kbytes" << std::endl;
  143. out << "-------------------------------------------------------------- "
  144. << std::endl;
  145. }
  146. storm::models::ModelType getType() {
  147. return CTMC;
  148. }
  149. private:
  150. /*! A matrix representing the transition rate function of the CTMC. */
  151. std::shared_ptr<storm::storage::SparseMatrix<T>> rateMatrix;
  152. /*! The labeling of the states of the CTMC. */
  153. std::shared_ptr<storm::models::AtomicPropositionsLabeling> stateLabeling;
  154. /*! The state-based rewards of the CTMC. */
  155. std::shared_ptr<std::vector<T>> stateRewards;
  156. /*! The transition-based rewards of the CTMC. */
  157. std::shared_ptr<storm::storage::SparseMatrix<T>> transitionRewardMatrix;
  158. /*!
  159. * A data structure that stores the predecessors for all states. This is
  160. * needed for backwards directed searches.
  161. */
  162. storm::models::GraphTransitions<T>* backwardTransitions;
  163. };
  164. } // namespace models
  165. } // namespace storm
  166. #endif /* STORM_MODELS_DTMC_H_ */