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