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.

86 lines
4.1 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/solver/GmmxxMinMaxLinearEquationSolver.h"
  4. #include "src/settings/SettingsManager.h"
  5. #include "src/settings/modules/GmmxxEquationSolverSettings.h"
  6. #include "src/storage/SparseMatrix.h"
  7. TEST(GmmxxMinMaxLinearEquationSolver, SolveWithStandardOptions) {
  8. storm::storage::SparseMatrixBuilder<double> builder(0, 0, 0, false, true);
  9. ASSERT_NO_THROW(builder.newRowGroup(0));
  10. ASSERT_NO_THROW(builder.addNextValue(0, 0, 0.9));
  11. storm::storage::SparseMatrix<double> A;
  12. ASSERT_NO_THROW(A = builder.build(2));
  13. std::vector<double> x(1);
  14. std::vector<double> b = {0.099, 0.5};
  15. storm::solver::GmmxxMinMaxLinearEquationSolver<double> solver(A);
  16. ASSERT_NO_THROW(solver.solveEquationSystem(true, x, b));
  17. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  18. ASSERT_NO_THROW(solver.solveEquationSystem(false, x, b));
  19. ASSERT_LT(std::abs(x[0] - 0.989991), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  20. }
  21. TEST(GmmxxMinMaxLinearEquationSolver, MatrixVectorMultiplication) {
  22. storm::storage::SparseMatrixBuilder<double> builder(0, 0, 0, false, true);
  23. ASSERT_NO_THROW(builder.newRowGroup(0));
  24. ASSERT_NO_THROW(builder.addNextValue(0, 0, 0.9));
  25. ASSERT_NO_THROW(builder.addNextValue(0, 1, 0.099));
  26. ASSERT_NO_THROW(builder.addNextValue(0, 2, 0.001));
  27. ASSERT_NO_THROW(builder.addNextValue(1, 1, 0.5));
  28. ASSERT_NO_THROW(builder.addNextValue(1, 2, 0.5));
  29. ASSERT_NO_THROW(builder.newRowGroup(2));
  30. ASSERT_NO_THROW(builder.addNextValue(2, 1, 1));
  31. ASSERT_NO_THROW(builder.newRowGroup(3));
  32. ASSERT_NO_THROW(builder.addNextValue(3, 2, 1));
  33. storm::storage::SparseMatrix<double> A;
  34. ASSERT_NO_THROW(A = builder.build());
  35. std::vector<double> x = {0, 1, 0};
  36. ASSERT_NO_THROW(storm::solver::GmmxxMinMaxLinearEquationSolver<double> solver(A));
  37. storm::solver::GmmxxMinMaxLinearEquationSolver<double> solver(A);
  38. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(true, x, nullptr, 1));
  39. ASSERT_LT(std::abs(x[0] - 0.099), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  40. x = {0, 1, 0};
  41. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(true, x, nullptr, 2));
  42. ASSERT_LT(std::abs(x[0] - 0.1881), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  43. x = {0, 1, 0};
  44. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(true, x, nullptr, 20));
  45. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  46. x = {0, 1, 0};
  47. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(false, x, nullptr, 1));
  48. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  49. x = {0, 1, 0};
  50. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(false, x, nullptr, 20));
  51. ASSERT_LT(std::abs(x[0] - 0.9238082658), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  52. }
  53. TEST(GmmxxMinMaxLinearEquationSolver, SolveWithPolicyIteration) {
  54. storm::storage::SparseMatrixBuilder<double> builder(0, 0, 0, false, true);
  55. ASSERT_NO_THROW(builder.newRowGroup(0));
  56. ASSERT_NO_THROW(builder.addNextValue(0, 0, 0.9));
  57. storm::storage::SparseMatrix<double> A;
  58. ASSERT_NO_THROW(A = builder.build(2));
  59. std::vector<double> x(1);
  60. std::vector<double> b = { 0.099, 0.5 };
  61. storm::settings::modules::GmmxxEquationSolverSettings const& settings = storm::settings::gmmxxEquationSolverSettings();
  62. storm::solver::GmmxxMinMaxLinearEquationSolver<double> solver(A, settings.getPrecision(), settings.getMaximalIterationCount(), storm::solver::MinMaxTechniqueSelection::PolicyIteration, settings.getConvergenceCriterion() == storm::settings::modules::GmmxxEquationSolverSettings::ConvergenceCriterion::Relative);
  63. ASSERT_NO_THROW(solver.solveEquationSystem(true, x, b));
  64. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  65. ASSERT_NO_THROW(solver.solveEquationSystem(false, x, b));
  66. ASSERT_LT(std::abs(x[0] - 0.99), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  67. }