Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SymbolicNativeLinearEquationSolver.cpp
Go to the documentation of this file.
2
3#include "storm/dd/Add.h"
12#include "storm/utility/dd.h"
14
15namespace storm {
16namespace solver {
17
18template<storm::dd::DdType DdType, typename ValueType>
22
23template<storm::dd::DdType DdType, typename ValueType>
25 storm::dd::Add<DdType, ValueType> const& A, storm::dd::Bdd<DdType> const& allRows, std::set<storm::expressions::Variable> const& rowMetaVariables,
26 std::set<storm::expressions::Variable> const& columnMetaVariables,
27 std::vector<std::pair<storm::expressions::Variable, storm::expressions::Variable>> const& rowColumnMetaVariablePairs)
29 this->setMatrix(A);
30}
31
32template<storm::dd::DdType DdType, typename ValueType>
34 storm::dd::Bdd<DdType> const& allRows, std::set<storm::expressions::Variable> const& rowMetaVariables,
35 std::set<storm::expressions::Variable> const& columnMetaVariables,
36 std::vector<std::pair<storm::expressions::Variable, storm::expressions::Variable>> const& rowColumnMetaVariablePairs)
38 // Intentionally left empty.
39}
40
41template<storm::dd::DdType DdType, typename ValueType>
42NativeLinearEquationSolverMethod SymbolicNativeLinearEquationSolver<DdType, ValueType>::getMethod(Environment const& env, bool isExactMode) const {
43 // Adjust the method if it is not supported by this solver.
44 // none was specified and we want exact computations
45 auto method = env.solver().native().getMethod();
46
47 if (isExactMode) {
48 if (method != NativeLinearEquationSolverMethod::RationalSearch) {
49 if (env.solver().native().isMethodSetFromDefault()) {
50 method = NativeLinearEquationSolverMethod::RationalSearch;
52 "Selecting '" + toString(method) +
53 "' as the solution technique to guarantee exact results. If you want to override this, please explicitly specify a different method.");
54 } else {
55 STORM_LOG_WARN("The selected solution method does not guarantee exact results.");
56 }
57 }
58 } else {
59 if (method != NativeLinearEquationSolverMethod::Power && method != NativeLinearEquationSolverMethod::RationalSearch &&
60 method != NativeLinearEquationSolverMethod::Jacobi) {
61 method = NativeLinearEquationSolverMethod::Jacobi;
62 STORM_LOG_INFO("The selected solution method is not supported in the dd engine. Falling back to '" + toString(method) + "'.");
63 }
64 STORM_LOG_WARN_COND_DEBUG(!env.solver().isForceSoundness(), "Sound computations are not supported in the Dd engine.");
65 }
66 return method;
67}
68
69template<storm::dd::DdType DdType, typename ValueType>
72 storm::dd::Add<DdType, ValueType> const& b) const {
73 switch (getMethod(env, std::is_same<ValueType, storm::RationalNumber>::value)) {
74 case NativeLinearEquationSolverMethod::Jacobi:
75 return solveEquationsJacobi(env, x, b);
76 case NativeLinearEquationSolverMethod::Power:
77 return solveEquationsPower(env, x, b);
78 case NativeLinearEquationSolverMethod::RationalSearch:
79 return solveEquationsRationalSearch(env, x, b);
80 default:
81 STORM_LOG_THROW(false, storm::exceptions::NotSupportedException, "The selected solution technique is not supported.");
83 }
84}
85
86template<storm::dd::DdType DdType, typename ValueType>
87storm::dd::Add<DdType, ValueType> SymbolicNativeLinearEquationSolver<DdType, ValueType>::solveEquationsJacobi(
89 storm::dd::DdManager<DdType>& manager = this->getDdManager();
90
92 uint64_t maxIter = env.solver().native().getMaximalNumberOfIterations();
93 bool relative = env.solver().native().getRelativeTerminationCriterion();
94
95 STORM_LOG_INFO("Solving symbolic linear equation system with NativeLinearEquationSolver (jacobi)");
96
97 // Start by computing the Jacobi decomposition of the matrix A.
98 storm::dd::Bdd<DdType> diagonal = storm::utility::dd::getRowColumnDiagonal(x.getDdManager(), this->rowColumnMetaVariablePairs);
99 diagonal &= this->allRows;
100
101 storm::dd::Add<DdType, ValueType> lu = diagonal.ite(manager.template getAddZero<ValueType>(), this->A);
102 storm::dd::Add<DdType, ValueType> diagonalAdd = diagonal.template toAdd<ValueType>();
103 storm::dd::Add<DdType, ValueType> diag = diagonalAdd.multiplyMatrix(this->A, this->columnMetaVariables);
104
105 storm::dd::Add<DdType, ValueType> scaledLu = lu / diag;
106 storm::dd::Add<DdType, ValueType> scaledB = b / diag;
107
108 // Set up additional environment variables.
110 uint_fast64_t iterationCount = 0;
111 bool converged = false;
112
113 while (!converged && iterationCount < maxIter) {
114 storm::dd::Add<DdType, ValueType> xCopyAsColumn = xCopy.swapVariables(this->rowColumnMetaVariablePairs);
115 storm::dd::Add<DdType, ValueType> tmp = scaledB - scaledLu.multiplyMatrix(xCopyAsColumn, this->columnMetaVariables);
116
117 // Now check if the process already converged within our precision.
118 converged = tmp.equalModuloPrecision(xCopy, precision, relative);
119
120 xCopy = tmp;
121
122 // Increase iteration count so we can abort if convergence is too slow.
123 ++iterationCount;
124 }
125
126 if (converged) {
127 STORM_LOG_INFO("Iterative solver (jacobi) converged in " << iterationCount << " iterations.");
128 } else {
129 STORM_LOG_WARN("Iterative solver (jacobi) did not converge in " << iterationCount << " iterations.");
130 }
131
132 return xCopy;
133}
134
135template<storm::dd::DdType DdType, typename ValueType>
136typename SymbolicNativeLinearEquationSolver<DdType, ValueType>::PowerIterationResult
137SymbolicNativeLinearEquationSolver<DdType, ValueType>::performPowerIteration(storm::dd::Add<DdType, ValueType> const& x,
138 storm::dd::Add<DdType, ValueType> const& b, ValueType const& precision,
139 bool relativeTerminationCriterion, uint64_t maximalIterations) const {
140 // Set up additional environment variables.
141 storm::dd::Add<DdType, ValueType> currentX = x;
142 uint_fast64_t iterations = 0;
144
145 while (status == SolverStatus::InProgress && iterations < maximalIterations) {
146 storm::dd::Add<DdType, ValueType> currentXAsColumn = currentX.swapVariables(this->rowColumnMetaVariablePairs);
147 storm::dd::Add<DdType, ValueType> tmp = this->A.multiplyMatrix(currentXAsColumn, this->columnMetaVariables) + b;
148
149 // Now check if the process already converged within our precision.
150 if (tmp.equalModuloPrecision(currentX, precision, relativeTerminationCriterion)) {
152 }
153
154 // Set up next iteration.
155 ++iterations;
156 currentX = tmp;
157 }
158
159 return PowerIterationResult(status, iterations, currentX);
160}
161
162template<storm::dd::DdType DdType, typename ValueType>
163storm::dd::Add<DdType, ValueType> SymbolicNativeLinearEquationSolver<DdType, ValueType>::solveEquationsPower(Environment const& env,
164 storm::dd::Add<DdType, ValueType> const& x,
165 storm::dd::Add<DdType, ValueType> const& b) const {
166 STORM_LOG_INFO("Solving symbolic linear equation system with NativeLinearEquationSolver (power)");
167 ValueType precision = storm::utility::convertNumber<ValueType>(env.solver().native().getPrecision());
168 PowerIterationResult result =
169 performPowerIteration(x, b, precision, env.solver().native().getRelativeTerminationCriterion(), env.solver().native().getMaximalNumberOfIterations());
170
171 if (result.status == SolverStatus::Converged) {
172 STORM_LOG_INFO("Iterative solver (power iteration) converged in " << result.iterations << " iterations.");
173 } else {
174 STORM_LOG_WARN("Iterative solver (power iteration) did not converge in " << result.iterations << " iterations.");
175 }
176
177 return result.values;
178}
179
180template<storm::dd::DdType DdType, typename ValueType>
181bool SymbolicNativeLinearEquationSolver<DdType, ValueType>::isSolutionFixedPoint(storm::dd::Add<DdType, ValueType> const& x,
182 storm::dd::Add<DdType, ValueType> const& b) const {
183 storm::dd::Add<DdType, ValueType> xAsColumn = x.swapVariables(this->rowColumnMetaVariablePairs);
184 storm::dd::Add<DdType, ValueType> tmp = this->A.multiplyMatrix(xAsColumn, this->columnMetaVariables);
185 tmp += b;
186
187 return x == tmp;
188}
189
190template<storm::dd::DdType DdType, typename ValueType>
191template<typename RationalType, typename ImpreciseType>
192storm::dd::Add<DdType, RationalType> SymbolicNativeLinearEquationSolver<DdType, ValueType>::sharpen(
193 uint64_t precision, SymbolicNativeLinearEquationSolver<DdType, RationalType> const& rationalSolver, storm::dd::Add<DdType, ImpreciseType> const& x,
194 storm::dd::Add<DdType, RationalType> const& rationalB, bool& isSolution) {
195 storm::dd::Add<DdType, RationalType> sharpenedX;
196
197 for (uint64_t p = 1; p <= precision; ++p) {
198 sharpenedX = x.sharpenKwekMehlhorn(p);
199
200 isSolution = rationalSolver.isSolutionFixedPoint(sharpenedX, rationalB);
201 if (isSolution) {
202 break;
203 }
204 }
205
206 return sharpenedX;
207}
208
209template<storm::dd::DdType DdType, typename ValueType>
210template<typename RationalType, typename ImpreciseType>
211storm::dd::Add<DdType, RationalType> SymbolicNativeLinearEquationSolver<DdType, ValueType>::solveEquationsRationalSearchHelper(
212 Environment const& env, SymbolicNativeLinearEquationSolver<DdType, RationalType> const& rationalSolver,
213 SymbolicNativeLinearEquationSolver<DdType, ImpreciseType> const& impreciseSolver, storm::dd::Add<DdType, RationalType> const& rationalB,
214 storm::dd::Add<DdType, ImpreciseType> const& x, storm::dd::Add<DdType, ImpreciseType> const& b) const {
215 // Storage for the rational sharpened vector and the power iteration intermediate vector.
216 storm::dd::Add<DdType, RationalType> sharpenedX;
217 storm::dd::Add<DdType, ImpreciseType> currentX = x;
218
219 // The actual rational search.
220 uint64_t overallIterations = 0;
221 uint64_t powerIterationInvocations = 0;
222 ValueType precision = storm::utility::convertNumber<ValueType>(env.solver().native().getPrecision());
223 uint64_t maxIter = env.solver().native().getMaximalNumberOfIterations();
224 bool relative = env.solver().native().getRelativeTerminationCriterion();
226 while (status == SolverStatus::InProgress && overallIterations < maxIter) {
227 typename SymbolicNativeLinearEquationSolver<DdType, ImpreciseType>::PowerIterationResult result = impreciseSolver.performPowerIteration(
228 currentX, b, storm::utility::convertNumber<ImpreciseType, ValueType>(precision), relative, maxIter - overallIterations);
229
230 ++powerIterationInvocations;
231 STORM_LOG_TRACE("Completed " << powerIterationInvocations << " power iteration invocations, the last one with precision " << precision
232 << " completed in " << result.iterations << " iterations.");
233
234 // Count the iterations.
235 overallIterations += result.iterations;
236
237 // Compute maximal precision until which to sharpen.
238 uint64_t p =
240
241 bool isSolution = false;
242 sharpenedX = sharpen<RationalType, ImpreciseType>(p, rationalSolver, result.values, rationalB, isSolution);
243
244 if (isSolution) {
246 } else {
247 currentX = result.values;
249 }
251 status = SolverStatus::Aborted;
252 }
253 }
254
255 if (status == SolverStatus::InProgress && overallIterations == maxIter) {
257 }
258
259 if (status == SolverStatus::Converged) {
260 STORM_LOG_INFO("Iterative solver (rational search) converged in " << overallIterations << " iterations.");
261 } else {
262 STORM_LOG_WARN("Iterative solver (rational search) did not converge in " << overallIterations << " iterations.");
263 }
264
265 return sharpenedX;
266}
267
268template<storm::dd::DdType DdType, typename ValueType>
269template<typename ImpreciseType>
270typename std::enable_if<std::is_same<ValueType, ImpreciseType>::value && storm::NumberTraits<ValueType>::IsExact, storm::dd::Add<DdType, ValueType>>::type
271SymbolicNativeLinearEquationSolver<DdType, ValueType>::solveEquationsRationalSearchHelper(Environment const& env, storm::dd::Add<DdType, ValueType> const& x,
272 storm::dd::Add<DdType, ValueType> const& b) const {
273 return solveEquationsRationalSearchHelper<ValueType, ValueType>(env, *this, *this, b, this->getLowerBoundsVector(), b);
274}
275
276template<storm::dd::DdType DdType, typename ValueType>
277template<typename ImpreciseType>
278typename std::enable_if<std::is_same<ValueType, ImpreciseType>::value && !storm::NumberTraits<ValueType>::IsExact, storm::dd::Add<DdType, ValueType>>::type
279SymbolicNativeLinearEquationSolver<DdType, ValueType>::solveEquationsRationalSearchHelper(Environment const& env, storm::dd::Add<DdType, ValueType> const& x,
280 storm::dd::Add<DdType, ValueType> const& b) const {
281 if constexpr (storm::dd::BackendTraits<DdType>::template supportsValueType<storm::RationalNumber>) {
282 storm::dd::Add<DdType, storm::RationalNumber> rationalB = b.template toValueType<storm::RationalNumber>();
283 SymbolicNativeLinearEquationSolver<DdType, storm::RationalNumber> rationalSolver(this->A.template toValueType<storm::RationalNumber>(), this->allRows,
284 this->rowMetaVariables, this->columnMetaVariables,
285 this->rowColumnMetaVariablePairs);
286
287 storm::dd::Add<DdType, storm::RationalNumber> rationalResult =
288 solveEquationsRationalSearchHelper<storm::RationalNumber, ImpreciseType>(env, rationalSolver, *this, rationalB, this->getLowerBoundsVector(), b);
289 return rationalResult.template toValueType<ValueType>();
290 } else {
291 STORM_LOG_THROW_UNCONDITIONALLY(storm::exceptions::NotSupportedException,
292 "Rational search is not supported, as the selected DD library does not support rational numbers.");
293 }
294}
295
296template<storm::dd::DdType DdType, typename ValueType>
297template<typename ImpreciseType>
298typename std::enable_if<!std::is_same<ValueType, ImpreciseType>::value, storm::dd::Add<DdType, ValueType>>::type
299SymbolicNativeLinearEquationSolver<DdType, ValueType>::solveEquationsRationalSearchHelper(Environment const& env, storm::dd::Add<DdType, ValueType> const& x,
300 storm::dd::Add<DdType, ValueType> const& b) const {
301 // First try to find a solution using the imprecise value type.
302 storm::dd::Add<DdType, ValueType> rationalResult;
303 storm::dd::Add<DdType, ImpreciseType> impreciseX;
304 try {
305 impreciseX = this->getLowerBoundsVector().template toValueType<ImpreciseType>();
306 storm::dd::Add<DdType, ImpreciseType> impreciseB = b.template toValueType<ImpreciseType>();
308 this->A.template toValueType<ImpreciseType>(), this->allRows, this->rowMetaVariables, this->columnMetaVariables, this->rowColumnMetaVariablePairs);
309
310 rationalResult = solveEquationsRationalSearchHelper<ValueType, ImpreciseType>(env, *this, impreciseSolver, b, impreciseX, impreciseB);
311 } catch (storm::exceptions::PrecisionExceededException const&) {
312 STORM_LOG_WARN("Precision of value type was exceeded, trying to recover by switching to rational arithmetic.");
313
314 // Fall back to precise value type if the precision of the imprecise value type was exceeded.
315 rationalResult = solveEquationsRationalSearchHelper<ValueType, ValueType>(env, *this, *this, b, impreciseX.template toValueType<ValueType>(), b);
316 }
317 return rationalResult.template toValueType<ValueType>();
318}
319
320template<storm::dd::DdType DdType, typename ValueType>
321storm::dd::Add<DdType, ValueType> SymbolicNativeLinearEquationSolver<DdType, ValueType>::solveEquationsRationalSearch(
322 Environment const& env, storm::dd::Add<DdType, ValueType> const& x, storm::dd::Add<DdType, ValueType> const& b) const {
323 STORM_LOG_INFO("Solving symbolic linear equation system with NativeLinearEquationSolver (rational search)");
324 return solveEquationsRationalSearchHelper<double>(env, x, b);
325}
326
327template<storm::dd::DdType DdType, typename ValueType>
329 auto method = getMethod(env, std::is_same<ValueType, storm::RationalNumber>::value);
330 if (method == NativeLinearEquationSolverMethod::Jacobi) {
332 }
334}
335
336template<storm::dd::DdType DdType, typename ValueType>
339 auto method = getMethod(env, std::is_same<ValueType, storm::RationalNumber>::value);
340 if (method == NativeLinearEquationSolverMethod::RationalSearch) {
341 requirements.requireLowerBounds();
342 }
343 return requirements;
344}
345
346template<storm::dd::DdType DdType, typename ValueType>
347std::unique_ptr<storm::solver::SymbolicLinearEquationSolver<DdType, ValueType>> SymbolicNativeLinearEquationSolverFactory<DdType, ValueType>::create(
348 Environment const& env) const {
349 return std::make_unique<SymbolicNativeLinearEquationSolver<DdType, ValueType>>();
350}
351
358} // namespace solver
359} // namespace storm
SolverEnvironment & solver()
storm::RationalNumber const & getPrecision() const
uint64_t const & getMaximalNumberOfIterations() const
bool const & getRelativeTerminationCriterion() const
storm::solver::NativeLinearEquationSolverMethod const & getMethod() const
NativeSolverEnvironment & native()
Add< LibraryType, ValueType > swapVariables(std::vector< std::pair< storm::expressions::Variable, storm::expressions::Variable > > const &metaVariablePairs) const
Swaps the given pairs of meta variables in the ADD.
Definition Add.cpp:276
Add< LibraryType, storm::RationalNumber > sharpenKwekMehlhorn(uint64_t precision) const
Retrieves the function that sharpens all values in the current ADD with the Kwek-Mehlhorn algorithm.
Definition Add.cpp:144
Add< LibraryType, ValueType > multiplyMatrix(Add< LibraryType, ValueType > const &otherMatrix, std::set< storm::expressions::Variable > const &summationMetaVariables) const
Multiplies the current ADD (representing a matrix) with the given matrix by summing over the given me...
Definition Add.cpp:356
bool equalModuloPrecision(Add< LibraryType, ValueType > const &other, ValueType const &precision, bool relative=true) const
Checks whether the current and the given ADD represent the same function modulo some given precision.
Definition Add.cpp:195
Bdd< LibraryType > ite(Bdd< LibraryType > const &thenBdd, Bdd< LibraryType > const &elseBdd) const
Performs an if-then-else with the given operands, i.e.
Definition Bdd.cpp:107
DdManager< LibraryType > & getDdManager() const
Retrieves the manager that is responsible for this DD.
Definition Dd.cpp:38
LinearEquationSolverRequirements & requireLowerBounds(bool critical=true)
std::vector< std::pair< storm::expressions::Variable, storm::expressions::Variable > > rowColumnMetaVariablePairs
virtual std::unique_ptr< storm::solver::SymbolicLinearEquationSolver< DdType, ValueType > > create(Environment const &env) const override
An interface that represents an abstract symbolic linear equation solver.
virtual LinearEquationSolverRequirements getRequirements(Environment const &env) const override
Retrieves the requirements of the solver under the current settings.
virtual storm::dd::Add< DdType, ValueType > solveEquations(Environment const &env, storm::dd::Add< DdType, ValueType > const &x, storm::dd::Add< DdType, ValueType > const &b) const override
Solves the equation system A*x = b.
virtual LinearEquationSolverProblemFormat getEquationProblemFormat(Environment const &env) const override
Retrieves the format in which this solver expects to solve equations.
#define STORM_LOG_INFO(message)
Definition logging.h:34
#define STORM_LOG_WARN(message)
Definition logging.h:35
#define STORM_LOG_TRACE(message)
Definition logging.h:22
#define STORM_LOG_THROW_UNCONDITIONALLY(exception, message)
Definition macros.h:48
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
#define STORM_LOG_WARN_COND_DEBUG(cond, message)
Definition macros.h:16
SFTBDDChecker::ValueType ValueType
std::string toString(GurobiSolverMethod const &method)
Yields a string representation of the GurobiSolverMethod.
storm::dd::Bdd< Type > getRowColumnDiagonal(storm::dd::DdManager< Type > const &ddManager, std::vector< std::pair< storm::expressions::Variable, storm::expressions::Variable > > const &rowColumnMetaVariablePairs)
Definition dd.cpp:88
bool isTerminate()
Check whether the program should terminate (due to some abort signal).
ValueType ceil(ValueType const &number)
ValueType one()
Definition constants.cpp:21
ValueType log10(ValueType const &number)
TargetType convertNumber(SourceType const &number)
The contract of a DD backend.
static const bool IsExact