Sebastian Junges
8 years ago
6 changed files with 541 additions and 155 deletions
-
85src/storm/models/sparse/ChoiceLabeling.cpp
-
102src/storm/models/sparse/ChoiceLabeling.h
-
137src/storm/models/sparse/ItemLabeling.cpp
-
185src/storm/models/sparse/ItemLabeling.h
-
100src/storm/models/sparse/StateLabeling.cpp
-
87src/storm/models/sparse/StateLabeling.h
@ -0,0 +1,85 @@ |
|||||
|
#include "ChoiceLabeling.h"
|
||||
|
|
||||
|
|
||||
|
|
||||
|
#include "storm/exceptions/OutOfRangeException.h"
|
||||
|
#include "storm/exceptions/InvalidArgumentException.h"
|
||||
|
|
||||
|
|
||||
|
namespace storm { |
||||
|
namespace models { |
||||
|
namespace sparse { |
||||
|
ChoiceLabeling::ChoiceLabeling(uint_fast64_t choiceCount) : ItemLabeling(choiceCount) { |
||||
|
// Intentionally left empty.
|
||||
|
} |
||||
|
|
||||
|
ChoiceLabeling::ChoiceLabeling(ItemLabeling const& itemLab) : ItemLabeling(itemLab) { |
||||
|
// Intentionally left empty.
|
||||
|
} |
||||
|
|
||||
|
ChoiceLabeling::ChoiceLabeling(ItemLabeling const&& itemLab) : ItemLabeling(itemLab) { |
||||
|
// Intentionally left empty.
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
bool ChoiceLabeling::operator==(ChoiceLabeling const& other) const { |
||||
|
if (itemCount != other.itemCount) { |
||||
|
return false; |
||||
|
} |
||||
|
if (this->getNumberOfLabels() != other.getNumberOfLabels()) { |
||||
|
return false; |
||||
|
} |
||||
|
for (auto const& labelIndexPair : this->nameToLabelingIndexMap) { |
||||
|
if (!other.containsLabel(labelIndexPair.first)) { |
||||
|
return false; |
||||
|
} |
||||
|
if (labelings[labelIndexPair.second] != other.getChoices(labelIndexPair.first)) { |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
ChoiceLabeling ChoiceLabeling::getSubLabeling(storm::storage::BitVector const& choices) const { |
||||
|
return ChoiceLabeling(ItemLabeling::getSubLabeling(choices)); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
std::set<std::string> ChoiceLabeling::getLabelsOfChoice(uint64_t choice) const { |
||||
|
return this->getLabelsOfItem(choice); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void ChoiceLabeling::addLabelToChoice(std::string const& label,uint64_t choice) { |
||||
|
this->addLabelToChoice(label, choice); |
||||
|
} |
||||
|
|
||||
|
bool ChoiceLabeling::getChoiceHasLabel(std::string const& label, uint64_t choice) const { |
||||
|
return this->getItemHasLabel(label, choice); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
storm::storage::BitVector const& ChoiceLabeling::getChoices(std::string const& label) const { |
||||
|
return this->getItems(label); |
||||
|
} |
||||
|
|
||||
|
void ChoiceLabeling::setChoices(std::string const& label, storage::BitVector const& labeling) { |
||||
|
this->setItems(label, labeling); |
||||
|
} |
||||
|
|
||||
|
void ChoiceLabeling::setChoices(std::string const& label, storage::BitVector&& labeling) { |
||||
|
this->setItems(label, labeling); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
std::ostream& operator<<(std::ostream& out, ChoiceLabeling const& labeling) { |
||||
|
labeling.printLabelingInformationToStream(out); |
||||
|
return out; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,102 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include "storm/models/sparse/ItemLabeling.h" |
||||
|
|
||||
|
|
||||
|
namespace storm { |
||||
|
namespace models { |
||||
|
namespace sparse { |
||||
|
|
||||
|
/*! |
||||
|
* This class manages the labeling of the choice space with a number of (atomic) labels. |
||||
|
*/ |
||||
|
class ChoiceLabeling : public ItemLabeling { |
||||
|
public: |
||||
|
/*! |
||||
|
* Constructs an empty labeling for the given number of choices. |
||||
|
* |
||||
|
* @param choiceCount The number of choices for which this labeling can hold the labels. |
||||
|
*/ |
||||
|
ChoiceLabeling(uint_fast64_t choiceCount = 0); |
||||
|
|
||||
|
ChoiceLabeling(ChoiceLabeling const& other) = default; |
||||
|
ChoiceLabeling(ItemLabeling const& other); |
||||
|
ChoiceLabeling(ItemLabeling const&& other); |
||||
|
ChoiceLabeling& operator=(ChoiceLabeling const& other) = default; |
||||
|
|
||||
|
|
||||
|
/*! |
||||
|
* Checks whether the two labelings are equal. |
||||
|
* |
||||
|
* @param other The labeling with which the current one is compared. |
||||
|
* @return True iff the labelings are equal. |
||||
|
*/ |
||||
|
bool operator==(ChoiceLabeling const& other) const; |
||||
|
|
||||
|
/*! |
||||
|
* Retrieves the sub labeling that represents the same labeling as the current one for all selected choices. |
||||
|
* |
||||
|
* @param choices The selected set of choices. |
||||
|
*/ |
||||
|
ChoiceLabeling getSubLabeling(storm::storage::BitVector const& choices) const; |
||||
|
|
||||
|
|
||||
|
/*! |
||||
|
* Retrieves the set of labels attached to the given choice. |
||||
|
* |
||||
|
* @param choice The choice for which to retrieve the labels. |
||||
|
* @return The labels attached to the given choice. |
||||
|
*/ |
||||
|
std::set<std::string> getLabelsOfChoice(uint64_t choice) const; |
||||
|
|
||||
|
|
||||
|
|
||||
|
/*! |
||||
|
* Adds a label to a given choice. |
||||
|
* |
||||
|
* @param label The name of the label to add. |
||||
|
* @param choice The index of the choice to label. |
||||
|
*/ |
||||
|
void addLabelToChoice(std::string const& label, uint64_t choice); |
||||
|
|
||||
|
/*! |
||||
|
* Checks whether a given choice is labeled with the given label. |
||||
|
* |
||||
|
* @param label The name of the label. |
||||
|
* @param choice The index of the choice to check. |
||||
|
* @return True if the node is labeled with the label, false otherwise. |
||||
|
*/ |
||||
|
bool getChoiceHasLabel(std::string const& label, uint64_t choice) const; |
||||
|
|
||||
|
|
||||
|
/*! |
||||
|
* Returns the labeling of choices associated with the given label. |
||||
|
* |
||||
|
* @param label The name of the label. |
||||
|
* @return A bit vector that represents the labeling of the choices with the given label. |
||||
|
*/ |
||||
|
storm::storage::BitVector const& getChoices(std::string const& label) const; |
||||
|
|
||||
|
/*! |
||||
|
* Sets the labeling of choices associated with the given label. |
||||
|
* |
||||
|
* @param label The name of the label. |
||||
|
* @param labeling A bit vector that represents the set of choices that will get this label. |
||||
|
*/ |
||||
|
void setChoices(std::string const& label, storage::BitVector const& labeling); |
||||
|
|
||||
|
/*! |
||||
|
* Sets the labeling of choices associated with the given label. |
||||
|
* |
||||
|
* @param label The name of the label. |
||||
|
* @param labeling A bit vector that represents the set of choices that will get this label. |
||||
|
*/ |
||||
|
void setChoices(std::string const& label, storage::BitVector&& labeling); |
||||
|
|
||||
|
friend std::ostream& operator<<(std::ostream& out, ChoiceLabeling const& labeling); |
||||
|
|
||||
|
}; |
||||
|
|
||||
|
} // namespace sparse |
||||
|
} // namespace models |
||||
|
} // namespace storm |
@ -0,0 +1,137 @@ |
|||||
|
#include "storm/models/sparse/ItemLabeling.h"
|
||||
|
|
||||
|
#include "storm/exceptions/OutOfRangeException.h"
|
||||
|
#include "storm/exceptions/InvalidArgumentException.h"
|
||||
|
|
||||
|
|
||||
|
namespace storm { |
||||
|
namespace models { |
||||
|
namespace sparse { |
||||
|
ItemLabeling::ItemLabeling(uint_fast64_t itemCount) : itemCount(itemCount), nameToLabelingIndexMap(), labelings() { |
||||
|
// Intentionally left empty.
|
||||
|
} |
||||
|
|
||||
|
bool ItemLabeling::operator==(ItemLabeling const& other) const { |
||||
|
if (itemCount != other.itemCount) { |
||||
|
return false; |
||||
|
} |
||||
|
if (this->getNumberOfLabels() != other.getNumberOfLabels()) { |
||||
|
return false; |
||||
|
} |
||||
|
for (auto const& labelIndexPair : this->nameToLabelingIndexMap) { |
||||
|
if (!other.containsLabel(labelIndexPair.first)) { |
||||
|
return false; |
||||
|
} |
||||
|
if (labelings[labelIndexPair.second] != other.getItems(labelIndexPair.first)) { |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
ItemLabeling ItemLabeling::getSubLabeling(storm::storage::BitVector const& items) const { |
||||
|
ItemLabeling result(items.getNumberOfSetBits()); |
||||
|
for (auto const& labelIndexPair : nameToLabelingIndexMap) { |
||||
|
result.addLabel(labelIndexPair.first, labelings[labelIndexPair.second] % items); |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
void ItemLabeling::addLabel(std::string const& label) { |
||||
|
addLabel(label, storage::BitVector(itemCount)); |
||||
|
} |
||||
|
|
||||
|
std::set<std::string> ItemLabeling::getLabels() const { |
||||
|
std::set<std::string> result; |
||||
|
for (auto const& labelIndexPair : this->nameToLabelingIndexMap) { |
||||
|
result.insert(labelIndexPair.first); |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
std::set<std::string> ItemLabeling::getLabelsOfItem(uint64_t item) const { |
||||
|
std::set<std::string> result; |
||||
|
for (auto const& labelIndexPair : this->nameToLabelingIndexMap) { |
||||
|
if (this->getItemHasLabel(labelIndexPair.first, item)) { |
||||
|
result.insert(labelIndexPair.first); |
||||
|
} |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
void ItemLabeling::addLabel(std::string const& label, storage::BitVector const& labeling) { |
||||
|
STORM_LOG_THROW(!this->containsLabel(label), storm::exceptions::InvalidArgumentException, "Label '" << label << "' already exists."); |
||||
|
STORM_LOG_THROW(labeling.size() == itemCount, storm::exceptions::InvalidArgumentException, "Labeling vector has invalid size. Expected: " << itemCount << " Actual: " << labeling.size()); |
||||
|
nameToLabelingIndexMap.emplace(label, labelings.size()); |
||||
|
labelings.push_back(labeling); |
||||
|
} |
||||
|
|
||||
|
void ItemLabeling::addLabel(std::string const& label, storage::BitVector&& labeling) { |
||||
|
STORM_LOG_THROW(!this->containsLabel(label), storm::exceptions::InvalidArgumentException, "Label '" << label << "' already exists."); |
||||
|
STORM_LOG_THROW(labeling.size() == itemCount, storm::exceptions::InvalidArgumentException, "Labeling vector has invalid size. Expected: " << itemCount << " Actual: " << labeling.size()); |
||||
|
nameToLabelingIndexMap.emplace(label, labelings.size()); |
||||
|
labelings.emplace_back(std::move(labeling)); |
||||
|
} |
||||
|
|
||||
|
bool ItemLabeling::containsLabel(std::string const& label) const { |
||||
|
return nameToLabelingIndexMap.find(label) != nameToLabelingIndexMap.end(); |
||||
|
} |
||||
|
|
||||
|
void ItemLabeling::addLabelToItem(std::string const& label, uint64_t item) { |
||||
|
STORM_LOG_THROW(this->containsLabel(label), storm::exceptions::OutOfRangeException, "Label '" << label << "' unknown."); |
||||
|
STORM_LOG_THROW(item < itemCount, storm::exceptions::OutOfRangeException, "Item index out of range."); |
||||
|
this->labelings[nameToLabelingIndexMap.at(label)].set(item, true); |
||||
|
} |
||||
|
|
||||
|
bool ItemLabeling::getItemHasLabel(std::string const& label, uint64_t item) const { |
||||
|
STORM_LOG_THROW(this->containsLabel(label), storm::exceptions::InvalidArgumentException, "The label '" << label << "' is invalid for the labeling of the model."); |
||||
|
return this->labelings[nameToLabelingIndexMap.at(label)].get(item); |
||||
|
} |
||||
|
|
||||
|
std::size_t ItemLabeling::getNumberOfLabels() const { |
||||
|
return labelings.size(); |
||||
|
} |
||||
|
|
||||
|
storm::storage::BitVector const& ItemLabeling::getItems(std::string const& label) const { |
||||
|
STORM_LOG_THROW(this->containsLabel(label), storm::exceptions::InvalidArgumentException, "The label " << label << " is invalid for the labeling of the model."); |
||||
|
return this->labelings[nameToLabelingIndexMap.at(label)]; |
||||
|
} |
||||
|
|
||||
|
void ItemLabeling::setItems(std::string const& label, storage::BitVector const& labeling) { |
||||
|
STORM_LOG_THROW(this->containsLabel(label), storm::exceptions::InvalidArgumentException, "The label " << label << " is invalid for the labeling of the model."); |
||||
|
STORM_LOG_THROW(labeling.size() == itemCount, storm::exceptions::InvalidArgumentException, "Labeling vector has invalid size."); |
||||
|
this->labelings[nameToLabelingIndexMap.at(label)] = labeling; |
||||
|
} |
||||
|
|
||||
|
void ItemLabeling::setItems(std::string const& label, storage::BitVector&& labeling) { |
||||
|
STORM_LOG_THROW(this->containsLabel(label), storm::exceptions::InvalidArgumentException, "The label " << label << " is invalid for the labeling of the model."); |
||||
|
STORM_LOG_THROW(labeling.size() == itemCount, storm::exceptions::InvalidArgumentException, "Labeling vector has invalid size."); |
||||
|
this->labelings[nameToLabelingIndexMap.at(label)] = labeling; |
||||
|
} |
||||
|
|
||||
|
void ItemLabeling::printLabelingInformationToStream(std::ostream& out) const { |
||||
|
out << "Labels: \t" << this->getNumberOfLabels() << std::endl; |
||||
|
for (auto const& labelIndexPair : this->nameToLabelingIndexMap) { |
||||
|
out << " * " << labelIndexPair.first << " -> " << this->labelings[labelIndexPair.second].getNumberOfSetBits() << " item(s)" << std::endl; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void ItemLabeling::printCompleteLabelingInformationToStream(std::ostream& out) const { |
||||
|
out << "Labels: \t" << this->getNumberOfLabels() << std::endl; |
||||
|
for (auto label : nameToLabelingIndexMap) { |
||||
|
out << "Label '" << label.first << "': "; |
||||
|
for (auto index : this->labelings[label.second]) { |
||||
|
out << index << " "; |
||||
|
} |
||||
|
out << std::endl; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
std::ostream& operator<<(std::ostream& out, ItemLabeling const& labeling) { |
||||
|
labeling.printLabelingInformationToStream(out); |
||||
|
return out; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,185 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
|
||||
|
#include <unordered_map> |
||||
|
#include <set> |
||||
|
#include <ostream> |
||||
|
|
||||
|
|
||||
|
|
||||
|
#include "storm/storage/BitVector.h" |
||||
|
#include "storm/utility/macros.h" |
||||
|
#include "storm/utility/OsDetection.h" |
||||
|
|
||||
|
namespace storm { |
||||
|
namespace models { |
||||
|
namespace sparse { |
||||
|
|
||||
|
/*! |
||||
|
* A base class managing the labeling of items with a number of (atomic) labels. |
||||
|
*/ |
||||
|
class ItemLabeling { |
||||
|
public: |
||||
|
/*! |
||||
|
* Constructs an empty labeling for the given number of items. |
||||
|
* |
||||
|
* @param itemCount The number of items for which this labeling can hold the labels. |
||||
|
*/ |
||||
|
explicit ItemLabeling(uint64_t itemCount = 0); |
||||
|
|
||||
|
ItemLabeling(ItemLabeling const& other) = default; |
||||
|
ItemLabeling& operator=(ItemLabeling const& other) = default; |
||||
|
|
||||
|
|
||||
|
/*! |
||||
|
* Checks whether the two labelings are equal. |
||||
|
* |
||||
|
* @param other The labeling with which the current one is compared. |
||||
|
* @return True iff the labelings are equal. |
||||
|
*/ |
||||
|
bool operator==(ItemLabeling const& other) const; |
||||
|
|
||||
|
|
||||
|
/*! |
||||
|
* Adds a new label to the labelings. Initially, no item is labeled with this label. |
||||
|
* |
||||
|
* @param label The name of the new label. |
||||
|
*/ |
||||
|
void addLabel(std::string const& label); |
||||
|
|
||||
|
/*! |
||||
|
* Retrieves the set of labels contained in this labeling. |
||||
|
* |
||||
|
* @return The set of known labels. |
||||
|
*/ |
||||
|
std::set<std::string> getLabels() const; |
||||
|
|
||||
|
|
||||
|
|
||||
|
/*! |
||||
|
* Creates a new label and attaches it to the given items. Note that the dimension of given labeling must |
||||
|
* match the number of items for which this item labeling is valid. |
||||
|
* |
||||
|
* @param label The new label. |
||||
|
* @param labeling A bit vector that indicates whether or not the new label is attached to a item. |
||||
|
*/ |
||||
|
void addLabel(std::string const& label, storage::BitVector const& labeling); |
||||
|
|
||||
|
/*! |
||||
|
* Creates a new label and attaches it to the given items. Note that the dimension of given labeling must |
||||
|
* match the number of items for which this item labeling is valid. |
||||
|
* |
||||
|
* @param label The new label. |
||||
|
* @param labeling A bit vector that indicates whether or not the new label is attached to a item. |
||||
|
*/ |
||||
|
void addLabel(std::string const& label, storage::BitVector&& labeling); |
||||
|
|
||||
|
/*! |
||||
|
* Checks whether a label is registered within this labeling. |
||||
|
* |
||||
|
* @return True if the label is known, false otherwise. |
||||
|
*/ |
||||
|
bool containsLabel(std::string const& label) const; |
||||
|
|
||||
|
|
||||
|
/*! |
||||
|
* Returns the number of labels managed by this object. |
||||
|
* |
||||
|
* @return The number of labels. |
||||
|
*/ |
||||
|
std::size_t getNumberOfLabels() const; |
||||
|
|
||||
|
|
||||
|
|
||||
|
/*! |
||||
|
* Prints information about the labeling to the specified stream. |
||||
|
* |
||||
|
* @param out The stream the information is to be printed to. |
||||
|
*/ |
||||
|
void printLabelingInformationToStream(std::ostream& out = std::cout) const; |
||||
|
|
||||
|
/*! |
||||
|
* Prints the complete labeling to the specified stream. |
||||
|
* |
||||
|
* @param out The stream the information is to be printed to. |
||||
|
*/ |
||||
|
void printCompleteLabelingInformationToStream(std::ostream& out = std::cout) const; |
||||
|
|
||||
|
friend std::ostream& operator<<(std::ostream& out, ItemLabeling const& labeling); |
||||
|
|
||||
|
protected: |
||||
|
|
||||
|
/*! |
||||
|
* Retrieves the sub labeling that represents the same labeling as the current one for all selected items. |
||||
|
* |
||||
|
* @param items The selected set of items. |
||||
|
*/ |
||||
|
ItemLabeling getSubLabeling(storm::storage::BitVector const& items) const; |
||||
|
|
||||
|
|
||||
|
/*! |
||||
|
* Retrieves the set of labels attached to the given item. |
||||
|
* |
||||
|
* @param item The item for which to retrieve the labels. |
||||
|
* @return The labels attached to the given item. |
||||
|
*/ |
||||
|
virtual std::set<std::string> getLabelsOfItem(uint64_t item) const; |
||||
|
|
||||
|
/*! |
||||
|
* Checks whether a given item is labeled with the given label. |
||||
|
* |
||||
|
* @param label The name of the label. |
||||
|
* @param item The index of the item to check. |
||||
|
* @return True if the node is labeled with the label, false otherwise. |
||||
|
*/ |
||||
|
virtual bool getItemHasLabel(std::string const& label, uint64_t item) const; |
||||
|
|
||||
|
|
||||
|
/*! |
||||
|
* Returns the labeling of items associated with the given label. |
||||
|
* |
||||
|
* @param label The name of the label. |
||||
|
* @return A bit vector that represents the labeling of the items with the given label. |
||||
|
*/ |
||||
|
virtual storm::storage::BitVector const& getItems(std::string const& label) const; |
||||
|
|
||||
|
/*! |
||||
|
* Sets the labeling of items associated with the given label. |
||||
|
* |
||||
|
* @param label The name of the label. |
||||
|
* @param labeling A bit vector that represents the set of items that will get this label. |
||||
|
*/ |
||||
|
virtual void setItems(std::string const& label, storage::BitVector const& labeling); |
||||
|
|
||||
|
/*! |
||||
|
* Sets the labeling of items associated with the given label. |
||||
|
* |
||||
|
* @param label The name of the label. |
||||
|
* @param labeling A bit vector that represents the set of items that will get this label. |
||||
|
*/ |
||||
|
virtual void setItems(std::string const& label, storage::BitVector&& labeling); |
||||
|
|
||||
|
/*! |
||||
|
* Adds a label to a given item. |
||||
|
* |
||||
|
* @param label The name of the label to add. |
||||
|
* @param item The index of the item to label. |
||||
|
*/ |
||||
|
virtual void addLabelToItem(std::string const& label, uint64_t item); |
||||
|
|
||||
|
|
||||
|
|
||||
|
// The number of items for which this object can hold the labeling. |
||||
|
uint64_t itemCount; |
||||
|
|
||||
|
// A mapping from labels to the index of the corresponding bit vector in the vector. |
||||
|
std::unordered_map<std::string, uint64_t> nameToLabelingIndexMap; |
||||
|
|
||||
|
// A vector that holds the labeling for all known labels. |
||||
|
std::vector<storm::storage::BitVector> labelings; |
||||
|
}; |
||||
|
|
||||
|
} // namespace sparse |
||||
|
} // namespace models |
||||
|
} // namespace storm |
||||
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue