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.

88 lines
4.4 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/solver/NativeMinMaxLinearEquationSolver.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. storm::solver::NativeMinMaxLinearEquationSolver<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::nativeEquationSolverSettings().getPrecision());
  18. ASSERT_NO_THROW(solver.solveEquationSystem(storm::OptimizationDirection::Maximize, x, b));
  19. ASSERT_LT(std::abs(x[0] - 0.989991), storm::settings::nativeEquationSolverSettings().getPrecision());
  20. }
  21. TEST(NativeMinMaxLinearEquationSolver, 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::NativeMinMaxLinearEquationSolver<double> solver(A));
  37. storm::solver::NativeMinMaxLinearEquationSolver<double> solver(A);
  38. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(storm::OptimizationDirection::Minimize, x, nullptr, 1));
  39. ASSERT_LT(std::abs(x[0] - 0.099), storm::settings::nativeEquationSolverSettings().getPrecision());
  40. x = {0, 1, 0};
  41. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(storm::OptimizationDirection::Minimize, x, nullptr, 2));
  42. ASSERT_LT(std::abs(x[0] - 0.1881), storm::settings::nativeEquationSolverSettings().getPrecision());
  43. x = {0, 1, 0};
  44. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(storm::OptimizationDirection::Minimize, x, nullptr, 20));
  45. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::nativeEquationSolverSettings().getPrecision());
  46. x = {0, 1, 0};
  47. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(storm::OptimizationDirection::Maximize, x, nullptr, 1));
  48. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::nativeEquationSolverSettings().getPrecision());
  49. x = {0, 1, 0};
  50. ASSERT_NO_THROW(solver.performMatrixVectorMultiplication(storm::OptimizationDirection::Maximize, x, nullptr, 20));
  51. ASSERT_LT(std::abs(x[0] - 0.9238082658), storm::settings::nativeEquationSolverSettings().getPrecision());
  52. }
  53. TEST(NativeMinMaxLinearEquationSolver, 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::NativeEquationSolverSettings const& settings = storm::settings::nativeEquationSolverSettings();
  62. storm::solver::NativeMinMaxLinearEquationSolver<double> solver(A, settings.getPrecision(), settings.getMaximalIterationCount(), storm::solver::MinMaxTechniqueSelection::PolicyIteration, settings.getConvergenceCriterion() == storm::settings::modules::NativeEquationSolverSettings::ConvergenceCriterion::Relative);
  63. ASSERT_NO_THROW(solver.solveEquationSystem(storm::OptimizationDirection::Minimize, x, b));
  64. ASSERT_LT(std::abs(x[0] - 0.5), storm::settings::nativeEquationSolverSettings().getPrecision());
  65. ASSERT_NO_THROW(solver.solveEquationSystem(storm::OptimizationDirection::Maximize, x, b));
  66. ASSERT_LT(std::abs(x[0] - 0.99), storm::settings::nativeEquationSolverSettings().getPrecision());
  67. }