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.

225 lines
12 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.solveEquationSystem(x, b));
  25. ASSERT_LT(std::abs(x[0] - 1), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  26. ASSERT_LT(std::abs(x[1] - 3), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  27. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::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. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(A, storm::solver::GmmxxLinearEquationSolver<double>::SolutionMethod::Gmres, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::Preconditioner::None, true, 50));
  46. storm::solver::GmmxxLinearEquationSolver<double> solver(A, storm::solver::GmmxxLinearEquationSolver<double>::SolutionMethod::Gmres, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::Preconditioner::None, true, 50);
  47. ASSERT_NO_THROW(solver.solveEquationSystem(x, b));
  48. ASSERT_LT(std::abs(x[0] - 1), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  49. ASSERT_LT(std::abs(x[1] - 3), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  50. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  51. }
  52. TEST(GmmxxLinearEquationSolver, qmr) {
  53. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  54. storm::storage::SparseMatrixBuilder<double> builder;
  55. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  56. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  57. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  58. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  59. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  60. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  61. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  62. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  63. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  64. storm::storage::SparseMatrix<double> A;
  65. ASSERT_NO_THROW(A = builder.build());
  66. std::vector<double> x(3);
  67. std::vector<double> b = {16, -4, -7};
  68. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(A, storm::solver::GmmxxLinearEquationSolver<double>::SolutionMethod::Qmr, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::Preconditioner::None));
  69. storm::solver::GmmxxLinearEquationSolver<double> solver(A, storm::solver::GmmxxLinearEquationSolver<double>::SolutionMethod::Qmr, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::Preconditioner::None, true, 50);
  70. ASSERT_NO_THROW(solver.solveEquationSystem(x, b));
  71. ASSERT_LT(std::abs(x[0] - 1), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  72. ASSERT_LT(std::abs(x[1] - 3), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  73. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  74. }
  75. TEST(GmmxxLinearEquationSolver, bicgstab) {
  76. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  77. storm::storage::SparseMatrixBuilder<double> builder;
  78. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  79. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  80. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  81. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  82. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  83. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  84. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  85. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  86. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  87. storm::storage::SparseMatrix<double> A;
  88. ASSERT_NO_THROW(A = builder.build());
  89. std::vector<double> x(3);
  90. std::vector<double> b = {16, -4, -7};
  91. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(A, storm::solver::GmmxxLinearEquationSolver<double>::SolutionMethod::Bicgstab, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::Preconditioner::None));
  92. storm::solver::GmmxxLinearEquationSolver<double> solver(A, storm::solver::GmmxxLinearEquationSolver<double>::SolutionMethod::Bicgstab, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::Preconditioner::None);
  93. ASSERT_NO_THROW(solver.solveEquationSystem(x, b));
  94. ASSERT_LT(std::abs(x[0] - 1), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  95. ASSERT_LT(std::abs(x[1] - 3), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  96. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  97. }
  98. TEST(GmmxxLinearEquationSolver, jacobi) {
  99. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  100. storm::storage::SparseMatrixBuilder<double> builder;
  101. ASSERT_NO_THROW(builder.addNextValue(0, 0, 4));
  102. ASSERT_NO_THROW(builder.addNextValue(0, 1, 2));
  103. ASSERT_NO_THROW(builder.addNextValue(0, 2, -1));
  104. ASSERT_NO_THROW(builder.addNextValue(1, 0, 1));
  105. ASSERT_NO_THROW(builder.addNextValue(1, 1, -5));
  106. ASSERT_NO_THROW(builder.addNextValue(1, 2, 2));
  107. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  108. ASSERT_NO_THROW(builder.addNextValue(2, 1, 2));
  109. ASSERT_NO_THROW(builder.addNextValue(2, 2, 4));
  110. storm::storage::SparseMatrix<double> A;
  111. ASSERT_NO_THROW(A = builder.build());
  112. std::vector<double> x(3);
  113. std::vector<double> b = {11, -16, 1};
  114. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(A, storm::solver::GmmxxLinearEquationSolver<double>::SolutionMethod::Jacobi, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::Preconditioner::None));
  115. storm::solver::GmmxxLinearEquationSolver<double> solver(A, storm::solver::GmmxxLinearEquationSolver<double>::SolutionMethod::Jacobi, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::Preconditioner::None);
  116. ASSERT_NO_THROW(solver.solveEquationSystem(x, b));
  117. ASSERT_LT(std::abs(x[0] - 1), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  118. ASSERT_LT(std::abs(x[1] - 3), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  119. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  120. }
  121. TEST(GmmxxLinearEquationSolver, gmresilu) {
  122. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  123. storm::storage::SparseMatrixBuilder<double> builder;
  124. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  125. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  126. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  127. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  128. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  129. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  130. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  131. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  132. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  133. storm::storage::SparseMatrix<double> A;
  134. ASSERT_NO_THROW(A = builder.build());
  135. std::vector<double> x(3);
  136. std::vector<double> b = {16, -4, -7};
  137. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(A, storm::solver::GmmxxLinearEquationSolver<double>::SolutionMethod::Gmres, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::Preconditioner::Ilu, true, 50));
  138. storm::solver::GmmxxLinearEquationSolver<double> solver(A, storm::solver::GmmxxLinearEquationSolver<double>::SolutionMethod::Gmres, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::Preconditioner::None, true, 50);
  139. ASSERT_NO_THROW(solver.solveEquationSystem(x, b));
  140. ASSERT_LT(std::abs(x[0] - 1), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  141. ASSERT_LT(std::abs(x[1] - 3), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  142. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  143. }
  144. TEST(GmmxxLinearEquationSolver, gmresdiag) {
  145. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  146. storm::storage::SparseMatrixBuilder<double> builder;
  147. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  148. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  149. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  150. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  151. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  152. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  153. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  154. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  155. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  156. storm::storage::SparseMatrix<double> A;
  157. ASSERT_NO_THROW(A = builder.build());
  158. std::vector<double> x(3);
  159. std::vector<double> b = {16, -4, -7};
  160. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(A, storm::solver::GmmxxLinearEquationSolver<double>::SolutionMethod::Gmres, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::Preconditioner::Diagonal, true, 50));
  161. storm::solver::GmmxxLinearEquationSolver<double> solver(A, storm::solver::GmmxxLinearEquationSolver<double>::SolutionMethod::Gmres, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::Preconditioner::None, true, 50);
  162. ASSERT_NO_THROW(solver.solveEquationSystem(x, b));
  163. ASSERT_LT(std::abs(x[0] - 1), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  164. ASSERT_LT(std::abs(x[1] - 3), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  165. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  166. }
  167. TEST(GmmxxLinearEquationSolver, MatrixVectorMultplication) {
  168. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  169. storm::storage::SparseMatrixBuilder<double> builder;
  170. ASSERT_NO_THROW(builder.addNextValue(0, 1, 0.5));
  171. ASSERT_NO_THROW(builder.addNextValue(0, 4, 0.5));
  172. ASSERT_NO_THROW(builder.addNextValue(1, 2, 0.5));
  173. ASSERT_NO_THROW(builder.addNextValue(1, 4, 0.5));
  174. ASSERT_NO_THROW(builder.addNextValue(2, 3, 0.5));
  175. ASSERT_NO_THROW(builder.addNextValue(2, 4, 0.5));
  176. ASSERT_NO_THROW(builder.addNextValue(3, 4, 1));
  177. ASSERT_NO_THROW(builder.addNextValue(4, 4, 1));
  178. storm::storage::SparseMatrix<double> A;
  179. ASSERT_NO_THROW(A = builder.build());
  180. std::vector<double> x(5);
  181. x[4] = 1;
  182. storm::solver::GmmxxLinearEquationSolver<double> solver(A);
  183. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(x, nullptr, 4));
  184. ASSERT_LT(std::abs(x[0] - 1), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  185. }