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.

223 lines
13 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/solver/GmmxxLinearEquationSolver.h"
  4. #include "src/settings/Settings.h"
  5. TEST(GmmxxLinearEquationSolver, SolveWithStandardOptions) {
  6. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  7. storm::storage::SparseMatrixBuilder<double> builder;
  8. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  9. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  10. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  11. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  12. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  13. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  14. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  15. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  16. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  17. storm::storage::SparseMatrix<double> A;
  18. ASSERT_NO_THROW(A = builder.build());
  19. std::vector<double> x(3);
  20. std::vector<double> b = {16, -4, -7};
  21. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver);
  22. storm::solver::GmmxxLinearEquationSolver<double> solver;
  23. ASSERT_NO_THROW(solver.solveEquationSystem(A, x, b));
  24. ASSERT_LT(std::abs(x[0] - 1), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  25. ASSERT_LT(std::abs(x[1] - 3), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  26. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  27. }
  28. TEST(GmmxxLinearEquationSolver, gmres) {
  29. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  30. storm::storage::SparseMatrixBuilder<double> builder;
  31. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  32. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  33. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  34. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  35. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  36. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  37. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  38. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  39. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  40. storm::storage::SparseMatrix<double> A;
  41. ASSERT_NO_THROW(A = builder.build());
  42. std::vector<double> x(3);
  43. std::vector<double> b = {16, -4, -7};
  44. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(storm::solver::GmmxxLinearEquationSolver<double>::GMRES, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::NONE, true, 50));
  45. storm::solver::GmmxxLinearEquationSolver<double> solver(storm::solver::GmmxxLinearEquationSolver<double>::GMRES, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::NONE, true, 50);
  46. ASSERT_NO_THROW(solver.solveEquationSystem(A, x, b));
  47. ASSERT_LT(std::abs(x[0] - 1), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  48. ASSERT_LT(std::abs(x[1] - 3), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  49. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  50. }
  51. TEST(GmmxxLinearEquationSolver, qmr) {
  52. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  53. storm::storage::SparseMatrixBuilder<double> builder;
  54. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  55. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  56. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  57. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  58. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  59. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  60. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  61. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  62. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  63. storm::storage::SparseMatrix<double> A;
  64. ASSERT_NO_THROW(A = builder.build());
  65. std::vector<double> x(3);
  66. std::vector<double> b = {16, -4, -7};
  67. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(storm::solver::GmmxxLinearEquationSolver<double>::QMR, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::NONE));
  68. storm::solver::GmmxxLinearEquationSolver<double> solver(storm::solver::GmmxxLinearEquationSolver<double>::QMR, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::NONE, 50);
  69. ASSERT_NO_THROW(solver.solveEquationSystem(A, x, b));
  70. ASSERT_LT(std::abs(x[0] - 1), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  71. ASSERT_LT(std::abs(x[1] - 3), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  72. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  73. }
  74. TEST(GmmxxLinearEquationSolver, bicgstab) {
  75. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  76. storm::storage::SparseMatrixBuilder<double> builder;
  77. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  78. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  79. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  80. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  81. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  82. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  83. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  84. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  85. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  86. storm::storage::SparseMatrix<double> A;
  87. ASSERT_NO_THROW(A = builder.build());
  88. std::vector<double> x(3);
  89. std::vector<double> b = {16, -4, -7};
  90. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(storm::solver::GmmxxLinearEquationSolver<double>::BICGSTAB, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::NONE));
  91. storm::solver::GmmxxLinearEquationSolver<double> solver(storm::solver::GmmxxLinearEquationSolver<double>::BICGSTAB, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::NONE);
  92. ASSERT_NO_THROW(solver.solveEquationSystem(A, x, b));
  93. ASSERT_LT(std::abs(x[0] - 1), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  94. ASSERT_LT(std::abs(x[1] - 3), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  95. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  96. }
  97. TEST(GmmxxLinearEquationSolver, jacobi) {
  98. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  99. storm::storage::SparseMatrixBuilder<double> builder;
  100. ASSERT_NO_THROW(builder.addNextValue(0, 0, 4));
  101. ASSERT_NO_THROW(builder.addNextValue(0, 1, 2));
  102. ASSERT_NO_THROW(builder.addNextValue(0, 2, -1));
  103. ASSERT_NO_THROW(builder.addNextValue(1, 0, 1));
  104. ASSERT_NO_THROW(builder.addNextValue(1, 1, -5));
  105. ASSERT_NO_THROW(builder.addNextValue(1, 2, 2));
  106. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  107. ASSERT_NO_THROW(builder.addNextValue(2, 1, 2));
  108. ASSERT_NO_THROW(builder.addNextValue(2, 2, 4));
  109. storm::storage::SparseMatrix<double> A;
  110. ASSERT_NO_THROW(A = builder.build());
  111. std::vector<double> x(3);
  112. std::vector<double> b = {11, -16, 1};
  113. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(storm::solver::GmmxxLinearEquationSolver<double>::JACOBI, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::NONE));
  114. storm::solver::GmmxxLinearEquationSolver<double> solver(storm::solver::GmmxxLinearEquationSolver<double>::JACOBI, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::NONE);
  115. ASSERT_NO_THROW(solver.solveEquationSystem(A, x, b));
  116. ASSERT_LT(std::abs(x[0] - 1), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  117. ASSERT_LT(std::abs(x[1] - 3), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  118. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  119. }
  120. TEST(GmmxxLinearEquationSolver, gmresilu) {
  121. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  122. storm::storage::SparseMatrixBuilder<double> builder;
  123. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  124. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  125. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  126. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  127. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  128. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  129. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  130. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  131. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  132. storm::storage::SparseMatrix<double> A;
  133. ASSERT_NO_THROW(A = builder.build());
  134. std::vector<double> x(3);
  135. std::vector<double> b = {16, -4, -7};
  136. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(storm::solver::GmmxxLinearEquationSolver<double>::GMRES, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::ILU, true, 50));
  137. storm::solver::GmmxxLinearEquationSolver<double> solver(storm::solver::GmmxxLinearEquationSolver<double>::GMRES, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::NONE, true, 50);
  138. ASSERT_NO_THROW(solver.solveEquationSystem(A, x, b));
  139. ASSERT_LT(std::abs(x[0] - 1), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  140. ASSERT_LT(std::abs(x[1] - 3), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  141. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  142. }
  143. TEST(GmmxxLinearEquationSolver, gmresdiag) {
  144. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  145. storm::storage::SparseMatrixBuilder<double> builder;
  146. ASSERT_NO_THROW(builder.addNextValue(0, 0, 2));
  147. ASSERT_NO_THROW(builder.addNextValue(0, 1, 4));
  148. ASSERT_NO_THROW(builder.addNextValue(0, 2, -2));
  149. ASSERT_NO_THROW(builder.addNextValue(1, 0, 4));
  150. ASSERT_NO_THROW(builder.addNextValue(1, 1, -1));
  151. ASSERT_NO_THROW(builder.addNextValue(1, 2, 5));
  152. ASSERT_NO_THROW(builder.addNextValue(2, 0, -1));
  153. ASSERT_NO_THROW(builder.addNextValue(2, 1, -1));
  154. ASSERT_NO_THROW(builder.addNextValue(2, 2, 3));
  155. storm::storage::SparseMatrix<double> A;
  156. ASSERT_NO_THROW(A = builder.build());
  157. std::vector<double> x(3);
  158. std::vector<double> b = {16, -4, -7};
  159. ASSERT_NO_THROW(storm::solver::GmmxxLinearEquationSolver<double> solver(storm::solver::GmmxxLinearEquationSolver<double>::GMRES, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::DIAGONAL, true, 50));
  160. storm::solver::GmmxxLinearEquationSolver<double> solver(storm::solver::GmmxxLinearEquationSolver<double>::GMRES, 1e-6, 10000, storm::solver::GmmxxLinearEquationSolver<double>::NONE, true, 50);
  161. ASSERT_NO_THROW(solver.solveEquationSystem(A, x, b));
  162. ASSERT_LT(std::abs(x[0] - 1), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  163. ASSERT_LT(std::abs(x[1] - 3), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  164. ASSERT_LT(std::abs(x[2] - (-1)), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  165. }
  166. TEST(GmmxxLinearEquationSolver, MatrixVectorMultplication) {
  167. ASSERT_NO_THROW(storm::storage::SparseMatrixBuilder<double> builder);
  168. storm::storage::SparseMatrixBuilder<double> builder;
  169. ASSERT_NO_THROW(builder.addNextValue(0, 1, 0.5));
  170. ASSERT_NO_THROW(builder.addNextValue(0, 4, 0.5));
  171. ASSERT_NO_THROW(builder.addNextValue(1, 2, 0.5));
  172. ASSERT_NO_THROW(builder.addNextValue(1, 4, 0.5));
  173. ASSERT_NO_THROW(builder.addNextValue(2, 3, 0.5));
  174. ASSERT_NO_THROW(builder.addNextValue(2, 4, 0.5));
  175. ASSERT_NO_THROW(builder.addNextValue(3, 4, 1));
  176. ASSERT_NO_THROW(builder.addNextValue(4, 4, 1));
  177. storm::storage::SparseMatrix<double> A;
  178. ASSERT_NO_THROW(A = builder.build());
  179. std::vector<double> x(5);
  180. x[4] = 1;
  181. storm::solver::GmmxxLinearEquationSolver<double> solver;
  182. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(A, x, nullptr, 4));
  183. ASSERT_LT(std::abs(x[0] - 1), storm::settings::Settings::getInstance()->getOptionByLongName("precision").getArgument(0).getValueAsDouble());
  184. }