Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
MenuGameAbstractor.cpp
Go to the documentation of this file.
2
4
6
9
10#include "storm/io/file.h"
13#include "storm/utility/dd.h"
14
15#include "storm-config.h"
17
18namespace storm::gbar {
19namespace abstraction {
20
21template<storm::dd::DdType DdType, typename ValueType>
23 : restrictToRelevantStates(storm::settings::getModule<storm::settings::modules::AbstractionSettings>().isRestrictToRelevantStatesSet()) {
24 // Intentionally left empty.
25}
26
27template<storm::dd::DdType DdType, typename ValueType>
28std::vector<std::map<storm::expressions::Variable, storm::expressions::Expression>> MenuGameAbstractor<DdType, ValueType>::getVariableUpdates(
29 uint64_t player1Choice) const {
30 std::vector<std::map<storm::expressions::Variable, storm::expressions::Expression>> result(this->getNumberOfUpdates(player1Choice));
31 for (uint64_t i = 0; i < result.size(); ++i) {
32 result[i] = this->getVariableUpdates(player1Choice, i);
33 }
34 return result;
35}
36
37template<typename ValueType>
38std::string getStateName(std::pair<storm::expressions::SimpleValuation, ValueType> const& stateValue,
39 std::set<storm::expressions::Variable> const& locationVariables, std::set<storm::expressions::Variable> const& predicateVariables,
40 storm::expressions::Variable const& bottomVariable) {
41 std::stringstream stateName;
42
43 if (!locationVariables.empty()) {
44 stateName << "loc";
45 }
46
47 for (auto const& variable : locationVariables) {
48 stateName << stateValue.first.getIntegerValue(variable);
49 }
50
51 if (!locationVariables.empty() && !predicateVariables.empty()) {
52 stateName << "_";
53 }
54
55 for (auto const& variable : predicateVariables) {
56 if (stateValue.first.getBooleanValue(variable)) {
57 stateName << "1";
58 } else {
59 stateName << "0";
60 }
61 }
62
63 if (stateValue.first.getBooleanValue(bottomVariable)) {
64 stateName << "bot";
65 }
66 return stateName.str();
67}
68
69template<storm::dd::DdType DdType, typename ValueType>
71 return restrictToRelevantStates;
72}
73
74template<storm::dd::DdType DdType, typename ValueType>
76 storm::dd::Bdd<DdType> const& highlightStatesBdd, storm::dd::Bdd<DdType> const& filter) const {
77 std::ofstream out;
78 storm::io::openFile(filename, out);
79 AbstractionInformation<DdType> const& abstractionInformation = this->getAbstractionInformation();
80
81 storm::dd::Add<DdType, ValueType> filteredTransitions = filter.template toAdd<ValueType>() * currentGame.getTransitionMatrix();
82 storm::dd::Bdd<DdType> filteredTransitionsBdd = filteredTransitions.toBdd().existsAbstract(currentGame.getNondeterminismVariables());
83 storm::dd::Bdd<DdType> filteredReachableStates = storm::utility::dd::computeReachableStates(currentGame.getInitialStates(), filteredTransitionsBdd,
84 currentGame.getRowVariables(), currentGame.getColumnVariables())
85 .first;
86 filteredTransitions *= filteredReachableStates.template toAdd<ValueType>();
87
88 // Determine all initial states so we can color them blue.
89 std::unordered_set<std::string> initialStates;
90 storm::dd::Add<DdType, ValueType> initialStatesAsAdd = currentGame.getInitialStates().template toAdd<ValueType>();
91 for (auto stateValue : initialStatesAsAdd) {
92 initialStates.insert(getStateName(stateValue, abstractionInformation.getSourceLocationVariables(), abstractionInformation.getSourcePredicateVariables(),
93 abstractionInformation.getBottomStateVariable(true)));
94 }
95
96 // Determine all highlight states so we can color them red.
97 std::unordered_set<std::string> highlightStates;
98 storm::dd::Add<DdType, ValueType> highlightStatesAdd = highlightStatesBdd.template toAdd<ValueType>();
99 for (auto stateValue : highlightStatesAdd) {
100 highlightStates.insert(getStateName(stateValue, abstractionInformation.getSourceLocationVariables(),
101 abstractionInformation.getSourcePredicateVariables(), abstractionInformation.getBottomStateVariable(true)));
102 }
103
104 out << "digraph game {\n";
105
106 // Create the player 1 nodes.
107 storm::dd::Add<DdType, ValueType> statesAsAdd = filteredReachableStates.template toAdd<ValueType>();
108 for (auto stateValue : statesAsAdd) {
109 out << "\tpl1_";
110 std::string stateName = getStateName(stateValue, abstractionInformation.getSourceLocationVariables(),
111 abstractionInformation.getSourcePredicateVariables(), abstractionInformation.getBottomStateVariable(true));
112 out << stateName;
113 out << " [ label=\"";
114 if (stateValue.first.getBooleanValue(abstractionInformation.getBottomStateVariable(true))) {
115 out << "*\", margin=0, width=0, height=0, shape=\"none\"";
116 } else {
117 out << stateName << "\", margin=0, width=0, height=0, shape=\"oval\"";
118 }
119 bool isInitial = initialStates.find(stateName) != initialStates.end();
120 bool isHighlight = highlightStates.find(stateName) != highlightStates.end();
121 if (isInitial && isHighlight) {
122 out << ", style=\"filled\", fillcolor=\"yellow\"";
123 } else if (isInitial) {
124 out << ", style=\"filled\", fillcolor=\"blue\"";
125 } else if (isHighlight) {
126 out << ", style=\"filled\", fillcolor=\"red\"";
127 }
128 out << " ];\n";
129 }
130
131 // Create the nodes of the second player.
132 storm::dd::Add<DdType, ValueType> player2States = filteredTransitions.toBdd()
133 .existsAbstract(currentGame.getColumnVariables())
134 .existsAbstract(currentGame.getPlayer2Variables())
135 .template toAdd<ValueType>();
136 for (auto stateValue : player2States) {
137 out << "\tpl2_";
138 std::string stateName = getStateName(stateValue, abstractionInformation.getSourceLocationVariables(),
139 abstractionInformation.getSourcePredicateVariables(), abstractionInformation.getBottomStateVariable(true));
140 uint_fast64_t index = abstractionInformation.decodePlayer1Choice(stateValue.first, abstractionInformation.getPlayer1VariableCount());
141 out << stateName << "_" << index;
142 out << " [ shape=\"square\", width=0, height=0, margin=0, label=\"" << index << "\" ];\n";
143 out << "\tpl1_" << stateName << " -> " << "pl2_" << stateName << "_" << index << " [ label=\"" << index << "\" ];\n";
144 }
145
146 // Create the nodes of the probabilistic player.
147 storm::dd::Add<DdType, ValueType> playerPStates = filteredTransitions.toBdd().existsAbstract(currentGame.getColumnVariables()).template toAdd<ValueType>();
148 for (auto stateValue : playerPStates) {
149 out << "\tplp_";
150 std::stringstream stateNameStream;
151 stateNameStream << getStateName(stateValue, abstractionInformation.getSourceLocationVariables(), abstractionInformation.getSourcePredicateVariables(),
152 abstractionInformation.getBottomStateVariable(true));
153 uint_fast64_t index = abstractionInformation.decodePlayer1Choice(stateValue.first, abstractionInformation.getPlayer1VariableCount());
154 stateNameStream << "_" << index;
155 std::string stateName = stateNameStream.str();
156 index = abstractionInformation.decodePlayer2Choice(stateValue.first, currentGame.getPlayer2Variables().size());
157 out << stateName << "_" << index;
158 out << " [ shape=\"point\", label=\"\" ];\n";
159 out << "\tpl2_" << stateName << " -> " << "plp_" << stateName << "_" << index << " [ label=\"" << index << "\" ];\n";
160 }
161
162 for (auto stateValue : filteredTransitions) {
163 std::string sourceStateName = getStateName(stateValue, abstractionInformation.getSourceLocationVariables(),
164 abstractionInformation.getSourcePredicateVariables(), abstractionInformation.getBottomStateVariable(true));
165 std::string successorStateName =
166 getStateName(stateValue, abstractionInformation.getSuccessorLocationVariables(), abstractionInformation.getSuccessorPredicateVariables(),
167 abstractionInformation.getBottomStateVariable(false));
168 uint_fast64_t pl1Index = abstractionInformation.decodePlayer1Choice(stateValue.first, abstractionInformation.getPlayer1VariableCount());
169 uint_fast64_t pl2Index = abstractionInformation.decodePlayer2Choice(stateValue.first, currentGame.getPlayer2Variables().size());
170 out << "\tplp_" << sourceStateName << "_" << pl1Index << "_" << pl2Index << " -> pl1_" << successorStateName << " [ label=\"" << stateValue.second
171 << "\"];\n";
172 }
173
174 out << "}\n";
176}
177
178template<storm::dd::DdType DdType, typename ValueType>
180 this->targetStateExpression = targetStateExpression;
181}
182
183template<storm::dd::DdType DdType, typename ValueType>
187
188template<storm::dd::DdType DdType, typename ValueType>
190 return this->targetStateExpression.isInitialized();
191}
192
195
196#ifdef STORM_HAVE_CARL
198#endif
199} // namespace abstraction
200} // 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:1187
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:16
storm::dd::Add< Type, ValueType > const & getTransitionMatrix() const
Retrieves the matrix representing the transitions of the model.
Definition Model.cpp:177
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:197
storm::dd::Bdd< Type > const & getInitialStates() const
Retrieves the initial states of the model.
Definition Model.cpp:107
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:192
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:16
LabParser.cpp.
Definition cli.cpp:18