#include "src/models/sparse/Model.h" #include #include "src/models/sparse/StandardRewardModel.h" #include "src/utility/vector.h" #include "src/adapters/CarlAdapter.h" #include "src/exceptions/IllegalArgumentException.h" #include "src/exceptions/IllegalFunctionCallException.h" namespace storm { namespace models { namespace sparse { template Model::Model(storm::models::ModelType const& modelType, storm::storage::SparseMatrix const& transitionMatrix, storm::models::sparse::StateLabeling const& stateLabeling, std::unordered_map const& rewardModels, boost::optional> const& optionalChoiceLabeling) : ModelBase(modelType), transitionMatrix(transitionMatrix), stateLabeling(stateLabeling), rewardModels(rewardModels), choiceLabeling(optionalChoiceLabeling) { for (auto const& rewardModel : this->getRewardModels()) { STORM_LOG_THROW(!rewardModel.second.hasTransitionRewards() || rewardModel.second.getTransitionRewardMatrix().isSubmatrixOf(this->getTransitionMatrix()), storm::exceptions::IllegalArgumentException, "The transition reward matrix is not a submatrix of the transition matrix, i.e. there are rewards for transitions that do not exist."); } } template Model::Model(storm::models::ModelType const& modelType, storm::storage::SparseMatrix&& transitionMatrix, storm::models::sparse::StateLabeling&& stateLabeling, std::unordered_map&& rewardModels, boost::optional>&& optionalChoiceLabeling) : ModelBase(modelType), transitionMatrix(std::move(transitionMatrix)), stateLabeling(std::move(stateLabeling)), rewardModels(std::move(rewardModels)), choiceLabeling(std::move(optionalChoiceLabeling)) { for (auto const& rewardModel : this->getRewardModels()) { STORM_LOG_THROW(!rewardModel.second.hasTransitionRewards() || rewardModel.second.getTransitionRewardMatrix().isSubmatrixOf(this->getTransitionMatrix()), storm::exceptions::IllegalArgumentException, "The transition reward matrix is not a submatrix of the transition matrix, i.e. there are rewards for transitions that do not exist."); } } template storm::storage::SparseMatrix Model::getBackwardTransitions() const { return this->getTransitionMatrix().transpose(true); } template typename storm::storage::SparseMatrix::const_rows Model::getRows(storm::storage::sparse::state_type state) const { return this->getTransitionMatrix().getRowGroup(state); } template uint_fast64_t Model::getNumberOfStates() const { return this->getTransitionMatrix().getColumnCount(); } template uint_fast64_t Model::getNumberOfTransitions() const { return this->getTransitionMatrix().getNonzeroEntryCount(); } template storm::storage::BitVector const& Model::getInitialStates() const { return this->getStates("init"); } template storm::storage::BitVector const& Model::getStates(std::string const& label) const { return stateLabeling.getStates(label); } template bool Model::hasLabel(std::string const& label) const { return stateLabeling.containsLabel(label); } template storm::storage::SparseMatrix const& Model::getTransitionMatrix() const { return transitionMatrix; } template storm::storage::SparseMatrix& Model::getTransitionMatrix() { return transitionMatrix; } template bool Model::hasRewardModel(std::string const& rewardModelName) const { return this->rewardModels.find(rewardModelName) != this->rewardModels.end(); } template RewardModelType& Model::rewardModel(std::string const& rewardModelName) { assert(this->hasRewardModel(rewardModelName)); return this->rewardModels.find(rewardModelName)->second; } template RewardModelType const& Model::getRewardModel(std::string const& rewardModelName) const { auto it = this->rewardModels.find(rewardModelName); if (it == this->rewardModels.end()) { if (rewardModelName.empty()) { if (this->hasUniqueRewardModel()) { return this->getUniqueRewardModel(); } else { STORM_LOG_THROW(false, storm::exceptions::IllegalArgumentException, "Unable to refer to default reward model, because there is no default model or it is not unique."); } } else { STORM_LOG_THROW(false, storm::exceptions::IllegalArgumentException, "The requested reward model '" << rewardModelName << "' does not exist."); } } return it->second; } template void Model::addRewardModel(std::string const& rewardModelName, RewardModelType const& newRewardModel) { if(this->hasRewardModel(rewardModelName)) { STORM_LOG_THROW(!(this->hasRewardModel(rewardModelName)), storm::exceptions::IllegalArgumentException, "A reward model with the given name '" << rewardModelName << "' already exists."); } assert(newRewardModel.isCompatible(this->getNumberOfStates(), this->getTransitionMatrix().getRowCount())); this->rewardModels.emplace(rewardModelName, newRewardModel); } template bool Model::removeRewardModel(std::string const& rewardModelName) { auto it = this->rewardModels.find(rewardModelName); bool res = (it != this->rewardModels.end()); if(res) { this->rewardModels.erase(it->first); } return res; } template bool Model::hasUniqueRewardModel() const { return this->getNumberOfRewardModels() == 1; } template bool Model::hasRewardModel() const { return !this->rewardModels.empty(); } template RewardModelType const& Model::getUniqueRewardModel() const { STORM_LOG_THROW(this->getNumberOfRewardModels() == 1, storm::exceptions::IllegalFunctionCallException, "The reward model is not unique."); return this->rewardModels.cbegin()->second; } template RewardModelType& Model::getUniqueRewardModel() { STORM_LOG_THROW(this->getNumberOfRewardModels() == 1, storm::exceptions::IllegalFunctionCallException, "The reward model is not unique."); return this->rewardModels.begin()->second; } template uint_fast64_t Model::getNumberOfRewardModels() const { return this->rewardModels.size(); } template std::vector const& Model::getChoiceLabeling() const { return choiceLabeling.get(); } template boost::optional> const& Model::getOptionalChoiceLabeling() const { return choiceLabeling; } template storm::models::sparse::StateLabeling const& Model::getStateLabeling() const { return stateLabeling; } template storm::models::sparse::StateLabeling& Model::getStateLabeling() { return stateLabeling; } template bool Model::hasChoiceLabeling() const { return static_cast(choiceLabeling); } template std::size_t Model::getSizeInBytes() const { std::size_t result = transitionMatrix.getSizeInBytes() + stateLabeling.getSizeInBytes(); for (auto const& rewardModel : this->rewardModels) { result += rewardModel.second.getSizeInBytes(); } if (hasChoiceLabeling()) { result += getChoiceLabeling().size() * sizeof(LabelSet); } return result; } template void Model::printModelInformationToStream(std::ostream& out) const { this->printModelInformationHeaderToStream(out); this->printModelInformationFooterToStream(out); } template void Model::printModelInformationHeaderToStream(std::ostream& out) const { out << "-------------------------------------------------------------- " << std::endl; out << "Model type: \t" << this->getType() << " (sparse)" << std::endl; out << "States: \t" << this->getNumberOfStates() << std::endl; out << "Transitions: \t" << this->getNumberOfTransitions() << std::endl; } template void Model::printModelInformationFooterToStream(std::ostream& out) const { this->printRewardModelsInformationToStream(out); this->getStateLabeling().printLabelingInformationToStream(out); out << "choice labels: \t" << (this->hasChoiceLabeling() ? "yes" : "no") << std::noboolalpha << std::endl; out << "Size in memory: " << (this->getSizeInBytes())/1024 << " kbytes" << std::endl; out << "-------------------------------------------------------------- " << std::endl; } template void Model::printRewardModelsInformationToStream(std::ostream& out) const { if (this->rewardModels.size()) { std::vector rewardModelNames; std::for_each(this->rewardModels.cbegin(), this->rewardModels.cend(), [&rewardModelNames] (typename std::pair const& nameRewardModelPair) { if (nameRewardModelPair.first.empty()) { rewardModelNames.push_back("(default)"); } else { rewardModelNames.push_back(nameRewardModelPair.first); } }); out << "Reward Models: " << boost::join(rewardModelNames, ", ") << std::endl; } else { out << "Reward Models: none" << std::endl; } } template void Model::writeDotToStream(std::ostream& outStream, bool includeLabeling, storm::storage::BitVector const* subsystem, std::vector const* firstValue, std::vector const* secondValue, std::vector const* stateColoring, std::vector const* colors, std::vector* scheduler, bool finalizeOutput) const { outStream << "digraph model {" << std::endl; // Write all states to the stream. for (uint_fast64_t state = 0, highestStateIndex = this->getNumberOfStates() - 1; state <= highestStateIndex; ++state) { if (subsystem == nullptr || subsystem->get(state)) { outStream << "\t" << state; if (includeLabeling || firstValue != nullptr || secondValue != nullptr || stateColoring != nullptr) { outStream << " [ "; // If we need to print some extra information, do so now. if (includeLabeling || firstValue != nullptr || secondValue != nullptr) { outStream << "label = \"" << state << ": "; // Now print the state labeling to the stream if requested. if (includeLabeling) { outStream << "{"; bool includeComma = false; for (std::string const& label : this->getLabelsOfState(state)) { if (includeComma) { outStream << ", "; } else { includeComma = true; } outStream << label; } outStream << "}"; } // If we are to include some values for the state as well, we do so now. if (firstValue != nullptr || secondValue != nullptr) { outStream << " ["; if (firstValue != nullptr) { outStream << (*firstValue)[state]; if (secondValue != nullptr) { outStream << ", "; } } if (secondValue != nullptr) { outStream << (*secondValue)[state]; } outStream << "]"; } outStream << "\""; // Now, we color the states if there were colors given. if (stateColoring != nullptr && colors != nullptr) { outStream << ", "; outStream << " style = filled, fillcolor = " << (*colors)[(*stateColoring)[state]]; } } outStream << " ]"; } outStream << ";" << std::endl; } } // If this methods has not been called from a derived class, we want to close the digraph here. if (finalizeOutput) { outStream << "}" << std::endl; } } template std::set Model::getLabelsOfState(storm::storage::sparse::state_type state) const { return this->stateLabeling.getLabelsOfState(state); } template void Model::setTransitionMatrix(storm::storage::SparseMatrix const& transitionMatrix) { this->transitionMatrix = transitionMatrix; } template void Model::setTransitionMatrix(storm::storage::SparseMatrix&& transitionMatrix) { this->transitionMatrix = std::move(transitionMatrix); } template bool Model::isSparseModel() const { return true; } template bool Model::supportsParameters() const { #ifdef STORM_HAVE_CARL return std::is_same::value; #else return false; #endif } template bool Model::hasParameters() const { if (!this->supportsParameters()) { return false; } // Check for parameters for (auto const& entry : this->getTransitionMatrix()) { if (!storm::utility::isConstant(entry.getValue())) { return true; } } // Only constant values present return false; } template bool Model::isExact() const { // TODO: change when dedicated data-structure for exact values is present return this->supportsParameters(); } template std::unordered_map& Model::getRewardModels() { return this->rewardModels; } template std::unordered_map const& Model::getRewardModels() const { return this->rewardModels; } #ifdef STORM_HAVE_CARL std::set getProbabilityParameters(Model const& model) { return storm::storage::getVariables(model.getTransitionMatrix()); } #endif template class Model; template class Model; #ifdef STORM_HAVE_CARL template class Model; template class Model>; template class Model; #endif } } }