#ifndef STORM_SOLVER_LINEAREQUATIONSOLVER_H_ #define STORM_SOLVER_LINEAREQUATIONSOLVER_H_ #include #include #include "storm/solver/AbstractEquationSolver.h" #include "storm/solver/MultiplicationStyle.h" #include "storm/solver/LinearEquationSolverProblemFormat.h" #include "storm/solver/LinearEquationSolverRequirements.h" #include "storm/solver/OptimizationDirection.h" #include "storm/utility/VectorHelper.h" #include "storm/storage/SparseMatrix.h" namespace storm { class Environment; namespace solver { /*! * An interface that represents an abstract linear equation solver. */ template class LinearEquationSolver : public AbstractEquationSolver { public: LinearEquationSolver(); virtual ~LinearEquationSolver() { // Intentionally left empty. } virtual void setMatrix(storm::storage::SparseMatrix const& A) = 0; virtual void setMatrix(storm::storage::SparseMatrix&& A) = 0; /*! * If the solver expects the equation system format, it solves Ax = b. If it it expects a fixed point * format, it solves Ax + b = x. In both versions, the matrix A is required to be square and the problem * is required to have a unique solution. The solution will be written to the vector x. Note that the matrix * A has to be given upon construction time of the solver object. * * @param x The solution vector that has to be computed. Its length must be equal to the number of rows of A. * @param b The vector b. Its length must be equal to the number of rows of A. * * @return true */ bool solveEquations(Environment const& env, std::vector& x, std::vector const& b) const; /*! * Retrieves the format in which this solver expects to solve equations. If the solver expects the equation * system format, it solves Ax = b. If it it expects a fixed point format, it solves Ax + b = x. */ virtual LinearEquationSolverProblemFormat getEquationProblemFormat(Environment const& env) const = 0; /*! * Retrieves the requirements of the solver under the current settings. Note that these requirements only * apply to solving linear equations and not to the matrix vector multiplications. */ virtual LinearEquationSolverRequirements getRequirements(Environment const& env) const; /*! * Sets whether some of the generated data during solver calls should be cached. * This possibly increases the runtime of subsequent calls but also increases memory consumption. */ void setCachingEnabled(bool value) const; /*! * Retrieves whether some of the generated data during solver calls should be cached. */ bool isCachingEnabled() const; /* * Clears the currently cached data that has been stored during previous calls of the solver. */ virtual void clearCache() const; protected: virtual bool internalSolveEquations(Environment const& env, std::vector& x, std::vector const& b) const = 0; // auxiliary storage. If set, this vector has getMatrixRowCount() entries. mutable std::unique_ptr> cachedRowVector; private: /*! * Retrieves the row count of the matrix associated with this solver. */ virtual uint64_t getMatrixRowCount() const = 0; /*! * Retrieves the column count of the matrix associated with this solver. */ virtual uint64_t getMatrixColumnCount() const = 0; /// Whether some of the generated data during solver calls should be cached. mutable bool cachingEnabled; /// An object that can be used to reduce vectors. storm::utility::VectorHelper vectorHelper; }; enum class EquationSolverType; template class LinearEquationSolverFactory { public: virtual ~LinearEquationSolverFactory() = default; /*! * Creates a new linear equation solver instance with the given matrix. * * @param matrix The matrix that defines the equation system. * @return A pointer to the newly created solver. */ std::unique_ptr> create(Environment const& env, storm::storage::SparseMatrix const& matrix) const; /*! * Creates a new linear equation solver instance with the given matrix. The caller gives up posession of the * matrix by calling this function. * * @param matrix The matrix that defines the equation system. * @return A pointer to the newly created solver. */ std::unique_ptr> create(Environment const& env, storm::storage::SparseMatrix&& matrix) const; /*! * Creates an equation solver with the current settings, but without a matrix. */ virtual std::unique_ptr> create(Environment const& env) const = 0; /*! * Creates a copy of this factory. */ virtual std::unique_ptr> clone() const = 0; /*! * Retrieves the problem format that the solver expects if it was created with the current settings. */ virtual LinearEquationSolverProblemFormat getEquationProblemFormat(Environment const& env) const; /*! * Retrieves the requirements of the solver if it was created with the current settings. Note that these * requirements only apply to solving linear equations and not to the matrix vector multiplications. */ LinearEquationSolverRequirements getRequirements(Environment const& env) const; }; template class GeneralLinearEquationSolverFactory : public LinearEquationSolverFactory { public: GeneralLinearEquationSolverFactory(); using LinearEquationSolverFactory::create; virtual std::unique_ptr> create(Environment const& env) const override; virtual std::unique_ptr> clone() const override; }; } // namespace solver } // namespace storm #endif /* STORM_SOLVER_LINEAREQUATIONSOLVER_H_ */