80 : certificateEnv(certificateEnv), transitionProbabilityMatrix(transitionProbabilityMatrix), totalRewardSpecification(totalRewardSpecification) {}
83 if (certificateEnv.getAlgorithm() == AlgorithmType::FpSmoothII || certificateEnv.getAlgorithm() == AlgorithmType::FpRoundII) {
85 STORM_LOG_THROW(certificateEnv.getGamma() < 1.0, storm::exceptions::InvalidEnvironmentException,
86 "The parameter gamma for smooth and rounding interval iteration must be smaller than 1.");
94 initializeValueVectors(alg);
95 if (!totalRewardSpecification.terminalStates.full()) {
96 if (certificateEnv.isUseTopological()) {
97 computeTopological(alg, relative, precision, ~totalRewardSpecification.terminalStates);
99 computeForSubsystem(alg, relative, precision, ~totalRewardSpecification.terminalStates);
102 if (globalUpperValues.empty()) {
104 globalUpperValues = globalLowerValues;
106 return {std::move(globalLowerValues), std::move(globalUpperValues)};
110 template<
typename VT,
bool SingleValue>
111 struct SubsystemData {
114 using ValueVectorType = std::conditional_t<SingleValue, std::vector<VT>, std::pair<std::vector<VT>, std::vector<VT>>>;
115 ValueVectorType offsets;
116 ValueVectorType operands;
117 std::optional<std::vector<VT>> exitProbabilities;
120 std::optional<std::vector<uint64_t>> originalToReducedStateMapping;
123 void initializeValueVectors(Algorithm
const& alg) {
125 uint64_t
const numberOfStates = totalRewardSpecification.terminalStates.size();
127 for (
auto const& [value, stateSet] : totalRewardSpecification.terminalStateValues) {
139 std::all_of(totalRewardSpecification.actionRewards->begin(), totalRewardSpecification.actionRewards->end(),
140 [](
auto const& v) { return v >= storm::utility::zero<ValueType>(); }),
141 storm::exceptions::NotSupportedException,
"Certificates for negative rewards are not supported.");
144 if (alg.type == AlgorithmType::FpII || alg.type == AlgorithmType::FpSmoothII || alg.type == AlgorithmType::FpRoundII) {
145 globalUpperValues = globalLowerValues;
147 STORM_LOG_ASSERT(alg.type == AlgorithmType::ExPI,
"Unsupported algorithm.");
148 globalUpperValues.clear();
152 storm::storage::BitVector getSubsystemExitChoices(storm::storage::BitVector
const& subsystem, uint64_t numSubystemRows,
153 std::optional<storm::storage::BitVector>
const& optionalSubsystemChoices)
const {
154 storm::storage::BitVector result(numSubystemRows,
false);
155 uint64_t currSubsystemRow = 0;
156 for (
auto const state : subsystem) {
157 for (
auto const rowIndex : transitionProbabilityMatrix.getRowGroupIndices(state)) {
158 if (optionalSubsystemChoices.has_value() && !optionalSubsystemChoices->get(rowIndex)) {
161 auto row = transitionProbabilityMatrix.getRow(rowIndex);
162 if (std::any_of(row.begin(), row.end(), [&subsystem](
auto const& entry) { return !subsystem.get(entry.getColumn()); })) {
163 result.set(currSubsystemRow);
171 template<
typename VT>
172 std::vector<VT> getSubsystemOffsets(storm::storage::BitVector
const& subsystem, std::optional<storm::storage::BitVector>
const& optionalSubsystemChoices,
173 storm::storage::BitVector
const& subsystemExitChoices, std::vector<ValueType>
const& globalValues)
const {
174 std::vector<VT> result;
175 result.reserve(subsystemExitChoices.
size());
176 for (
auto const state : subsystem) {
177 for (
auto const rowIndex : transitionProbabilityMatrix.getRowGroupIndices(state)) {
178 if (optionalSubsystemChoices.has_value() && !optionalSubsystemChoices->get(rowIndex)) {
181 auto rowValue = totalRewardSpecification.actionRewards.has_value()
183 : storm::utility::
zero<VT>();
184 if (subsystemExitChoices.
get(result.size())) {
185 for (auto const& entry : transitionProbabilityMatrix.getRow(rowIndex)) {
186 if (!subsystem.get(entry.getColumn())) {
187 rowValue += storm::utility::convertNumber<VT, ValueType>(entry.getValue() * globalValues[entry.getColumn()]);
191 result.push_back(std::move(rowValue));
194 result.shrink_to_fit();
195 STORM_LOG_ASSERT(result.size() == subsystemExitChoices.
size(),
"Unexpected number of rows in subsystem.");
199 template<
typename VT>
200 std::vector<VT> getSubsystemExitProbabilities(storm::storage::BitVector
const& subsystem,
201 std::optional<storm::storage::BitVector>
const& optionalSubsystemChoices,
202 storm::storage::BitVector
const& subsystemExitChoices)
const {
203 std::vector<VT> result;
204 result.reserve(subsystemExitChoices.
size());
205 for (
auto const state : subsystem) {
206 for (
auto const rowIndex : transitionProbabilityMatrix.getRowGroupIndices(state)) {
207 if (optionalSubsystemChoices.has_value() && !optionalSubsystemChoices->get(rowIndex)) {
211 if (subsystemExitChoices.
get(result.size())) {
212 for (
auto const& entry : transitionProbabilityMatrix.getRow(rowIndex)) {
213 if (!subsystem.get(entry.getColumn())) {
218 result.push_back(std::move(rowValue));
221 result.shrink_to_fit();
222 STORM_LOG_ASSERT(result.size() == subsystemExitChoices.
size(),
"Unexpected number of rows in subsystem.");
226 std::optional<storm::storage::BitVector> getSubsystemChoicesIfNonTrivial(storm::storage::BitVector
const& subsystem)
const {
227 STORM_LOG_ASSERT(totalRewardSpecification.terminalStateValues.size() == 1,
"Unexpected number of terminal state values.");
229 auto const& [value, stateSet] = totalRewardSpecification.terminalStateValues.front();
233 return transitionProbabilityMatrix.getRowFilter(subsystem, ~stateSet);
238 template<
typename VT>
239 storm::storage::SparseMatrix<VT> getSubsystemMatrix(storm::storage::BitVector
const& subsystemStates,
240 std::optional<storm::storage::BitVector>
const& subsystemChoices)
const {
241 storm::storage::SparseMatrix<ValueType> subMatrix = subsystemChoices.has_value()
242 ? transitionProbabilityMatrix.getSubmatrix(
false, *subsystemChoices, subsystemStates)
243 : transitionProbabilityMatrix.getSubmatrix(
true, subsystemStates, subsystemStates);
245 if constexpr (std::is_same_v<VT, ValueType>) {
248 return subMatrix.template toValueType<VT>();
252 template<
typename VT,
bool SingleValue>
253 SubsystemData<VT, SingleValue> initializeSubsystemData(storm::storage::BitVector
const& subsystem, AlgorithmType
const algorithmType)
const {
254 auto optionalSubsystemChoices = getSubsystemChoicesIfNonTrivial(subsystem);
255 auto result = SubsystemData<VT, SingleValue>{getSubsystemMatrix<VT>(subsystem, optionalSubsystemChoices), {}, {}, {}, {}, {}};
256 result.exitChoices = getSubsystemExitChoices(subsystem, result.transitions.getRowCount(), optionalSubsystemChoices);
257 if constexpr (SingleValue) {
258 STORM_LOG_ASSERT(globalUpperValues.empty(),
"Expected upper values not to be initialized.");
259 result.offsets = getSubsystemOffsets<VT>(subsystem, optionalSubsystemChoices, result.exitChoices, globalLowerValues);
261 STORM_LOG_ASSERT(!globalUpperValues.empty(),
"Expected upper values to be initialized.");
262 result.offsets = {getSubsystemOffsets<VT>(subsystem, optionalSubsystemChoices, result.exitChoices, globalLowerValues),
263 getSubsystemOffsets<VT>(subsystem, optionalSubsystemChoices, result.exitChoices, globalUpperValues)};
265 bool const computeInitialBounds =
266 algorithmType == AlgorithmType::FpII || algorithmType == AlgorithmType::FpSmoothII || algorithmType == AlgorithmType::FpRoundII;
267 if (computeInitialBounds) {
268 result.exitProbabilities = getSubsystemExitProbabilities<VT>(subsystem, optionalSubsystemChoices, result.exitChoices);
270 if (!totalRewardSpecification.terminalStatesUniversallyAlmostSurelyReached) {
271 result = eliminateECs(std::move(result));
273 if (computeInitialBounds) {
274 std::optional<storm::OptimizationDirection>
constexpr optionalDir =
275 Nondeterministic ? std::optional<storm::OptimizationDirection>(Dir) : std::nullopt;
277 if constexpr (SingleValue) {
279 result.exitProbabilities.value(), optionalDir);
282 result.exitProbabilities.value(), optionalDir);
284 result.exitProbabilities.value(), optionalDir);
287 if constexpr (SingleValue) {
298 template<
typename VT,
bool SingleValue>
299 SubsystemData<VT, SingleValue> eliminateECs(SubsystemData<VT, SingleValue>&& original)
const {
300 storm::storage::BitVector allStates(original.transitions.getRowGroupCount(),
true);
301 auto possibleECChoices = ~original.exitChoices;
302 if (totalRewardSpecification.actionRewards) {
304 auto const candidateChoices = possibleECChoices;
305 for (
auto const choice : candidateChoices) {
306 if constexpr (SingleValue) {
308 possibleECChoices.set(choice,
false);
312 "Unexpected offsets in non-exiting choice.");
314 possibleECChoices.set(choice,
false);
319 storm::storage::MaximalEndComponentDecomposition<VT> ecs(original.transitions, original.transitions.transpose(
true), allStates, possibleECChoices);
322 return std::move(original);
325 SubsystemData<VT, SingleValue> reduced{
326 std::move(reductionRes.matrix), std::move(reductionRes.sinkRows), {}, {}, {}, std::move(reductionRes.oldToNewStateMapping)};
327 if constexpr (SingleValue) {
328 reduced.offsets.reserve(reduced.exitChoices.size());
330 reduced.offsets.first.reserve(reduced.exitChoices.size());
331 reduced.offsets.second.reserve(reduced.exitChoices.size());
333 if (original.exitProbabilities.has_value()) {
334 reduced.exitProbabilities.emplace();
335 reduced.exitProbabilities->reserve(reduced.exitChoices.size());
337 for (uint64_t newRow = 0; newRow < reduced.exitChoices.size(); ++newRow) {
338 auto const oldRow = reductionRes.newToOldRowMapping[newRow];
339 if (original.exitProbabilities.has_value()) {
341 reduced.exitProbabilities->push_back(reduced.exitChoices.get(newRow) ?
storm::utility::one<VT>() : original.exitProbabilities->at(oldRow));
343 if (original.exitChoices.get(oldRow)) {
344 reduced.exitChoices.set(newRow,
true);
346 if constexpr (SingleValue) {
347 reduced.offsets.push_back(original.offsets[oldRow]);
349 reduced.offsets.first.push_back(original.offsets.first[oldRow]);
350 reduced.offsets.second.push_back(original.offsets.second[oldRow]);
353 STORM_LOG_ASSERT(std::all_of(reduced.originalToReducedStateMapping->begin(), reduced.originalToReducedStateMapping->end(),
354 [&reduced](
auto const& i) { return i < reduced.transitions.getRowGroupCount(); }),
355 "No representative for some state found in the reduced subsystem.");
359 template<
typename VT>
360 void setGlobalValuesFromSubsystemVector(storm::storage::BitVector
const& subsystemStates, std::vector<VT>
const& localValues,
361 std::vector<ValueType>& globalValues, std::optional<std::vector<uint64_t>>
const& reducedStateMapping,
363 STORM_LOG_ASSERT(subsystemStates.
size() == globalValues.size(),
"Unexpected number of values.");
364 if (reducedStateMapping) {
366 uint64_t unreducedSubsystemState = 0;
367 for (
auto globalState : subsystemStates) {
368 auto const reducedSubsystemState = reducedStateMapping->at(unreducedSubsystemState);
369 STORM_LOG_ASSERT(reducedSubsystemState < localValues.size(),
"unexpeced index in reduced subsystem.");
372 globalValues[globalState] = -globalValues[globalState];
374 ++unreducedSubsystemState;
377 STORM_LOG_ASSERT(subsystemStates.getNumberOfSetBits() == localValues.size(),
"Unexpected number of values in subsystem.");
378 auto localValuesIt = localValues.cbegin();
379 for (
auto i : subsystemStates) {
382 globalValues[
i] = -globalValues[
i];
389 template<
typename VT,
bool SingleValue>
390 void setGlobalValuesFromSubsystem(storm::storage::BitVector
const& subsystemStates, SubsystemData<VT, SingleValue>
const& subsystemData,
391 bool invertLowerValues) {
392 if constexpr (SingleValue) {
393 setGlobalValuesFromSubsystemVector<VT>(subsystemStates, subsystemData.operands, globalLowerValues, subsystemData.originalToReducedStateMapping,
395 STORM_LOG_ASSERT(globalUpperValues.empty(),
"Expected upper values not to be initialized.");
397 setGlobalValuesFromSubsystemVector<VT>(subsystemStates, subsystemData.operands.first, globalLowerValues,
398 subsystemData.originalToReducedStateMapping, invertLowerValues);
399 setGlobalValuesFromSubsystemVector<VT>(subsystemStates, subsystemData.operands.second, globalUpperValues,
400 subsystemData.originalToReducedStateMapping,
false);
404 void computeForSingleStateSubsystem(uint64_t state) {
405 storm::utility::Extremum<Dir, ValueType> lowerValue, upperValue;
406 bool const computeUpperValues = !globalUpperValues.empty();
409 auto const& infinityStates = totalRewardSpecification.terminalStateValues.front().second;
410 bool const ignoreChoicesToInfinity =
412 for (
auto const rowIndex : transitionProbabilityMatrix.getRowGroupIndices(state)) {
413 if (ignoreChoicesToInfinity) {
414 auto const row = transitionProbabilityMatrix.getRow(rowIndex);
415 if (std::any_of(row.begin(), row.end(), [&infinityStates](
auto const& entry) { return infinityStates.get(entry.getColumn()); })) {
419 auto const reward = totalRewardSpecification.actionRewards.has_value() ? totalRewardSpecification.actionRewards.value()[rowIndex]
421 auto lowerRowValue = reward;
422 auto upperRowValue = reward;
423 std::optional<ValueType> diagonalEntry;
424 for (
auto const& entry : transitionProbabilityMatrix.getRow(rowIndex)) {
425 if (state == entry.getColumn()) {
427 diagonalEntry = entry.getValue();
429 lowerRowValue += entry.getValue() * globalLowerValues[entry.getColumn()];
430 if (computeUpperValues) {
431 upperRowValue += entry.getValue() * globalUpperValues[entry.getColumn()];
435 if (diagonalEntry.has_value()) {
441 if (computeUpperValues) {
445 lowerValue &= lowerRowValue;
446 if (computeUpperValues) {
447 upperValue &= upperRowValue;
451 "Failed to compute lower and/or upper value for state " << state <<
".");
452 globalLowerValues[state] = *lowerValue;
453 if (computeUpperValues) {
454 globalUpperValues[state] = *upperValue;
458 void computeForSubsystemFpII(Algorithm
const& alg,
bool relative, ValueType
const& precision, storm::storage::BitVector
const& subsystemStates) {
459 using VT = std::conditional_t<storm::NumberTraits<ValueType>::IsExact, double,
ValueType>;
460 std::optional<storm::OptimizationDirection>
constexpr optionalDir = Nondeterministic ? std::optional<storm::OptimizationDirection>(Dir) : std::nullopt;
461 auto subsystemData = initializeSubsystemData<VT, false>(subsystemStates, alg.type);
462 if (alg.type == AlgorithmType::FpRoundII) {
463 auto invertVector = [](
auto& vector) {
464 for (
auto& value : vector) {
468 invertVector(subsystemData.operands.first);
469 invertVector(subsystemData.offsets.first);
472 auto viOp = std::make_shared<storm::solver::helper::ValueIterationOperator<VT, !Nondeterministic>>();
473 viOp->setMatrixBackwards(subsystemData.transitions);
474 storm::solver::helper::IntervalIterationHelper<VT, !Nondeterministic> iiHelper(viOp);
475 uint64_t numIterations = 0;
476 if (alg.type == AlgorithmType::FpII) {
478 }
else if (alg.type == AlgorithmType::FpRoundII) {
479 ScopedRoundingMode
const roundUpwards(FE_UPWARD);
483 STORM_LOG_ASSERT(alg.type == AlgorithmType::FpSmoothII,
"Unsupported algorithm.");
487 setGlobalValuesFromSubsystem<VT>(subsystemStates, subsystemData, alg.type == AlgorithmType::FpRoundII);
490 void computeForSubsystemExPI(storm::storage::BitVector
const& subsystemStates) {
491 using VT = std::conditional_t<storm::NumberTraits<ValueType>::IsExact,
ValueType, storm::RationalNumber>;
493 auto subsystemData = initializeSubsystemData<VT, true>(subsystemStates, AlgorithmType::ExPI);
495 storm::Environment env;
501 if constexpr (Nondeterministic) {
502 auto solverFactory = storm::solver::GeneralMinMaxLinearEquationSolverFactory<VT>();
503 auto req = solverFactory.getRequirements(env,
true,
false, Dir,
false,
false);
504 std::vector<uint64_t> initSched;
505 if (req.validInitialScheduler()) {
506 storm::storage::SparseMatrix<VT>
const& submatrix = subsystemData.transitions;
508 storm::storage::Scheduler<VT> validScheduler(exitStates.size());
510 ~exitStates, exitStates, validScheduler);
513 initSched.reserve(exitStates.size());
514 for (uint64_t i = 0;
i < exitStates.size(); ++
i) {
515 if (exitStates.get(i)) {
516 initSched.push_back(validScheduler.getChoice(i).getDeterministicChoice());
519 auto globalChoice = subsystemData.exitChoices.getNextSetIndex(groupStart);
521 initSched.push_back(globalChoice - groupStart);
524 req.clearValidInitialScheduler();
526 STORM_LOG_THROW(!req.hasEnabledCriticalRequirement(), storm::exceptions::UnmetRequirementException,
527 "Unmet requirements for MinMax linear equation solver: " << req.getEnabledRequirementsAsString() <<
".");
528 auto solver = solverFactory.create(env, std::move(subsystemData.transitions));
529 solver->setHasUniqueSolution(
true);
530 if (!initSched.empty()) {
531 solver->setInitialScheduler(std::move(initSched));
533 solver->setRequirementsChecked(
true);
534 solver->solveEquations(env, Dir, subsystemData.operands, subsystemData.offsets);
536 auto solverFactory = storm::solver::GeneralLinearEquationSolverFactory<VT>();
538 subsystemData.transitions = storm::storage::SparseMatrix<VT>(subsystemData.transitions,
true);
539 subsystemData.transitions.convertToEquationSystem();
541 auto req = solverFactory.getRequirements(env);
542 STORM_LOG_THROW(!req.hasEnabledCriticalRequirement(), storm::exceptions::UnmetRequirementException,
543 "Unmet requirements for linear equation solver: " << req.getEnabledRequirementsAsString() <<
".");
544 auto solver = solverFactory.create(env, std::move(subsystemData.transitions));
545 solver->solveEquations(env, subsystemData.operands, subsystemData.offsets);
547 setGlobalValuesFromSubsystem<VT>(subsystemStates, subsystemData,
false);
550 void computeForSubsystem(Algorithm
const& alg,
bool relative, ValueType
const& precision, storm::storage::BitVector
const& subsystemStates) {
552 case AlgorithmType::FpII:
553 case AlgorithmType::FpSmoothII:
554 case AlgorithmType::FpRoundII:
555 computeForSubsystemFpII(alg, relative, precision, subsystemStates);
557 case AlgorithmType::ExPI:
558 computeForSubsystemExPI(subsystemStates);
565 void computeTopological(Algorithm
const& alg,
bool relative, ValueType
const& precision, storm::storage::BitVector
const& nonTerminalStates) {
567 storm::utility::Stopwatch sccSw(
true);
568 storm::storage::StronglyConnectedComponentDecomposition<ValueType>
const sccDecomposition(
569 transitionProbabilityMatrix,
570 storm::storage::StronglyConnectedComponentDecompositionOptions().forceTopologicalSort().computeSccDepths().subsystem(nonTerminalStates));
571 uint64_t
const longestSccChainSize = sccDecomposition.getMaxSccDepth() + 1;
574 << sccSw <<
". Found " << sccDecomposition.size() <<
" SCC(s) containing a total of " << nonTerminalStates.
getNumberOfSetBits()
575 <<
" states. Average SCC size is "
576 <<
static_cast<double>(transitionProbabilityMatrix.getRowGroupCount()) /
static_cast<double>(sccDecomposition.size())
577 <<
". Longest SCC chain size is " << longestSccChainSize <<
".");
578 if (sccDecomposition.size() == 1) {
580 computeForSubsystem(alg, relative, precision, nonTerminalStates);
584 storm::storage::BitVector sccAsBitvector(transitionProbabilityMatrix.getRowGroupCount(),
false);
585 for (uint64_t sccIndex = 0; sccIndex < sccDecomposition.size(); ++sccIndex) {
586 auto const& scc = sccDecomposition.getBlock(sccIndex);
587 if (scc.size() == 1) {
589 computeForSingleStateSubsystem(*scc.begin());
592 sccAsBitvector.set(scc.begin(), scc.end());
595 computeForSubsystem(alg, relative, sccPrecision, sccAsBitvector);
596 sccAsBitvector.clear();
600 storm::CertificateModelCheckerEnvironment
const& certificateEnv;
601 storm::storage::SparseMatrix<ValueType>
const& transitionProbabilityMatrix;
604 std::vector<ValueType> globalLowerValues, globalUpperValues;