From 812b101c40c9077fc3a56edff439a32342e7ad5d Mon Sep 17 00:00:00 2001 From: sjunges Date: Mon, 10 Aug 2015 16:15:09 +0200 Subject: [PATCH 1/2] better program checks, some extensions in model and matrix Former-commit-id: 8efaaf2ca9902278654603ede3eddd7ffa8fbf90 --- src/models/sparse/Mdp.cpp | 12 ++ src/models/sparse/Mdp.h | 8 + src/models/sparse/Model.cpp | 5 + src/models/sparse/Model.h | 12 +- src/storage/SparseMatrix.cpp | 8 + src/storage/SparseMatrix.h | 8 + .../expressions/IfThenElseExpression.cpp | 2 +- src/storage/prism/Program.cpp | 155 +++++++++++++----- src/storage/prism/Program.h | 24 ++- src/utility/cli.h | 113 ++++++------- 10 files changed, 245 insertions(+), 102 deletions(-) diff --git a/src/models/sparse/Mdp.cpp b/src/models/sparse/Mdp.cpp index 1c8bd1b50..469dfe54e 100644 --- a/src/models/sparse/Mdp.cpp +++ b/src/models/sparse/Mdp.cpp @@ -72,9 +72,21 @@ namespace storm { this->hasStateRewards() ? boost::optional>(this->getStateRewardVector()) : boost::optional>(), this->hasTransitionRewards() ? boost::optional>(this->getTransitionRewardMatrix()) : boost::optional>(), boost::optional>(newChoiceLabeling)); + return restrictedMdp; } + template + Mdp Mdp::restrictActions(storm::storage::BitVector const& enabledActions) const { + storm::storage::SparseMatrix restrictedTransitions = this->getTransitionMatrix().restrictRows(enabledActions); + if(this->hasTransitionRewards()) { + return Mdp(restrictedTransitions, this->getStateLabeling(), this->getOptionalStateRewardVector(), boost::optional>(this->getTransitionRewardMatrix().restrictRows(enabledActions)), this->getOptionalChoiceLabeling()); + } else { + return Mdp( restrictedTransitions, this->getStateLabeling(), this->getOptionalStateRewardVector(), boost::optional>(), this->getOptionalChoiceLabeling()); + } + + } + template bool Mdp::checkValidityOfProbabilityMatrix() const { storm::utility::ConstantsComparator comparator; diff --git a/src/models/sparse/Mdp.h b/src/models/sparse/Mdp.h index 18879ee76..3b66d0e61 100644 --- a/src/models/sparse/Mdp.h +++ b/src/models/sparse/Mdp.h @@ -63,6 +63,14 @@ namespace storm { */ Mdp restrictChoiceLabels(LabelSet const& enabledChoiceLabels) const; + /*! + * Constructs an MDP by copying the current MDP and restricting the choices of each state to the ones given by the bitvector. + * + * @param enabledActions A BitVector of lenght numberOfChoices(), which is one iff the action should be kept. + * @return A subMDP. + */ + Mdp restrictActions(storm::storage::BitVector const& enabledActions) const; + private: /*! * Checks the probability matrix for validity. diff --git a/src/models/sparse/Model.cpp b/src/models/sparse/Model.cpp index b2ca03c1d..506083cbc 100644 --- a/src/models/sparse/Model.cpp +++ b/src/models/sparse/Model.cpp @@ -113,6 +113,11 @@ namespace storm { return choiceLabeling.get(); } + template + boost::optional> const& Model::getOptionalChoiceLabeling() const { + return choiceLabeling; + } + template storm::models::sparse::StateLabeling const& Model::getStateLabeling() const { return stateLabeling; diff --git a/src/models/sparse/Model.h b/src/models/sparse/Model.h index 5457370e6..4d1901d1f 100644 --- a/src/models/sparse/Model.h +++ b/src/models/sparse/Model.h @@ -173,13 +173,20 @@ namespace storm { boost::optional> const& getOptionalStateRewardVector() const; /*! - * Retrieves the labels for the choices of the model. Note that calling this method is only valud if the - * model has a choice labling. + * Retrieves the labels for the choices of the model. Note that calling this method is only valid if the + * model has a choice labeling. * * @return The labels for the choices of the model. */ std::vector const& getChoiceLabeling() const; + /*! + * Retrieves an optional value that contains the choice labeling if there is one. + * + * @return The labels for the choices, if they're saved. + */ + boost::optional> const& getOptionalChoiceLabeling() const; + /*! * Returns the state labeling associated with this model. * @@ -194,6 +201,7 @@ namespace storm { */ storm::models::sparse::StateLabeling& getStateLabeling(); + /*! * Retrieves whether this model has state rewards. * diff --git a/src/storage/SparseMatrix.cpp b/src/storage/SparseMatrix.cpp index ce5419099..2cb9f995b 100644 --- a/src/storage/SparseMatrix.cpp +++ b/src/storage/SparseMatrix.cpp @@ -614,6 +614,14 @@ namespace storm { return matrixBuilder.build(); } + template + SparseMatrix SparseMatrix::restrictRows(storm::storage::BitVector const& rowsToKeep) const { + // For now, we use the expensive call to submatrix. + SparseMatrix res(getSubmatrix(false, rowsToKeep, storm::storage::BitVector(getColumnCount(), true), false)); + assert(getRowGroupCount() == res.getRowGroupCount()); + return res; + } + template SparseMatrix SparseMatrix::selectRowsFromRowGroups(std::vector const& rowGroupToRowIndexMapping, bool insertDiagonalEntries) const { // First, we need to count how many non-zero entries the resulting matrix will have and reserve space for diff --git a/src/storage/SparseMatrix.h b/src/storage/SparseMatrix.h index 35ff09658..7d7092377 100644 --- a/src/storage/SparseMatrix.h +++ b/src/storage/SparseMatrix.h @@ -610,6 +610,14 @@ namespace storm { */ SparseMatrix getSubmatrix(bool useGroups, storm::storage::BitVector const& rowConstraint, storm::storage::BitVector const& columnConstraint, bool insertDiagonalEntries = false) const; + /*! + * Restrict rows in grouped rows matrix. Ensures that the number of groups stays the same. + * + * @param rowsToKeep A bit vector indicating which rows to keep. + * + */ + SparseMatrix restrictRows(storm::storage::BitVector const& rowsToKeep) const; + /*! * Selects exactly one row from each row group of this matrix and returns the resulting matrix. * diff --git a/src/storage/expressions/IfThenElseExpression.cpp b/src/storage/expressions/IfThenElseExpression.cpp index f3e65a94f..4692675bf 100644 --- a/src/storage/expressions/IfThenElseExpression.cpp +++ b/src/storage/expressions/IfThenElseExpression.cpp @@ -70,7 +70,7 @@ namespace storm { } std::shared_ptr IfThenElseExpression::simplify() const { - std::shared_ptr conditionSimplified; + std::shared_ptr conditionSimplified = this->condition->simplify(); if (conditionSimplified->isTrue()) { return this->thenExpression->simplify(); } else if (conditionSimplified->isFalse()) { diff --git a/src/storage/prism/Program.cpp b/src/storage/prism/Program.cpp index 0b61fd795..3792bea94 100644 --- a/src/storage/prism/Program.cpp +++ b/src/storage/prism/Program.cpp @@ -12,7 +12,16 @@ namespace storm { namespace prism { - Program::Program(std::shared_ptr manager, ModelType modelType, std::vector const& constants, std::vector const& globalBooleanVariables, std::vector const& globalIntegerVariables, std::vector const& formulas, std::vector const& modules, std::map const& actionToIndexMap, std::vector const& rewardModels, bool fixInitialConstruct, storm::prism::InitialConstruct const& initialConstruct, std::vector