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.

179 lines
8.0 KiB

12 months ago
  1. #include "Grid.h"
  2. #include <boost/algorithm/string/find.hpp>
  3. #include <algorithm>
  4. Grid::Grid(cells gridCells, cells background, const std::map<coordinates, float> &stateRewards, const float probIntended, const float faultyProbability)
  5. : allGridCells(gridCells), background(background), stateRewards(stateRewards), probIntended(probIntended), faultyProbability(faultyProbability)
  6. {
  7. cell max = allGridCells.at(allGridCells.size() - 1);
  8. maxBoundaries = std::make_pair(max.column - 1, max.row - 1);
  9. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(walls), [](cell c) { return c.type == Type::Wall; });
  10. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lava), [](cell c) { return c.type == Type::Lava; });
  11. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(floor), [](cell c) { return c.type == Type::Floor; }); // TODO CHECK IF ALL AGENTS ARE ADDED TO FLOOR
  12. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyNorth), [](cell c) { return c.type == Type::SlipperyNorth; });
  13. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyEast), [](cell c) { return c.type == Type::SlipperyEast; });
  14. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperySouth), [](cell c) { return c.type == Type::SlipperySouth; });
  15. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyWest), [](cell c) { return c.type == Type::SlipperyWest; });
  16. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lockedDoors), [](cell c) { return c.type == Type::LockedDoor; });
  17. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(unlockedDoors), [](cell c) { return c.type == Type::Door; });
  18. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(goals), [](cell c) { return c.type == Type::Goal; });
  19. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(keys), [](cell c) { return c.type == Type::Key; });
  20. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(boxes), [](cell c) { return c.type == Type::Box; });
  21. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(balls), [](cell c) { return c.type == Type::Ball; });
  22. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(adversaries), [](cell c) { return c.type == Type::Adversary; });
  23. agent = *std::find_if(gridCells.begin(), gridCells.end(), [](cell c) { return c.type == Type::Agent; });
  24. floor.push_back(agent);
  25. agentNameAndPositionMap.insert({ "Agent", agent.getCoordinates() });
  26. for(auto const& adversary : adversaries) {
  27. std::string color = adversary.getColor();
  28. color.at(0) = std::toupper(color.at(0));
  29. try {
  30. auto success = agentNameAndPositionMap.insert({ color, adversary.getCoordinates() });
  31. floor.push_back(adversary);
  32. if(!success.second) {
  33. throw std::logic_error("Agent with " + color + " already present\n");
  34. }
  35. } catch(const std::logic_error& e) {
  36. std::cerr << "Expected agents colors to be different. Agent with color : '" << color << "' already present." << std::endl;
  37. throw;
  38. }
  39. }
  40. for(auto const& key : keys) {
  41. std::string color = key.getColor();
  42. try {
  43. auto success = keyNameAndPositionMap.insert({color, key.getCoordinates() });
  44. if (!success.second) {
  45. throw std::logic_error("Multiple keys with same color not supported " + color + "\n");
  46. }
  47. } catch(const std::logic_error& e) {
  48. std::cerr << "Expected key colors to be different. Key with color : '" << color << "' already present." << std::endl;
  49. throw;
  50. }
  51. }
  52. for(auto const& color : allColors) {
  53. cells cellsOfColor;
  54. std::copy_if(background.begin(), background.end(), std::back_inserter(cellsOfColor), [&color](cell c) {
  55. return c.type == Type::Floor && c.color == color;
  56. });
  57. if(cellsOfColor.size() > 0) {
  58. backgroundTiles.emplace(color, cellsOfColor);
  59. }
  60. }
  61. if(adversaries.empty()) {
  62. modelType = prism::ModelType::MDP;
  63. } else {
  64. modelType = prism::ModelType::SMG;
  65. }
  66. }
  67. std::ostream& operator<<(std::ostream& os, const Grid& grid) {
  68. int lastRow = 1;
  69. for(auto const& cell : grid.allGridCells) {
  70. if(lastRow != cell.row)
  71. os << std::endl;
  72. os << static_cast<char>(cell.type) << static_cast<char>(cell.color);
  73. lastRow = cell.row;
  74. }
  75. return os;
  76. }
  77. cells Grid::getGridCells() {
  78. return allGridCells;
  79. }
  80. bool Grid::isBlocked(coordinates p) {
  81. return isWall(p);
  82. }
  83. bool Grid::isWall(coordinates p) {
  84. return std::find_if(walls.begin(), walls.end(),
  85. [p](cell cell) {
  86. return cell.row == p.second && cell.column == p.first;
  87. }) != walls.end();
  88. }
  89. void Grid::applyOverwrites(std::string& str, std::vector<Configuration>& configuration) {
  90. for (auto& config : configuration) {
  91. if (!config.overwrite_) {
  92. continue;
  93. }
  94. for (auto& index : config.indexes_) {
  95. size_t start_pos;
  96. std::string search;
  97. if (config.type_ == ConfigType::Formula) {
  98. search = "formula " + config.identifier_;
  99. } else if (config.type_ == ConfigType::Label) {
  100. search = "label " + config.identifier_;
  101. } else if (config.type_ == ConfigType::Module) {
  102. search = config.identifier_;
  103. }
  104. else if (config.type_ == ConfigType::Constant) {
  105. search = config.identifier_;
  106. }
  107. auto iter = boost::find_nth(str, search, index);
  108. start_pos = std::distance(str.begin(), iter.begin());
  109. size_t end_pos = str.find(';', start_pos) + 1;
  110. if (end_pos != std::string::npos && end_pos != 0) {
  111. std::string expression = config.expression_;
  112. str.replace(start_pos, end_pos - start_pos , expression);
  113. }
  114. }
  115. }
  116. }
  117. void Grid::printToPrism(std::ostream& os, std::vector<Configuration>& configuration) {
  118. cells northRestriction, eastRestriction, southRestriction, westRestriction;
  119. cells walkable = floor;
  120. walkable.insert(walkable.end(), goals.begin(), goals.end());
  121. walkable.insert(walkable.end(), boxes.begin(), boxes.end());
  122. walkable.insert(walkable.end(), lava.begin(), lava.end());
  123. walkable.insert(walkable.end(), keys.begin(), keys.end());
  124. walkable.insert(walkable.end(), balls.begin(), balls.end());
  125. for(auto const& c : walkable) {
  126. if(isWall(c.getNorth())) northRestriction.push_back(c);
  127. if(isWall(c.getEast())) eastRestriction.push_back(c);
  128. if(isWall(c.getSouth())) southRestriction.push_back(c);
  129. if(isWall(c.getWest())) westRestriction.push_back(c);
  130. }
  131. std::map<std::string, cells> wallRestrictions = {{"North", northRestriction}, {"East", eastRestriction}, {"South", southRestriction}, {"West", westRestriction}};
  132. std::map<std::string, cells> slipperyTiles = {{"North", slipperyNorth}, {"East", slipperyEast}, {"South", slipperySouth}, {"West", slipperyWest}};
  133. std::vector<AgentName> agentNames;
  134. std::transform(agentNameAndPositionMap.begin(),
  135. agentNameAndPositionMap.end(),
  136. std::back_inserter(agentNames),
  137. [](const std::map<AgentNameAndPosition::first_type,AgentNameAndPosition::second_type>::value_type &pair){return pair.first;});
  138. std::string agentName = agentNames.at(0);
  139. prism::PrismFormulaPrinter formulas(os, wallRestrictions, walls, lockedDoors, unlockedDoors, keys, slipperyTiles, lava, goals, agentNameAndPositionMap, faultyProbability > 0.0);
  140. prism::PrismModulesPrinter modules(os, modelType, maxBoundaries, lockedDoors, unlockedDoors, keys, slipperyTiles, agentNameAndPositionMap, configuration, probIntended, faultyProbability, !lava.empty(), !goals.empty());
  141. modules.printModelType(modelType);
  142. for(const auto &agentName : agentNames) {
  143. formulas.print(agentName);
  144. }
  145. if(agentNameAndPositionMap.size() > 1) formulas.printCollisionFormula(agentName);
  146. formulas.printInitStruct();
  147. modules.print();
  148. //if(!stateRewards.empty()) {
  149. // modules.printRewards(os, agentName, stateRewards, lava, goals, backgroundTiles);
  150. //}
  151. //if (!configuration.empty()) {
  152. // modules.printConfiguration(os, configuration);
  153. //}
  154. }