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.

91 lines
4.5 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/NativeEquationSolverSettings.h"
  6. #include "src/storage/SparseMatrix.h"
  7. TEST(NativeMinMaxLinearEquationSolver, 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::NativeMinMaxLinearEquationSolverFactory<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::NativeEquationSolverSettings>().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::NativeEquationSolverSettings>().getPrecision());
  21. }
  22. TEST(NativeMinMaxLinearEquationSolver, MatrixVectorMultiplication) {
  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. ASSERT_NO_THROW(builder.addNextValue(0, 1, 0.099));
  27. ASSERT_NO_THROW(builder.addNextValue(0, 2, 0.001));
  28. ASSERT_NO_THROW(builder.addNextValue(1, 1, 0.5));
  29. ASSERT_NO_THROW(builder.addNextValue(1, 2, 0.5));
  30. ASSERT_NO_THROW(builder.newRowGroup(2));
  31. ASSERT_NO_THROW(builder.addNextValue(2, 1, 1));
  32. ASSERT_NO_THROW(builder.newRowGroup(3));
  33. ASSERT_NO_THROW(builder.addNextValue(3, 2, 1));
  34. storm::storage::SparseMatrix<double> A;
  35. ASSERT_NO_THROW(A = builder.build());
  36. std::vector<double> x = {0, 1, 0};
  37. auto factory = storm::solver::NativeMinMaxLinearEquationSolverFactory<double>();
  38. auto solver = factory.create(A);
  39. ASSERT_NO_THROW(solver->repeatedMultiply(storm::OptimizationDirection::Minimize, x, nullptr, 1));
  40. ASSERT_LT(std::abs(x[0] - 0.099), storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  41. x = {0, 1, 0};
  42. ASSERT_NO_THROW(solver->repeatedMultiply(storm::OptimizationDirection::Minimize, x, nullptr, 2));
  43. ASSERT_LT(std::abs(x[0] - 0.1881), storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  44. x = {0, 1, 0};
  45. ASSERT_NO_THROW(solver->repeatedMultiply(storm::OptimizationDirection::Minimize, x, nullptr, 20));
  46. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  47. x = {0, 1, 0};
  48. ASSERT_NO_THROW(solver->repeatedMultiply(storm::OptimizationDirection::Maximize, x, nullptr, 1));
  49. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  50. x = {0, 1, 0};
  51. ASSERT_NO_THROW(solver->repeatedMultiply(storm::OptimizationDirection::Maximize, x, nullptr, 20));
  52. ASSERT_LT(std::abs(x[0] - 0.9238082658), storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  53. }
  54. TEST(NativeMinMaxLinearEquationSolver, SolveWithPolicyIteration) {
  55. storm::storage::SparseMatrixBuilder<double> builder(0, 0, 0, false, true);
  56. ASSERT_NO_THROW(builder.newRowGroup(0));
  57. ASSERT_NO_THROW(builder.addNextValue(0, 0, 0.9));
  58. storm::storage::SparseMatrix<double> A;
  59. ASSERT_NO_THROW(A = builder.build(2));
  60. std::vector<double> x(1);
  61. std::vector<double> b = { 0.099, 0.5 };
  62. auto factory = storm::solver::NativeMinMaxLinearEquationSolverFactory<double>();
  63. factory.getSettings().setSolutionMethod(storm::solver::StandardMinMaxLinearEquationSolverSettings<double>::SolutionMethod::PolicyIteration);
  64. auto solver = factory.create(A);
  65. ASSERT_NO_THROW(solver->solveEquations(storm::OptimizationDirection::Minimize, x, b));
  66. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  67. ASSERT_NO_THROW(solver->solveEquations(storm::OptimizationDirection::Maximize, x, b));
  68. ASSERT_LT(std::abs(x[0] - 0.99), storm::settings::getModule<storm::settings::modules::NativeEquationSolverSettings>().getPrecision());
  69. }