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.

233 lines
11 KiB

  1. #include "Grid.h"
  2. #include <algorithm>
  3. Grid::Grid(cells gridCells, cells background, const GridOptions &gridOptions, const std::map<coordinates, float> &stateRewards)
  4. : allGridCells(gridCells), background(background), gridOptions(gridOptions), stateRewards(stateRewards)
  5. {
  6. cell max = allGridCells.at(allGridCells.size() - 1);
  7. maxBoundaries = std::make_pair(max.row - 1, max.column - 1);
  8. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(walls), [](cell c) {
  9. return c.type == Type::Wall;
  10. });
  11. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lava), [](cell c) {
  12. return c.type == Type::Lava;
  13. });
  14. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(floor), [](cell c) {
  15. return c.type == Type::Floor;
  16. });
  17. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyNorth), [](cell c) {
  18. return c.type == Type::SlipperyNorth;
  19. });
  20. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyEast), [](cell c) {
  21. return c.type == Type::SlipperyEast;
  22. });
  23. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperySouth), [](cell c) {
  24. return c.type == Type::SlipperySouth;
  25. });
  26. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyWest), [](cell c) {
  27. return c.type == Type::SlipperyWest;
  28. });
  29. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lockedDoors), [](cell c) {
  30. return c.type == Type::LockedDoor;
  31. });
  32. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(goals), [](cell c) {
  33. return c.type == Type::Goal;
  34. });
  35. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(keys), [](cell c) {
  36. return c.type == Type::Key;
  37. });
  38. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(boxes), [](cell c) {
  39. return c.type == Type::Box;
  40. });
  41. agent = *std::find_if(gridCells.begin(), gridCells.end(), [](cell c) {
  42. return c.type == Type::Agent;
  43. });
  44. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(adversaries), [](cell c) {
  45. return c.type == Type::Adversary;
  46. });
  47. agentNameAndPositionMap.insert({ "Agent", agent.getCoordinates() });
  48. for(auto const& adversary : adversaries) {
  49. std::string color = adversary.getColor();
  50. color.at(0) = std::toupper(color.at(0));
  51. try {
  52. if(gridOptions.agentsToBeConsidered.size() != 0 && std::find(gridOptions.agentsToBeConsidered.begin(), gridOptions.agentsToBeConsidered.end(), color) == gridOptions.agentsToBeConsidered.end()) continue;
  53. auto success = agentNameAndPositionMap.insert({ color, adversary.getCoordinates() });
  54. if(!success.second) {
  55. throw std::logic_error("Agent with " + color + " already present\n");
  56. }
  57. } catch(const std::logic_error& e) {
  58. std::cerr << "Expected agents colors to be different. Agent with color : '" << color << "' already present." << std::endl;
  59. throw;
  60. }
  61. }
  62. for(auto const& color : allColors) {
  63. cells cellsOfColor;
  64. std::copy_if(background.begin(), background.end(), std::back_inserter(cellsOfColor), [&color](cell c) {
  65. return c.type == Type::Floor && c.color == color;
  66. });
  67. if(cellsOfColor.size() > 0) {
  68. backgroundTiles.emplace(color, cellsOfColor);
  69. }
  70. }
  71. }
  72. std::ostream& operator<<(std::ostream& os, const Grid& grid) {
  73. int lastRow = 1;
  74. for(auto const& cell : grid.allGridCells) {
  75. if(lastRow != cell.row)
  76. os << std::endl;
  77. os << static_cast<char>(cell.type) << static_cast<char>(cell.color);
  78. lastRow = cell.row;
  79. }
  80. return os;
  81. }
  82. cells Grid::getGridCells() {
  83. return allGridCells;
  84. }
  85. bool Grid::isBlocked(coordinates p) {
  86. return isWall(p) || isLockedDoor(p) || isKey(p);
  87. }
  88. bool Grid::isWall(coordinates p) {
  89. return std::find_if(walls.begin(), walls.end(),
  90. [p](cell cell) {
  91. return cell.row == p.first && cell.column == p.second;
  92. }) != walls.end();
  93. }
  94. bool Grid::isLockedDoor(coordinates p) {
  95. return std::find_if(lockedDoors.begin(), lockedDoors.end(),
  96. [p](cell cell) {
  97. return cell.row == p.first && cell.column == p.second;
  98. }) != lockedDoors.end();
  99. }
  100. bool Grid::isKey(coordinates p) {
  101. return std::find_if(keys.begin(), keys.end(),
  102. [p](cell cell) {
  103. return cell.row == p.first && cell.column == p.second;
  104. }) != keys.end();
  105. }
  106. bool Grid::isBox(coordinates p) {
  107. return std::find_if(boxes.begin(), boxes.end(),
  108. [p](cell cell) {
  109. return cell.row == p.first && cell.column == p.second;
  110. }) != boxes.end();
  111. }
  112. void Grid::printToPrism(std::ostream& os, const prism::ModelType& modelType) {
  113. cells northRestriction;
  114. cells eastRestriction;
  115. cells southRestriction;
  116. cells westRestriction;
  117. cells walkable = floor;
  118. walkable.insert(walkable.end(), goals.begin(), goals.end());
  119. walkable.insert(walkable.end(), boxes.begin(), boxes.end());
  120. walkable.push_back(agent);
  121. walkable.insert(walkable.end(), adversaries.begin(), adversaries.end());
  122. walkable.insert(walkable.end(), lava.begin(), lava.end());
  123. walkable.insert(walkable.end(), keys.begin(), keys.end());
  124. walkable.insert(walkable.end(), lockedDoors.begin(), lockedDoors.end());
  125. for(auto const& c : walkable) {
  126. if(isBlocked(c.getNorth())) northRestriction.push_back(c);
  127. if(isBlocked(c.getEast())) eastRestriction.push_back(c);
  128. if(isBlocked(c.getSouth())) southRestriction.push_back(c);
  129. if(isBlocked(c.getWest())) westRestriction.push_back(c);
  130. }
  131. prism::PrismModulesPrinter printer(modelType, agentNameAndPositionMap.size(), gridOptions.enforceOneWays);
  132. printer.printModel(os, modelType);
  133. if(modelType == prism::ModelType::SMG) {
  134. printer.printGlobalMoveVariable(os, agentNameAndPositionMap.size());
  135. }
  136. for(auto const &backgroundTilesOfColor : backgroundTiles) {
  137. for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition) {
  138. printer.printBackgroundLabels(os, agentNameAndPosition->first, backgroundTilesOfColor);
  139. }
  140. }
  141. cells noTurnFloor;
  142. if(gridOptions.enforceOneWays) {
  143. for(auto const& c : floor) {
  144. cell east = c.getEast(allGridCells);
  145. cell south = c.getSouth(allGridCells);
  146. cell west = c.getWest(allGridCells);
  147. cell north = c.getNorth(allGridCells);
  148. if( (east.type == Type::Wall && west.type == Type::Wall) or
  149. (north.type == Type::Wall && south.type == Type::Wall) ) {
  150. noTurnFloor.push_back(c);
  151. }
  152. }
  153. }
  154. for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition) {
  155. printer.printFormulas(os, agentNameAndPosition->first, northRestriction, eastRestriction, southRestriction, westRestriction, { slipperyNorth, slipperyEast, slipperySouth, slipperyWest }, lava, walls, noTurnFloor, slipperyNorth, slipperyEast, slipperySouth, slipperyWest);
  156. printer.printGoalLabel(os, agentNameAndPosition->first, goals);
  157. printer.printKeysLabels(os, agentNameAndPosition->first, keys);
  158. }
  159. std::vector<AgentName> agentNames;
  160. std::transform(agentNameAndPositionMap.begin(),
  161. agentNameAndPositionMap.end(),
  162. std::back_inserter(agentNames),
  163. [](const std::map<AgentNameAndPosition::first_type,AgentNameAndPosition::second_type>::value_type &pair){return pair.first;});
  164. if(modelType == prism::ModelType::SMG) {
  165. printer.printCrashLabel(os, agentNames);
  166. }
  167. size_t agentIndex = 0;
  168. for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition, agentIndex++) {
  169. AgentName agentName = agentNameAndPosition->first;
  170. //std::cout << "Agent Name: " << agentName << std::endl;
  171. bool agentWithView = std::find(gridOptions.agentsWithView.begin(), gridOptions.agentsWithView.end(), agentName) != gridOptions.agentsWithView.end();
  172. bool agentWithProbabilisticBehaviour = std::find(gridOptions.agentsWithProbabilisticBehaviour.begin(), gridOptions.agentsWithProbabilisticBehaviour.end(), agentName) != gridOptions.agentsWithProbabilisticBehaviour.end();
  173. std::set<std::string> slipperyActions;
  174. printer.printInitStruct(os, agentName, keys);
  175. if(agentWithProbabilisticBehaviour) printer.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView, gridOptions.probabilitiesForActions);
  176. else printer.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView);
  177. for(auto const& c : slipperyNorth) {
  178. printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North);
  179. if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North);
  180. }
  181. for(auto const& c : slipperyEast) {
  182. printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East);
  183. if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East);
  184. }
  185. for(auto const& c : slipperySouth) {
  186. printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South);
  187. if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South);
  188. }
  189. for(auto const& c : slipperyWest) {
  190. printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West);
  191. if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West);
  192. }
  193. printer.printEndmodule(os);
  194. if(modelType == prism::ModelType::SMG) {
  195. if(agentWithProbabilisticBehaviour) printer.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, gridOptions.probabilitiesForActions, slipperyActions);
  196. else printer.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, {}, slipperyActions);
  197. }
  198. //if(!stateRewards.empty()) {
  199. printer.printRewards(os, agentName, stateRewards, lava, goals, backgroundTiles);
  200. //}
  201. }
  202. }
  203. std::array<bool, 8> Grid::getWalkableDirOf8Neighborhood(cell c) /* const */ {
  204. return (std::array<bool, 8>)
  205. {
  206. !isBlocked(c.getNorth()),
  207. !isBlocked(c.getNorth(allGridCells).getEast()),
  208. !isBlocked(c.getEast()),
  209. !isBlocked(c.getSouth(allGridCells).getEast()),
  210. !isBlocked(c.getSouth()),
  211. !isBlocked(c.getSouth(allGridCells).getWest()),
  212. !isBlocked(c.getWest()),
  213. !isBlocked(c.getNorth(allGridCells).getWest())
  214. };
  215. }