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.

120 lines
6.2 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/solver/StandardMinMaxLinearEquationSolver.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. auto factory = storm::solver::GmmxxMinMaxLinearEquationSolverFactory<double>();
  16. auto solver = factory.create(A);
  17. ASSERT_NO_THROW(solver->solveEquations(storm::OptimizationDirection::Minimize, x, b));
  18. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  19. ASSERT_NO_THROW(solver->solveEquations(storm::OptimizationDirection::Maximize, x, b));
  20. ASSERT_LT(std::abs(x[0] - 0.989991), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  21. }
  22. // TODO add better tests here.
  23. TEST(GmmxxMinMaxLinearEquationSolver, SolveWithStandardOptionsAndEarlyTermination) {
  24. storm::storage::SparseMatrixBuilder<double> builder(0, 0, 0, false, true);
  25. ASSERT_NO_THROW(builder.newRowGroup(0));
  26. ASSERT_NO_THROW(builder.addNextValue(0, 0, 0.9));
  27. storm::storage::SparseMatrix<double> A;
  28. ASSERT_NO_THROW(A = builder.build(2));
  29. std::vector<double> x(1);
  30. std::vector<double> b = {0.099, 0.5};
  31. double bound = 0.8;
  32. auto factory = storm::solver::GmmxxMinMaxLinearEquationSolverFactory<double>();
  33. auto solver = factory.create(A);
  34. solver->setTerminationCondition(std::make_unique<storm::solver::TerminateIfFilteredExtremumExceedsThreshold<double>>(storm::storage::BitVector(A.getRowGroupCount(), true), false, bound, true));
  35. ASSERT_NO_THROW(solver->solveEquations(storm::OptimizationDirection::Minimize, x, b));
  36. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  37. ASSERT_NO_THROW(solver->solveEquations(storm::OptimizationDirection::Maximize, x, b));
  38. ASSERT_LT(std::abs(x[0] - 0.989991), 0.989991 - bound - storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  39. bound = 0.6;
  40. solver->setTerminationCondition(std::make_unique<storm::solver::TerminateIfFilteredExtremumExceedsThreshold<double>>(storm::storage::BitVector(A.getRowGroupCount(), true), false, bound, true));
  41. ASSERT_NO_THROW(solver->solveEquations(storm::OptimizationDirection::Maximize, x, b));
  42. ASSERT_LT(std::abs(x[0] - 0.989991), 0.989991 - bound - storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  43. }
  44. TEST(GmmxxMinMaxLinearEquationSolver, MatrixVectorMultiplication) {
  45. storm::storage::SparseMatrixBuilder<double> builder(0, 0, 0, false, true);
  46. ASSERT_NO_THROW(builder.newRowGroup(0));
  47. ASSERT_NO_THROW(builder.addNextValue(0, 0, 0.9));
  48. ASSERT_NO_THROW(builder.addNextValue(0, 1, 0.099));
  49. ASSERT_NO_THROW(builder.addNextValue(0, 2, 0.001));
  50. ASSERT_NO_THROW(builder.addNextValue(1, 1, 0.5));
  51. ASSERT_NO_THROW(builder.addNextValue(1, 2, 0.5));
  52. ASSERT_NO_THROW(builder.newRowGroup(2));
  53. ASSERT_NO_THROW(builder.addNextValue(2, 1, 1));
  54. ASSERT_NO_THROW(builder.newRowGroup(3));
  55. ASSERT_NO_THROW(builder.addNextValue(3, 2, 1));
  56. storm::storage::SparseMatrix<double> A;
  57. ASSERT_NO_THROW(A = builder.build());
  58. std::vector<double> x = {0, 1, 0};
  59. auto factory = storm::solver::GmmxxMinMaxLinearEquationSolverFactory<double>();
  60. auto solver = factory.create(A);
  61. ASSERT_NO_THROW(solver->repeatedMultiply(storm::OptimizationDirection::Minimize, x, nullptr, 1));
  62. ASSERT_LT(std::abs(x[0] - 0.099), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  63. x = {0, 1, 0};
  64. ASSERT_NO_THROW(solver->repeatedMultiply(storm::OptimizationDirection::Minimize, x, nullptr, 2));
  65. ASSERT_LT(std::abs(x[0] - 0.1881), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  66. x = {0, 1, 0};
  67. ASSERT_NO_THROW(solver->repeatedMultiply(storm::OptimizationDirection::Minimize, x, nullptr, 20));
  68. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  69. x = {0, 1, 0};
  70. ASSERT_NO_THROW(solver->repeatedMultiply(storm::OptimizationDirection::Maximize, x, nullptr, 1));
  71. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  72. x = {0, 1, 0};
  73. ASSERT_NO_THROW(solver->repeatedMultiply(storm::OptimizationDirection::Maximize, x, nullptr, 20));
  74. ASSERT_LT(std::abs(x[0] - 0.9238082658), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  75. }
  76. TEST(GmmxxMinMaxLinearEquationSolver, SolveWithPolicyIteration) {
  77. storm::storage::SparseMatrixBuilder<double> builder(0, 0, 0, false, true);
  78. ASSERT_NO_THROW(builder.newRowGroup(0));
  79. ASSERT_NO_THROW(builder.addNextValue(0, 0, 0.9));
  80. storm::storage::SparseMatrix<double> A;
  81. ASSERT_NO_THROW(A = builder.build(2));
  82. std::vector<double> x(1);
  83. std::vector<double> b = { 0.099, 0.5 };
  84. auto factory = storm::solver::GmmxxMinMaxLinearEquationSolverFactory<double>();
  85. factory.getSettings().setSolutionMethod(storm::solver::StandardMinMaxLinearEquationSolverSettings<double>::SolutionMethod::PolicyIteration);
  86. auto solver = factory.create(A);
  87. ASSERT_NO_THROW(solver->solveEquations(storm::OptimizationDirection::Minimize, x, b));
  88. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  89. ASSERT_NO_THROW(solver->solveEquations(storm::OptimizationDirection::Maximize, x, b));
  90. ASSERT_LT(std::abs(x[0] - 0.99), storm::settings::getModule<storm::settings::modules::GmmxxEquationSolverSettings>().getPrecision());
  91. }