#include "SparseDtmcInstantiationModelChecker.h" #include "storm/logic/FragmentSpecification.h" #include "storm/modelchecker/results/ExplicitQuantitativeCheckResult.h" #include "storm/modelchecker/results/ExplicitQualitativeCheckResult.h" #include "storm/modelchecker/hints/ExplicitModelCheckerHint.h" #include "storm/exceptions/InvalidArgumentException.h" #include "storm/exceptions/InvalidStateException.h" namespace storm { namespace modelchecker { namespace parametric { template SparseDtmcInstantiationModelChecker::SparseDtmcInstantiationModelChecker(SparseModelType const& parametricModel) : SparseInstantiationModelChecker(parametricModel), modelInstantiator(parametricModel) { //Intentionally left empty } template std::unique_ptr SparseDtmcInstantiationModelChecker::check(storm::utility::parametric::Valuation const& valuation) { STORM_LOG_THROW(this->currentCheckTask, storm::exceptions::InvalidStateException, "Checking has been invoked but no property has been specified before."); auto const& instantiatedModel = modelInstantiator.instantiate(valuation); STORM_LOG_ASSERT(instantiatedModel.getTransitionMatrix().isProbabilistic(), "Instantiated matrix is not probabilistic!"); storm::modelchecker::SparseDtmcPrctlModelChecker> modelChecker(instantiatedModel); // Check if there are some optimizations implemented for the specified property if(this->currentCheckTask->getFormula().isInFragment(storm::logic::reachability())) { return checkReachabilityProbabilityFormula(modelChecker); } else if (this->currentCheckTask->getFormula().isInFragment(storm::logic::propositional().setRewardOperatorsAllowed(true).setReachabilityRewardFormulasAllowed(true).setOperatorAtTopLevelRequired(true).setNestedOperatorsAllowed(false))) { return checkReachabilityRewardFormula(modelChecker); } else if (this->currentCheckTask->getFormula().isInFragment(storm::logic::propositional().setProbabilityOperatorsAllowed(true).setBoundedUntilFormulasAllowed(true).setStepBoundedUntilFormulasAllowed(true).setTimeBoundedUntilFormulasAllowed(true).setOperatorAtTopLevelRequired(true).setNestedOperatorsAllowed(false))) { return checkBoundedUntilFormula(modelChecker); } else { return modelChecker.check(*this->currentCheckTask); } } template std::unique_ptr SparseDtmcInstantiationModelChecker::checkReachabilityProbabilityFormula(storm::modelchecker::SparseDtmcPrctlModelChecker>& modelChecker) { if (!this->currentCheckTask->getHint().isExplicitModelCheckerHint()) { this->currentCheckTask->setHint(std::make_shared>()); } ExplicitModelCheckerHint& hint = this->currentCheckTask->getHint().template asExplicitModelCheckerHint(); std::unique_ptr result; // Check the formula and store the result as a hint for the next call. // For qualitative properties, we still want a quantitative result hint. Hence we perform the check on the subformula if (this->currentCheckTask->getFormula().asOperatorFormula().hasQuantitativeResult()) { result = modelChecker.check(*this->currentCheckTask); hint.setResultHint(result->template asExplicitQuantitativeCheckResult().getValueVector()); } else { auto newCheckTask = this->currentCheckTask->substituteFormula(this->currentCheckTask->getFormula().asOperatorFormula().getSubformula()).setOnlyInitialStatesRelevant(false); std::unique_ptr quantitativeResult = modelChecker.computeProbabilities(newCheckTask); result = quantitativeResult->template asExplicitQuantitativeCheckResult().compareAgainstBound(this->currentCheckTask->getFormula().asOperatorFormula().getComparisonType(), this->currentCheckTask->getFormula().asOperatorFormula().template getThresholdAs()); hint.setResultHint(std::move(quantitativeResult->template asExplicitQuantitativeCheckResult().getValueVector())); } if (this->getInstantiationsAreGraphPreserving() && !hint.hasMaybeStates()) { // Extract the maybe states from the current result. assert(hint.hasResultHint()); storm::storage::BitVector maybeStates(hint.getResultHint().size(), true); uint_fast64_t stateIndex = 0; for (auto const& value : hint.getResultHint()) { if (storm::utility::isZero(value) || storm::utility::isOne(value)) { maybeStates.set(stateIndex, false); } ++stateIndex; } hint.setMaybeStates(std::move(maybeStates)); hint.setComputeOnlyMaybeStates(true); } return result; } template std::unique_ptr SparseDtmcInstantiationModelChecker::checkReachabilityRewardFormula(storm::modelchecker::SparseDtmcPrctlModelChecker>& modelChecker) { if (!this->currentCheckTask->getHint().isExplicitModelCheckerHint()) { this->currentCheckTask->setHint(std::make_shared>()); } ExplicitModelCheckerHint& hint = this->currentCheckTask->getHint().template asExplicitModelCheckerHint(); std::unique_ptr result; // Check the formula and store the result as a hint for the next call. // For qualitative properties, we still want a quantitative result hint. Hence we perform the check on the subformula if (this->currentCheckTask->getFormula().asOperatorFormula().hasQuantitativeResult()) { result = modelChecker.check(*this->currentCheckTask); this->currentCheckTask->getHint().template asExplicitModelCheckerHint().setResultHint(result->template asExplicitQuantitativeCheckResult().getValueVector()); } else { auto newCheckTask = this->currentCheckTask->substituteFormula(this->currentCheckTask->getFormula().asOperatorFormula().getSubformula()).setOnlyInitialStatesRelevant(false); std::unique_ptr quantitativeResult = modelChecker.computeRewards(this->currentCheckTask->getFormula().asRewardOperatorFormula().getMeasureType(), newCheckTask); result = quantitativeResult->template asExplicitQuantitativeCheckResult().compareAgainstBound(this->currentCheckTask->getFormula().asOperatorFormula().getComparisonType(), this->currentCheckTask->getFormula().asOperatorFormula().template getThresholdAs()); this->currentCheckTask->getHint().template asExplicitModelCheckerHint().setResultHint(std::move(quantitativeResult->template asExplicitQuantitativeCheckResult().getValueVector())); } if (this->getInstantiationsAreGraphPreserving() && !hint.hasMaybeStates()) { // Extract the maybe states from the current result. assert(hint.hasResultHint()); storm::storage::BitVector maybeStates(hint.getResultHint().size(), true); uint_fast64_t stateIndex = 0; for (auto const& value : hint.getResultHint()) { if (storm::utility::isInfinity(value)) { maybeStates.set(stateIndex, false); } ++stateIndex; } // We need to exclude the target states from the maybe states. // Note that we can not consider the states with reward zero since a valuation might set a reward to zero std::unique_ptr subFormulaResult = modelChecker.check(this->currentCheckTask->getFormula().asOperatorFormula().getSubformula().asEventuallyFormula().getSubformula()); maybeStates = maybeStates & ~(subFormulaResult->asExplicitQualitativeCheckResult().getTruthValuesVector()); hint.setMaybeStates(std::move(maybeStates)); hint.setComputeOnlyMaybeStates(true); } return result; } template std::unique_ptr SparseDtmcInstantiationModelChecker::checkBoundedUntilFormula(storm::modelchecker::SparseDtmcPrctlModelChecker>& modelChecker) { if (!this->currentCheckTask->getHint().isExplicitModelCheckerHint()) { this->currentCheckTask->setHint(std::make_shared>()); } std::unique_ptr result; ExplicitModelCheckerHint& hint = this->currentCheckTask->getHint().template asExplicitModelCheckerHint(); if (this->getInstantiationsAreGraphPreserving() && !hint.hasMaybeStates()) { // We extract the maybestates from the quantitative result // For qualitative properties, we still need a quantitative result. Hence we perform the check on the subformula if (this->currentCheckTask->getFormula().asOperatorFormula().hasQuantitativeResult()) { result = modelChecker.check(*this->currentCheckTask); hint.setResultHint(result->template asExplicitQuantitativeCheckResult().getValueVector()); } else { auto newCheckTask = this->currentCheckTask->substituteFormula(this->currentCheckTask->getFormula().asOperatorFormula().getSubformula()).setOnlyInitialStatesRelevant(false); std::unique_ptr quantitativeResult = modelChecker.computeProbabilities(newCheckTask); result = quantitativeResult->template asExplicitQuantitativeCheckResult().compareAgainstBound(this->currentCheckTask->getFormula().asOperatorFormula().getComparisonType(), this->currentCheckTask->getFormula().asOperatorFormula().template getThresholdAs()); hint.setResultHint(std::move(quantitativeResult->template asExplicitQuantitativeCheckResult().getValueVector())); } storm::storage::BitVector maybeStates(hint.getResultHint().size(), true); uint_fast64_t stateIndex = 0; for (auto const& value : hint.getResultHint()) { if (storm::utility::isZero(value)) { maybeStates.set(stateIndex, false); } ++stateIndex; } // We need to exclude the target states from the maybe states. // Note that we can not consider the states with probability one since a state might reach a target state with prob 1 within >0 steps std::unique_ptr subFormulaResult = modelChecker.check(this->currentCheckTask->getFormula().asOperatorFormula().getSubformula().asBoundedUntilFormula().getRightSubformula()); maybeStates = maybeStates & ~(subFormulaResult->asExplicitQualitativeCheckResult().getTruthValuesVector()); hint.setMaybeStates(std::move(maybeStates)); hint.setComputeOnlyMaybeStates(true); } else { result = modelChecker.check(*this->currentCheckTask); } return result; } template class SparseDtmcInstantiationModelChecker, double>; template class SparseDtmcInstantiationModelChecker, storm::RationalNumber>; } } }