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.

115 lines
6.0 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(storm::OptimizationDirection::Minimize, x, b));
  17. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  18. ASSERT_NO_THROW(solver.solveEquationSystem(storm::OptimizationDirection::Maximize, x, b));
  19. ASSERT_LT(std::abs(x[0] - 0.989991), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  20. }
  21. // TODO add better tests here.
  22. TEST(GmmxxMinMaxLinearEquationSolver, SolveWithStandardOptionsAndEarlyTermination) {
  23. storm::storage::SparseMatrixBuilder<double> builder(0, 0, 0, false, true);
  24. ASSERT_NO_THROW(builder.newRowGroup(0));
  25. ASSERT_NO_THROW(builder.addNextValue(0, 0, 0.9));
  26. storm::storage::SparseMatrix<double> A;
  27. ASSERT_NO_THROW(A = builder.build(2));
  28. std::vector<double> x(1);
  29. std::vector<double> b = {0.099, 0.5};
  30. double bound = 0.8;
  31. storm::solver::GmmxxMinMaxLinearEquationSolver<double> solver(A);
  32. solver.setEarlyTerminationCriterion(std::make_unique<storm::solver::TerminateAfterFilteredExtremumPassesThresholdValue<double>>(storm::storage::BitVector(1, true), bound, true));
  33. ASSERT_NO_THROW(solver.solveEquationSystem(storm::OptimizationDirection::Minimize, x, b));
  34. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  35. ASSERT_NO_THROW(solver.solveEquationSystem(storm::OptimizationDirection::Maximize, x, b));
  36. ASSERT_LT(std::abs(x[0] - 0.989991), 0.989991 - bound - storm::settings::gmmxxEquationSolverSettings().getPrecision());
  37. bound = 0.6;
  38. solver.setEarlyTerminationCriterion(std::make_unique<storm::solver::TerminateAfterFilteredExtremumPassesThresholdValue<double>>(storm::storage::BitVector(1, true), bound, true));
  39. ASSERT_NO_THROW(solver.solveEquationSystem(storm::OptimizationDirection::Maximize, x, b));
  40. ASSERT_LT(std::abs(x[0] - 0.989991), 0.989991 - bound - storm::settings::gmmxxEquationSolverSettings().getPrecision());
  41. }
  42. TEST(GmmxxMinMaxLinearEquationSolver, MatrixVectorMultiplication) {
  43. storm::storage::SparseMatrixBuilder<double> builder(0, 0, 0, false, true);
  44. ASSERT_NO_THROW(builder.newRowGroup(0));
  45. ASSERT_NO_THROW(builder.addNextValue(0, 0, 0.9));
  46. ASSERT_NO_THROW(builder.addNextValue(0, 1, 0.099));
  47. ASSERT_NO_THROW(builder.addNextValue(0, 2, 0.001));
  48. ASSERT_NO_THROW(builder.addNextValue(1, 1, 0.5));
  49. ASSERT_NO_THROW(builder.addNextValue(1, 2, 0.5));
  50. ASSERT_NO_THROW(builder.newRowGroup(2));
  51. ASSERT_NO_THROW(builder.addNextValue(2, 1, 1));
  52. ASSERT_NO_THROW(builder.newRowGroup(3));
  53. ASSERT_NO_THROW(builder.addNextValue(3, 2, 1));
  54. storm::storage::SparseMatrix<double> A;
  55. ASSERT_NO_THROW(A = builder.build());
  56. std::vector<double> x = {0, 1, 0};
  57. ASSERT_NO_THROW(storm::solver::GmmxxMinMaxLinearEquationSolver<double> solver(A));
  58. storm::solver::GmmxxMinMaxLinearEquationSolver<double> solver(A);
  59. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(storm::OptimizationDirection::Minimize, x, nullptr, 1));
  60. ASSERT_LT(std::abs(x[0] - 0.099), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  61. x = {0, 1, 0};
  62. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(storm::OptimizationDirection::Minimize, x, nullptr, 2));
  63. ASSERT_LT(std::abs(x[0] - 0.1881), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  64. x = {0, 1, 0};
  65. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(storm::OptimizationDirection::Minimize, x, nullptr, 20));
  66. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  67. x = {0, 1, 0};
  68. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(storm::OptimizationDirection::Maximize, x, nullptr, 1));
  69. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  70. x = {0, 1, 0};
  71. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(storm::OptimizationDirection::Maximize, x, nullptr, 20));
  72. ASSERT_LT(std::abs(x[0] - 0.9238082658), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  73. }
  74. TEST(GmmxxMinMaxLinearEquationSolver, SolveWithPolicyIteration) {
  75. storm::storage::SparseMatrixBuilder<double> builder(0, 0, 0, false, true);
  76. ASSERT_NO_THROW(builder.newRowGroup(0));
  77. ASSERT_NO_THROW(builder.addNextValue(0, 0, 0.9));
  78. storm::storage::SparseMatrix<double> A;
  79. ASSERT_NO_THROW(A = builder.build(2));
  80. std::vector<double> x(1);
  81. std::vector<double> b = { 0.099, 0.5 };
  82. storm::settings::modules::GmmxxEquationSolverSettings const& settings = storm::settings::gmmxxEquationSolverSettings();
  83. storm::solver::GmmxxMinMaxLinearEquationSolver<double> solver(A, settings.getPrecision(), settings.getMaximalIterationCount(), storm::solver::MinMaxTechniqueSelection::PolicyIteration, settings.getConvergenceCriterion() == storm::settings::modules::GmmxxEquationSolverSettings::ConvergenceCriterion::Relative);
  84. ASSERT_NO_THROW(solver.solveEquationSystem(storm::OptimizationDirection::Minimize, x, b));
  85. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  86. ASSERT_NO_THROW(solver.solveEquationSystem(storm::OptimizationDirection::Maximize, x, b));
  87. ASSERT_LT(std::abs(x[0] - 0.99), storm::settings::gmmxxEquationSolverSettings().getPrecision());
  88. }