#include "storm/solver/SymbolicMinMaxLinearEquationSolver.h" #include "storm/storage/dd/DdManager.h" #include "storm/storage/dd/Add.h" #include "storm/storage/dd/Bdd.h" #include "storm/utility/constants.h" #include "storm/settings/SettingsManager.h" #include "storm/settings/modules/MinMaxEquationSolverSettings.h" #include "storm/utility/dd.h" #include "storm/utility/macros.h" #include "storm/exceptions/InvalidSettingsException.h" namespace storm { namespace solver { template SymbolicMinMaxLinearEquationSolverSettings::SymbolicMinMaxLinearEquationSolverSettings() { // Get the settings object to customize linear solving. storm::settings::modules::MinMaxEquationSolverSettings const& settings = storm::settings::getModule(); maximalNumberOfIterations = settings.getMaximalIterationCount(); precision = storm::utility::convertNumber(settings.getPrecision()); relative = settings.getConvergenceCriterion() == storm::settings::modules::MinMaxEquationSolverSettings::ConvergenceCriterion::Relative; auto method = settings.getMinMaxEquationSolvingMethod(); switch (method) { case MinMaxMethod::ValueIteration: this->solutionMethod = SolutionMethod::ValueIteration; break; case MinMaxMethod::PolicyIteration: this->solutionMethod = SolutionMethod::PolicyIteration; break; default: STORM_LOG_THROW(false, storm::exceptions::InvalidSettingsException, "Unsupported technique."); } } template void SymbolicMinMaxLinearEquationSolverSettings::setSolutionMethod(SolutionMethod const& solutionMethod) { this->solutionMethod = solutionMethod; } template void SymbolicMinMaxLinearEquationSolverSettings::setMaximalNumberOfIterations(uint64_t maximalNumberOfIterations) { this->maximalNumberOfIterations = maximalNumberOfIterations; } template void SymbolicMinMaxLinearEquationSolverSettings::setRelativeTerminationCriterion(bool value) { this->relative = value; } template void SymbolicMinMaxLinearEquationSolverSettings::setPrecision(ValueType precision) { this->precision = precision; } template typename SymbolicMinMaxLinearEquationSolverSettings::SolutionMethod const& SymbolicMinMaxLinearEquationSolverSettings::getSolutionMethod() const { return solutionMethod; } template uint64_t SymbolicMinMaxLinearEquationSolverSettings::getMaximalNumberOfIterations() const { return maximalNumberOfIterations; } template ValueType SymbolicMinMaxLinearEquationSolverSettings::getPrecision() const { return precision; } template bool SymbolicMinMaxLinearEquationSolverSettings::getRelativeTerminationCriterion() const { return relative; } template SymbolicMinMaxLinearEquationSolver::SymbolicMinMaxLinearEquationSolver(storm::dd::Add const& A, storm::dd::Bdd const& allRows, storm::dd::Bdd const& illegalMask, std::set const& rowMetaVariables, std::set const& columnMetaVariables, std::set const& choiceVariables, std::vector> const& rowColumnMetaVariablePairs, std::unique_ptr>&& linearEquationSolverFactory, SymbolicMinMaxLinearEquationSolverSettings const& settings) : A(A), allRows(allRows), illegalMaskAdd(illegalMask.ite(A.getDdManager().getConstant(storm::utility::infinity()), A.getDdManager().template getAddZero())), rowMetaVariables(rowMetaVariables), columnMetaVariables(columnMetaVariables), choiceVariables(choiceVariables), rowColumnMetaVariablePairs(rowColumnMetaVariablePairs), linearEquationSolverFactory(std::move(linearEquationSolverFactory)), settings(settings) { // Intentionally left empty. } template storm::dd::Add SymbolicMinMaxLinearEquationSolver::solveEquations(bool minimize, storm::dd::Add const& x, storm::dd::Add const& b) const { switch (this->getSettings().getSolutionMethod()) { case SymbolicMinMaxLinearEquationSolverSettings::SolutionMethod::ValueIteration: return solveEquationsValueIteration(minimize, x, b); break; case SymbolicMinMaxLinearEquationSolverSettings::SolutionMethod::PolicyIteration: return solveEquationsPolicyIteration(minimize, x, b); break; } } template storm::dd::Add SymbolicMinMaxLinearEquationSolver::solveEquationsValueIteration(bool minimize, storm::dd::Add const& x, storm::dd::Add const& b) const { // Set up the environment. storm::dd::Add xCopy = x; uint_fast64_t iterations = 0; bool converged = false; while (!converged && iterations < this->settings.getMaximalNumberOfIterations()) { // Compute tmp = A * x + b storm::dd::Add xCopyAsColumn = xCopy.swapVariables(this->rowColumnMetaVariablePairs); storm::dd::Add tmp = this->A.multiplyMatrix(xCopyAsColumn, this->columnMetaVariables); tmp += b; if (minimize) { tmp += illegalMaskAdd; tmp = tmp.minAbstract(this->choiceVariables); } else { tmp = tmp.maxAbstract(this->choiceVariables); } // Now check if the process already converged within our precision. converged = xCopy.equalModuloPrecision(tmp, this->settings.getPrecision(), this->settings.getRelativeTerminationCriterion()); xCopy = tmp; ++iterations; } if (converged) { STORM_LOG_INFO("Iterative solver (value iteration) converged in " << iterations << " iterations."); } else { STORM_LOG_WARN("Iterative solver (value iteration) did not converge in " << iterations << " iterations."); } return xCopy; } template storm::dd::Add SymbolicMinMaxLinearEquationSolver::solveEquationsPolicyIteration(bool minimize, storm::dd::Add const& x, storm::dd::Add const& b) const { // Set up the environment. storm::dd::Add currentSolution = x; storm::dd::Add diagonal = (storm::utility::dd::getRowColumnDiagonal(x.getDdManager(), this->rowColumnMetaVariablePairs) && this->allRows).template toAdd(); uint_fast64_t iterations = 0; bool converged = false; // Pick arbitrary initial scheduler. storm::dd::Bdd scheduler = this->A.sumAbstract(this->columnMetaVariables).maxAbstractRepresentative(this->choiceVariables); // And apply it to the matrix and vector. storm::dd::Add schedulerA = diagonal - scheduler.ite(this->A, scheduler.getDdManager().template getAddZero()).sumAbstract(this->choiceVariables); storm::dd::Add schedulerB = scheduler.ite(b, scheduler.getDdManager().template getAddZero()).sumAbstract(this->choiceVariables); // Initialize linear equation solver. std::unique_ptr> linearEquationSolver = linearEquationSolverFactory->create(schedulerA, this->allRows, this->rowMetaVariables, this->columnMetaVariables, this->rowColumnMetaVariablePairs); // Iteratively solve and improve the scheduler. while (!converged && iterations < this->settings.getMaximalNumberOfIterations()) { // Solve for the value of the scheduler. storm::dd::Add schedulerX = linearEquationSolver->solveEquations(currentSolution, schedulerB); // Policy improvement step. storm::dd::Add choiceValues = this->A.multiplyMatrix(schedulerX.swapVariables(this->rowColumnMetaVariablePairs), this->columnMetaVariables) + b; storm::dd::Bdd nextScheduler; if (minimize) { choiceValues += illegalMaskAdd; nextScheduler = choiceValues.minAbstractRepresentative(this->choiceVariables); } else { nextScheduler = choiceValues.maxAbstractRepresentative(this->choiceVariables); } // Check for convergence. converged = nextScheduler == scheduler; // Set up next iteration. if (!converged) { scheduler = nextScheduler; schedulerA = diagonal - scheduler.ite(this->A, scheduler.getDdManager().template getAddZero()).sumAbstract(this->choiceVariables); linearEquationSolver->setMatrix(schedulerA); schedulerB = scheduler.ite(b, scheduler.getDdManager().template getAddZero()).sumAbstract(this->choiceVariables); } currentSolution = schedulerX; ++iterations; } if (converged) { STORM_LOG_INFO("Iterative solver (policy iteration) converged in " << iterations << " iterations."); } else { STORM_LOG_WARN("Iterative solver (policy iteration) did not converge in " << iterations << " iterations."); } return currentSolution; } template storm::dd::Add SymbolicMinMaxLinearEquationSolver::multiply(bool minimize, storm::dd::Add const& x, storm::dd::Add const* b, uint_fast64_t n) const { storm::dd::Add xCopy = x; // Perform matrix-vector multiplication while the bound is met. for (uint_fast64_t i = 0; i < n; ++i) { xCopy = xCopy.swapVariables(this->rowColumnMetaVariablePairs); xCopy = this->A.multiplyMatrix(xCopy, this->columnMetaVariables); if (b != nullptr) { xCopy += *b; } if (minimize) { // This is a hack and only here because of the lack of a suitable minAbstract/maxAbstract function // that can properly deal with a restriction of the choices. xCopy += illegalMaskAdd; xCopy = xCopy.minAbstract(this->choiceVariables); } else { xCopy = xCopy.maxAbstract(this->choiceVariables); } } return xCopy; } template SymbolicMinMaxLinearEquationSolverSettings const& SymbolicMinMaxLinearEquationSolver::getSettings() const { return settings; } template std::unique_ptr> SymbolicGeneralMinMaxLinearEquationSolverFactory::create(storm::dd::Add const& A, storm::dd::Bdd const& allRows, storm::dd::Bdd const& illegalMask, std::set const& rowMetaVariables, std::set const& columnMetaVariables, std::set const& choiceVariables, std::vector> const& rowColumnMetaVariablePairs) const { return std::make_unique>(A, allRows, illegalMask, rowMetaVariables, columnMetaVariables, choiceVariables, rowColumnMetaVariablePairs, std::make_unique>(), settings); } template SymbolicMinMaxLinearEquationSolverSettings& SymbolicGeneralMinMaxLinearEquationSolverFactory::getSettings() { return settings; } template SymbolicMinMaxLinearEquationSolverSettings const& SymbolicGeneralMinMaxLinearEquationSolverFactory::getSettings() const { return settings; } template class SymbolicMinMaxLinearEquationSolverSettings; template class SymbolicMinMaxLinearEquationSolverSettings; template class SymbolicMinMaxLinearEquationSolver; template class SymbolicMinMaxLinearEquationSolver; template class SymbolicMinMaxLinearEquationSolver; template class SymbolicGeneralMinMaxLinearEquationSolverFactory; template class SymbolicGeneralMinMaxLinearEquationSolverFactory; template class SymbolicGeneralMinMaxLinearEquationSolverFactory; } }