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.

188 lines
9.0 KiB

  1. #ifndef STORM_BUILDER_EXPLICITMODELBUILDER_H
  2. #define STORM_BUILDER_EXPLICITMODELBUILDER_H
  3. #include <memory>
  4. #include <utility>
  5. #include <vector>
  6. #include <deque>
  7. #include <cstdint>
  8. #include <boost/functional/hash.hpp>
  9. #include <boost/container/flat_set.hpp>
  10. #include <boost/container/flat_map.hpp>
  11. #include <boost/variant.hpp>
  12. #include "storm/models/sparse/StandardRewardModel.h"
  13. #include "storm/storage/prism/Program.h"
  14. #include "storm/storage/expressions/ExpressionEvaluator.h"
  15. #include "storm/storage/BitVectorHashMap.h"
  16. #include "storm/logic/Formulas.h"
  17. #include "storm/models/sparse/StateAnnotation.h"
  18. #include "storm/models/sparse/Model.h"
  19. #include "storm/models/sparse/StateLabeling.h"
  20. #include "storm/storage/SparseMatrix.h"
  21. #include "storm/storage/sparse/StateValuations.h"
  22. #include "storm/storage/sparse/StateStorage.h"
  23. #include "storm/settings/SettingsManager.h"
  24. #include "storm/utility/prism.h"
  25. #include "storm/builder/ExplorationOrder.h"
  26. #include "storm/generator/NextStateGenerator.h"
  27. #include "storm/generator/CompressedState.h"
  28. #include "storm/generator/VariableInformation.h"
  29. namespace storm {
  30. namespace utility {
  31. template<typename ValueType> class ConstantsComparator;
  32. }
  33. namespace builder {
  34. using namespace storm::utility::prism;
  35. using namespace storm::generator;
  36. // Forward-declare classes.
  37. template <typename ValueType> struct RewardModelBuilder;
  38. template<typename ValueType, typename RewardModelType = storm::models::sparse::StandardRewardModel<ValueType>, typename StateType = uint32_t>
  39. class ExplicitModelBuilder {
  40. public:
  41. // A structure holding the individual components of a model.
  42. struct ModelComponents {
  43. ModelComponents();
  44. // The transition matrix.
  45. storm::storage::SparseMatrix<ValueType> transitionMatrix;
  46. // The state labeling.
  47. storm::models::sparse::StateLabeling stateLabeling;
  48. // The reward models associated with the model.
  49. std::unordered_map<std::string, storm::models::sparse::StandardRewardModel<typename RewardModelType::ValueType>> rewardModels;
  50. // A vector that stores a labeling for each choice.
  51. boost::optional<std::vector<boost::container::flat_set<uint_fast64_t>>> choiceLabeling;
  52. // A vector that stores which states are markovian.
  53. boost::optional<storm::storage::BitVector> markovianStates;
  54. };
  55. struct Options {
  56. /*!
  57. * Creates an object representing the default building options.
  58. */
  59. Options();
  60. // The order in which to explore the model.
  61. ExplorationOrder explorationOrder;
  62. // A flag that indicates whether or not to store the state information after successfully building the
  63. // model. If it is to be preserved, it can be retrieved via the appropriate methods after a successful
  64. // call to <code>translateProgram</code>.
  65. bool buildStateValuations;
  66. };
  67. /*!
  68. * Creates an explicit model builder that uses the provided generator.
  69. *
  70. * @param generator The generator to use.
  71. */
  72. ExplicitModelBuilder(std::shared_ptr<storm::generator::NextStateGenerator<ValueType, StateType>> const& generator, Options const& options = Options());
  73. /*!
  74. * Creates an explicit model builder for the given PRISM program.
  75. *
  76. * @param program The program for which to build the model.
  77. */
  78. ExplicitModelBuilder(storm::prism::Program const& program, storm::generator::NextStateGeneratorOptions const& generatorOptions = storm::generator::NextStateGeneratorOptions(), Options const& builderOptions = Options());
  79. /*!
  80. * Creates an explicit model builder for the given JANI model.
  81. *
  82. * @param model The JANI model for which to build the model.
  83. */
  84. ExplicitModelBuilder(storm::jani::Model const& model, storm::generator::NextStateGeneratorOptions const& generatorOptions = storm::generator::NextStateGeneratorOptions(), Options const& builderOptions = Options());
  85. /*!
  86. * Convert the program given at construction time to an abstract model. The type of the model is the one
  87. * specified in the program. The given reward model name selects the rewards that the model will contain.
  88. *
  89. * @param program The program to translate.
  90. * @param constantDefinitionString A string that contains a comma-separated definition of all undefined
  91. * constants in the model.
  92. * @param rewardModel The reward model that is to be built.
  93. * @return The explicit model that was given by the probabilistic program.
  94. */
  95. std::shared_ptr<storm::models::sparse::Model<ValueType, RewardModelType>> build();
  96. /*!
  97. * If requested in the options, information about the variable valuations in the reachable states can be
  98. * retrieved via this function.
  99. *
  100. * @return A structure that stores information about all reachable states.
  101. */
  102. storm::storage::sparse::StateValuations const& getStateValuations() const;
  103. private:
  104. /*!
  105. * Retrieves the state id of the given state. If the state has not been encountered yet, it will be added to
  106. * the lists of all states with a new id. If the state was already known, the object that is pointed to by
  107. * the given state pointer is deleted and the old state id is returned. Note that the pointer should not be
  108. * used after invoking this method.
  109. *
  110. * @param state A pointer to a state for which to retrieve the index. This must not be used after the call.
  111. * @return A pair indicating whether the state was already discovered before and the state id of the state.
  112. */
  113. StateType getOrAddStateIndex(CompressedState const& state);
  114. /*!
  115. * Builds the transition matrix and the transition reward matrix based for the given program.
  116. *
  117. * @param transitionMatrixBuilder The builder of the transition matrix.
  118. * @param rewardModelBuilders The builders for the selected reward models.
  119. * @param choiceLabels is set to a vector containing the labels associated with each choice (is only set if choice labels are requested).
  120. * @param markovianChoices is set to a bit vector storing whether a choice is markovian (is only set if the model type requires this information).
  121. */
  122. void buildMatrices(storm::storage::SparseMatrixBuilder<ValueType>& transitionMatrixBuilder, std::vector<RewardModelBuilder<typename RewardModelType::ValueType>>& rewardModelBuilders, boost::optional<std::vector<boost::container::flat_set<uint_fast64_t>>>& choiceLabels , boost::optional<storm::storage::BitVector>& markovianChoices);
  123. /*!
  124. * Explores the state space of the given program and returns the components of the model as a result.
  125. *
  126. * @return A structure containing the components of the resulting model.
  127. */
  128. ModelComponents buildModelComponents();
  129. /*!
  130. * Builds the state labeling for the given program.
  131. *
  132. * @return The state labeling of the given program.
  133. */
  134. storm::models::sparse::StateLabeling buildStateLabeling();
  135. /// The generator to use for the building process.
  136. std::shared_ptr<storm::generator::NextStateGenerator<ValueType, StateType>> generator;
  137. /// The options to be used for the building process.
  138. Options options;
  139. /// Internal information about the states that were explored.
  140. storm::storage::sparse::StateStorage<StateType> stateStorage;
  141. /// This member holds information about reachable states that can be retrieved from the outside after a
  142. /// successful build.
  143. boost::optional<storm::storage::sparse::StateValuations> stateValuations;
  144. /// A set of states that still need to be explored.
  145. std::deque<CompressedState> statesToExplore;
  146. /// An optional mapping from state indices to the row groups in which they actually reside. This needs to be
  147. /// built in case the exploration order is not BFS.
  148. boost::optional<std::vector<uint_fast64_t>> stateRemapping;
  149. };
  150. } // namespace adapters
  151. } // namespace storm
  152. #endif /* STORM_BUILDER_EXPLICITMODELBUILDER_H */