Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
MenuGameAbstractor.cpp
Go to the documentation of this file.
2
5#include "storm/io/file.h"
10#include "storm/utility/dd.h"
11
12namespace storm::gbar {
13namespace abstraction {
14
15template<storm::dd::DdType DdType, typename ValueType>
17 : restrictToRelevantStates(storm::settings::getModule<storm::settings::modules::AbstractionSettings>().isRestrictToRelevantStatesSet()) {
18 // Intentionally left empty.
19}
20
21template<storm::dd::DdType DdType, typename ValueType>
22std::vector<std::map<storm::expressions::Variable, storm::expressions::Expression>> MenuGameAbstractor<DdType, ValueType>::getVariableUpdates(
23 uint64_t player1Choice) const {
24 std::vector<std::map<storm::expressions::Variable, storm::expressions::Expression>> result(this->getNumberOfUpdates(player1Choice));
25 for (uint64_t i = 0; i < result.size(); ++i) {
26 result[i] = this->getVariableUpdates(player1Choice, i);
27 }
28 return result;
29}
30
31template<typename ValueType>
32std::string getStateName(std::pair<storm::expressions::SimpleValuation, ValueType> const& stateValue,
33 std::set<storm::expressions::Variable> const& locationVariables, std::set<storm::expressions::Variable> const& predicateVariables,
34 storm::expressions::Variable const& bottomVariable) {
35 std::stringstream stateName;
36
37 if (!locationVariables.empty()) {
38 stateName << "loc";
39 }
40
41 for (auto const& variable : locationVariables) {
42 stateName << stateValue.first.getIntegerValue(variable);
43 }
44
45 if (!locationVariables.empty() && !predicateVariables.empty()) {
46 stateName << "_";
47 }
48
49 for (auto const& variable : predicateVariables) {
50 if (stateValue.first.getBooleanValue(variable)) {
51 stateName << "1";
52 } else {
53 stateName << "0";
54 }
55 }
56
57 if (stateValue.first.getBooleanValue(bottomVariable)) {
58 stateName << "bot";
59 }
60 return stateName.str();
61}
62
63template<storm::dd::DdType DdType, typename ValueType>
65 return restrictToRelevantStates;
66}
67
68template<storm::dd::DdType DdType, typename ValueType>
70 storm::dd::Bdd<DdType> const& highlightStatesBdd, storm::dd::Bdd<DdType> const& filter) const {
71 std::ofstream out;
72 storm::io::openFile(filename, out);
73 AbstractionInformation<DdType> const& abstractionInformation = this->getAbstractionInformation();
74
75 storm::dd::Add<DdType, ValueType> filteredTransitions = filter.template toAdd<ValueType>() * currentGame.getTransitionMatrix();
76 storm::dd::Bdd<DdType> filteredTransitionsBdd = filteredTransitions.toBdd().existsAbstract(currentGame.getNondeterminismVariables());
77 storm::dd::Bdd<DdType> filteredReachableStates = storm::utility::dd::computeReachableStates(currentGame.getInitialStates(), filteredTransitionsBdd,
78 currentGame.getRowVariables(), currentGame.getColumnVariables())
79 .first;
80 filteredTransitions *= filteredReachableStates.template toAdd<ValueType>();
81
82 // Determine all initial states so we can color them blue.
83 std::unordered_set<std::string> initialStates;
84 storm::dd::Add<DdType, ValueType> initialStatesAsAdd = currentGame.getInitialStates().template toAdd<ValueType>();
85 for (auto stateValue : initialStatesAsAdd) {
86 initialStates.insert(getStateName(stateValue, abstractionInformation.getSourceLocationVariables(), abstractionInformation.getSourcePredicateVariables(),
87 abstractionInformation.getBottomStateVariable(true)));
88 }
89
90 // Determine all highlight states so we can color them red.
91 std::unordered_set<std::string> highlightStates;
92 storm::dd::Add<DdType, ValueType> highlightStatesAdd = highlightStatesBdd.template toAdd<ValueType>();
93 for (auto stateValue : highlightStatesAdd) {
94 highlightStates.insert(getStateName(stateValue, abstractionInformation.getSourceLocationVariables(),
95 abstractionInformation.getSourcePredicateVariables(), abstractionInformation.getBottomStateVariable(true)));
96 }
97
98 out << "digraph game {\n";
99
100 // Create the player 1 nodes.
101 storm::dd::Add<DdType, ValueType> statesAsAdd = filteredReachableStates.template toAdd<ValueType>();
102 for (auto stateValue : statesAsAdd) {
103 out << "\tpl1_";
104 std::string stateName = getStateName(stateValue, abstractionInformation.getSourceLocationVariables(),
105 abstractionInformation.getSourcePredicateVariables(), abstractionInformation.getBottomStateVariable(true));
106 out << stateName;
107 out << " [ label=\"";
108 if (stateValue.first.getBooleanValue(abstractionInformation.getBottomStateVariable(true))) {
109 out << "*\", margin=0, width=0, height=0, shape=\"none\"";
110 } else {
111 out << stateName << "\", margin=0, width=0, height=0, shape=\"oval\"";
112 }
113 bool isInitial = initialStates.find(stateName) != initialStates.end();
114 bool isHighlight = highlightStates.find(stateName) != highlightStates.end();
115 if (isInitial && isHighlight) {
116 out << ", style=\"filled\", fillcolor=\"yellow\"";
117 } else if (isInitial) {
118 out << ", style=\"filled\", fillcolor=\"blue\"";
119 } else if (isHighlight) {
120 out << ", style=\"filled\", fillcolor=\"red\"";
121 }
122 out << " ];\n";
123 }
124
125 // Create the nodes of the second player.
126 storm::dd::Add<DdType, ValueType> player2States = filteredTransitions.toBdd()
127 .existsAbstract(currentGame.getColumnVariables())
128 .existsAbstract(currentGame.getPlayer2Variables())
129 .template toAdd<ValueType>();
130 for (auto stateValue : player2States) {
131 out << "\tpl2_";
132 std::string stateName = getStateName(stateValue, abstractionInformation.getSourceLocationVariables(),
133 abstractionInformation.getSourcePredicateVariables(), abstractionInformation.getBottomStateVariable(true));
134 uint_fast64_t index = abstractionInformation.decodePlayer1Choice(stateValue.first, abstractionInformation.getPlayer1VariableCount());
135 out << stateName << "_" << index;
136 out << " [ shape=\"square\", width=0, height=0, margin=0, label=\"" << index << "\" ];\n";
137 out << "\tpl1_" << stateName << " -> " << "pl2_" << stateName << "_" << index << " [ label=\"" << index << "\" ];\n";
138 }
139
140 // Create the nodes of the probabilistic player.
141 storm::dd::Add<DdType, ValueType> playerPStates = filteredTransitions.toBdd().existsAbstract(currentGame.getColumnVariables()).template toAdd<ValueType>();
142 for (auto stateValue : playerPStates) {
143 out << "\tplp_";
144 std::stringstream stateNameStream;
145 stateNameStream << getStateName(stateValue, abstractionInformation.getSourceLocationVariables(), abstractionInformation.getSourcePredicateVariables(),
146 abstractionInformation.getBottomStateVariable(true));
147 uint_fast64_t index = abstractionInformation.decodePlayer1Choice(stateValue.first, abstractionInformation.getPlayer1VariableCount());
148 stateNameStream << "_" << index;
149 std::string stateName = stateNameStream.str();
150 index = abstractionInformation.decodePlayer2Choice(stateValue.first, currentGame.getPlayer2Variables().size());
151 out << stateName << "_" << index;
152 out << " [ shape=\"point\", label=\"\" ];\n";
153 out << "\tpl2_" << stateName << " -> " << "plp_" << stateName << "_" << index << " [ label=\"" << index << "\" ];\n";
154 }
155
156 for (auto stateValue : filteredTransitions) {
157 std::string sourceStateName = getStateName(stateValue, abstractionInformation.getSourceLocationVariables(),
158 abstractionInformation.getSourcePredicateVariables(), abstractionInformation.getBottomStateVariable(true));
159 std::string successorStateName =
160 getStateName(stateValue, abstractionInformation.getSuccessorLocationVariables(), abstractionInformation.getSuccessorPredicateVariables(),
161 abstractionInformation.getBottomStateVariable(false));
162 uint_fast64_t pl1Index = abstractionInformation.decodePlayer1Choice(stateValue.first, abstractionInformation.getPlayer1VariableCount());
163 uint_fast64_t pl2Index = abstractionInformation.decodePlayer2Choice(stateValue.first, currentGame.getPlayer2Variables().size());
164 out << "\tplp_" << sourceStateName << "_" << pl1Index << "_" << pl2Index << " -> pl1_" << successorStateName << " [ label=\"" << stateValue.second
165 << "\"];\n";
166 }
167
168 out << "}\n";
170}
171
172template<storm::dd::DdType DdType, typename ValueType>
174 this->targetStateExpression = targetStateExpression;
175}
176
177template<storm::dd::DdType DdType, typename ValueType>
181
182template<storm::dd::DdType DdType, typename ValueType>
184 return this->targetStateExpression.isInitialized();
185}
186
190} // namespace abstraction
191} // namespace storm::gbar
Bdd< LibraryType > toBdd() const
Converts the ADD to a BDD by mapping all values unequal to zero to 1.
Definition Add.cpp:1180
bool isInitialized() const
Checks whether the object encapsulates a base-expression.
std::set< storm::expressions::Variable > const & getSourceLocationVariables() const
Retrieves the source location variables.
std::size_t getPlayer1VariableCount() const
Retrieves the number of player 1 variables.
std::set< storm::expressions::Variable > const & getSuccessorLocationVariables() const
Retrieves the source location variables.
std::set< storm::expressions::Variable > const & getSuccessorPredicateVariables() const
Retrieves the set of successor predicate meta variables.
uint_fast64_t decodePlayer2Choice(storm::expressions::Valuation const &valuation, uint_fast64_t end) const
Decodes the player 2 choice in the given valuation.
uint_fast64_t decodePlayer1Choice(storm::expressions::Valuation const &valuation, uint_fast64_t end) const
Decodes the player 1 choice in the given valuation.
storm::expressions::Variable const & getBottomStateVariable(bool source) const
Retrieves the meta variable marking the bottom states.
std::set< storm::expressions::Variable > const & getSourcePredicateVariables() const
Retrieves the set of source predicate meta variables.
storm::expressions::Expression const & getTargetStateExpression() const
void setTargetStates(storm::expressions::Expression const &targetStateExpression)
Sets the expression characterizing the target states.
std::vector< std::map< storm::expressions::Variable, storm::expressions::Expression > > getVariableUpdates(uint64_t player1Choice) const
virtual void exportToDot(std::string const &filename, storm::dd::Bdd< DdType > const &highlightStatesBdd, storm::dd::Bdd< DdType > const &filter) const =0
Exports a representation of the current abstraction state in the dot format.
This class represents a discrete-time stochastic two-player game.
Definition MenuGame.h:14
storm::dd::Add< Type, ValueType > const & getTransitionMatrix() const
Retrieves the matrix representing the transitions of the model.
Definition Model.cpp:172
std::set< storm::expressions::Variable > const & getColumnVariables() const
Retrieves the meta variables used to encode the columns of the transition matrix and the vector indic...
Definition Model.cpp:192
storm::dd::Bdd< Type > const & getInitialStates() const
Retrieves the initial states of the model.
Definition Model.cpp:102
std::set< storm::expressions::Variable > const & getRowVariables() const
Retrieves the meta variables used to encode the rows of the transition matrix and the vector indices.
Definition Model.cpp:187
virtual std::set< storm::expressions::Variable > const & getNondeterminismVariables() const override
Retrieves the meta variables used to encode the nondeterminism in the model.
std::set< storm::expressions::Variable > const & getPlayer2Variables() const
Retrieeves the set of meta variables used to encode the nondeterministic choices of player 2.
This class represents the settings for the abstraction procedures.
std::string getStateName(std::pair< storm::expressions::SimpleValuation, ValueType > const &stateValue, std::set< storm::expressions::Variable > const &locationVariables, std::set< storm::expressions::Variable > const &predicateVariables, storm::expressions::Variable const &bottomVariable)
void closeFile(std::ofstream &stream)
Close the given file after writing.
Definition file.h:47
void openFile(std::string const &filepath, std::ofstream &filestream, bool append=false, bool silent=false)
Open the given file for writing.
Definition file.h:18
std::pair< storm::dd::Bdd< Type >, uint64_t > computeReachableStates(storm::dd::Bdd< Type > const &initialStates, storm::dd::Bdd< Type > const &transitions, std::set< storm::expressions::Variable > const &rowMetaVariables, std::set< storm::expressions::Variable > const &columnMetaVariables)
Definition dd.cpp:13