#include "gtest/gtest.h" #include "storm-config.h" #include "src/storage/dd/DdManager.h" #include "src/utility/solver.h" #include "src/settings/SettingsManager.h" #include "src/solver/SymbolicGameSolver.h" #include "src/settings/modules/NativeEquationSolverSettings.h" TEST(FullySymbolicGameSolverTest, Solve_Cudd) { // Create some variables. std::shared_ptr> manager(new storm::dd::DdManager()); std::pair state = manager->addMetaVariable("x", 1, 4); std::pair pl1 = manager->addMetaVariable("a", 0, 1); std::pair pl2 = manager->addMetaVariable("b", 0, 1); storm::dd::Bdd allRows = manager->getBddZero(); std::set rowMetaVariables({state.first}); std::set columnMetaVariables({state.second}); std::vector> rowColumnMetaVariablePairs = {state}; std::set player1Variables({pl1.first}); std::set player2Variables({pl2.first}); // Construct simple game. storm::dd::Add matrix = manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 2).template toAdd() * manager->getEncoding(pl1.first, 0).template toAdd() * manager->getEncoding(pl2.first, 0).template toAdd() * manager->getConstant(0.6); matrix += manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 1).template toAdd() * manager->getEncoding(pl1.first, 0).template toAdd() * manager->getEncoding(pl2.first, 0).template toAdd() * manager->getConstant(0.4); matrix += manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 2).template toAdd() * manager->getEncoding(pl1.first, 0).template toAdd() * manager->getEncoding(pl2.first, 1).template toAdd() * manager->getConstant(0.2); matrix += manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 3).template toAdd() * manager->getEncoding(pl1.first, 0).template toAdd() * manager->getEncoding(pl2.first, 1).template toAdd() * manager->getConstant(0.8); matrix += manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 3).template toAdd() * manager->getEncoding(pl1.first, 1).template toAdd() * manager->getEncoding(pl2.first, 0).template toAdd() * manager->getConstant(0.5); matrix += manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 4).template toAdd() * manager->getEncoding(pl1.first, 1).template toAdd() * manager->getEncoding(pl2.first, 0).template toAdd() * manager->getConstant(0.5); matrix += manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 1).template toAdd() * manager->getEncoding(pl1.first, 1).template toAdd() * manager->getEncoding(pl2.first, 1).template toAdd() * manager->getConstant(1); std::unique_ptr> solverFactory(new storm::utility::solver::SymbolicGameSolverFactory()); std::unique_ptr> solver = solverFactory->create(matrix, allRows, rowMetaVariables, columnMetaVariables, rowColumnMetaVariablePairs, player1Variables,player2Variables); // Create solution and target state vector. storm::dd::Add x = manager->template getAddZero(); storm::dd::Add b = manager->getEncoding(state.first, 2).template toAdd() + manager->getEncoding(state.first, 4).template toAdd(); // Now solve the game with different strategies for the players. storm::dd::Add result = solver->solveGame(storm::OptimizationDirection::Minimize, storm::OptimizationDirection::Minimize, x, b); result *= manager->getEncoding(state.first, 1).template toAdd(); result = result.sumAbstract({state.first}); EXPECT_NEAR(0, result.getValue(), storm::settings::nativeEquationSolverSettings().getPrecision()); x = manager->getAddZero(); result = solver->solveGame(storm::OptimizationDirection::Minimize, storm::OptimizationDirection::Maximize, x, b); result *= manager->getEncoding(state.first, 1).template toAdd(); result = result.sumAbstract({state.first}); EXPECT_NEAR(0.5, result.getValue(), storm::settings::nativeEquationSolverSettings().getPrecision()); x = manager->getAddZero(); result = solver->solveGame(storm::OptimizationDirection::Maximize, storm::OptimizationDirection::Minimize, x, b); result *= manager->getEncoding(state.first, 1).template toAdd(); result = result.sumAbstract({state.first}); EXPECT_NEAR(0.2, result.getValue(), storm::settings::nativeEquationSolverSettings().getPrecision()); x = manager->getAddZero(); result = solver->solveGame(storm::OptimizationDirection::Maximize, storm::OptimizationDirection::Maximize, x, b); result *= manager->getEncoding(state.first, 1).template toAdd(); result = result.sumAbstract({state.first}); EXPECT_NEAR(0.99999892625817599, result.getValue(), storm::settings::nativeEquationSolverSettings().getPrecision()); } TEST(FullySymbolicGameSolverTest, Solve_Sylvan) { // Create some variables. std::shared_ptr> manager(new storm::dd::DdManager()); std::pair state = manager->addMetaVariable("x", 1, 4); std::pair pl1 = manager->addMetaVariable("a", 0, 1); std::pair pl2 = manager->addMetaVariable("b", 0, 1); storm::dd::Bdd allRows = manager->getBddZero(); std::set rowMetaVariables({state.first}); std::set columnMetaVariables({state.second}); std::vector> rowColumnMetaVariablePairs = {state}; std::set player1Variables({pl1.first}); std::set player2Variables({pl2.first}); // Construct simple game. storm::dd::Add matrix = manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 2).template toAdd() * manager->getEncoding(pl1.first, 0).template toAdd() * manager->getEncoding(pl2.first, 0).template toAdd() * manager->getConstant(0.6); matrix += manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 1).template toAdd() * manager->getEncoding(pl1.first, 0).template toAdd() * manager->getEncoding(pl2.first, 0).template toAdd() * manager->getConstant(0.4); matrix += manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 2).template toAdd() * manager->getEncoding(pl1.first, 0).template toAdd() * manager->getEncoding(pl2.first, 1).template toAdd() * manager->getConstant(0.2); matrix += manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 3).template toAdd() * manager->getEncoding(pl1.first, 0).template toAdd() * manager->getEncoding(pl2.first, 1).template toAdd() * manager->getConstant(0.8); matrix += manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 3).template toAdd() * manager->getEncoding(pl1.first, 1).template toAdd() * manager->getEncoding(pl2.first, 0).template toAdd() * manager->getConstant(0.5); matrix += manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 4).template toAdd() * manager->getEncoding(pl1.first, 1).template toAdd() * manager->getEncoding(pl2.first, 0).template toAdd() * manager->getConstant(0.5); matrix += manager->getEncoding(state.first, 1).template toAdd() * manager->getEncoding(state.second, 1).template toAdd() * manager->getEncoding(pl1.first, 1).template toAdd() * manager->getEncoding(pl2.first, 1).template toAdd() * manager->getConstant(1); std::unique_ptr> solverFactory(new storm::utility::solver::SymbolicGameSolverFactory()); std::unique_ptr> solver = solverFactory->create(matrix, allRows, rowMetaVariables, columnMetaVariables, rowColumnMetaVariablePairs, player1Variables,player2Variables); // Create solution and target state vector. storm::dd::Add x = manager->template getAddZero(); storm::dd::Add b = manager->getEncoding(state.first, 2).template toAdd() + manager->getEncoding(state.first, 4).template toAdd(); // Now solve the game with different strategies for the players. storm::dd::Add result = solver->solveGame(storm::OptimizationDirection::Minimize, storm::OptimizationDirection::Minimize, x, b); result *= manager->getEncoding(state.first, 1).template toAdd(); result = result.sumAbstract({state.first}); EXPECT_NEAR(0, result.getValue(), storm::settings::nativeEquationSolverSettings().getPrecision()); x = manager->getAddZero(); result = solver->solveGame(storm::OptimizationDirection::Minimize, storm::OptimizationDirection::Maximize, x, b); result *= manager->getEncoding(state.first, 1).template toAdd(); result = result.sumAbstract({state.first}); EXPECT_NEAR(0.5, result.getValue(), storm::settings::nativeEquationSolverSettings().getPrecision()); x = manager->getAddZero(); result = solver->solveGame(storm::OptimizationDirection::Maximize, storm::OptimizationDirection::Minimize, x, b); result *= manager->getEncoding(state.first, 1).template toAdd(); result = result.sumAbstract({state.first}); EXPECT_NEAR(0.2, result.getValue(), storm::settings::nativeEquationSolverSettings().getPrecision()); x = manager->getAddZero(); result = solver->solveGame(storm::OptimizationDirection::Maximize, storm::OptimizationDirection::Maximize, x, b); result *= manager->getEncoding(state.first, 1).template toAdd(); result = result.sumAbstract({state.first}); EXPECT_NEAR(0.99999892625817599, result.getValue(), storm::settings::nativeEquationSolverSettings().getPrecision()); }