Browse Source

working on making model composition in PRISM work again

Former-commit-id: 080a6d80cd
tempestpy_adaptions
dehnert 9 years ago
parent
commit
ebb47aaa13
  1. 206
      src/builder/DdPrismModelBuilder.cpp
  2. 12
      src/builder/DdPrismModelBuilder.h
  3. 2
      src/storage/prism/InterleavingParallelComposition.cpp
  4. 4
      src/storage/prism/InterleavingParallelComposition.h
  5. 27
      src/storage/prism/Program.cpp
  6. 7
      src/storage/prism/Program.h
  7. 2
      src/storage/prism/RestrictedParallelComposition.cpp
  8. 2
      src/storage/prism/RestrictedParallelComposition.h
  9. 2
      src/storage/prism/SynchronizingParallelComposition.cpp
  10. 4
      src/storage/prism/SynchronizingParallelComposition.h

206
src/builder/DdPrismModelBuilder.cpp

@ -1,5 +1,7 @@
#include "src/builder/DdPrismModelBuilder.h" #include "src/builder/DdPrismModelBuilder.h"
#include <boost/algorithm/string/join.hpp>
#include "src/models/symbolic/Dtmc.h" #include "src/models/symbolic/Dtmc.h"
#include "src/models/symbolic/Ctmc.h" #include "src/models/symbolic/Ctmc.h"
#include "src/models/symbolic/Mdp.h" #include "src/models/symbolic/Mdp.h"
@ -9,12 +11,13 @@
#include "src/settings/SettingsManager.h" #include "src/settings/SettingsManager.h"
#include "src/exceptions/InvalidStateException.h" #include "src/exceptions/InvalidStateException.h"
#include "src/exceptions/NotSupportedException.h"
#include "src/exceptions/InvalidArgumentException.h" #include "src/exceptions/InvalidArgumentException.h"
#include "src/utility/prism.h" #include "src/utility/prism.h"
#include "src/utility/math.h" #include "src/utility/math.h"
#include "src/storage/prism/Program.h" #include "src/storage/prism/Program.h"
#include "src/storage/prism/Compositions.h"
#include "src/storage/dd/Add.h" #include "src/storage/dd/Add.h"
#include "src/storage/dd/cudd/CuddAddIterator.h" #include "src/storage/dd/cudd/CuddAddIterator.h"
@ -189,6 +192,150 @@ namespace storm {
} }
}; };
template <storm::dd::DdType Type, typename ValueType>
class ModuleComposer : public storm::prism::CompositionVisitor {
public:
ModuleComposer(typename DdPrismModelBuilder<Type, ValueType>::GenerationInformation& generationInfo) : generationInfo(generationInfo) {
for (auto const& actionIndex : generationInfo.program.getSynchronizingActionIndices()) {
synchronizingActionToOffsetMap[actionIndex] = 0;
}
}
typename DdPrismModelBuilder<Type, ValueType>::ModuleDecisionDiagram compose(storm::prism::Composition const& composition) {
std::cout << "composing the system " << composition << std::endl;
return boost::any_cast<typename DdPrismModelBuilder<Type, ValueType>::ModuleDecisionDiagram>(composition.accept(*this));
}
virtual boost::any visit(storm::prism::ModuleComposition const& composition) override {
STORM_LOG_TRACE("Translating module '" << composition.getModuleName() << "'.");
typename DdPrismModelBuilder<Type, ValueType>::ModuleDecisionDiagram result = DdPrismModelBuilder<Type, ValueType>::createModuleDecisionDiagram(generationInfo, generationInfo.program.getModule(composition.getModuleName()), synchronizingActionToOffsetMap);
// Update the offset indices.
for (auto const& actionIndex : generationInfo.program.getSynchronizingActionIndices()) {
if (result.hasSynchronizingAction(actionIndex)) {
synchronizingActionToOffsetMap[actionIndex] = result.synchronizingActionToDecisionDiagramMap[actionIndex].numberOfUsedNondeterminismVariables;
}
}
return result;
}
virtual boost::any visit(storm::prism::RenamingComposition const& composition) override {
STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Renaming is currently not supported in symbolic model building.");
}
virtual boost::any visit(storm::prism::HidingComposition const& composition) override {
STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Hiding is currently not supported in symbolic model building.");
}
virtual boost::any visit(storm::prism::SynchronizingParallelComposition const& composition) override {
// First, we translate the subcompositions.
typename DdPrismModelBuilder<Type, ValueType>::ModuleDecisionDiagram left = boost::any_cast<typename DdPrismModelBuilder<Type, ValueType>::ModuleDecisionDiagram>(composition.getLeftSubcomposition().accept(*this));
typename DdPrismModelBuilder<Type, ValueType>::ModuleDecisionDiagram right = boost::any_cast<typename DdPrismModelBuilder<Type, ValueType>::ModuleDecisionDiagram>(composition.getRightSubcomposition().accept(*this));
// Then, determine the action indices on which we need to synchronize.
std::set<uint_fast64_t> leftSynchronizationActionIndices = left.getSynchronizingActionIndices();
for (auto const& entry : leftSynchronizationActionIndices) {
std::cout << "entry 1: " << entry << std::endl;
}
std::set<uint_fast64_t> rightSynchronizationActionIndices = right.getSynchronizingActionIndices();
for (auto const& entry : rightSynchronizationActionIndices) {
std::cout << "entry 2: " << entry << std::endl;
}
std::set<uint_fast64_t> synchronizationActionIndices;
std::set_intersection(leftSynchronizationActionIndices.begin(), leftSynchronizationActionIndices.end(), rightSynchronizationActionIndices.begin(), rightSynchronizationActionIndices.end(), std::inserter(synchronizationActionIndices, synchronizationActionIndices.begin()));
// Finally, we compose the subcompositions to create the result. For this, we modify the left
// subcomposition in place and later return it.
composeInParallel(left, right, synchronizationActionIndices);
return left;
}
virtual boost::any visit(storm::prism::InterleavingParallelComposition const& composition) override {
STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Interleaving is currently not supported in symbolic model building.");
}
virtual boost::any visit(storm::prism::RestrictedParallelComposition const& composition) override {
STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "Restricted parallel composition is currently not supported in symbolic model building.");
}
private:
/*!
* Composes the given modules while synchronizing over the provided action indices. As a result, the first
* module is modified in place and will contain the composition after a call to this method.
*/
void composeInParallel(typename DdPrismModelBuilder<Type, ValueType>::ModuleDecisionDiagram& left, typename DdPrismModelBuilder<Type, ValueType>::ModuleDecisionDiagram& right, std::set<uint_fast64_t> const& synchronizationActionIndices) {
std::vector<std::string> actionNames;
for (auto const& actionIndex : synchronizationActionIndices) {
actionNames.push_back(generationInfo.program.getActionName(actionIndex));
}
STORM_LOG_TRACE("Composing two modules over the actions '" << boost::join(actionNames, ", ") << "'.");
// Combine the tau action.
uint_fast64_t numberOfUsedNondeterminismVariables = right.independentAction.numberOfUsedNondeterminismVariables;
left.independentAction = DdPrismModelBuilder<Type, ValueType>::combineUnsynchronizedActions(generationInfo, left.independentAction, right.independentAction, left.identity, right.identity);
numberOfUsedNondeterminismVariables = std::max(numberOfUsedNondeterminismVariables, left.independentAction.numberOfUsedNondeterminismVariables);
// Create an empty action for the case where one of the modules does not have a certain action.
typename DdPrismModelBuilder<Type, ValueType>::ActionDecisionDiagram emptyAction(*generationInfo.manager);
// Treat all non-tau actions of the left module.
for (auto& action : left.synchronizingActionToDecisionDiagramMap) {
// If we need to synchronize over this action index, we try to do so now.
if (synchronizationActionIndices.find(action.first) != synchronizationActionIndices.end()) {
// If we are to synchronize over an action that does not exist in the second module, the result
// is that the synchronization is the empty action.
if (right.hasSynchronizingAction(action.first)) {
action.second = emptyAction;
} else {
// Otherwise, the actions of the modules are synchronized.
action.second = DdPrismModelBuilder<Type, ValueType>::combineSynchronizingActions(generationInfo, action.second, right.synchronizingActionToDecisionDiagramMap[action.first]);
}
} else {
// If we don't synchronize over this action, we need to construct the interleaving.
// If both modules contain the action, we need to mutually multiply the other identity.
if (right.hasSynchronizingAction(action.first)) {
action.second = DdPrismModelBuilder<Type, ValueType>::combineUnsynchronizedActions(generationInfo, action.second, right.synchronizingActionToDecisionDiagramMap[action.first], left.identity, right.identity);
} else {
// If only the first module has this action, we need to use a dummy action decision diagram
// for the second module.
action.second = DdPrismModelBuilder<Type, ValueType>::combineUnsynchronizedActions(generationInfo, action.second, emptyAction, left.identity, right.identity);
}
}
numberOfUsedNondeterminismVariables = std::max(numberOfUsedNondeterminismVariables, action.second.numberOfUsedNondeterminismVariables);
}
// Treat all non-tau actions of the right module.
for (auto const& actionIndex : right.getSynchronizingActionIndices()) {
// Here, we only need to treat actions that the first module does not have, because we have handled
// this case earlier.
if (!left.hasSynchronizingAction(actionIndex)) {
if (synchronizationActionIndices.find(actionIndex) != synchronizationActionIndices.end()) {
// If we are to synchronize over this action that does not exist in the first module, the
// result is that the synchronization is the empty action.
left.synchronizingActionToDecisionDiagramMap[actionIndex] = emptyAction;
} else {
// If only the second module has this action, we need to use a dummy action decision diagram
// for the first module.
left.synchronizingActionToDecisionDiagramMap[actionIndex] = DdPrismModelBuilder<Type, ValueType>::combineUnsynchronizedActions(generationInfo, emptyAction, right.synchronizingActionToDecisionDiagramMap[actionIndex], left.identity, right.identity);
}
}
numberOfUsedNondeterminismVariables = std::max(numberOfUsedNondeterminismVariables, left.synchronizingActionToDecisionDiagramMap[actionIndex].numberOfUsedNondeterminismVariables);
}
// Combine identity matrices.
left.identity = left.identity * right.identity;
// Keep track of the number of nondeterminism variables used.
left.numberOfUsedNondeterminismVariables = std::max(left.numberOfUsedNondeterminismVariables, numberOfUsedNondeterminismVariables);
}
typename DdPrismModelBuilder<Type, ValueType>::GenerationInformation& generationInfo;
std::map<uint_fast64_t, uint_fast64_t> synchronizingActionToOffsetMap;
};
template <storm::dd::DdType Type, typename ValueType> template <storm::dd::DdType Type, typename ValueType>
DdPrismModelBuilder<Type, ValueType>::Options::Options() : buildAllRewardModels(true), rewardModelsToBuild(), constantDefinitions(), buildAllLabels(true), labelsToBuild(), terminalStates(), negatedTerminalStates() { DdPrismModelBuilder<Type, ValueType>::Options::Options() : buildAllRewardModels(true), rewardModelsToBuild(), constantDefinitions(), buildAllLabels(true), labelsToBuild(), terminalStates(), negatedTerminalStates() {
// Intentionally left empty. // Intentionally left empty.
@ -789,61 +936,8 @@ namespace storm {
template <storm::dd::DdType Type, typename ValueType> template <storm::dd::DdType Type, typename ValueType>
typename DdPrismModelBuilder<Type, ValueType>::SystemResult DdPrismModelBuilder<Type, ValueType>::createSystemDecisionDiagram(GenerationInformation& generationInfo) { typename DdPrismModelBuilder<Type, ValueType>::SystemResult DdPrismModelBuilder<Type, ValueType>::createSystemDecisionDiagram(GenerationInformation& generationInfo) {
// Create the initial offset mapping.
std::map<uint_fast64_t, uint_fast64_t> synchronizingActionToOffsetMap;
for (auto const& actionIndex : generationInfo.program.getSynchronizingActionIndices()) {
synchronizingActionToOffsetMap[actionIndex] = 0;
}
// Start by creating DDs for the first module.
STORM_LOG_TRACE("Translating (first) module '" << generationInfo.program.getModule(0).getName() << "'.");
ModuleDecisionDiagram system = createModuleDecisionDiagram(generationInfo, generationInfo.program.getModule(0), synchronizingActionToOffsetMap);
// Now translate module by module and combine it with the system created thus far.
for (uint_fast64_t i = 1; i < generationInfo.program.getNumberOfModules(); ++i) {
storm::prism::Module const& currentModule = generationInfo.program.getModule(i);
STORM_LOG_TRACE("Translating module '" << currentModule.getName() << "'.");
// Update the offset index.
for (auto const& actionIndex : generationInfo.program.getSynchronizingActionIndices()) {
if (system.hasSynchronizingAction(actionIndex)) {
synchronizingActionToOffsetMap[actionIndex] = system.synchronizingActionToDecisionDiagramMap[actionIndex].numberOfUsedNondeterminismVariables;
}
}
ModuleDecisionDiagram currentModuleDd = createModuleDecisionDiagram(generationInfo, currentModule, synchronizingActionToOffsetMap);
// Combine the non-synchronizing action.
uint_fast64_t numberOfUsedNondeterminismVariables = currentModuleDd.numberOfUsedNondeterminismVariables;
system.independentAction = combineUnsynchronizedActions(generationInfo, system.independentAction, currentModuleDd.independentAction, system.identity, currentModuleDd.identity);
numberOfUsedNondeterminismVariables = std::max(numberOfUsedNondeterminismVariables, system.independentAction.numberOfUsedNondeterminismVariables);
ActionDecisionDiagram emptyAction(*generationInfo.manager);
// For all synchronizing actions that the next module does not have, we need to multiply the identity of the next module.
for (auto& action : system.synchronizingActionToDecisionDiagramMap) {
if (!currentModuleDd.hasSynchronizingAction(action.first)) {
action.second = combineUnsynchronizedActions(generationInfo, action.second, emptyAction, system.identity, currentModuleDd.identity);
}
}
// Combine synchronizing actions.
for (auto const& actionIndex : currentModule.getSynchronizingActionIndices()) {
if (system.hasSynchronizingAction(actionIndex)) {
system.synchronizingActionToDecisionDiagramMap[actionIndex] = combineSynchronizingActions(generationInfo, system.synchronizingActionToDecisionDiagramMap[actionIndex], currentModuleDd.synchronizingActionToDecisionDiagramMap[actionIndex]);
numberOfUsedNondeterminismVariables = std::max(numberOfUsedNondeterminismVariables, system.synchronizingActionToDecisionDiagramMap[actionIndex].numberOfUsedNondeterminismVariables);
} else {
system.synchronizingActionToDecisionDiagramMap[actionIndex] = combineUnsynchronizedActions(generationInfo, emptyAction, currentModuleDd.synchronizingActionToDecisionDiagramMap[actionIndex], system.identity, currentModuleDd.identity);
numberOfUsedNondeterminismVariables = std::max(numberOfUsedNondeterminismVariables, system.synchronizingActionToDecisionDiagramMap[actionIndex].numberOfUsedNondeterminismVariables);
}
}
// Combine identity matrices.
system.identity = system.identity * currentModuleDd.identity;
// Keep track of the number of nondeterminism variables used.
system.numberOfUsedNondeterminismVariables = std::max(system.numberOfUsedNondeterminismVariables, numberOfUsedNondeterminismVariables);
}
ModuleComposer<Type, ValueType> composer(generationInfo);
ModuleDecisionDiagram system = composer.compose(generationInfo.program.specifiesSystemComposition() ? generationInfo.program.getSystemCompositionConstruct().getSystemComposition() : *generationInfo.program.getDefaultSystemComposition());
storm::dd::Add<Type, ValueType> result = createSystemFromModule(generationInfo, system); storm::dd::Add<Type, ValueType> result = createSystemFromModule(generationInfo, system);

12
src/builder/DdPrismModelBuilder.h

@ -188,6 +188,14 @@ namespace storm {
return synchronizingActionToDecisionDiagramMap.find(actionIndex) != synchronizingActionToDecisionDiagramMap.end(); return synchronizingActionToDecisionDiagramMap.find(actionIndex) != synchronizingActionToDecisionDiagramMap.end();
} }
std::set<uint_fast64_t> getSynchronizingActionIndices() const {
std::set<uint_fast64_t> result;
for (auto const& entry : synchronizingActionToDecisionDiagramMap) {
result.insert(entry.first);
}
return result;
}
// The decision diagram for the independent action. // The decision diagram for the independent action.
ActionDecisionDiagram independentAction; ActionDecisionDiagram independentAction;
@ -210,7 +218,11 @@ namespace storm {
* Structure to store the result of the system creation phase. * Structure to store the result of the system creation phase.
*/ */
struct SystemResult; struct SystemResult;
private: private:
template <storm::dd::DdType TypePrime, typename ValueTypePrime>
friend class ModuleComposer;
static std::set<storm::expressions::Variable> equalizeAssignedGlobalVariables(GenerationInformation const& generationInfo, ActionDecisionDiagram& action1, ActionDecisionDiagram& action2); static std::set<storm::expressions::Variable> equalizeAssignedGlobalVariables(GenerationInformation const& generationInfo, ActionDecisionDiagram& action1, ActionDecisionDiagram& action2);
static std::set<storm::expressions::Variable> equalizeAssignedGlobalVariables(GenerationInformation const& generationInfo, std::vector<ActionDecisionDiagram>& actionDds); static std::set<storm::expressions::Variable> equalizeAssignedGlobalVariables(GenerationInformation const& generationInfo, std::vector<ActionDecisionDiagram>& actionDds);

2
src/storage/prism/InterleavingParallelComposition.cpp

@ -12,7 +12,7 @@ namespace storm {
} }
void InterleavingParallelComposition::writeToStream(std::ostream& stream) const { void InterleavingParallelComposition::writeToStream(std::ostream& stream) const {
stream << "(" << *left << " ||| " << *right << ")";
stream << "(" << this->getLeftSubcomposition() << " ||| " << this->getRightSubcomposition() << ")";
} }
} }

4
src/storage/prism/InterleavingParallelComposition.h

@ -13,10 +13,6 @@ namespace storm {
protected: protected:
virtual void writeToStream(std::ostream& stream) const override; virtual void writeToStream(std::ostream& stream) const override;
private:
std::shared_ptr<Composition> left;
std::shared_ptr<Composition> right;
}; };
} }
} }

27
src/storage/prism/Program.cpp

@ -27,11 +27,21 @@ namespace storm {
} }
bool isValid(Composition const& composition) { bool isValid(Composition const& composition) {
return boost::any_cast<bool>(composition.accept(*this));
bool isValid = boost::any_cast<bool>(composition.accept(*this));
if (appearingModules.size() != program.getNumberOfModules()) {
isValid = false;
STORM_LOG_THROW(isValid, storm::exceptions::WrongFormatException, "Not every module is used in the system composition.");
}
return isValid;
} }
virtual boost::any visit(ModuleComposition const& composition) override { virtual boost::any visit(ModuleComposition const& composition) override {
return program.hasModule(composition.getModuleName());
bool isValid = program.hasModule(composition.getModuleName());
STORM_LOG_THROW(isValid, storm::exceptions::WrongFormatException, "The module \"" << composition.getModuleName() << "\" referred to in the system composition does not exist.");
isValid = appearingModules.find(composition.getModuleName()) == appearingModules.end();
STORM_LOG_THROW(isValid, storm::exceptions::WrongFormatException, "The module \"" << composition.getModuleName() << "\" is referred to more than once in the system composition.");
appearingModules.insert(composition.getModuleName());
return isValid;
} }
virtual boost::any visit(RenamingComposition const& composition) override { virtual boost::any visit(RenamingComposition const& composition) override {
@ -56,6 +66,7 @@ namespace storm {
private: private:
storm::prism::Program const& program; storm::prism::Program const& program;
std::set<std::string> appearingModules;
}; };
Program::Program(std::shared_ptr<storm::expressions::ExpressionManager> manager, ModelType modelType, std::vector<Constant> const& constants, std::vector<BooleanVariable> const& globalBooleanVariables, std::vector<IntegerVariable> const& globalIntegerVariables, std::vector<Formula> const& formulas, std::vector<Module> const& modules, std::map<std::string, uint_fast64_t> const& actionToIndexMap, std::vector<RewardModel> const& rewardModels, std::vector<Label> const& labels, boost::optional<InitialConstruct> const& initialConstruct, boost::optional<SystemCompositionConstruct> const& compositionConstruct, std::string const& filename, uint_fast64_t lineNumber, bool finalModel) Program::Program(std::shared_ptr<storm::expressions::ExpressionManager> manager, ModelType modelType, std::vector<Constant> const& constants, std::vector<BooleanVariable> const& globalBooleanVariables, std::vector<IntegerVariable> const& globalIntegerVariables, std::vector<Formula> const& formulas, std::vector<Module> const& modules, std::map<std::string, uint_fast64_t> const& actionToIndexMap, std::vector<RewardModel> const& rewardModels, std::vector<Label> const& labels, boost::optional<InitialConstruct> const& initialConstruct, boost::optional<SystemCompositionConstruct> const& compositionConstruct, std::string const& filename, uint_fast64_t lineNumber, bool finalModel)
@ -352,6 +363,18 @@ namespace storm {
return systemCompositionConstruct; return systemCompositionConstruct;
} }
std::shared_ptr<Composition> Program::getDefaultSystemComposition() const {
std::shared_ptr<Composition> current = std::make_shared<ModuleComposition>(this->modules.front().getName());
for (uint_fast64_t index = 1; index < this->modules.size(); ++index) {
std::shared_ptr<Composition> newComposition = std::make_shared<SynchronizingParallelComposition>(current, std::make_shared<ModuleComposition>(this->modules[index].getName()));
current = newComposition;
}
return current;
}
std::set<std::string> const& Program::getActions() const { std::set<std::string> const& Program::getActions() const {
return this->actions; return this->actions;
} }

7
src/storage/prism/Program.h

@ -296,6 +296,13 @@ namespace storm {
*/ */
boost::optional<SystemCompositionConstruct> getOptionalSystemCompositionConstruct() const; boost::optional<SystemCompositionConstruct> getOptionalSystemCompositionConstruct() const;
/*!
* Retrieves the default system composition for this program.
*
* @return The default system composition.
*/
std::shared_ptr<Composition> getDefaultSystemComposition() const;
/*! /*!
* Retrieves the set of actions present in the program. * Retrieves the set of actions present in the program.
* *

2
src/storage/prism/RestrictedParallelComposition.cpp

@ -14,7 +14,7 @@ namespace storm {
} }
void RestrictedParallelComposition::writeToStream(std::ostream& stream) const { void RestrictedParallelComposition::writeToStream(std::ostream& stream) const {
stream << "(" << *left << " |[" << boost::algorithm::join(synchronizingActions, ", ") << "]| " << *right << ")";
stream << "(" << this->getLeftSubcomposition() << " |[" << boost::algorithm::join(synchronizingActions, ", ") << "]| " << this->getRightSubcomposition() << ")";
} }
} }

2
src/storage/prism/RestrictedParallelComposition.h

@ -18,9 +18,7 @@ namespace storm {
virtual void writeToStream(std::ostream& stream) const override; virtual void writeToStream(std::ostream& stream) const override;
private: private:
std::shared_ptr<Composition> left;
std::set<std::string> synchronizingActions; std::set<std::string> synchronizingActions;
std::shared_ptr<Composition> right;
}; };
} }
} }

2
src/storage/prism/SynchronizingParallelComposition.cpp

@ -12,7 +12,7 @@ namespace storm {
} }
void SynchronizingParallelComposition::writeToStream(std::ostream& stream) const { void SynchronizingParallelComposition::writeToStream(std::ostream& stream) const {
stream << "(" << *left << " || " << *right << ")";
stream << "(" << this->getLeftSubcomposition() << " || " << this->getRightSubcomposition() << ")";
} }
} }

4
src/storage/prism/SynchronizingParallelComposition.h

@ -13,10 +13,6 @@ namespace storm {
protected: protected:
virtual void writeToStream(std::ostream& stream) const override; virtual void writeToStream(std::ostream& stream) const override;
private:
std::shared_ptr<Composition> left;
std::shared_ptr<Composition> right;
}; };
} }
} }

Loading…
Cancel
Save