Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SparseCbAchievabilityQuery.cpp
Go to the documentation of this file.
2
17
18namespace storm {
19namespace modelchecker {
20namespace multiobjective {
21
22template<class SparseModelType>
30
31template<class SparseModelType>
32std::unique_ptr<CheckResult> SparseCbAchievabilityQuery<SparseModelType>::check(Environment const& env) {
33 bool result = this->checkAchievability();
34
35 return std::unique_ptr<CheckResult>(new ExplicitQualitativeCheckResult<ValueType>(this->originalModel.getInitialStates().getNextSetIndex(0), result));
36}
37
38template<class SparseModelType>
39bool SparseCbAchievabilityQuery<SparseModelType>::checkAchievability() {
40 STORM_LOG_INFO("Building constraint system to check achievability.");
41 // this->preprocessedModel->writeDotToStream(std::cout);
42 storm::utility::Stopwatch swInitialization(true);
43 initializeConstraintSystem();
44 STORM_LOG_INFO("Constraint system consists of " << expectedChoiceVariables.size() << " + " << bottomStateVariables.size() << " variables");
45 addObjectiveConstraints();
46 swInitialization.stop();
47
48 storm::utility::Stopwatch swCheck(true);
49 STORM_LOG_INFO("Invoking SMT Solver.");
51 swCheck.stop();
52
53 STORM_LOG_STATISTICS("Building the constraintsystem took " << swInitialization << " seconds and checking the SMT formula took " << swCheck
54 << " seconds.\n");
55
56 switch (result) {
58 // std::cout << "\nSatisfying assignment: \n" << solver->getModelAsValuation().toString(true) << '\n';
59 return true;
61 // std::cout << "\nUnsatisfiability core: {\n";
62 // for (auto const& expr : solver->getUnsatCore()) {
63 // std::cout << "\t " << expr << '\n';
64 // }
65 // std::cout << "}\n";
66 return false;
67 default:
68 STORM_LOG_THROW(false, storm::exceptions::UnexpectedException, "SMT solver yielded an unexpected result.");
69 }
70
71 return false;
72}
73
74template<class SparseModelType>
75void SparseCbAchievabilityQuery<SparseModelType>::initializeConstraintSystem() {
76 uint_fast64_t numStates = this->preprocessedModel->getNumberOfStates();
77 uint_fast64_t numChoices = this->preprocessedModel->getNumberOfChoices();
78 uint_fast64_t numBottomStates = this->reward0EStates.getNumberOfSetBits();
79 STORM_LOG_THROW(numBottomStates > 0, storm::exceptions::UnexpectedException, "No bottom states in the preprocessed model.");
80 storm::expressions::Expression zero = this->expressionManager->rational(storm::utility::zero<ValueType>());
81 storm::expressions::Expression one = this->expressionManager->rational(storm::utility::one<ValueType>());
82
83 // Declare the variables for the choices and bottom states
84 expectedChoiceVariables.reserve(numChoices);
85 for (uint_fast64_t choice = 0; choice < numChoices; ++choice) {
86 expectedChoiceVariables.push_back(this->expressionManager->declareRationalVariable("y" + std::to_string(choice)));
87 }
88 bottomStateVariables.reserve(numBottomStates);
89 for (uint_fast64_t bottomState = 0; bottomState < numBottomStates; ++bottomState) {
90 bottomStateVariables.push_back(this->expressionManager->declareRationalVariable("z" + std::to_string(bottomState)));
91 }
92
93 // assert that the values are greater zero and that the bottom state values sum up to one
94 for (auto& var : expectedChoiceVariables) {
95 solver->add(var.getExpression() >= zero);
96 }
97 std::vector<storm::expressions::Expression> bottomStateVarsAsExpression;
98 bottomStateVarsAsExpression.reserve(bottomStateVariables.size());
99 for (auto& var : bottomStateVariables) {
100 solver->add(var.getExpression() >= zero);
101 bottomStateVarsAsExpression.push_back(var.getExpression());
102 }
103 auto bottomStateSum = storm::expressions::sum(bottomStateVarsAsExpression).simplify();
104 solver->add(bottomStateSum == one);
105
106 // assert that the "incoming" value of each state equals the "outgoing" value
107 storm::storage::SparseMatrix<ValueType> backwardsTransitions = this->preprocessedModel->getTransitionMatrix().transpose();
108 auto bottomStateVariableIt = bottomStateVariables.begin();
109 std::vector<storm::expressions::Expression> valueSummands; // initialization here to avoid re-allocations
110 for (uint_fast64_t state = 0; state < numStates; ++state) {
111 // get the "incomming" value
112 valueSummands.clear();
113 if (this->preprocessedModel->getInitialStates().get(state)) {
114 valueSummands.push_back(one);
115 }
116 for (auto const& backwardsEntry : backwardsTransitions.getRow(state)) {
117 valueSummands.push_back(this->expressionManager->rational(backwardsEntry.getValue()) *
118 expectedChoiceVariables[backwardsEntry.getColumn()].getExpression());
119 }
120
121 // subtract the "outgoing" value
122 for (uint_fast64_t choice = this->preprocessedModel->getTransitionMatrix().getRowGroupIndices()[state];
123 choice < this->preprocessedModel->getTransitionMatrix().getRowGroupIndices()[state + 1]; ++choice) {
124 valueSummands.push_back(-expectedChoiceVariables[choice]);
125 }
126 if (this->reward0EStates.get(state)) {
127 valueSummands.push_back(-(*bottomStateVariableIt));
128 ++bottomStateVariableIt;
129 }
130 solver->add(storm::expressions::sum(valueSummands) == zero);
131 }
132 STORM_LOG_ASSERT(bottomStateVariableIt == bottomStateVariables.end(), "Unexpected bottom state variable.");
133}
134
135template<class SparseModelType>
136void SparseCbAchievabilityQuery<SparseModelType>::addObjectiveConstraints() {
137 storm::expressions::Expression zero = this->expressionManager->rational(storm::utility::zero<ValueType>());
138 for (Objective<ValueType> const& obj : this->objectives) {
139 STORM_LOG_THROW(obj.formula->isRewardOperatorFormula() && obj.formula->getSubformula().isTotalRewardFormula(),
140 storm::exceptions::InvalidOperationException,
141 "Constraint-based solver only supports total-reward objectives. Got " << *obj.formula << " instead.");
142 STORM_LOG_THROW(obj.formula->hasBound(), storm::exceptions::InvalidOperationException,
143 "Invoked achievability query but no bound was specified for at least one objective.");
144 STORM_LOG_THROW(obj.formula->asRewardOperatorFormula().hasRewardModelName(), storm::exceptions::InvalidOperationException,
145 "Expected reward operator with a reward model name. Got " << *obj.formula << " instead.");
146 std::vector<ValueType> rewards = getActionBasedExpectedRewards(obj.formula->asRewardOperatorFormula().getRewardModelName());
147
148 // Get the sum of all objective values
149 std::vector<storm::expressions::Expression> objectiveValues;
150 for (uint_fast64_t choice = 0; choice < rewards.size(); ++choice) {
151 if (!storm::utility::isZero(rewards[choice])) {
152 objectiveValues.push_back(this->expressionManager->rational(rewards[choice]) * expectedChoiceVariables[choice].getExpression());
153 }
154 }
155 if (objectiveValues.empty()) {
156 objectiveValues.push_back(this->expressionManager->rational(storm::utility::zero<storm::RationalNumber>()));
157 }
158 auto objValue = storm::expressions::sum(objectiveValues).simplify();
159
160 // We need to actually evaluate the threshold as rational number. Otherwise a threshold like '<=16/9' might be considered as 1 due to integer division
161 storm::expressions::Expression threshold = this->expressionManager->rational(obj.formula->getThreshold().evaluateAsRational());
162 switch (obj.formula->getBound().comparisonType) {
164 solver->add(objValue > threshold);
165 break;
167 solver->add(objValue >= threshold);
168 break;
170 solver->add(objValue < threshold);
171 break;
173 solver->add(objValue <= threshold);
174 break;
175 default:
176 STORM_LOG_THROW(false, storm::exceptions::InvalidOperationException, "One or more objectives have an invalid comparison type.");
177 }
178 }
179}
180
181template<>
182std::vector<double> SparseCbAchievabilityQuery<storm::models::sparse::Mdp<double>>::getActionBasedExpectedRewards(std::string const& rewardModelName) const {
183 return this->preprocessedModel->getRewardModel(rewardModelName).getTotalRewardVector(this->preprocessedModel->getTransitionMatrix());
184}
185
186template<>
187std::vector<double> SparseCbAchievabilityQuery<storm::models::sparse::MarkovAutomaton<double>>::getActionBasedExpectedRewards(
188 std::string const& rewardModelName) const {
189 auto const& rewModel = this->preprocessedModel->getRewardModel(rewardModelName);
190 STORM_LOG_ASSERT(!rewModel.hasTransitionRewards(), "Preprocessed Reward model has transition rewards which is not expected.");
191 std::vector<double> result = rewModel.hasStateActionRewards()
192 ? rewModel.getStateActionRewardVector()
193 : std::vector<double>(this->preprocessedModel->getNumberOfChoices(), storm::utility::zero<ValueType>());
194 if (rewModel.hasStateRewards()) {
195 // Note that state rewards are earned over time and thus play no role for probabilistic states
196 for (uint64_t markovianState : this->preprocessedModel->getMarkovianStates()) {
197 result[this->preprocessedModel->getTransitionMatrix().getRowGroupIndices()[markovianState]] +=
198 rewModel.getStateReward(markovianState) / this->preprocessedModel->getExitRate(markovianState);
199 }
200 }
201 return result;
202}
203
204template<>
205std::vector<storm::RationalNumber> SparseCbAchievabilityQuery<storm::models::sparse::MarkovAutomaton<storm::RationalNumber>>::getActionBasedExpectedRewards(
206 std::string const& rewardModelName) const {
207 auto const& rewModel = this->preprocessedModel->getRewardModel(rewardModelName);
208 STORM_LOG_ASSERT(!rewModel.hasTransitionRewards(), "Preprocessed Reward model has transition rewards which is not expected.");
209 std::vector<storm::RationalNumber> result =
210 rewModel.hasStateActionRewards() ? rewModel.getStateActionRewardVector()
211 : std::vector<storm::RationalNumber>(this->preprocessedModel->getNumberOfChoices(), storm::utility::zero<ValueType>());
212 if (rewModel.hasStateRewards()) {
213 // Note that state rewards are earned over time and thus play no role for probabilistic states
214 for (uint64_t markovianState : this->preprocessedModel->getMarkovianStates()) {
215 result[this->preprocessedModel->getTransitionMatrix().getRowGroupIndices()[markovianState]] +=
216 rewModel.getStateReward(markovianState) / this->preprocessedModel->getExitRate(markovianState);
217 }
218 }
219 return result;
220}
221
222template<>
223std::vector<storm::RationalNumber> SparseCbAchievabilityQuery<storm::models::sparse::Mdp<storm::RationalNumber>>::getActionBasedExpectedRewards(
224 std::string const& rewardModelName) const {
225 return this->preprocessedModel->getRewardModel(rewardModelName).getTotalRewardVector(this->preprocessedModel->getTransitionMatrix());
226}
227
230
233} // namespace multiobjective
234} // namespace modelchecker
235} // namespace storm
Expression simplify() const
Simplifies the expression according to some basic rules.
A qualitative check result over the states of a sparse model.
SparseCbAchievabilityQuery(Environment const &env, preprocessing::SparseMultiObjectivePreprocessorResult< SparseModelType > const &preprocessorResult)
virtual std::unique_ptr< CheckResult > check(Environment const &env) override
std::shared_ptr< storm::expressions::ExpressionManager > expressionManager
SparseCbQuery(preprocessing::SparseMultiObjectivePreprocessorResult< SparseModelType > const &preprocessorResult)
CheckResult
possible check results
Definition SmtSolver.h:24
const_rows getRow(index_type row) const
Returns an object representing the given row.
A class that provides convenience operations to display run times.
Definition Stopwatch.h:13
#define STORM_LOG_INFO(message)
Definition logging.h:34
#define STORM_LOG_STATISTICS(message)
Definition logging.h:48
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
Expression sum(std::vector< storm::expressions::Expression > const &expressions)
std::unique_ptr< storm::solver::SmtSolver > getSmtSolver(storm::expressions::ExpressionManager &manager)
Creates a new SMT solver instance.
Definition solver.cpp:215
bool isZero(ValueType const &a)
Definition constants.cpp:44
ValueType zero()
Definition constants.cpp:26
ValueType one()
Definition constants.cpp:21
The contract of a DD backend.