#include "storm/storage/dd/sylvan/InternalSylvanBdd.h" #include #include "storm/storage/dd/sylvan/InternalSylvanDdManager.h" #include "storm/storage/dd/sylvan/InternalSylvanAdd.h" #include "storm/storage/dd/sylvan/SylvanAddIterator.h" #include "storm/storage/BitVector.h" #include "storm/storage/PairHash.h" #include "storm/utility/macros.h" #include "storm/exceptions/InvalidOperationException.h" #include "storm/exceptions/NotSupportedException.h" #include "storm/adapters/CarlAdapter.h" #include "storm-config.h" namespace storm { namespace dd { InternalBdd::InternalBdd(InternalDdManager const* ddManager, sylvan::Bdd const& sylvanBdd) : ddManager(ddManager), sylvanBdd(sylvanBdd) { // Intentionally left empty. } template InternalBdd InternalBdd::fromVector(InternalDdManager const* ddManager, std::vector const& values, Odd const& odd, std::vector const& sortedDdVariableIndices, std::function const& filter) { uint_fast64_t offset = 0; return InternalBdd(ddManager, sylvan::Bdd(fromVectorRec(offset, 0, sortedDdVariableIndices.size(), values, odd, sortedDdVariableIndices, filter))); } template BDD InternalBdd::fromVectorRec(uint_fast64_t& currentOffset, uint_fast64_t currentLevel, uint_fast64_t maxLevel, std::vector const& values, Odd const& odd, std::vector const& ddVariableIndices, std::function const& filter) { if (currentLevel == maxLevel) { // If we are in a terminal node of the ODD, we need to check whether the then-offset of the ODD is one // (meaning the encoding is a valid one) or zero (meaning the encoding is not valid). Consequently, we // need to copy the next value of the vector iff the then-offset is greater than zero. if (odd.getThenOffset() > 0) { if (filter(values[currentOffset++])) { return sylvan_true; } else { return sylvan_false; } } else { return sylvan_false; } } else { // If the total offset is zero, we can just return the constant zero DD. if (odd.getThenOffset() + odd.getElseOffset() == 0) { return sylvan_false; } // Determine the new else-successor. BDD elseSuccessor; if (odd.getElseOffset() > 0) { elseSuccessor = fromVectorRec(currentOffset, currentLevel + 1, maxLevel, values, odd.getElseSuccessor(), ddVariableIndices, filter); } else { elseSuccessor = sylvan_false; } bdd_refs_push(elseSuccessor); // Determine the new then-successor. BDD thenSuccessor; if (odd.getThenOffset() > 0) { thenSuccessor = fromVectorRec(currentOffset, currentLevel + 1, maxLevel, values, odd.getThenSuccessor(), ddVariableIndices, filter); } else { thenSuccessor = sylvan_false; } bdd_refs_push(thenSuccessor); // Create a node representing ITE(currentVar, thenSuccessor, elseSuccessor); BDD currentVar = sylvan_ithvar(static_cast(ddVariableIndices[currentLevel])); bdd_refs_push(currentVar); LACE_ME; BDD result = sylvan_ite(currentVar, thenSuccessor, elseSuccessor); // Dispose of the intermediate results. bdd_refs_pop(3); return result; } } bool InternalBdd::operator==(InternalBdd const& other) const { return sylvanBdd == other.sylvanBdd; } bool InternalBdd::operator!=(InternalBdd const& other) const { return sylvanBdd != other.sylvanBdd; } InternalBdd InternalBdd::relationalProduct(InternalBdd const& relation, std::vector> const&, std::vector> const&) const { return InternalBdd(ddManager, this->sylvanBdd.RelNext(relation.sylvanBdd, sylvan::Bdd(sylvan_false))); } InternalBdd InternalBdd::inverseRelationalProduct(InternalBdd const& relation, std::vector> const&, std::vector> const&) const { return InternalBdd(ddManager, this->sylvanBdd.RelPrev(relation.sylvanBdd, sylvan::Bdd(sylvan_false))); } InternalBdd InternalBdd::inverseRelationalProductWithExtendedRelation(InternalBdd const& relation, std::vector> const& rowVariables, std::vector> const& columnVariables) const { // Currently, there is no specialized version to perform this operation, so we fall back to the regular operations. InternalBdd columnCube = ddManager->getBddOne(); for (auto const& variable : columnVariables) { columnCube &= variable; } return this->swapVariables(rowVariables, columnVariables).andExists(relation, columnCube); } InternalBdd InternalBdd::ite(InternalBdd const& thenDd, InternalBdd const& elseDd) const { return InternalBdd(ddManager, this->sylvanBdd.Ite(thenDd.sylvanBdd, elseDd.sylvanBdd)); } template InternalAdd InternalBdd::ite(InternalAdd const& thenAdd, InternalAdd const& elseAdd) const { return InternalAdd(ddManager, this->sylvanBdd.Ite(thenAdd.getSylvanMtbdd(), elseAdd.getSylvanMtbdd())); } InternalBdd InternalBdd::operator||(InternalBdd const& other) const { return InternalBdd(ddManager, this->sylvanBdd | other.sylvanBdd); } InternalBdd& InternalBdd::operator|=(InternalBdd const& other) { this->sylvanBdd |= other.sylvanBdd; return *this; } InternalBdd InternalBdd::operator&&(InternalBdd const& other) const { return InternalBdd(ddManager, this->sylvanBdd & other.sylvanBdd); } InternalBdd& InternalBdd::operator&=(InternalBdd const& other) { this->sylvanBdd &= other.sylvanBdd; return *this; } InternalBdd InternalBdd::iff(InternalBdd const& other) const { return InternalBdd(ddManager, !(this->sylvanBdd ^ other.sylvanBdd)); } InternalBdd InternalBdd::exclusiveOr(InternalBdd const& other) const { return InternalBdd(ddManager, this->sylvanBdd ^ other.sylvanBdd); } InternalBdd InternalBdd::implies(InternalBdd const& other) const { return InternalBdd(ddManager, (!this->sylvanBdd) | other.sylvanBdd); } InternalBdd InternalBdd::operator!() const { return InternalBdd(ddManager, !this->sylvanBdd); } InternalBdd& InternalBdd::complement() { this->sylvanBdd = !this->sylvanBdd; return *this; } InternalBdd InternalBdd::existsAbstract(InternalBdd const& cube) const { return InternalBdd(ddManager, this->sylvanBdd.ExistAbstract(cube.sylvanBdd)); } InternalBdd InternalBdd::existsAbstractRepresentative(InternalBdd const& cube) const { return InternalBdd(ddManager, this->sylvanBdd.ExistAbstractRepresentative(cube.sylvanBdd)); } InternalBdd InternalBdd::universalAbstract(InternalBdd const& cube) const { return InternalBdd(ddManager, this->sylvanBdd.UnivAbstract(cube.sylvanBdd)); } InternalBdd InternalBdd::andExists(InternalBdd const& other, InternalBdd const& cube) const { return InternalBdd(ddManager, this->sylvanBdd.AndAbstract(other.sylvanBdd, cube.sylvanBdd)); } InternalBdd InternalBdd::constrain(InternalBdd const& constraint) const { return InternalBdd(ddManager, this->sylvanBdd.Constrain(constraint.sylvanBdd)); } InternalBdd InternalBdd::restrict(InternalBdd const& constraint) const { return InternalBdd(ddManager, this->sylvanBdd.Restrict(constraint.sylvanBdd)); } InternalBdd InternalBdd::swapVariables(std::vector> const& from, std::vector> const& to) const { std::vector fromIndices; std::vector toIndices; for (auto it1 = from.begin(), ite1 = from.end(), it2 = to.begin(); it1 != ite1; ++it1, ++it2) { fromIndices.push_back(it1->getIndex()); fromIndices.push_back(it2->getIndex()); toIndices.push_back(it2->getIndex()); toIndices.push_back(it1->getIndex()); } return InternalBdd(ddManager, this->sylvanBdd.Permute(fromIndices, toIndices)); } InternalBdd InternalBdd::getSupport() const { return InternalBdd(ddManager, this->sylvanBdd.Support()); } uint_fast64_t InternalBdd::getNonZeroCount(uint_fast64_t numberOfDdVariables) const { if (numberOfDdVariables == 0) { return 0; } return static_cast(this->sylvanBdd.SatCount(numberOfDdVariables)); } uint_fast64_t InternalBdd::getLeafCount() const { // For BDDs, the leaf count is always one, because the only leaf is the false leaf (and true is represented // by a negation edge to false). return 1; } uint_fast64_t InternalBdd::getNodeCount() const { // We have to add one to also count the false-leaf, which is the only leaf appearing in BDDs. return static_cast(this->sylvanBdd.NodeCount()) + 1; } bool InternalBdd::isOne() const { return this->sylvanBdd.isOne(); } bool InternalBdd::isZero() const { return this->sylvanBdd.isZero(); } uint_fast64_t InternalBdd::getIndex() const { return static_cast(this->sylvanBdd.TopVar()); } uint_fast64_t InternalBdd::getLevel() const { return this->getIndex(); } void InternalBdd::exportToDot(std::string const& filename, std::vector const&) const { FILE* filePointer = fopen(filename.c_str() , "w"); this->sylvanBdd.PrintDot(filePointer); fclose(filePointer); } sylvan::Bdd& InternalBdd::getSylvanBdd() { return sylvanBdd; } sylvan::Bdd const& InternalBdd::getSylvanBdd() const { return sylvanBdd; } template InternalAdd InternalBdd::toAdd() const { if (std::is_same::value) { return InternalAdd(ddManager, this->sylvanBdd.toDoubleMtbdd()); } else if (std::is_same::value) { return InternalAdd(ddManager, this->sylvanBdd.toInt64Mtbdd()); } #ifdef STORM_HAVE_CARL else if (std::is_same::value) { return InternalAdd(ddManager, this->sylvanBdd.toStormRationalFunctionMtbdd()); } #endif else { STORM_LOG_THROW(false, storm::exceptions::InvalidOperationException, "Illegal ADD type."); } } storm::storage::BitVector InternalBdd::toVector(storm::dd::Odd const& rowOdd, std::vector const& ddVariableIndices) const { storm::storage::BitVector result(rowOdd.getTotalOffset()); this->toVectorRec(bdd_regular(this->getSylvanBdd().GetBDD()), result, rowOdd, bdd_isnegated(this->getSylvanBdd().GetBDD()), 0, ddVariableIndices.size(), 0, ddVariableIndices); return result; } void InternalBdd::toVectorRec(BDD dd, storm::storage::BitVector& result, Odd const& rowOdd, bool complement, uint_fast64_t currentRowLevel, uint_fast64_t maxLevel, uint_fast64_t currentRowOffset, std::vector const& ddRowVariableIndices) const { // If there are no more values to select, we can directly return. if (dd == sylvan_false && !complement) { return; } else if (dd == sylvan_true && complement) { return; } // If we are at the maximal level, the value to be set is stored as a constant in the DD. if (currentRowLevel == maxLevel) { result.set(currentRowOffset, true); } else if (ddRowVariableIndices[currentRowLevel] < sylvan_var(dd)) { toVectorRec(dd, result, rowOdd.getElseSuccessor(), complement, currentRowLevel + 1, maxLevel, currentRowOffset, ddRowVariableIndices); toVectorRec(dd, result, rowOdd.getThenSuccessor(), complement, currentRowLevel + 1, maxLevel, currentRowOffset + rowOdd.getElseOffset(), ddRowVariableIndices); } else { // Otherwise, we compute the ODDs for both the then- and else successors. BDD elseDdNode = sylvan_low(dd); BDD thenDdNode = sylvan_high(dd); // Determine whether we have to evaluate the successors as if they were complemented. bool elseComplemented = bdd_isnegated(elseDdNode) ^ complement; bool thenComplemented = bdd_isnegated(thenDdNode) ^ complement; toVectorRec(bdd_regular(elseDdNode), result, rowOdd.getElseSuccessor(), elseComplemented, currentRowLevel + 1, maxLevel, currentRowOffset, ddRowVariableIndices); toVectorRec(bdd_regular(thenDdNode), result, rowOdd.getThenSuccessor(), thenComplemented, currentRowLevel + 1, maxLevel, currentRowOffset + rowOdd.getElseOffset(), ddRowVariableIndices); } } Odd InternalBdd::createOdd(std::vector const& ddVariableIndices) const { // Prepare a unique table for each level that keeps the constructed ODD nodes unique. std::vector, std::shared_ptr, HashFunctor>> uniqueTableForLevels(ddVariableIndices.size() + 1); // Now construct the ODD structure from the BDD. std::shared_ptr rootOdd = createOddRec(bdd_regular(this->getSylvanBdd().GetBDD()), bdd_isnegated(this->getSylvanBdd().GetBDD()), 0, ddVariableIndices.size(), ddVariableIndices, uniqueTableForLevels); // Return a copy of the root node to remove the shared_ptr encapsulation. return Odd(*rootOdd); } std::size_t InternalBdd::HashFunctor::operator()(std::pair const& key) const { std::size_t result = 0; boost::hash_combine(result, key.first); boost::hash_combine(result, key.second); return result; } std::shared_ptr InternalBdd::createOddRec(BDD dd, bool complement, uint_fast64_t currentLevel, uint_fast64_t maxLevel, std::vector const& ddVariableIndices, std::vector, std::shared_ptr, HashFunctor>>& uniqueTableForLevels) { // Check whether the ODD for this node has already been computed (for this level) and if so, return this instead. auto const& iterator = uniqueTableForLevels[currentLevel].find(std::make_pair(dd, complement)); if (iterator != uniqueTableForLevels[currentLevel].end()) { return iterator->second; } else { // Otherwise, we need to recursively compute the ODD. // If we are already at the maximal level that is to be considered, we can simply create an Odd without // successors. if (currentLevel == maxLevel) { uint_fast64_t elseOffset = 0; uint_fast64_t thenOffset = 0; // If the DD is not the zero leaf, then the then-offset is 1. if (dd != mtbdd_false) { thenOffset = 1; } // If we need to complement the 'terminal' node, we need to negate its offset. if (complement) { thenOffset = 1 - thenOffset; } auto oddNode = std::make_shared(nullptr, elseOffset, nullptr, thenOffset); uniqueTableForLevels[currentLevel].emplace(std::make_pair(dd, complement), oddNode); return oddNode; } else if (bdd_isterminal(dd) || ddVariableIndices[currentLevel] < sylvan_var(dd)) { // If we skipped the level in the DD, we compute the ODD just for the else-successor and use the same // node for the then-successor as well. std::shared_ptr elseNode = createOddRec(dd, complement, currentLevel + 1, maxLevel, ddVariableIndices, uniqueTableForLevels); std::shared_ptr thenNode = elseNode; uint_fast64_t totalOffset = elseNode->getElseOffset() + elseNode->getThenOffset(); auto oddNode = std::make_shared(elseNode, totalOffset, thenNode, totalOffset); uniqueTableForLevels[currentLevel].emplace(std::make_pair(dd, complement), oddNode); return oddNode; } else { // Otherwise, we compute the ODDs for both the then- and else successors. BDD thenDdNode = sylvan_high(dd); BDD elseDdNode = sylvan_low(dd); // Determine whether we have to evaluate the successors as if they were complemented. bool elseComplemented = bdd_isnegated(elseDdNode) ^ complement; bool thenComplemented = bdd_isnegated(thenDdNode) ^ complement; std::shared_ptr elseNode = createOddRec(bdd_regular(elseDdNode), elseComplemented, currentLevel + 1, maxLevel, ddVariableIndices, uniqueTableForLevels); std::shared_ptr thenNode = createOddRec(bdd_regular(thenDdNode), thenComplemented, currentLevel + 1, maxLevel, ddVariableIndices, uniqueTableForLevels); auto oddNode = std::make_shared(elseNode, elseNode->getElseOffset() + elseNode->getThenOffset(), thenNode, thenNode->getElseOffset() + thenNode->getThenOffset()); uniqueTableForLevels[currentLevel].emplace(std::make_pair(dd, complement), oddNode); return oddNode; } } } template void InternalBdd::filterExplicitVector(Odd const& odd, std::vector const& ddVariableIndices, std::vector const& sourceValues, std::vector& targetValues) const { uint_fast64_t currentIndex = 0; filterExplicitVectorRec(bdd_regular(this->getSylvanBdd().GetBDD()), 0, bdd_isnegated(this->getSylvanBdd().GetBDD()), ddVariableIndices.size(), ddVariableIndices, 0, odd, targetValues, currentIndex, sourceValues); } template void InternalBdd::filterExplicitVectorRec(BDD dd, uint_fast64_t currentLevel, bool complement, uint_fast64_t maxLevel, std::vector const& ddVariableIndices, uint_fast64_t currentOffset, storm::dd::Odd const& odd, std::vector& result, uint_fast64_t& currentIndex, std::vector const& values) { // If there are no more values to select, we can directly return. if (dd == sylvan_false && !complement) { return; } else if (dd == sylvan_true && complement) { return; } if (currentLevel == maxLevel) { result[currentIndex++] = values[currentOffset]; } else if (ddVariableIndices[currentLevel] < sylvan_var(dd)) { // If we skipped a level, we need to enumerate the explicit entries for the case in which the bit is set // and for the one in which it is not set. filterExplicitVectorRec(dd, currentLevel + 1, complement, maxLevel, ddVariableIndices, currentOffset, odd.getElseSuccessor(), result, currentIndex, values); filterExplicitVectorRec(dd, currentLevel + 1, complement, maxLevel, ddVariableIndices, currentOffset + odd.getElseOffset(), odd.getThenSuccessor(), result, currentIndex, values); } else { // Otherwise, we compute the ODDs for both the then- and else successors. BDD thenDdNode = sylvan_high(dd); BDD elseDdNode = sylvan_low(dd); // Determine whether we have to evaluate the successors as if they were complemented. bool elseComplemented = bdd_isnegated(elseDdNode) ^ complement; bool thenComplemented = bdd_isnegated(thenDdNode) ^ complement; filterExplicitVectorRec(bdd_regular(elseDdNode), currentLevel + 1, elseComplemented, maxLevel, ddVariableIndices, currentOffset, odd.getElseSuccessor(), result, currentIndex, values); filterExplicitVectorRec(bdd_regular(thenDdNode), currentLevel + 1, thenComplemented, maxLevel, ddVariableIndices, currentOffset + odd.getElseOffset(), odd.getThenSuccessor(), result, currentIndex, values); } } std::pair, std::unordered_map> InternalBdd::toExpression(storm::expressions::ExpressionManager& manager) const { std::pair, std::unordered_map> result; // Create (and maintain) a mapping from the DD nodes to a counter that says the how-many-th node (within the // nodes of equal index) the node was. std::unordered_map nodeToCounterMap; std::vector nextCounterForIndex(ddManager->getNumberOfDdVariables(), 0); std::unordered_map, storm::expressions::Variable> countIndexToVariablePair; bool negated = bdd_isnegated(this->getSylvanBdd().GetBDD()); // Translate from the top node downwards. storm::expressions::Variable topVariable = this->toExpressionRec(bdd_regular(this->getSylvanBdd().GetBDD()), manager, result.first, result.second, countIndexToVariablePair, nodeToCounterMap, nextCounterForIndex); // Create the final expression. if (negated) { result.first.push_back(!topVariable); } else { result.first.push_back(topVariable); } return result; } storm::expressions::Variable InternalBdd::toExpressionRec(BDD dd, storm::expressions::ExpressionManager& manager, std::vector& expressions, std::unordered_map& indexToVariableMap, std::unordered_map, storm::expressions::Variable>& countIndexToVariablePair, std::unordered_map& nodeToCounterMap, std::vector& nextCounterForIndex) { STORM_LOG_ASSERT(!bdd_isnegated(dd), "Expected non-negated BDD node."); // First, try to look up the current node if it's not a terminal node. auto nodeCounterIt = nodeToCounterMap.find(dd); if (nodeCounterIt != nodeToCounterMap.end()) { // If we have found the node, this means we can look up the counter-index pair and get the corresponding variable. auto variableIt = countIndexToVariablePair.find(std::make_pair(nodeCounterIt->second, sylvan_var(dd))); STORM_LOG_ASSERT(variableIt != countIndexToVariablePair.end(), "Unable to find node."); return variableIt->second; } // If the node was not yet encountered, we create a variable and associate it with the appropriate expression. storm::expressions::Variable newNodeVariable = manager.declareFreshBooleanVariable(); // Since we want to reuse the variable whenever possible, we insert the appropriate entries in the hash table. if (!bdd_isterminal(dd)) { // If we are dealing with a non-terminal node, we count it as a new node with this index. nodeToCounterMap[dd] = nextCounterForIndex[sylvan_var(dd)]; countIndexToVariablePair[std::make_pair(nextCounterForIndex[sylvan_var(dd)], sylvan_var(dd))] = newNodeVariable; ++nextCounterForIndex[sylvan_var(dd)]; } else { // If it's a terminal node, it is the one leaf and there's no need to keep track of a counter for this level. nodeToCounterMap[dd] = 0; countIndexToVariablePair[std::make_pair(0, sylvan_var(dd))] = newNodeVariable; } // In the terminal case, we can only have a one since we are considering non-negated nodes only. if (bdd_isterminal(dd)) { if (dd == sylvan_true) { expressions.push_back(storm::expressions::iff(manager.boolean(true), newNodeVariable)); } else { expressions.push_back(storm::expressions::iff(manager.boolean(false), newNodeVariable)); } } else { // In the non-terminal case, we recursively translate the children nodes and then construct and appropriate ite-expression. BDD t = sylvan_high(dd); BDD e = sylvan_low(dd); BDD T = bdd_regular(t); BDD E = bdd_regular(e); storm::expressions::Variable thenVariable = toExpressionRec(T, manager, expressions, indexToVariableMap, countIndexToVariablePair, nodeToCounterMap, nextCounterForIndex); storm::expressions::Variable elseVariable = toExpressionRec(E, manager, expressions, indexToVariableMap, countIndexToVariablePair, nodeToCounterMap, nextCounterForIndex); // Create the appropriate expression. // Create the appropriate expression. auto indexVariable = indexToVariableMap.find(sylvan_var(dd)); storm::expressions::Variable levelVariable; if (indexVariable == indexToVariableMap.end()) { levelVariable = manager.declareFreshBooleanVariable(); indexToVariableMap[sylvan_var(dd)] = levelVariable; } else { levelVariable = indexVariable->second; } expressions.push_back(storm::expressions::iff(newNodeVariable, storm::expressions::ite(levelVariable, t == T ? thenVariable : !thenVariable, e == E ? elseVariable : !elseVariable))); } // Return the variable for this node. return newNodeVariable; } template InternalBdd InternalBdd::fromVector(InternalDdManager const* ddManager, std::vector const& values, Odd const& odd, std::vector const& sortedDdVariableIndices, std::function const& filter); template InternalBdd InternalBdd::fromVector(InternalDdManager const* ddManager, std::vector const& values, Odd const& odd, std::vector const& sortedDdVariableIndices, std::function const& filter); template InternalBdd InternalBdd::fromVector(InternalDdManager const* ddManager, std::vector const& values, Odd const& odd, std::vector const& sortedDdVariableIndices, std::function const& filter); template InternalAdd InternalBdd::toAdd() const; template InternalAdd InternalBdd::toAdd() const; template InternalAdd InternalBdd::toAdd() const; template void InternalBdd::filterExplicitVector(Odd const& odd, std::vector const& ddVariableIndices, std::vector const& sourceValues, std::vector& targetValues) const; template void InternalBdd::filterExplicitVector(Odd const& odd, std::vector const& ddVariableIndices, std::vector const& sourceValues, std::vector& targetValues) const; template void InternalBdd::filterExplicitVector(Odd const& odd, std::vector const& ddVariableIndices, std::vector const& sourceValues, std::vector& targetValues) const; template InternalAdd InternalBdd::ite(InternalAdd const& thenAdd, InternalAdd const& elseAdd) const; template InternalAdd InternalBdd::ite(InternalAdd const& thenAdd, InternalAdd const& elseAdd) const; template InternalAdd InternalBdd::ite(InternalAdd const& thenAdd, InternalAdd const& elseAdd) const; } }