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.

63 lines
4.7 KiB

  1. #include "gtest/gtest.h"
  2. #include "storm-config.h"
  3. #include "src/storage/dd/CuddDdManager.h"
  4. #include "src/utility/solver.h"
  5. TEST(FullySymbolicGameSolverTest, Solve) {
  6. // Create some variables.
  7. std::shared_ptr<storm::dd::DdManager<storm::dd::DdType::CUDD>> manager(new storm::dd::DdManager<storm::dd::DdType::CUDD>());
  8. std::pair<storm::expressions::Variable, storm::expressions::Variable> state = manager->addMetaVariable("x", 1, 4);
  9. std::pair<storm::expressions::Variable, storm::expressions::Variable> pl1 = manager->addMetaVariable("a", 0, 1);
  10. std::pair<storm::expressions::Variable, storm::expressions::Variable> pl2 = manager->addMetaVariable("b", 0, 1);
  11. storm::dd::Bdd<storm::dd::DdType::CUDD> allRows = manager->getBddZero();
  12. std::set<storm::expressions::Variable> rowMetaVariables({state.first});
  13. std::set<storm::expressions::Variable> columnMetaVariables({state.second});
  14. std::vector<std::pair<storm::expressions::Variable, storm::expressions::Variable>> rowColumnMetaVariablePairs = {state};
  15. std::set<storm::expressions::Variable> player1Variables({pl1.first});
  16. std::set<storm::expressions::Variable> player2Variables({pl2.first});
  17. // Construct simple game.
  18. storm::dd::Add<storm::dd::DdType::CUDD> matrix = manager->getEncoding(state.first, 1).toAdd() * manager->getEncoding(state.second, 2).toAdd() * manager->getEncoding(pl1.first, 0).toAdd() * manager->getEncoding(pl2.first, 0).toAdd() * manager->getConstant(0.6);
  19. matrix += manager->getEncoding(state.first, 1).toAdd() * manager->getEncoding(state.second, 1).toAdd() * manager->getEncoding(pl1.first, 0).toAdd() * manager->getEncoding(pl2.first, 0).toAdd() * manager->getConstant(0.4);
  20. matrix += manager->getEncoding(state.first, 1).toAdd() * manager->getEncoding(state.second, 2).toAdd() * manager->getEncoding(pl1.first, 0).toAdd() * manager->getEncoding(pl2.first, 1).toAdd() * manager->getConstant(0.2);
  21. matrix += manager->getEncoding(state.first, 1).toAdd() * manager->getEncoding(state.second, 3).toAdd() * manager->getEncoding(pl1.first, 0).toAdd() * manager->getEncoding(pl2.first, 1).toAdd() * manager->getConstant(0.8);
  22. matrix += manager->getEncoding(state.first, 1).toAdd() * manager->getEncoding(state.second, 3).toAdd() * manager->getEncoding(pl1.first, 1).toAdd() * manager->getEncoding(pl2.first, 0).toAdd() * manager->getConstant(0.5);
  23. matrix += manager->getEncoding(state.first, 1).toAdd() * manager->getEncoding(state.second, 4).toAdd() * manager->getEncoding(pl1.first, 1).toAdd() * manager->getEncoding(pl2.first, 0).toAdd() * manager->getConstant(0.5);
  24. matrix += manager->getEncoding(state.first, 1).toAdd() * manager->getEncoding(state.second, 1).toAdd() * manager->getEncoding(pl1.first, 1).toAdd() * manager->getEncoding(pl2.first, 1).toAdd() * manager->getConstant(1);
  25. std::unique_ptr<storm::utility::solver::SymbolicGameSolverFactory<storm::dd::DdType::CUDD>> solverFactory(new storm::utility::solver::SymbolicGameSolverFactory<storm::dd::DdType::CUDD>());
  26. std::unique_ptr<storm::solver::SymbolicGameSolver<storm::dd::DdType::CUDD>> solver = solverFactory->create(matrix, allRows, rowMetaVariables, columnMetaVariables, rowColumnMetaVariablePairs, player1Variables,player2Variables);
  27. // Create solution and target state vector.
  28. storm::dd::Add<storm::dd::DdType::CUDD> x = manager->getAddZero();
  29. storm::dd::Add<storm::dd::DdType::CUDD> b = manager->getEncoding(state.first, 2).toAdd() + manager->getEncoding(state.first, 4).toAdd();
  30. // Now solve the game with different strategies for the players.
  31. storm::dd::Add<storm::dd::DdType::CUDD> result = solver->solveGame(true, true, x, b);
  32. result *= manager->getEncoding(state.first, 1).toAdd();
  33. result = result.sumAbstract({state.first});
  34. EXPECT_NEAR(0, result.getValue(), storm::settings::nativeEquationSolverSettings().getPrecision());
  35. x = manager->getAddZero();
  36. result = solver->solveGame(true, false, x, b);
  37. result *= manager->getEncoding(state.first, 1).toAdd();
  38. result = result.sumAbstract({state.first});
  39. EXPECT_NEAR(0.5, result.getValue(), storm::settings::nativeEquationSolverSettings().getPrecision());
  40. x = manager->getAddZero();
  41. result = solver->solveGame(false, true, x, b);
  42. result *= manager->getEncoding(state.first, 1).toAdd();
  43. result = result.sumAbstract({state.first});
  44. EXPECT_NEAR(0.2, result.getValue(), storm::settings::nativeEquationSolverSettings().getPrecision());
  45. x = manager->getAddZero();
  46. result = solver->solveGame(false, false, x, b);
  47. result *= manager->getEncoding(state.first, 1).toAdd();
  48. result = result.sumAbstract({state.first});
  49. EXPECT_NEAR(0.99999892625817599, result.getValue(), storm::settings::nativeEquationSolverSettings().getPrecision());
  50. }