#include "src/models/sparse/Model.h" #include "src/utility/vector.h" #include "src/adapters/CarlAdapter.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, boost::optional> const& optionalStateRewardVector, boost::optional> const& optionalTransitionRewardMatrix, boost::optional>> const& optionalChoiceLabeling) : ModelBase(modelType), transitionMatrix(transitionMatrix), stateLabeling(stateLabeling), stateRewardVector(optionalStateRewardVector), transitionRewardMatrix(optionalTransitionRewardMatrix), choiceLabeling(optionalChoiceLabeling) { // Intentionally left empty. } template Model::Model(storm::models::ModelType const& modelType, storm::storage::SparseMatrix&& transitionMatrix, storm::models::sparse::StateLabeling&& stateLabeling, boost::optional>&& optionalStateRewardVector, boost::optional>&& optionalTransitionRewardMatrix, boost::optional>>&& optionalChoiceLabeling) : ModelBase(modelType), transitionMatrix(std::move(transitionMatrix)), stateLabeling(std::move(stateLabeling)), stateRewardVector(std::move(optionalStateRewardVector)), transitionRewardMatrix(std::move(optionalTransitionRewardMatrix)), choiceLabeling(std::move(optionalChoiceLabeling)) { // Intentionally left empty. } 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 storm::storage::SparseMatrix const& Model::getTransitionRewardMatrix() const { return transitionRewardMatrix.get(); } template storm::storage::SparseMatrix& Model::getTransitionRewardMatrix() { return transitionRewardMatrix.get(); } template boost::optional> const& Model::getOptionalTransitionRewardMatrix() const { return transitionRewardMatrix; } template std::vector const& Model::getStateRewardVector() const { return stateRewardVector.get(); } template boost::optional> const& Model::getOptionalStateRewardVector() const { return stateRewardVector; } template std::vector> const& Model::getChoiceLabeling() const { return choiceLabeling.get(); } template storm::models::sparse::StateLabeling const& Model::getStateLabeling() const { return stateLabeling; } template storm::models::sparse::StateLabeling& Model::getStateLabeling() { return stateLabeling; } template bool Model::hasStateRewards() const { return static_cast(stateRewardVector); } template bool Model::hasTransitionRewards() const { return static_cast(transitionRewardMatrix); } template bool Model::hasChoiceLabeling() const { return static_cast(choiceLabeling); } template void Model::convertTransitionRewardsToStateRewards() { STORM_LOG_THROW(this->hasTransitionRewards(), storm::exceptions::InvalidOperationException, "Cannot reduce non-existant transition rewards to state rewards."); if (this->hasStateRewards()) { storm::utility::vector::addVectors(stateRewardVector.get(), transitionMatrix.getPointwiseProductRowSumVector(transitionRewardMatrix.get()), stateRewardVector.get()); } else { this->stateRewardVector = transitionMatrix.getPointwiseProductRowSumVector(transitionRewardMatrix.get()); } this->transitionRewardMatrix = boost::optional>(); } template std::size_t Model::getSizeInBytes() const { std::size_t result = transitionMatrix.getSizeInBytes() + stateLabeling.getSizeInBytes(); if (stateRewardVector) { result += getStateRewardVector().size() * sizeof(ValueType); } if (hasTransitionRewards()) { result += getTransitionRewardMatrix().getSizeInBytes(); } if (hasChoiceLabeling()) { result += getChoiceLabeling().size() * sizeof(boost::container::flat_set); } return result; } template void Model::printModelInformationToStream(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; this->getStateLabeling().printLabelingInformationToStream(out); out << "Size in memory: \t" << (this->getSizeInBytes())/1024 << " kbytes" << std::endl; out << "-------------------------------------------------------------- " << 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 class Model; template class Model; #ifdef STORM_HAVE_CARL template class Model; #endif } } }