You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
13 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/solver/GmmxxLinearEquationSolver.h"
  4. #include "src/settings/SettingsManager.h"
  5. #include "src/settings/modules/GmmxxEquationSolverSettings.h"
  6. TEST(GmmxxLinearEquationSolver, SolveWithStandardOptions) {
  7. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  8. storm::storage::SparseMatrixBuilder<double> builder;
  9. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  10. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  11. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  12. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  13. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  14. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  15. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  16. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  17. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  18. storm::storage::SparseMatrix<double> A;
  19. ASSERT_NO_THROW(A = builder.build());
  20. std::vector<double> x(3);
  21. std::vector<double> b = {16, -4, -7};
  22. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(A));
  23. storm::solver::GmmxxLinearEquationSolver<double> solver(A);
  24. ASSERT_NO_THROW(solver.solveEquations(x, b));
  25. ASSERT_LT(std::abs(x[0] - 1), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  26. ASSERT_LT(std::abs(x[1] - 3), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  27. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  28. }
  29. TEST(GmmxxLinearEquationSolver, gmres) {
  30. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  31. storm::storage::SparseMatrixBuilder<double> builder;
  32. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  33. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  34. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  35. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  36. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  37. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  38. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  39. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  40. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  41. storm::storage::SparseMatrix<double> A;
  42. ASSERT_NO_THROW(A = builder.build());
  43. std::vector<double> x(3);
  44. std::vector<double> b = {16, -4, -7};
  45. storm::solver::GmmxxLinearEquationSolver<double> solver(A);
  46. solver.getSettings().setSolutionMethod(storm::solver::GmmxxLinearEquationSolverSettings<double>::SolutionMethod::Gmres);
  47. solver.getSettings().setPrecision(1e-6);
  48. solver.getSettings().setMaximalNumberOfIterations(10000);
  49. solver.getSettings().setPreconditioner(storm::solver::GmmxxLinearEquationSolverSettings<double>::Preconditioner::None);
  50. solver.getSettings().setNumberOfIterationsUntilRestart(50);
  51. ASSERT_NO_THROW(solver.solveEquations(x, b));
  52. ASSERT_LT(std::abs(x[0] - 1), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  53. ASSERT_LT(std::abs(x[1] - 3), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  54. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  55. }
  56. TEST(GmmxxLinearEquationSolver, qmr) {
  57. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  58. storm::storage::SparseMatrixBuilder<double> builder;
  59. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  60. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  61. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  62. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  63. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  64. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  65. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  66. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  67. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  68. storm::storage::SparseMatrix<double> A;
  69. ASSERT_NO_THROW(A = builder.build());
  70. std::vector<double> x(3);
  71. std::vector<double> b = {16, -4, -7};
  72. storm::solver::GmmxxLinearEquationSolver<double> solver(A);
  73. solver.getSettings().setSolutionMethod(storm::solver::GmmxxLinearEquationSolverSettings<double>::SolutionMethod::Qmr);
  74. solver.getSettings().setPrecision(1e-6);
  75. solver.getSettings().setMaximalNumberOfIterations(10000);
  76. solver.getSettings().setPreconditioner(storm::solver::GmmxxLinearEquationSolverSettings<double>::Preconditioner::None);
  77. storm::solver::GmmxxLinearEquationSolver<double> solver2(A);
  78. solver2.getSettings().setSolutionMethod(storm::solver::GmmxxLinearEquationSolverSettings<double>::SolutionMethod::Qmr);
  79. solver2.getSettings().setPrecision(1e-6);
  80. solver2.getSettings().setMaximalNumberOfIterations(10000);
  81. solver2.getSettings().setPreconditioner(storm::solver::GmmxxLinearEquationSolverSettings<double>::Preconditioner::None);
  82. ASSERT_NO_THROW(solver2.solveEquations(x, b));
  83. ASSERT_LT(std::abs(x[0] - 1), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  84. ASSERT_LT(std::abs(x[1] - 3), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  85. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  86. }
  87. TEST(GmmxxLinearEquationSolver, bicgstab) {
  88. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  89. storm::storage::SparseMatrixBuilder<double> builder;
  90. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  91. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  92. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  93. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  94. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  95. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  96. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  97. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  98. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  99. storm::storage::SparseMatrix<double> A;
  100. ASSERT_NO_THROW(A = builder.build());
  101. std::vector<double> x(3);
  102. std::vector<double> b = {16, -4, -7};
  103. storm::solver::GmmxxLinearEquationSolver<double> solver(A);
  104. solver.getSettings().setSolutionMethod(storm::solver::GmmxxLinearEquationSolverSettings<double>::SolutionMethod::Bicgstab);
  105. solver.getSettings().setPrecision(1e-6);
  106. solver.getSettings().setMaximalNumberOfIterations(10000);
  107. solver.getSettings().setPreconditioner(storm::solver::GmmxxLinearEquationSolverSettings<double>::Preconditioner::None);
  108. ASSERT_NO_THROW(solver.solveEquations(x, b));
  109. ASSERT_LT(std::abs(x[0] - 1), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  110. ASSERT_LT(std::abs(x[1] - 3), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  111. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  112. }
  113. TEST(GmmxxLinearEquationSolver, jacobi) {
  114. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  115. storm::storage::SparseMatrixBuilder<double> builder;
  116. ASSERT_NO_THROW(builder.addNextValue(0, 0, 4));
  117. ASSERT_NO_THROW(builder.addNextValue(0, 1, 2));
  118. ASSERT_NO_THROW(builder.addNextValue(0, 2, -1));
  119. ASSERT_NO_THROW(builder.addNextValue(1, 0, 1));
  120. ASSERT_NO_THROW(builder.addNextValue(1, 1, -5));
  121. ASSERT_NO_THROW(builder.addNextValue(1, 2, 2));
  122. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  123. ASSERT_NO_THROW(builder.addNextValue(2, 1, 2));
  124. ASSERT_NO_THROW(builder.addNextValue(2, 2, 4));
  125. storm::storage::SparseMatrix<double> A;
  126. ASSERT_NO_THROW(A = builder.build());
  127. std::vector<double> x(3);
  128. std::vector<double> b = {11, -16, 1};
  129. storm::solver::GmmxxLinearEquationSolver<double> solver(A);
  130. solver.getSettings().setSolutionMethod(storm::solver::GmmxxLinearEquationSolverSettings<double>::SolutionMethod::Jacobi);
  131. solver.getSettings().setPrecision(1e-6);
  132. solver.getSettings().setMaximalNumberOfIterations(10000);
  133. solver.getSettings().setPreconditioner(storm::solver::GmmxxLinearEquationSolverSettings<double>::Preconditioner::None);
  134. ASSERT_NO_THROW(solver.solveEquations(x, b));
  135. ASSERT_LT(std::abs(x[0] - 1), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  136. ASSERT_LT(std::abs(x[1] - 3), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  137. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  138. }
  139. TEST(GmmxxLinearEquationSolver, gmresilu) {
  140. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  141. storm::storage::SparseMatrixBuilder<double> builder;
  142. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  143. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  144. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  145. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  146. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  147. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  148. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  149. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  150. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  151. storm::storage::SparseMatrix<double> A;
  152. ASSERT_NO_THROW(A = builder.build());
  153. std::vector<double> x(3);
  154. std::vector<double> b = {16, -4, -7};
  155. storm::solver::GmmxxLinearEquationSolver<double> solver(A);
  156. solver.getSettings().setSolutionMethod(storm::solver::GmmxxLinearEquationSolverSettings<double>::SolutionMethod::Gmres);
  157. solver.getSettings().setPrecision(1e-6);
  158. solver.getSettings().setMaximalNumberOfIterations(10000);
  159. solver.getSettings().setPreconditioner(storm::solver::GmmxxLinearEquationSolverSettings<double>::Preconditioner::Ilu);
  160. solver.getSettings().setNumberOfIterationsUntilRestart(50);
  161. ASSERT_NO_THROW(solver.solveEquations(x, b));
  162. ASSERT_LT(std::abs(x[0] - 1), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  163. ASSERT_LT(std::abs(x[1] - 3), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  164. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  165. }
  166. TEST(GmmxxLinearEquationSolver, gmresdiag) {
  167. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  168. storm::storage::SparseMatrixBuilder<double> builder;
  169. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  170. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  171. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  172. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  173. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  174. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  175. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  176. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  177. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  178. storm::storage::SparseMatrix<double> A;
  179. ASSERT_NO_THROW(A = builder.build());
  180. std::vector<double> x(3);
  181. std::vector<double> b = {16, -4, -7};
  182. storm::solver::GmmxxLinearEquationSolver<double> solver(A);
  183. solver.getSettings().setSolutionMethod(storm::solver::GmmxxLinearEquationSolverSettings<double>::SolutionMethod::Gmres);
  184. solver.getSettings().setPrecision(1e-6);
  185. solver.getSettings().setMaximalNumberOfIterations(10000);
  186. solver.getSettings().setPreconditioner(storm::solver::GmmxxLinearEquationSolverSettings<double>::Preconditioner::Diagonal);
  187. solver.getSettings().setNumberOfIterationsUntilRestart(50);
  188. ASSERT_NO_THROW(solver.solveEquations(x, b));
  189. ASSERT_LT(std::abs(x[0] - 1), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  190. ASSERT_LT(std::abs(x[1] - 3), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  191. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  192. }
  193. TEST(GmmxxLinearEquationSolver, MatrixVectorMultiplication) {
  194. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  195. storm::storage::SparseMatrixBuilder<double> builder;
  196. ASSERT_NO_THROW(builder.addNextValue(0, 1, 0.5));
  197. ASSERT_NO_THROW(builder.addNextValue(0, 4, 0.5));
  198. ASSERT_NO_THROW(builder.addNextValue(1, 2, 0.5));
  199. ASSERT_NO_THROW(builder.addNextValue(1, 4, 0.5));
  200. ASSERT_NO_THROW(builder.addNextValue(2, 3, 0.5));
  201. ASSERT_NO_THROW(builder.addNextValue(2, 4, 0.5));
  202. ASSERT_NO_THROW(builder.addNextValue(3, 4, 1));
  203. ASSERT_NO_THROW(builder.addNextValue(4, 4, 1));
  204. storm::storage::SparseMatrix<double> A;
  205. ASSERT_NO_THROW(A = builder.build());
  206. std::vector<double> x(5);
  207. x[4] = 1;
  208. storm::solver::GmmxxLinearEquationSolver<double> solver(A);
  209. ASSERT_NO_THROW(solver.repeatedMultiply(x, nullptr, 4));
  210. ASSERT_LT(std::abs(x[0] - 1), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  211. }