From 684c239f8087c6531986c008d0d85ac947150065 Mon Sep 17 00:00:00 2001 From: Tim Quatmann Date: Thu, 3 Sep 2020 13:56:26 +0200 Subject: [PATCH] Using the revamped optimistic value iteration helper also for the lower bound. --- .../IterativeMinMaxLinearEquationSolver.cpp | 58 +--- .../IterativeMinMaxLinearEquationSolver.h | 2 + .../solver/NativeLinearEquationSolver.cpp | 53 +-- src/storm/solver/NativeLinearEquationSolver.h | 2 + .../helper/OptimisticValueIterationHelper.cpp | 321 ++++++++++-------- .../helper/OptimisticValueIterationHelper.h | 56 ++- 6 files changed, 232 insertions(+), 260 deletions(-) diff --git a/src/storm/solver/IterativeMinMaxLinearEquationSolver.cpp b/src/storm/solver/IterativeMinMaxLinearEquationSolver.cpp index 089ba266e..5e71c0503 100644 --- a/src/storm/solver/IterativeMinMaxLinearEquationSolver.cpp +++ b/src/storm/solver/IterativeMinMaxLinearEquationSolver.cpp @@ -2,7 +2,6 @@ #include #include "storm/solver/IterativeMinMaxLinearEquationSolver.h" -#include "storm/solver/helper/OptimisticValueIterationHelper.h" #include "storm/environment/solver/MinMaxSolverEnvironment.h" #include "storm/environment/solver/OviSolverEnvironment.h" @@ -369,63 +368,28 @@ namespace storm { return true; } - if (!this->multiplierA) { - this->multiplierA = storm::solver::MultiplierFactory().create(env, *this->A); - } - if (!auxiliaryRowGroupVector) { auxiliaryRowGroupVector = std::make_unique>(this->A->getRowGroupCount()); } - if (!auxiliaryRowGroupVector2) { - auxiliaryRowGroupVector2 = std::make_unique>(this->A->getRowGroupCount()); + if (!optimisticValueIterationHelper) { + optimisticValueIterationHelper = std::make_unique>(*this->A); } - // By default, we can not provide any guarantee - SolverGuarantee guarantee = SolverGuarantee::None; - // Get handle to multiplier. - storm::solver::Multiplier const &multiplier = *this->multiplierA; - // Allow aliased multiplications. - storm::solver::MultiplicationStyle multiplicationStyle = env.solver().minMax().getMultiplicationStyle(); - bool useGaussSeidelMultiplication = multiplicationStyle == storm::solver::MultiplicationStyle::GaussSeidel; - - boost::optional relevantValues; - if (this->hasRelevantValues()) { - relevantValues = this->getRelevantValues(); - } + storm::solver::helper::OptimisticValueIterationHelper helper(*this->A); // x has to start with a lower bound. this->createLowerBoundsVector(x); std::vector* lowerX = &x; std::vector* upperX = auxiliaryRowGroupVector.get(); - std::vector* auxVector = auxiliaryRowGroupVector2.get(); - this->startMeasureProgress(); - storm::solver::helper::OptimisticValueIterationHelper helper(*this->A); - auto statusIters = helper.solveEquationsOptimisticValueIteration(env, lowerX, upperX, auxVector, b, - [&] (std::vector*& y, std::vector*& yPrime, ValueType const& precision, bool const& relative, uint64_t const& i, uint64_t const& maxI) { - this->showProgressIterative(i); - auto result = performValueIteration(env, dir, y, yPrime, b, precision, relative, guarantee, i, maxI, multiplicationStyle); - return std::make_pair(result.iterations, result.status); - }, - [&] (std::vector* y, std::vector* yPrime, uint64_t const& i) { - this->showProgressIterative(i); - if (useGaussSeidelMultiplication) { - // Copy over the current vectors so we can modify them in-place. - // This is necessary as we want to compare the new values with the current ones. - *yPrime = *y; - multiplier.multiplyAndReduceGaussSeidel(env, dir, *y, &b); - } else { - multiplier.multiplyAndReduce(env, dir, *y, &b, *yPrime); - std::swap(y, yPrime); - } - }, - env.solver().minMax().getRelativeTerminationCriterion(), - storm::utility::convertNumber(env.solver().minMax().getPrecision()), - env.solver().minMax().getMaximalNumberOfIterations(), - dir, - relevantValues); + auto statusIters = helper.solveEquations(env, lowerX, upperX, b, + env.solver().minMax().getRelativeTerminationCriterion(), + storm::utility::convertNumber(env.solver().minMax().getPrecision()), + env.solver().minMax().getMaximalNumberOfIterations(), + dir, + this->getOptionalRelevantValues()); auto two = storm::utility::convertNumber(2.0); storm::utility::vector::applyPointwise(*lowerX, *upperX, x, [&two] (ValueType const& a, ValueType const& b) -> ValueType { return (a + b) / two; }); @@ -434,8 +398,7 @@ namespace storm { // If requested, we store the scheduler for retrieval. if (this->isTrackSchedulerSet()) { this->schedulerChoices = std::vector(this->A->getRowGroupCount()); - this->multiplierA->multiplyAndReduce(env, dir, x, &b, *auxiliaryRowGroupVector.get(), &this->schedulerChoices.get()); - this->multiplierA->multiplyAndReduce(env, dir, x, &b, *auxiliaryRowGroupVector.get(), &this->schedulerChoices.get()); + this->A->multiplyAndReduce(dir, this->A->getRowGroupIndices(), x, &b, *auxiliaryRowGroupVector.get(), &this->schedulerChoices.get()); } if (!this->isCachingEnabled()) { @@ -1109,6 +1072,7 @@ namespace storm { auxiliaryRowGroupVector.reset(); auxiliaryRowGroupVector2.reset(); soundValueIterationHelper.reset(); + optimisticValueIterationHelper.reset(); StandardMinMaxLinearEquationSolver::clearCache(); } diff --git a/src/storm/solver/IterativeMinMaxLinearEquationSolver.h b/src/storm/solver/IterativeMinMaxLinearEquationSolver.h index 6582caefa..be268f35f 100644 --- a/src/storm/solver/IterativeMinMaxLinearEquationSolver.h +++ b/src/storm/solver/IterativeMinMaxLinearEquationSolver.h @@ -8,6 +8,7 @@ #include "storm/solver/Multiplier.h" #include "storm/solver/StandardMinMaxLinearEquationSolver.h" #include "storm/solver/helper/SoundValueIterationHelper.h" +#include "storm/solver/helper/OptimisticValueIterationHelper.h" #include "storm/solver/SolverStatus.h" @@ -85,6 +86,7 @@ namespace storm { mutable std::unique_ptr> auxiliaryRowGroupVector; // A.rowGroupCount() entries mutable std::unique_ptr> auxiliaryRowGroupVector2; // A.rowGroupCount() entries mutable std::unique_ptr> soundValueIterationHelper; + mutable std::unique_ptr> optimisticValueIterationHelper; }; diff --git a/src/storm/solver/NativeLinearEquationSolver.cpp b/src/storm/solver/NativeLinearEquationSolver.cpp index 9ca9683fc..82bf7d5ec 100644 --- a/src/storm/solver/NativeLinearEquationSolver.cpp +++ b/src/storm/solver/NativeLinearEquationSolver.cpp @@ -643,62 +643,26 @@ namespace storm { return true; } - if (!this->multiplier) { - this->multiplier = storm::solver::MultiplierFactory().create(env, *this->A); - } - if (!this->cachedRowVector) { this->cachedRowVector = std::make_unique>(this->A->getRowCount()); } - if (!this->cachedRowVector2) { - this->cachedRowVector2 = std::make_unique>(this->A->getRowCount()); + if (!optimisticValueIterationHelper) { + optimisticValueIterationHelper = std::make_unique>(*this->A); } - // By default, we can not provide any guarantee - SolverGuarantee guarantee = SolverGuarantee::None; - // Get handle to multiplier. - storm::solver::Multiplier const &multiplier = *this->multiplier; - // Allow aliased multiplications. - storm::solver::MultiplicationStyle multiplicationStyle = env.solver().native().getPowerMethodMultiplicationStyle(); - bool useGaussSeidelMultiplication = multiplicationStyle == storm::solver::MultiplicationStyle::GaussSeidel; - - boost::optional relevantValues; - if (this->hasRelevantValues()) { - relevantValues = this->getRelevantValues(); - } - // x has to start with a lower bound. this->createLowerBoundsVector(x); std::vector* lowerX = &x; std::vector* upperX = this->cachedRowVector.get(); - std::vector* auxVector = this->cachedRowVector2.get(); - this->startMeasureProgress(); storm::solver::helper::OptimisticValueIterationHelper helper(*this->A); - auto statusIters = helper.solveEquationsOptimisticValueIteration(env, lowerX, upperX, auxVector, b, - [&] (std::vector*& y, std::vector*& yPrime, ValueType const& precision, bool const& relative, uint64_t const& i, uint64_t const& maxI) { - this->showProgressIterative(i); - auto result = performPowerIteration(env, y, yPrime, b, precision, relative, guarantee, i, maxI, multiplicationStyle); - return std::make_pair(result.iterations, result.status); - }, - [&] (std::vector* y, std::vector* yPrime, uint64_t const& i) { - this->showProgressIterative(i); - if (useGaussSeidelMultiplication) { - // Copy over the current vectors so we can modify them in-place. - // This is necessary as we want to compare the new values with the current ones. - *yPrime = *y; - multiplier.multiplyGaussSeidel(env, *y, &b); - } else { - multiplier.multiply(env, *y, &b, *yPrime); - std::swap(y, yPrime); - } - }, - env.solver().native().getRelativeTerminationCriterion(), - storm::utility::convertNumber(env.solver().native().getPrecision()), - env.solver().native().getMaximalNumberOfIterations(), - boost::none, // No optimization dir - relevantValues); + auto statusIters = helper.solveEquations(env, lowerX, upperX, b, + env.solver().native().getRelativeTerminationCriterion(), + storm::utility::convertNumber(env.solver().native().getPrecision()), + env.solver().native().getMaximalNumberOfIterations(), + boost::none, // No optimization dir + this->getOptionalRelevantValues()); auto two = storm::utility::convertNumber(2.0); storm::utility::vector::applyPointwise(*lowerX, *upperX, x, [&two] (ValueType const& a, ValueType const& b) -> ValueType { return (a + b) / two; }); this->logIterations(statusIters.first == SolverStatus::Converged, statusIters.first == SolverStatus::TerminatedEarly, statusIters.second); @@ -1066,6 +1030,7 @@ namespace storm { walkerChaeData.reset(); multiplier.reset(); soundValueIterationHelper.reset(); + optimisticValueIterationHelper.reset(); LinearEquationSolver::clearCache(); } diff --git a/src/storm/solver/NativeLinearEquationSolver.h b/src/storm/solver/NativeLinearEquationSolver.h index d551cd931..93c627a86 100644 --- a/src/storm/solver/NativeLinearEquationSolver.h +++ b/src/storm/solver/NativeLinearEquationSolver.h @@ -9,6 +9,7 @@ #include "storm/solver/NativeMultiplier.h" #include "storm/solver/SolverStatus.h" #include "storm/solver/helper/SoundValueIterationHelper.h" +#include "storm/solver/helper/OptimisticValueIterationHelper.h" #include "storm/utility/NumberTraits.h" @@ -96,6 +97,7 @@ namespace storm { // cached auxiliary data mutable std::unique_ptr> cachedRowVector2; // A.getRowCount() rows mutable std::unique_ptr> soundValueIterationHelper; + mutable std::unique_ptr> optimisticValueIterationHelper; struct JacobiDecomposition { JacobiDecomposition(Environment const& env, storm::storage::SparseMatrix const& A); diff --git a/src/storm/solver/helper/OptimisticValueIterationHelper.cpp b/src/storm/solver/helper/OptimisticValueIterationHelper.cpp index d52d41d37..28644d5de 100644 --- a/src/storm/solver/helper/OptimisticValueIterationHelper.cpp +++ b/src/storm/solver/helper/OptimisticValueIterationHelper.cpp @@ -35,7 +35,7 @@ namespace storm { template ValueType computeMaxRelDiff(std::vector const& allOldValues, std::vector const& allNewValues, storm::storage::BitVector const& relevantValues) { ValueType result = storm::utility::zero(); - for (auto const& i : relevantValues) { + for (auto i : relevantValues) { STORM_LOG_ASSERT(!storm::utility::isZero(allNewValues[i]) || storm::utility::isZero(allOldValues[i]), "Unexpected entry in iteration vector."); if (!storm::utility::isZero(allNewValues[i])) { result = storm::utility::max(result, storm::utility::abs(allNewValues[i] - allOldValues[i]) / allNewValues[i]); @@ -48,7 +48,7 @@ namespace storm { ValueType computeMaxRelDiff(std::vector const& allOldValues, std::vector const& allNewValues) { ValueType result = storm::utility::zero(); for (uint64_t i = 0; i < allOldValues.size(); ++i) { - STORM_LOG_ASSERT(!storm::utility::isZero(allNewValues[i]) || storm::utility::isZero(allOldValues[i]), "Unexpected entry in iteration vector."); + STORM_LOG_WARN_COND(!storm::utility::isZero(allNewValues[i]) || storm::utility::isZero(allOldValues[i]), "Unexpected entry in iteration vector."); if (!storm::utility::isZero(allNewValues[i])) { result = storm::utility::max(result, storm::utility::abs(allNewValues[i] - allOldValues[i]) / allNewValues[i]); } @@ -57,14 +57,9 @@ namespace storm { } template - ValueType updateIterationPrecision(storm::Environment const& env, std::vector const& currentX, std::vector const& newX, bool const& relative, boost::optional const& relevantValues) { + ValueType updateIterationPrecision(storm::Environment const& env, ValueType const& diff) { auto factor = storm::utility::convertNumber(env.solver().ovi().getPrecisionUpdateFactor()); - bool useRelevant = relevantValues.is_initialized() && env.solver().ovi().useRelevantValuesForPrecisionUpdate(); - if (relative) { - return (useRelevant ? computeMaxRelDiff(newX, currentX, relevantValues.get()) : computeMaxRelDiff(newX, currentX)) * factor; - } else { - return (useRelevant ? computeMaxAbsDiff(newX, currentX, relevantValues.get()) : computeMaxAbsDiff(newX, currentX)) * factor; - } + return factor * diff; } template @@ -78,7 +73,7 @@ namespace storm { } template - UpperBoundIterator::UpperBoundIterator(storm::storage::SparseMatrix const& matrix) { + IterationHelper::IterationHelper(storm::storage::SparseMatrix const& matrix) { STORM_LOG_THROW(static_cast(std::numeric_limits::max()) > matrix.getRowCount() + 1, storm::exceptions::NotSupportedException, "Matrix dimensions too large."); STORM_LOG_THROW(static_cast(std::numeric_limits::max()) > matrix.getEntryCount(), storm::exceptions::NotSupportedException, "Matrix dimensions too large."); matrixValues.reserve(matrix.getNonzeroEntryCount()); @@ -97,23 +92,120 @@ namespace storm { } } + + template + ValueType IterationHelper::singleIterationWithDiff(std::vector& x, std::vector const& b, bool computeRelativeDiff) { + return singleIterationWithDiffInternal(x, b, computeRelativeDiff); + } + + template + ValueType IterationHelper::singleIterationWithDiff(storm::solver::OptimizationDirection const& dir, std::vector& x, std::vector const& b, bool computeRelativeDiff) { + if (minimize(dir)) { + return singleIterationWithDiffInternal(x, b, computeRelativeDiff); + } else { + return singleIterationWithDiffInternal(x, b, computeRelativeDiff); + } + } + template - typename UpperBoundIterator::IterateResult UpperBoundIterator::iterate(std::vector& x, std::vector const& b, bool takeMinOfOldAndNew) { - return iterateInternal(x, b, takeMinOfOldAndNew); + template + ValueType IterationHelper::singleIterationWithDiffInternal(std::vector& x, std::vector const& b, bool computeRelativeDiff) { + STORM_LOG_ASSERT(x.size() > 0, "Empty equation system not expected."); + ValueType diff = storm::utility::zero(); + + IndexType i = x.size(); + while (i > 0) { + --i; + ValueType newXi = HasRowGroups ? multiplyRowGroup(i, b, x) : multiplyRow(i, b[i], x); + ValueType& oldXi = x[i]; + if (computeRelativeDiff) { + if (storm::utility::isZero(newXi)) { + if (storm::utility::isZero(oldXi)) { + // this operation has no effect: + // diff = std::max(diff, storm::utility::zero()); + } else { + diff = std::max(diff, storm::utility::one()); + } + } else { + diff = std::max(diff, storm::utility::abs((newXi - oldXi) / newXi)); + } + } else { + diff = std::max(diff, storm::utility::abs(newXi - oldXi)); + } + oldXi = std::move(newXi); + } + return diff; + } + + template + uint64_t IterationHelper::repeatedIterate(storm::solver::OptimizationDirection const& dir, std::vector& x, std::vector const& b, ValueType precision, bool relative) { + if (minimize(dir)) { + return repeatedIterateInternal(x, b, precision, relative); + } else { + return repeatedIterateInternal(x, b, precision, relative); + } + } + + template + uint64_t IterationHelper::repeatedIterate(std::vector& x, const std::vector& b, ValueType precision, bool relative) { + return repeatedIterateInternal(x, b, precision, relative); + } + + template + template + uint64_t IterationHelper::repeatedIterateInternal(std::vector& x, std::vector const& b, ValueType precision, bool relative) { + // Do a backwards gauss-seidel style iteration + bool convergence = true; + IndexType i = x.size(); + while (i > 0) { + --i; + ValueType newXi = HasRowGroups ? multiplyRowGroup(i, b, x) : multiplyRow(i, b[i], x); + ValueType& oldXi = x[i]; + // Check if we converged + if (relative) { + if (storm::utility::isZero(oldXi)) { + if (!storm::utility::isZero(newXi)) { + convergence = false; + break; + } + } else if (storm::utility::abs((newXi - oldXi) / oldXi) > precision) { + convergence = false; + break; + } + } else { + if (storm::utility::abs((newXi - oldXi)) > precision) { + convergence = false; + break; + } + } + } + if (!convergence) { + // we now know that we did not converge. We still need to set the remaining values + while (i > 0) { + --i; + x[i] = HasRowGroups ? multiplyRowGroup(i, b, x) : multiplyRow(i, b[i], x); + } + } + return convergence; } template - typename UpperBoundIterator::IterateResult UpperBoundIterator::iterate(storm::solver::OptimizationDirection const& dir, std::vector& x, std::vector const& b, bool takeMinOfOldAndNew) { + typename IterationHelper::IterateResult IterationHelper::iterateUpper(storm::solver::OptimizationDirection const& dir, std::vector& x, std::vector const& b, bool takeMinOfOldAndNew) { if (minimize(dir)) { - return iterateInternal(x, b, takeMinOfOldAndNew); + return iterateUpperInternal(x, b, takeMinOfOldAndNew); } else { - return iterateInternal(x, b, takeMinOfOldAndNew); + return iterateUpperInternal(x, b, takeMinOfOldAndNew); } } + template + typename IterationHelper::IterateResult IterationHelper::iterateUpper(std::vector& x, std::vector const& b, bool takeMinOfOldAndNew) { + return iterateUpperInternal(x, b, takeMinOfOldAndNew); + } + template template - typename UpperBoundIterator::IterateResult UpperBoundIterator::iterateInternal(std::vector& x, std::vector const& b, bool takeMinOfOldAndNew) { + typename IterationHelper::IterateResult IterationHelper::iterateUpperInternal(std::vector& x, std::vector const& b, bool takeMinOfOldAndNew) { // For each row compare the new upper bound candidate with the old one bool newUpperBoundAlwaysHigherEqual = true; bool newUpperBoundAlwaysLowerEqual = true; @@ -150,7 +242,7 @@ namespace storm { } template - ValueType UpperBoundIterator::multiplyRow(IndexType const& rowIndex, ValueType const& bi, std::vector const& x) { + ValueType IterationHelper::multiplyRow(IndexType const& rowIndex, ValueType const& bi, std::vector const& x) { assert(rowIndex < rowIndications.size()); ValueType xRes = bi; @@ -165,7 +257,7 @@ namespace storm { template template - ValueType UpperBoundIterator::multiplyRowGroup(IndexType const& rowGroupIndex, std::vector const& b, std::vector const& x) { + ValueType IterationHelper::multiplyRowGroup(IndexType const& rowGroupIndex, std::vector const& b, std::vector const& x) { STORM_LOG_ASSERT(rowGroupIndices != nullptr, "No row group indices available."); auto row = (*rowGroupIndices)[rowGroupIndex]; auto const& groupEnd = (*rowGroupIndices)[rowGroupIndex + 1]; @@ -180,24 +272,21 @@ namespace storm { } template - OptimisticValueIterationHelper::OptimisticValueIterationHelper(storm::storage::SparseMatrix const& matrix) : upperBoundIterator(matrix) { + OptimisticValueIterationHelper::OptimisticValueIterationHelper(storm::storage::SparseMatrix const& matrix) : iterationHelper(matrix) { // Intentionally left empty. } template - std::pair OptimisticValueIterationHelper::solveEquationsOptimisticValueIteration(Environment const& env, std::vector* lowerX, std::vector* upperX, std::vector* auxVector, std::vector const& b, ValueIterationCallBackType const& valueIterationCallback, SingleIterationCallBackType const& singleIterationCallback, bool relative, ValueType precision, uint64_t maxOverallIterations, boost::optional dir, boost::optional relevantValues) { + std::pair OptimisticValueIterationHelper::solveEquations(Environment const& env, std::vector* lowerX, std::vector* upperX, std::vector const& b, bool relative, ValueType precision, uint64_t maxOverallIterations, boost::optional dir, boost::optional const& relevantValues) { STORM_LOG_ASSERT(lowerX->size() == upperX->size(), "Dimension missmatch."); - STORM_LOG_ASSERT(lowerX->size() == auxVector->size(), "Dimension missmatch."); // As we will shuffle pointers around, let's store the original positions here. std::vector* initLowerX = lowerX; std::vector* initUpperX = upperX; - std::vector* initAux = auxVector; uint64_t overallIterations = 0; uint64_t lastValueIterationIterations = 0; uint64_t currentVerificationIterations = 0; - uint64_t valueIterationInvocations = 0; // Get some parameters for the algorithm // 2 @@ -215,135 +304,101 @@ namespace storm { SolverStatus status = SolverStatus::InProgress; while (status == SolverStatus::InProgress && overallIterations < maxOverallIterations) { - // Perform value iteration until convergence - ++valueIterationInvocations; - auto result = valueIterationCallback(lowerX, auxVector, iterationPrecision, relative, overallIterations, maxOverallIterations); - lastValueIterationIterations = result.first; - overallIterations += result.first; + lastValueIterationIterations = dir ? iterationHelper.repeatedIterate(dir.get(), *lowerX, b, precision, relative) : iterationHelper.repeatedIterate(*lowerX, b, precision, relative); + overallIterations += lastValueIterationIterations; - if (result.second != SolverStatus::Converged) { - status = result.second; - } else { - bool intervalIterationNeeded = false; - currentVerificationIterations = 0; + bool intervalIterationNeeded = false; + currentVerificationIterations = 0; - if (relative) { - oviinternal::guessUpperBoundRelative(*lowerX, *upperX, relativeBoundGuessingScaler); - } else { - oviinternal::guessUpperBoundAbsolute(*lowerX, *upperX, precision); - } + if (relative) { + oviinternal::guessUpperBoundRelative(*lowerX, *upperX, relativeBoundGuessingScaler); + } else { + oviinternal::guessUpperBoundAbsolute(*lowerX, *upperX, precision); + } - bool cancelGuess = false; - while (status == SolverStatus::InProgress && overallIterations < maxOverallIterations) { - ++overallIterations; - ++currentVerificationIterations; - // Perform value iteration stepwise for lower bound and guessed upper bound + bool cancelGuess = false; + while (status == SolverStatus::InProgress && overallIterations < maxOverallIterations) { + ++overallIterations; + ++currentVerificationIterations; + // Perform value iteration stepwise for lower bound and guessed upper bound - // Upper bound iteration - auto upperBoundIterResult = dir ? upperBoundIterator.iterate(dir.get(), *upperX, b, !noTerminationGuarantee) : upperBoundIterator.iterate(*upperX, b, !noTerminationGuarantee); + // Upper bound iteration + auto upperBoundIterResult = dir ? iterationHelper.iterateUpper(dir.get(), *upperX, b, !noTerminationGuarantee) : iterationHelper.iterateUpper(*upperX, b, !noTerminationGuarantee); - if (upperBoundIterResult == oviinternal::UpperBoundIterator::IterateResult::AlwaysHigherOrEqual) { - // All values moved up (and did not stay the same) - // That means the guess for an upper bound is actually a lower bound - iterationPrecision = oviinternal::updateIterationPrecision(env, *auxVector, *upperX, relative, relevantValues); - // We assume to have a single fixed point. We can thus safely set the new lower bound, to the wrongly guessed upper bound - // Set lowerX to the upper bound candidate - std::swap(lowerX, upperX); - break; - } else if (upperBoundIterResult == oviinternal::UpperBoundIterator::IterateResult::AlwaysLowerOrEqual) { - // All values moved down (and stayed not the same) - // This is a valid upper bound. We still need to check the precision. - // We can safely use twice the requested precision, as we calculate the center of both vectors - bool reachedPrecision; - if (relevantValues) { - reachedPrecision = storm::utility::vector::equalModuloPrecision(*lowerX, *upperX, relevantValues.get(), doublePrecision, relative); - } else { - reachedPrecision = storm::utility::vector::equalModuloPrecision(*lowerX, *upperX, doublePrecision, relative); - } - if (reachedPrecision) { - status = SolverStatus::Converged; - break; - } else { - // From now on, we keep updating both bounds - intervalIterationNeeded = true; - } - // The following case below covers that both vectors (old and new) are equal. - // Theoretically, this means that the precise fixpoint has been reached. However, numerical instabilities can be tricky and this detection might be incorrect (see the haddad-monmege model). - // We therefore disable it. It is very unlikely that we guessed the right fixpoint anyway. - //} else if (upperBoundIterResult == oviinternal::UpperBoundIterator::IterateResult::Equal) { - // In this case, the guessed upper bound is the precise fixpoint - // status = SolverStatus::Converged; - // std::swap(lowerX, auxVector); - // break; + if (upperBoundIterResult == oviinternal::IterationHelper::IterateResult::AlwaysHigherOrEqual) { + // All values moved up (and did not stay the same) + // That means the guess for an upper bound is actually a lower bound + auto diff = dir ? iterationHelper.singleIterationWithDiff(dir.get(), *upperX, b, relative) : iterationHelper.singleIterationWithDiff(*upperX, b, relative); + iterationPrecision = oviinternal::updateIterationPrecision(env, diff); + // We assume to have a single fixed point. We can thus safely set the new lower bound, to the wrongly guessed upper bound + // Set lowerX to the upper bound candidate + std::swap(lowerX, upperX); + break; + } else if (upperBoundIterResult == oviinternal::IterationHelper::IterateResult::AlwaysLowerOrEqual) { + // All values moved down (and stayed not the same) + // This is a valid upper bound. We still need to check the precision. + // We can safely use twice the requested precision, as we calculate the center of both vectors + bool reachedPrecision; + if (relevantValues) { + reachedPrecision = storm::utility::vector::equalModuloPrecision(*lowerX, *upperX, relevantValues.get(), doublePrecision, relative); + } else { + reachedPrecision = storm::utility::vector::equalModuloPrecision(*lowerX, *upperX, doublePrecision, relative); } - - // Check whether we tried this guess for too long - ValueType scaledIterationCount = storm::utility::convertNumber(currentVerificationIterations) * storm::utility::convertNumber(env.solver().ovi().getMaxVerificationIterationFactor()); - if (!intervalIterationNeeded && scaledIterationCount >= storm::utility::convertNumber(lastValueIterationIterations)) { - cancelGuess = true; - // In this case we will make one more iteration on the lower bound (mainly to obtain a new iterationPrecision) + if (reachedPrecision) { + status = SolverStatus::Converged; + break; + } else { + // From now on, we keep updating both bounds + intervalIterationNeeded = true; } + // The following case below covers that both vectors (old and new) are equal. + // Theoretically, this means that the precise fixpoint has been reached. However, numerical instabilities can be tricky and this detection might be incorrect (see the haddad-monmege model). + // We therefore disable it. It is very unlikely that we guessed the right fixpoint anyway. + //} else if (upperBoundIterResult == oviinternal::IterationHelper::IterateResult::Equal) { + // In this case, the guessed upper bound is the precise fixpoint + // status = SolverStatus::Converged; + // break; + } - // Lower bound iteration (only if needed) - if (cancelGuess || intervalIterationNeeded || currentVerificationIterations > upperBoundOnlyIterations) { - singleIterationCallback(lowerX, auxVector, overallIterations); - // At this point, auxVector contains the old values for the lower bound whereas lowerX contains the new ones. + // Check whether we tried this guess for too long + ValueType scaledIterationCount = storm::utility::convertNumber(currentVerificationIterations) * storm::utility::convertNumber(env.solver().ovi().getMaxVerificationIterationFactor()); + if (!intervalIterationNeeded && scaledIterationCount >= storm::utility::convertNumber(lastValueIterationIterations)) { + cancelGuess = true; + // In this case we will make one more iteration on the lower bound (mainly to obtain a new iterationPrecision) + } - // Check whether the upper and lower bounds have crossed, i.e., the upper bound is smaller than the lower bound. - bool valuesCrossed = false; - for (uint64_t i = 0; i < lowerX->size(); ++i) { - if ((*upperX)[i] < (*lowerX)[i]) { - valuesCrossed = true; - break; - } - } + // Lower bound iteration (only if needed) + if (cancelGuess || intervalIterationNeeded || currentVerificationIterations > upperBoundOnlyIterations) { + auto diff = dir ? iterationHelper.singleIterationWithDiff(dir.get(), *lowerX, b, relative) : iterationHelper.singleIterationWithDiff(*lowerX, b, relative); - if (cancelGuess || valuesCrossed) { - // A new guess is needed. - iterationPrecision = oviinternal::updateIterationPrecision(env, *auxVector, *lowerX, relative, relevantValues); + // Check whether the upper and lower bounds have crossed, i.e., the upper bound is smaller than the lower bound. + bool valuesCrossed = false; + for (uint64_t i = 0; i < lowerX->size(); ++i) { + if ((*upperX)[i] < (*lowerX)[i]) { + valuesCrossed = true; break; } } + + if (cancelGuess || valuesCrossed) { + // A new guess is needed. + iterationPrecision = oviinternal::updateIterationPrecision(env, diff); + break; + } } - if (storm::utility::resources::isTerminate()) { - status = SolverStatus::Aborted; - } + } + if (storm::utility::resources::isTerminate()) { + status = SolverStatus::Aborted; } } // end while - // Swap the results into the output vectors. - if (initLowerX == lowerX) { - // lowerX is already at the correct position. We still have to care for upperX - if (initUpperX != upperX) { - // UpperX is not at the correct position. It has to be at the auxVector - assert(initAux == upperX); - std::swap(*initUpperX, *initAux); - } - } else if (initUpperX == upperX) { - // UpperX is already at the correct position. - // We already know that lowerX is at the wrong position. It has to be at the auxVector - assert(initAux == lowerX); - std::swap(*initLowerX, *initAux); - } else if (initAux == auxVector) { - // We know that upperX and lowerX are swapped. - assert(initLowerX == upperX); + // Swap the results into the output vectors (if necessary). + assert(initLowerX != lowerX || (initLowerX == lowerX && initUpperX == upperX)); + if (initLowerX != lowerX) { assert(initUpperX == lowerX); - std::swap(*initUpperX, *initLowerX); - } else { - // Now we know that all vectors are at the wrong position. There are only two possibilities left - if (initLowerX == upperX) { - assert(initUpperX == auxVector); - assert(initAux == lowerX); - std::swap(*initLowerX, *initAux); - std::swap(*initUpperX, *initAux); - } else { - assert(initLowerX == auxVector); - assert(initUpperX == lowerX); - assert (initAux == upperX); - std::swap(*initUpperX, *initAux); - std::swap(*initLowerX, *initAux); - } + assert(initLowerX == upperX); + lowerX->swap(*upperX); } if (overallIterations > maxOverallIterations) { diff --git a/src/storm/solver/helper/OptimisticValueIterationHelper.h b/src/storm/solver/helper/OptimisticValueIterationHelper.h index abf67dd3c..d559d508d 100644 --- a/src/storm/solver/helper/OptimisticValueIterationHelper.h +++ b/src/storm/solver/helper/OptimisticValueIterationHelper.h @@ -18,10 +18,10 @@ namespace storm { namespace oviinternal { template - class UpperBoundIterator { + class IterationHelper { public: typedef uint32_t IndexType; - UpperBoundIterator(storm::storage::SparseMatrix const& matrix); + IterationHelper(storm::storage::SparseMatrix const& matrix); enum class IterateResult { AlwaysHigherOrEqual, AlwaysLowerOrEqual, @@ -29,13 +29,26 @@ namespace storm { Incomparable }; - IterateResult iterate(std::vector& x, std::vector const& b, bool takeMinOfOldAndNew); - IterateResult iterate(storm::solver::OptimizationDirection const& dir, std::vector& x, std::vector const& b, bool takeMinOfOldAndNew); + /// performs a single iteration and returns the maximal difference between the iterations as well as the index where this difference happened + ValueType singleIterationWithDiff(std::vector& x, std::vector const& b, bool computeRelativeDiff); + ValueType singleIterationWithDiff(storm::solver::OptimizationDirection const& dir, std::vector& x, std::vector const& b, bool computeRelativeDiff); + + /// returns the number of performed iterations + uint64_t repeatedIterate(std::vector& x, std::vector const& b, ValueType precision, bool relative); + uint64_t repeatedIterate(storm::solver::OptimizationDirection const& dir, std::vector& x, std::vector const& b, ValueType precision, bool relative); + + /// Performs a single iteration for the upper bound + IterateResult iterateUpper(std::vector& x, std::vector const& b, bool takeMinOfOldAndNew); + IterateResult iterateUpper(storm::solver::OptimizationDirection const& dir, std::vector& x, std::vector const& b, bool takeMinOfOldAndNew); private: template - IterateResult iterateInternal(std::vector& x, std::vector const& b, bool takeMinOfOldAndNew); + ValueType singleIterationWithDiffInternal(std::vector& x, std::vector const& b, bool computeRelativeDiff); + template + uint64_t repeatedIterateInternal(std::vector& x, std::vector const& b, ValueType precision, bool relative); + template + IterateResult iterateUpperInternal(std::vector& x, std::vector const& b, bool takeMinOfOldAndNew); ValueType multiplyRow(IndexType const& rowIndex, ValueType const& bi, std::vector const& x); template ValueType multiplyRowGroup(IndexType const& rowGroupIndex, std::vector const& b, std::vector const& x); @@ -56,49 +69,20 @@ namespace storm { public: OptimisticValueIterationHelper(storm::storage::SparseMatrix const& matrix); - /*! - * Return type of value iteration callback. - * The first component shall be the number of performed iterations, the second component is the final convergence status. - */ - typedef std::pair ValueIterationReturnType; - - /*! - * Value iteration callback. Performs conventional value iteration for the given input. - * @pre y points to a vector that contains starting values - * @post y points to a vector that contains resulting values - * @param yPrime points to auxiliary storage - * @param precision is the target precision - * @param relative sets whether relative precision is considered - * @param maxI the maximal number of iterations to perform - */ - typedef std::function*& y, std::vector*& yPrime, ValueType const& precision, bool const& relative, uint64_t const& i, uint64_t const& maxI)> ValueIterationCallBackType; - - /*! - * Should perform a single value iteration step (using conventional value iteration). - * @pre y points to a vector that contains starting values - * @post y points to a vector that contains resulting values - * @param yPrime points to auxiliary storage - * @param currI the current iteration (can be used to display progress within the callback) - */ - typedef std::function* y, std::vector* yPrime, uint64_t const& currI)> SingleIterationCallBackType; - /*! * @param env * @param lowerX Needs to be some arbitrary lower bound on the actual values initially * @param upperX Does not need to be an upper bound initially - * @param auxVector auxiliary storage (same size as lowerX and upperX) * @param b the values added to each matrix row (the b in A*x+b) - * @param valueIterationCallback Function that should perform standard value iteration on the input vector - * @param singleIterationCallback Function that should perform a single value iteration step on the input vector e.g. ( x' = min/max(A*x + b)) * @param dir The optimization direction * @param relevantValues If given, we only check the precision at the states with the given indices. * @return The status upon termination as well as the number of iterations Also, the maximum (relative/absolute) difference between lowerX and upperX will be 2*epsilon * with the provided precision parameters. */ - std::pair solveEquationsOptimisticValueIteration(Environment const& env, std::vector* lowerX, std::vector* upperX, std::vector* auxVector, std::vector const& b, ValueIterationCallBackType const& valueIterationCallback, SingleIterationCallBackType const& singleIterationCallback, bool relative, ValueType precision, uint64_t maxOverallIterations, boost::optional dir, boost::optional relevantValues = boost::none); + std::pair solveEquations(Environment const& env, std::vector* lowerX, std::vector* upperX, std::vector const& b, bool relative, ValueType precision, uint64_t maxOverallIterations, boost::optional dir, boost::optional const& relevantValues); private: - oviinternal::UpperBoundIterator upperBoundIterator; + oviinternal::IterationHelper iterationHelper; }; }