#include "src/solver/NativeLinearEquationSolver.h" #include #include "src/settings/SettingsManager.h" #include "src/settings/modules/NativeEquationSolverSettings.h" #include "src/utility/vector.h" #include "src/exceptions/InvalidStateException.h" #include "src/exceptions/InvalidSettingsException.h" namespace storm { namespace solver { template NativeLinearEquationSolverSettings::NativeLinearEquationSolverSettings() { storm::settings::modules::NativeEquationSolverSettings const& settings = storm::settings::getModule(); storm::settings::modules::NativeEquationSolverSettings::LinearEquationMethod methodAsSetting = settings.getLinearEquationSystemMethod(); if (methodAsSetting == storm::settings::modules::NativeEquationSolverSettings::LinearEquationMethod::GaussSeidel) { method = SolutionMethod::GaussSeidel; } else if (methodAsSetting == storm::settings::modules::NativeEquationSolverSettings::LinearEquationMethod::Jacobi) { method = SolutionMethod::Jacobi; } else if (methodAsSetting == storm::settings::modules::NativeEquationSolverSettings::LinearEquationMethod::SOR) { method = SolutionMethod::SOR; } else { STORM_LOG_THROW(false, storm::exceptions::InvalidSettingsException, "The selected solution technique is invalid for this solver."); } maximalNumberOfIterations = settings.getMaximalIterationCount(); precision = settings.getPrecision(); relative = settings.getConvergenceCriterion() == storm::settings::modules::NativeEquationSolverSettings::ConvergenceCriterion::Relative; omega = storm::settings::getModule().getOmega(); } template void NativeLinearEquationSolverSettings::setSolutionMethod(SolutionMethod const& method) { this->method = method; } template void NativeLinearEquationSolverSettings::setPrecision(ValueType precision) { this->precision = precision; } template void NativeLinearEquationSolverSettings::setMaximalNumberOfIterations(uint64_t maximalNumberOfIterations) { this->maximalNumberOfIterations = maximalNumberOfIterations; } template void NativeLinearEquationSolverSettings::setRelativeTerminationCriterion(bool value) { this->relative = value; } template void NativeLinearEquationSolverSettings::setOmega(ValueType omega) { this->omega = omega; } template typename NativeLinearEquationSolverSettings::SolutionMethod NativeLinearEquationSolverSettings::getSolutionMethod() const { return method; } template ValueType NativeLinearEquationSolverSettings::getPrecision() const { return precision; } template uint64_t NativeLinearEquationSolverSettings::getMaximalNumberOfIterations() const { return maximalNumberOfIterations; } template uint64_t NativeLinearEquationSolverSettings::getRelativeTerminationCriterion() const { return relative; } template ValueType NativeLinearEquationSolverSettings::getOmega() const { return omega; } template NativeLinearEquationSolver::NativeLinearEquationSolver(storm::storage::SparseMatrix const& A, NativeLinearEquationSolverSettings const& settings) : localA(nullptr), A(nullptr), settings(settings) { this->setMatrix(A); } template NativeLinearEquationSolver::NativeLinearEquationSolver(storm::storage::SparseMatrix&& A, NativeLinearEquationSolverSettings const& settings) : localA(nullptr), A(nullptr), settings(settings) { this->setMatrix(std::move(A)); } template void NativeLinearEquationSolver::setMatrix(storm::storage::SparseMatrix const& A) { localA.reset(); this->A = &A; resetAuxiliaryData(); } template void NativeLinearEquationSolver::setMatrix(storm::storage::SparseMatrix&& A) { localA = std::make_unique>(std::move(A)); this->A = localA.get(); resetAuxiliaryData(); } template bool NativeLinearEquationSolver::solveEquations(std::vector& x, std::vector const& b) const { if(!this->auxiliaryRowVector) { this->auxiliaryRowVector = std::make_unique>(getMatrixRowCount()); } if (this->getSettings().getSolutionMethod() == NativeLinearEquationSolverSettings::SolutionMethod::SOR || this->getSettings().getSolutionMethod() == NativeLinearEquationSolverSettings::SolutionMethod::GaussSeidel) { // Define the omega used for SOR. ValueType omega = this->getSettings().getSolutionMethod() == NativeLinearEquationSolverSettings::SolutionMethod::SOR ? this->getSettings().getOmega() : storm::utility::one(); // Set up additional environment variables. uint_fast64_t iterationCount = 0; bool converged = false; while (!converged && iterationCount < this->getSettings().getMaximalNumberOfIterations()) { A->performSuccessiveOverRelaxationStep(omega, x, b); // Now check if the process already converged within our precision. converged = storm::utility::vector::equalModuloPrecision(*this->auxiliaryRowVector, x, static_cast(this->getSettings().getPrecision()), this->getSettings().getRelativeTerminationCriterion()) || (this->hasCustomTerminationCondition() && this->getTerminationCondition().terminateNow(x)); // If we did not yet converge, we need to backup the contents of x. if (!converged) { *this->auxiliaryRowVector = x; } // Increase iteration count so we can abort if convergence is too slow. ++iterationCount; } return converged; } else { // Get a Jacobi decomposition of the matrix A. if(!jacobiDecomposition) { jacobiDecomposition = std::make_unique, std::vector>>(A->getJacobiDecomposition()); } storm::storage::SparseMatrix const& jacobiLU = jacobiDecomposition->first; std::vector const& jacobiD = jacobiDecomposition->second; std::vector* currentX = &x; std::vector* nextX = this->auxiliaryRowVector.get(); // Set up additional environment variables. uint_fast64_t iterationCount = 0; bool converged = false; while (!converged && iterationCount < this->getSettings().getMaximalNumberOfIterations() && !(this->hasCustomTerminationCondition() && this->getTerminationCondition().terminateNow(*currentX))) { // Compute D^-1 * (b - LU * x) and store result in nextX. jacobiLU.multiplyWithVector(*currentX, *nextX); storm::utility::vector::subtractVectors(b, *nextX, *nextX); storm::utility::vector::multiplyVectorsPointwise(jacobiD, *nextX, *nextX); // Now check if the process already converged within our precision. converged = storm::utility::vector::equalModuloPrecision(*currentX, *nextX, static_cast(this->getSettings().getPrecision()), this->getSettings().getRelativeTerminationCriterion()); // Swap the two pointers as a preparation for the next iteration. std::swap(nextX, currentX); // Increase iteration count so we can abort if convergence is too slow. ++iterationCount; } // If the last iteration did not write to the original x we have to swap the contents, because the // output has to be written to the input parameter x. if (currentX == this->auxiliaryRowVector.get()) { std::swap(x, *currentX); } return iterationCount < this->getSettings().getMaximalNumberOfIterations(); } } template void NativeLinearEquationSolver::multiply(std::vector& x, std::vector const* b, std::vector& result) const { if (&x != &result) { A->multiplyWithVector(x, result); if (b != nullptr) { storm::utility::vector::addVectors(result, *b, result); } } else { // If the two vectors are aliases, we need to create a temporary. if(!this->auxiliaryRowVector) { this->auxiliaryRowVector = std::make_unique>(getMatrixRowCount()); } A->multiplyWithVector(x, *this->auxiliaryRowVector); if (b != nullptr) { storm::utility::vector::addVectors(*this->auxiliaryRowVector, *b, result); } else { result.swap(*this->auxiliaryRowVector); } } } template void NativeLinearEquationSolver::setSettings(NativeLinearEquationSolverSettings const& newSettings) { settings = newSettings; } template NativeLinearEquationSolverSettings const& NativeLinearEquationSolver::getSettings() const { return settings; } template void NativeLinearEquationSolver::resetAuxiliaryData() const { jacobiDecomposition.reset(); LinearEquationSolver::resetAuxiliaryData(); } template uint64_t NativeLinearEquationSolver::getMatrixRowCount() const { return this->A->getRowCount(); } template uint64_t NativeLinearEquationSolver::getMatrixColumnCount() const { return this->A->getColumnCount(); } template std::unique_ptr> NativeLinearEquationSolverFactory::create(storm::storage::SparseMatrix const& matrix) const { return std::make_unique>(matrix, settings); } template std::unique_ptr> NativeLinearEquationSolverFactory::create(storm::storage::SparseMatrix&& matrix) const { return std::make_unique>(std::move(matrix), settings); } template NativeLinearEquationSolverSettings& NativeLinearEquationSolverFactory::getSettings() { return settings; } template NativeLinearEquationSolverSettings const& NativeLinearEquationSolverFactory::getSettings() const { return settings; } template std::unique_ptr> NativeLinearEquationSolverFactory::clone() const { return std::make_unique>(*this); } // Explicitly instantiate the linear equation solver. template class NativeLinearEquationSolverSettings; template class NativeLinearEquationSolver; template class NativeLinearEquationSolverFactory; } }