#include "storm-pomdp/transformer/BinaryPomdpTransformer.h" #include #include "storm/storage/sparse/ModelComponents.h" #include "storm/utility/macros.h" #include "storm/exceptions/NotSupportedException.h" namespace storm { namespace transformer { template BinaryPomdpTransformer::BinaryPomdpTransformer() { // Intentionally left empty } template std::shared_ptr> BinaryPomdpTransformer::transform(storm::models::sparse::Pomdp const& pomdp, bool transformSimple, bool keepStateValuations) const { auto data = transformTransitions(pomdp, transformSimple); storm::storage::sparse::ModelComponents components; components.stateLabeling = transformStateLabeling(pomdp, data); for (auto const& rewModel : pomdp.getRewardModels()) { components.rewardModels.emplace(rewModel.first, transformRewardModel(pomdp, rewModel.second, data)); } components.transitionMatrix = std::move(data.simpleMatrix); components.observabilityClasses = std::move(data.simpleObservations); if (keepStateValuations && pomdp.hasStateValuations()) { components.stateValuations = pomdp.getStateValuations().blowup(data.simpleStateToOriginalState); } return std::make_shared>(std::move(components), true); } struct BinaryPomdpTransformerRowGroup { BinaryPomdpTransformerRowGroup(uint64_t origState, uint64_t firstRow, uint64_t endRow, uint32_t origStateObservation) : origState(origState), firstRow(firstRow), endRow(endRow), origStateObservation(origStateObservation) { // Intentionally left empty. } uint64_t origState; uint64_t firstRow; uint64_t endRow; uint32_t origStateObservation; storm::storage::BitVector auxStateId; uint64_t size() const { return endRow - firstRow; } std::vector split() const { assert(size() > 1); uint64_t midRow = firstRow + size()/2; std::vector res; res.emplace_back(origState, firstRow, midRow, origStateObservation); storm::storage::BitVector newAuxStateId = auxStateId; newAuxStateId.resize(auxStateId.size() + 1, false); res.back().auxStateId = newAuxStateId; res.emplace_back(origState, midRow, endRow, origStateObservation); newAuxStateId.set(auxStateId.size(), true); res.back().auxStateId = newAuxStateId; return res; } }; struct BinaryPomdpTransformerRowGroupCompare { bool operator () (BinaryPomdpTransformerRowGroup const& lhs, BinaryPomdpTransformerRowGroup const& rhs) const { if (lhs.origStateObservation == rhs.origStateObservation) { return lhs.auxStateId < rhs.auxStateId; } else { return lhs.origStateObservation < rhs.origStateObservation; } } }; template typename BinaryPomdpTransformer::TransformationData BinaryPomdpTransformer::transformTransitions(storm::models::sparse::Pomdp const& pomdp, bool transformSimple) const { auto const& matrix = pomdp.getTransitionMatrix(); // Initialize a FIFO Queue that stores the start and the end of each row group std::queue queue; for (uint64_t state = 0; state < matrix.getRowGroupCount(); ++state) { queue.emplace(state, matrix.getRowGroupIndices()[state], matrix.getRowGroupIndices()[state+1], pomdp.getObservation(state)); } std::vector newObservations; std::map observationMap; storm::storage::SparseMatrixBuilder builder(0,0,0,true,true); uint64_t currRow = 0; std::vector origRowToSimpleRowMap(pomdp.getNumberOfChoices(), std::numeric_limits::max()); uint64_t currAuxState = queue.size(); std::vector origStates; while (!queue.empty()) { auto group = std::move(queue.front()); queue.pop(); // Get the observation uint64_t newObservation = observationMap.insert(std::make_pair(group, observationMap.size())).first->second; newObservations.push_back(newObservation); // Add matrix entries builder.newRowGroup(currRow); if (group.size() == 1) { // Insert the row directly for (auto const& entry : matrix.getRow(group.firstRow)) { builder.addNextValue(currRow, entry.getColumn(), entry.getValue()); } origRowToSimpleRowMap[group.firstRow] = currRow; ++currRow; } else if (group.size() > 1) { // Split the row group into two equally large parts for (auto& splittedGroup : group.split()) { // Check whether we can insert the row now or whether an auxiliary state is needed if (splittedGroup.size() == 1 && (!transformSimple || matrix.getRow(splittedGroup.firstRow).getNumberOfEntries() < 2)) { for (auto const& entry : matrix.getRow(splittedGroup.firstRow)) { builder.addNextValue(currRow, entry.getColumn(), entry.getValue()); } origRowToSimpleRowMap[splittedGroup.firstRow] = currRow; ++currRow; } else { queue.push(std::move(splittedGroup)); builder.addNextValue(currRow, currAuxState, storm::utility::one()); ++currAuxState; ++currRow; } } } // Nothing to be done if group has size zero origStates.push_back(group.origState); } TransformationData result; result.simpleMatrix = builder.build(currRow, currAuxState, currAuxState); result.simpleObservations = std::move(newObservations); result.originalToSimpleChoiceMap = std::move(origRowToSimpleRowMap); result.simpleStateToOriginalState = std::move(origStates); return result; } template storm::models::sparse::StateLabeling BinaryPomdpTransformer::transformStateLabeling(storm::models::sparse::Pomdp const& pomdp, TransformationData const& data) const { storm::models::sparse::StateLabeling labeling(data.simpleMatrix.getRowGroupCount()); for (auto const& labelName : pomdp.getStateLabeling().getLabels()) { storm::storage::BitVector newStates = pomdp.getStateLabeling().getStates(labelName); newStates.resize(data.simpleMatrix.getRowGroupCount(), false); if (labelName != "init") { for (uint64_t newState = pomdp.getNumberOfStates(); newState < data.simpleMatrix.getRowGroupCount(); ++newState) { newStates.set(newState, newStates[data.simpleStateToOriginalState[newState]]); } } labeling.addLabel(labelName, std::move(newStates)); } return labeling; } template storm::models::sparse::StandardRewardModel BinaryPomdpTransformer::transformRewardModel(storm::models::sparse::Pomdp const& pomdp, storm::models::sparse::StandardRewardModel const& rewardModel, TransformationData const& data) const { boost::optional> stateRewards, actionRewards; if (rewardModel.hasStateRewards()) { stateRewards = rewardModel.getStateRewardVector(); stateRewards.get().resize(data.simpleMatrix.getRowGroupCount(), storm::utility::zero()); } if (rewardModel.hasStateActionRewards()) { actionRewards = std::vector(data.simpleMatrix.getRowCount(), storm::utility::zero()); for (uint64_t pomdpChoice = 0; pomdpChoice < pomdp.getNumberOfChoices(); ++pomdpChoice) { STORM_LOG_ASSERT(data.originalToSimpleChoiceMap[pomdpChoice] < data.simpleMatrix.getRowCount(), "Invalid entry in map for choice " << pomdpChoice); actionRewards.get()[data.originalToSimpleChoiceMap[pomdpChoice]] = rewardModel.getStateActionReward(pomdpChoice); } } STORM_LOG_THROW(!rewardModel.hasTransitionRewards(), storm::exceptions::NotSupportedException, "Transition rewards are currently not supported."); return storm::models::sparse::StandardRewardModel(std::move(stateRewards), std::move(actionRewards)); } template class BinaryPomdpTransformer; template class BinaryPomdpTransformer; template class BinaryPomdpTransformer; } }