22template<
class SparseModelType>
27 "Invalid query Type.");
31template<
class SparseModelType>
33 bool result = this->checkAchievability();
38template<
class SparseModelType>
39bool SparseCbAchievabilityQuery<SparseModelType>::checkAchievability() {
40 STORM_LOG_INFO(
"Building constraint system to check achievability.");
43 initializeConstraintSystem();
44 STORM_LOG_INFO(
"Constraint system consists of " << expectedChoiceVariables.size() <<
" + " << bottomStateVariables.size() <<
" variables");
45 addObjectiveConstraints();
46 swInitialization.stop();
53 STORM_LOG_STATISTICS(
"Building the constraintsystem took " << swInitialization <<
" seconds and checking the SMT formula took " << swCheck
68 STORM_LOG_THROW(
false, storm::exceptions::UnexpectedException,
"SMT solver yielded an unexpected result.");
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.");
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)));
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)));
94 for (
auto& var : expectedChoiceVariables) {
95 solver->add(var.getExpression() >= zero);
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());
104 solver->add(bottomStateSum == one);
107 storm::storage::SparseMatrix<ValueType> backwardsTransitions = this->preprocessedModel->getTransitionMatrix().transpose();
108 auto bottomStateVariableIt = bottomStateVariables.begin();
109 std::vector<storm::expressions::Expression> valueSummands;
110 for (uint_fast64_t state = 0; state < numStates; ++state) {
112 valueSummands.clear();
113 if (this->preprocessedModel->getInitialStates().get(state)) {
114 valueSummands.push_back(one);
116 for (
auto const& backwardsEntry : backwardsTransitions.
getRow(state)) {
117 valueSummands.push_back(this->expressionManager->rational(backwardsEntry.getValue()) *
118 expectedChoiceVariables[backwardsEntry.getColumn()].getExpression());
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]);
126 if (this->reward0EStates.get(state)) {
127 valueSummands.push_back(-(*bottomStateVariableIt));
128 ++bottomStateVariableIt;
132 STORM_LOG_ASSERT(bottomStateVariableIt == bottomStateVariables.end(),
"Unexpected bottom state variable.");
135template<
class SparseModelType>
136void SparseCbAchievabilityQuery<SparseModelType>::addObjectiveConstraints() {
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());
149 std::vector<storm::expressions::Expression> objectiveValues;
150 for (uint_fast64_t choice = 0; choice < rewards.size(); ++choice) {
152 objectiveValues.push_back(this->expressionManager->rational(rewards[choice]) * expectedChoiceVariables[choice].getExpression());
155 if (objectiveValues.empty()) {
161 storm::expressions::Expression threshold = this->expressionManager->rational(obj.formula->getThreshold().evaluateAsRational());
162 switch (obj.formula->getBound().comparisonType) {
164 solver->add(objValue > threshold);
167 solver->add(objValue >= threshold);
170 solver->add(objValue < threshold);
173 solver->add(objValue <= threshold);
176 STORM_LOG_THROW(
false, storm::exceptions::InvalidOperationException,
"One or more objectives have an invalid comparison type.");
183 return this->preprocessedModel->getRewardModel(rewardModelName).getTotalRewardVector(this->preprocessedModel->getTransitionMatrix());
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()
194 if (rewModel.hasStateRewards()) {
196 for (uint64_t markovianState : this->preprocessedModel->getMarkovianStates()) {
197 result[this->preprocessedModel->getTransitionMatrix().getRowGroupIndices()[markovianState]] +=
198 rewModel.getStateReward(markovianState) / this->preprocessedModel->getExitRate(markovianState);
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()
212 if (rewModel.hasStateRewards()) {
214 for (uint64_t markovianState : this->preprocessedModel->getMarkovianStates()) {
215 result[this->preprocessedModel->getTransitionMatrix().getRowGroupIndices()[markovianState]] +=
216 rewModel.getStateReward(markovianState) / this->preprocessedModel->getExitRate(markovianState);
224 std::string
const& rewardModelName)
const {
225 return this->preprocessedModel->getRewardModel(rewardModelName).getTotalRewardVector(this->preprocessedModel->getTransitionMatrix());