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.

68 lines
3.1 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/storage/SparseMatrix.h"
  4. #include "src/utility/solver.h"
  5. #include "src/settings/SettingsManager.h"
  6. #include "src/solver/GameSolver.h"
  7. #include "src/settings/modules/NativeEquationSolverSettings.h"
  8. TEST(GameSolverTest, Solve) {
  9. // Construct simple game. Start with player 2 matrix.
  10. storm::storage::SparseMatrixBuilder<double> player2MatrixBuilder(0, 0, 0, false, true);
  11. player2MatrixBuilder.newRowGroup(0);
  12. player2MatrixBuilder.addNextValue(0, 0, 0.4);
  13. player2MatrixBuilder.addNextValue(0, 1, 0.6);
  14. player2MatrixBuilder.addNextValue(1, 1, 0.2);
  15. player2MatrixBuilder.addNextValue(1, 2, 0.8);
  16. player2MatrixBuilder.newRowGroup(2);
  17. player2MatrixBuilder.addNextValue(2, 2, 0.5);
  18. player2MatrixBuilder.addNextValue(2, 3, 0.5);
  19. player2MatrixBuilder.addNextValue(3, 0, 1);
  20. player2MatrixBuilder.newRowGroup(4);
  21. player2MatrixBuilder.newRowGroup(5);
  22. player2MatrixBuilder.newRowGroup(6);
  23. storm::storage::SparseMatrix<double> player2Matrix = player2MatrixBuilder.build();
  24. // Now build player 1 matrix.
  25. storm::storage::SparseMatrixBuilder<storm::storage::sparse::state_type> player1MatrixBuilder(0, 0, 0, false, true);
  26. player1MatrixBuilder.newRowGroup(0);
  27. player1MatrixBuilder.addNextValue(0, 0, 1);
  28. player1MatrixBuilder.addNextValue(1, 1, 1);
  29. player1MatrixBuilder.newRowGroup(2);
  30. player1MatrixBuilder.addNextValue(2, 2, 1);
  31. player1MatrixBuilder.newRowGroup(3);
  32. player1MatrixBuilder.addNextValue(3, 3, 1);
  33. player1MatrixBuilder.newRowGroup(4);
  34. player1MatrixBuilder.addNextValue(4, 4, 1);
  35. storm::storage::SparseMatrix<storm::storage::sparse::state_type> player1Matrix = player1MatrixBuilder.build();
  36. std::unique_ptr<storm::utility::solver::GameSolverFactory<double>> solverFactory(new storm::utility::solver::GameSolverFactory<double>());
  37. std::unique_ptr<storm::solver::GameSolver<double>> solver = solverFactory->create(player1Matrix, player2Matrix);
  38. // Create solution and target state vector.
  39. std::vector<double> result(4);
  40. std::vector<double> b(7);
  41. b[4] = 1;
  42. b[6] = 1;
  43. // Now solve the game with different strategies for the players.
  44. solver->solveGame(storm::OptimizationDirection::Minimize, storm::OptimizationDirection::Minimize, result, b);
  45. EXPECT_NEAR(0, result[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  46. result = std::vector<double>(4);
  47. solver->solveGame(storm::OptimizationDirection::Minimize, storm::OptimizationDirection::Maximize, result, b);
  48. EXPECT_NEAR(0.5, result[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  49. result = std::vector<double>(4);
  50. solver->solveGame(storm::OptimizationDirection::Maximize, storm::OptimizationDirection::Minimize, result, b);
  51. EXPECT_NEAR(0.2, result[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  52. result = std::vector<double>(4);
  53. solver->solveGame(storm::OptimizationDirection::Maximize, storm::OptimizationDirection::Maximize, result, b);
  54. EXPECT_NEAR(0.99999892625817599, result[0], storm::settings::nativeEquationSolverSettings().getPrecision());
  55. }