/* * File: ModelInstantiator.cpp * Author: Tim Quatmann * * Created on February 23, 2016 */ #include "src/utility/ModelInstantiator.h" #include "src/models/sparse/StandardRewardModel.h" namespace storm { namespace utility { template ModelInstantiator::ModelInstantiator(ParametricSparseModelType const& parametricModel){ //Now pre-compute the information for the equation system. initializeModelSpecificData(parametricModel); initializeMatrixMapping(this->instantiatedModel->getTransitionMatrix(), this->functions, this->matrixMapping, parametricModel.getTransitionMatrix()); for(auto& rewModel : this->instantiatedModel->getRewardModels()) { if(rewModel.second.hasStateRewards()){ initializeVectorMapping(rewModel.second.getStateRewardVector(), this->functions, this->vectorMapping, parametricModel.getRewardModel(rewModel.first).getStateRewardVector()); } if(rewModel.second.hasStateActionRewards()){ initializeVectorMapping(rewModel.second.getStateActionRewardVector(), this->functions, this->vectorMapping, parametricModel.getRewardModel(rewModel.first).getStateActionRewardVector()); } if(rewModel.second.hasTransitionRewards()){ initializeMatrixMapping(rewModel.second.getTransitionRewardMatrix(), this->functions, this->matrixMapping, parametricModel.getRewardModel(rewModel.first).getTransitionRewardMatrix()); } } } template ModelInstantiator::~ModelInstantiator() { //Intentionally left empty } template storm::storage::SparseMatrix ModelInstantiator::buildDummyMatrix(storm::storage::SparseMatrix const& parametricMatrix) const{ storm::storage::SparseMatrixBuilder matrixBuilder(parametricMatrix.getRowCount(), parametricMatrix.getColumnCount(), parametricMatrix.getEntryCount(), true, // no force dimensions true, //Custom row grouping parametricMatrix.getRowGroupCount()); for(std::size_t rowGroup = 0; rowGroup < parametricMatrix.getRowGroupCount(); ++rowGroup){ matrixBuilder.newRowGroup(parametricMatrix.getRowGroupIndices()[rowGroup]); for(std::size_t row = parametricMatrix.getRowGroupIndices()[rowGroup]; row < parametricMatrix.getRowGroupIndices()[rowGroup+1]; ++row){ ConstantType dummyValue = storm::utility::one(); for(auto const& paramEntry : parametricMatrix.getRow(row)){ matrixBuilder.addNextValue(row, paramEntry.getColumn(), dummyValue); dummyValue = storm::utility::zero(); } } } return matrixBuilder.build(); } template std::unordered_map ModelInstantiator::buildDummyRewardModels(std::unordered_map const& parametricRewardModel) const { std::unordered_map result; for(auto const& paramRewardModel : parametricRewardModel){ auto const& rewModel = paramRewardModel.second; boost::optional> optionalStateRewardVector; if(rewModel.hasStateRewards()) { optionalStateRewardVector = std::vector(rewModel.getStateRewardVector().size()); } boost::optional> optionalStateActionRewardVector; if(rewModel.hasStateActionRewards()) { optionalStateActionRewardVector = std::vector(rewModel.getStateActionRewardVector().size()); } boost::optional> optionalTransitionRewardMatrix; if(rewModel.hasTransitionRewards()) { optionalTransitionRewardMatrix = buildDummyMatrix(rewModel.getTransitionRewardMatrix()); } result.insert(std::make_pair(paramRewardModel.first, storm::models::sparse::StandardRewardModel(std::move(optionalStateRewardVector), std::move(optionalStateActionRewardVector), std::move(optionalTransitionRewardMatrix)) )); } return result; } template void ModelInstantiator::initializeMatrixMapping(storm::storage::SparseMatrix& constantMatrix, std::unordered_map& functions, std::vector::iterator, ConstantType*>>& mapping, storm::storage::SparseMatrix const& parametricMatrix) const{ ConstantType dummyValue = storm::utility::one(); auto constantEntryIt = constantMatrix.begin(); auto parametricEntryIt = parametricMatrix.begin(); while(parametricEntryIt != parametricMatrix.end()){ STORM_LOG_ASSERT(parametricEntryIt->getColumn() == constantEntryIt->getColumn(), "Entries of parametric and constant matrix are not at the same position"); if(storm::utility::isConstant(parametricEntryIt->getValue())){ //Constant entries can be inserted directly constantEntryIt->setValue(storm::utility::convertNumber(storm::utility::parametric::getConstantPart(parametricEntryIt->getValue()))); } else { //insert the new function and store that the current constantMatrix entry needs to be set to the value of this function auto functionsIt = functions.insert(std::make_pair(parametricEntryIt->getValue(), dummyValue)).first; mapping.emplace_back(std::make_pair(constantEntryIt, &(functionsIt->second))); //Note that references to elements of an unordered map remain valid after calling unordered_map::insert. } ++constantEntryIt; ++parametricEntryIt; } STORM_LOG_ASSERT(constantEntryIt == constantMatrix.end(), "Parametric matrix seems to have more or less entries then the constant matrix"); //TODO: is this necessary? constantMatrix.updateNonzeroEntryCount(); } template void ModelInstantiator::initializeVectorMapping(std::vector& constantVector, std::unordered_map& functions, std::vector::iterator, ConstantType*>>& mapping, std::vector const& parametricVector) const{ ConstantType dummyValue = storm::utility::one(); auto constantEntryIt = constantVector.begin(); auto parametricEntryIt = parametricVector.begin(); while(parametricEntryIt != parametricVector.end()){ if(storm::utility::isConstant(storm::utility::simplify(*parametricEntryIt))){ //Constant entries can be inserted directly *constantEntryIt = storm::utility::convertNumber(storm::utility::parametric::getConstantPart(*parametricEntryIt)); } else { //insert the new function and store that the current constantVector entry needs to be set to the value of this function auto functionsIt = functions.insert(std::make_pair(*parametricEntryIt, dummyValue)).first; mapping.emplace_back(std::make_pair(constantEntryIt, &(functionsIt->second))); //Note that references to elements of an unordered map remain valid after calling unordered_map::insert. } ++constantEntryIt; ++parametricEntryIt; } STORM_LOG_ASSERT(constantEntryIt == constantVector.end(), "Parametric vector seems to have more or less entries then the constant vector"); } template ConstantSparseModelType const& ModelInstantiator::instantiate(std::mapconst& valuation){ //Write results into the placeholders for(auto& functionResult : this->functions){ functionResult.second=storm::utility::convertNumber( storm::utility::parametric::evaluate(functionResult.first, valuation)); } //Write the instantiated values to the matrices and vectors according to the stored mappings for(auto& entryValuePair : this->matrixMapping){ entryValuePair.first->setValue(*(entryValuePair.second)); } for(auto& entryValuePair : this->vectorMapping){ *(entryValuePair.first)=*(entryValuePair.second); } return *this->instantiatedModel; } template void ModelInstantiator::checkValid() const { // TODO write some checks } #ifdef STORM_HAVE_CARL template class ModelInstantiator, storm::models::sparse::Dtmc>; template class ModelInstantiator, storm::models::sparse::Mdp>; template class ModelInstantiator, storm::models::sparse::Ctmc>; template class ModelInstantiator, storm::models::sparse::MarkovAutomaton>; template class ModelInstantiator, storm::models::sparse::StochasticTwoPlayerGame>; #endif } //namespace utility } //namespace storm