Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Update.cpp
Go to the documentation of this file.
1#include "Update.h"
2
3#include <algorithm>
4#include <boost/algorithm/string/join.hpp>
7
10
11namespace storm {
12namespace prism {
13Update::Update(uint_fast64_t globalIndex, storm::expressions::Expression const& likelihoodExpression, std::vector<storm::prism::Assignment> const& assignments,
14 std::string const& filename, uint_fast64_t lineNumber)
15 : LocatedInformation(filename, lineNumber),
16 likelihoodExpression(likelihoodExpression),
17 assignments(assignments),
18 variableToAssignmentIndexMap(),
19 globalIndex(globalIndex) {
21 [&likelihoodExpression]() {
22 auto simplified = likelihoodExpression.simplify();
23 return !simplified.isLiteral() || simplified.evaluateAsRational() >= 0;
24 }(),
25 "Negative likelihood expressions are not allowed.");
26 std::sort(this->assignments.begin(), this->assignments.end(), [](storm::prism::Assignment const& assignment1, storm::prism::Assignment const& assignment2) {
27 bool smaller = assignment1.getVariable().getType().isBooleanType() && !assignment2.getVariable().getType().isBooleanType();
28 if (!smaller) {
29 smaller = assignment1.getVariable() < assignment2.getVariable();
30 }
31 return smaller;
32 });
33 this->createAssignmentMapping();
34}
35
36storm::expressions::Expression const& Update::getLikelihoodExpression() const {
37 return likelihoodExpression;
38}
39
40std::size_t Update::getNumberOfAssignments() const {
41 return this->assignments.size();
42}
43
44std::vector<storm::prism::Assignment> const& Update::getAssignments() const {
45 return this->assignments;
46}
47
48std::vector<storm::prism::Assignment>& Update::getAssignments() {
49 return this->assignments;
50}
51
52storm::prism::Assignment const& Update::getAssignment(std::string const& variableName) const {
53 auto const& variableIndexPair = this->variableToAssignmentIndexMap.find(variableName);
54 STORM_LOG_THROW(variableIndexPair != this->variableToAssignmentIndexMap.end(), storm::exceptions::OutOfRangeException,
55 "Variable '" << variableName << "' is not assigned in update.");
56 return this->getAssignments()[variableIndexPair->second];
57}
58
59std::map<storm::expressions::Variable, storm::expressions::Expression> Update::getAsVariableToExpressionMap() const {
60 std::map<storm::expressions::Variable, storm::expressions::Expression> result;
61
62 for (auto const& assignment : this->getAssignments()) {
63 result.emplace(assignment.getVariable(), assignment.getExpression());
64 }
65
66 return result;
67}
68
69uint_fast64_t Update::getGlobalIndex() const {
70 return this->globalIndex;
71}
72
73void Update::createAssignmentMapping() {
74 this->variableToAssignmentIndexMap.clear();
75 for (uint_fast64_t assignmentIndex = 0; assignmentIndex < this->getAssignments().size(); ++assignmentIndex) {
76 this->variableToAssignmentIndexMap[this->getAssignments()[assignmentIndex].getVariableName()] = assignmentIndex;
77 }
78}
79
80Update Update::substitute(std::map<storm::expressions::Variable, storm::expressions::Expression> const& substitution) const {
81 std::vector<Assignment> newAssignments;
82 newAssignments.reserve(this->getNumberOfAssignments());
83 for (auto const& assignment : this->getAssignments()) {
84 newAssignments.emplace_back(assignment.substitute(substitution));
85 }
86 auto newLikelihoodExpression = this->getLikelihoodExpression().substitute(substitution).simplify();
87 STORM_LOG_THROW(!newLikelihoodExpression.isLiteral() || newLikelihoodExpression.evaluateAsRational() >= 0, storm::exceptions::IllegalArgumentException,
88 "Substitution yielding negative probabilities in '" << this->getLikelihoodExpression() << "' are not allowed.");
89 return Update(this->getGlobalIndex(), newLikelihoodExpression, newAssignments, this->getFilename(), this->getLineNumber());
90}
91
92Update Update::substituteNonStandardPredicates() const {
93 std::vector<Assignment> newAssignments;
94 newAssignments.reserve(this->getNumberOfAssignments());
95 for (auto const& assignment : this->getAssignments()) {
96 newAssignments.emplace_back(assignment.substituteNonStandardPredicates());
97 }
98 auto newLikelihoodExpression = this->getLikelihoodExpression().substituteNonStandardPredicates().simplify();
99 STORM_LOG_THROW(!newLikelihoodExpression.isLiteral() || newLikelihoodExpression.evaluateAsRational() >= 0, storm::exceptions::IllegalArgumentException,
100 "Substitution yielding negative probabilities in '" << this->getLikelihoodExpression() << "' are not allowed.");
101 return Update(this->getGlobalIndex(), newLikelihoodExpression, newAssignments, this->getFilename(), this->getLineNumber());
102}
103
104Update Update::removeIdentityAssignments() const {
105 std::vector<Assignment> newAssignments;
106 newAssignments.reserve(getNumberOfAssignments());
107 for (auto const& ass : this->assignments) {
108 if (!ass.isIdentity()) {
109 newAssignments.push_back(ass);
110 }
111 }
112 return Update(this->globalIndex, this->likelihoodExpression, newAssignments, getFilename(), getLineNumber());
113}
114
115Update Update::simplify() const {
116 std::vector<Assignment> newAssignments;
117 newAssignments.reserve(getNumberOfAssignments());
118 for (auto const& ass : this->assignments) {
119 Assignment newAssignment(ass.getVariable(), ass.getExpression().simplify().reduceNesting(), ass.getFilename(), ass.getLineNumber());
120 if (!newAssignment.isIdentity()) {
121 newAssignments.push_back(std::move(newAssignment));
122 }
123 }
124 return Update(this->globalIndex, this->likelihoodExpression.simplify().reduceNesting(), newAssignments, getFilename(), getLineNumber());
125}
126
127std::ostream& operator<<(std::ostream& stream, Update const& update) {
128 stream << update.getLikelihoodExpression() << " : ";
129 if (update.getAssignments().empty()) {
130 stream << "true";
131 } else {
132 std::vector<std::string> assignmentsAsStrings;
133 std::for_each(update.getAssignments().cbegin(), update.getAssignments().cend(), [&assignmentsAsStrings](Assignment const& assignment) {
134 std::stringstream stream;
135 stream << assignment;
136 assignmentsAsStrings.push_back(stream.str());
137 });
138 stream << boost::algorithm::join(assignmentsAsStrings, " & ");
139 }
140 return stream;
141}
142
143} // namespace prism
144} // namespace storm
Expression simplify() const
Simplifies the expression according to some basic rules.
bool isLiteral() const
Retrieves whether the expression is a literal.
bool isIdentity() const
Checks whether the assignment is an identity (lhs equals rhs)
Update substituteNonStandardPredicates() const
Definition Update.cpp:92
Update substitute(std::map< storm::expressions::Variable, storm::expressions::Expression > const &substitution) const
Substitutes all identifiers in the update according to the given map.
Definition Update.cpp:80
Update simplify() const
Simplifies the update in various ways (also removes identity assignments)
Definition Update.cpp:115
std::vector< storm::prism::Assignment > const & getAssignments() const
Retrieves a reference to the map of variable names to their respective assignments.
Definition Update.cpp:44
storm::expressions::Expression const & getLikelihoodExpression() const
Retrieves the expression for the likelihood of this update.
Definition Update.cpp:36
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
LabParser.cpp.
Definition cli.cpp:18