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.

342 lines
15 KiB

12 months ago
12 months ago
  1. #include "Grid.h"
  2. #include <boost/algorithm/string/find.hpp>
  3. #include <algorithm>
  4. prism::ModelType GridOptions::getModelType() const
  5. {
  6. if (agentsWithView.size() > 1) {
  7. return prism::ModelType::SMG;
  8. }
  9. return prism::ModelType::MDP;
  10. }
  11. Grid::Grid(cells gridCells, cells background, const GridOptions &gridOptions, const std::map<coordinates, float> &stateRewards)
  12. : allGridCells(gridCells), background(background), gridOptions(gridOptions), stateRewards(stateRewards)
  13. {
  14. cell max = allGridCells.at(allGridCells.size() - 1);
  15. maxBoundaries = std::make_pair(max.row - 1, max.column - 1);
  16. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(walls), [](cell c) {
  17. return c.type == Type::Wall;
  18. });
  19. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lava), [](cell c) {
  20. return c.type == Type::Lava;
  21. });
  22. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(floor), [](cell c) {
  23. return c.type == Type::Floor;
  24. });
  25. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyNorth), [](cell c) {
  26. return c.type == Type::SlipperyNorth;
  27. });
  28. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyEast), [](cell c) {
  29. return c.type == Type::SlipperyEast;
  30. });
  31. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperySouth), [](cell c) {
  32. return c.type == Type::SlipperySouth;
  33. });
  34. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyWest), [](cell c) {
  35. return c.type == Type::SlipperyWest;
  36. });
  37. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lockedDoors), [](cell c) {
  38. return c.type == Type::LockedDoor;
  39. });
  40. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(unlockedDoors), [](cell c) {
  41. return c.type == Type::Door;
  42. });
  43. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(goals), [](cell c) {
  44. return c.type == Type::Goal;
  45. });
  46. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(keys), [this](cell c) {
  47. return c.type == Type::Key;
  48. });
  49. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(boxes), [](cell c) {
  50. return c.type == Type::Box;
  51. });
  52. agent = *std::find_if(gridCells.begin(), gridCells.end(), [](cell c) {
  53. return c.type == Type::Agent;
  54. });
  55. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(adversaries), [](cell c) {
  56. return c.type == Type::Adversary;
  57. });
  58. agentNameAndPositionMap.insert({ "Agent", agent.getCoordinates() });
  59. for(auto const& adversary : adversaries) {
  60. std::string color = adversary.getColor();
  61. color.at(0) = std::toupper(color.at(0));
  62. try {
  63. if(gridOptions.agentsToBeConsidered.size() != 0 && std::find(gridOptions.agentsToBeConsidered.begin(), gridOptions.agentsToBeConsidered.end(), color) == gridOptions.agentsToBeConsidered.end()) continue;
  64. auto success = agentNameAndPositionMap.insert({ color, adversary.getCoordinates() });
  65. if(!success.second) {
  66. throw std::logic_error("Agent with " + color + " already present\n");
  67. }
  68. } catch(const std::logic_error& e) {
  69. std::cerr << "Expected agents colors to be different. Agent with color : '" << color << "' already present." << std::endl;
  70. throw;
  71. }
  72. }
  73. for(auto const& key : keys) {
  74. std::string color = key.getColor();
  75. try {
  76. auto success = keyNameAndPositionMap.insert({color, key.getCoordinates() });
  77. if (!success.second) {
  78. throw std::logic_error("Multiple keys with same color not supported " + color + "\n");
  79. }
  80. } catch(const std::logic_error& e) {
  81. std::cerr << "Expected key colors to be different. Key with color : '" << color << "' already present." << std::endl;
  82. throw;
  83. }
  84. }
  85. for(auto const& color : allColors) {
  86. cells cellsOfColor;
  87. std::copy_if(background.begin(), background.end(), std::back_inserter(cellsOfColor), [&color](cell c) {
  88. return c.type == Type::Floor && c.color == color;
  89. });
  90. if(cellsOfColor.size() > 0) {
  91. backgroundTiles.emplace(color, cellsOfColor);
  92. }
  93. }
  94. }
  95. std::ostream& operator<<(std::ostream& os, const Grid& grid) {
  96. int lastRow = 1;
  97. for(auto const& cell : grid.allGridCells) {
  98. if(lastRow != cell.row)
  99. os << std::endl;
  100. os << static_cast<char>(cell.type) << static_cast<char>(cell.color);
  101. lastRow = cell.row;
  102. }
  103. return os;
  104. }
  105. cells Grid::getGridCells() {
  106. return allGridCells;
  107. }
  108. bool Grid::isBlocked(coordinates p) {
  109. return isWall(p); //|| isLockedDoor(p) || isKey(p);
  110. }
  111. bool Grid::isWall(coordinates p) {
  112. return std::find_if(walls.begin(), walls.end(),
  113. [p](cell cell) {
  114. return cell.row == p.first && cell.column == p.second;
  115. }) != walls.end();
  116. }
  117. bool Grid::isLockedDoor(coordinates p) {
  118. return std::find_if(lockedDoors.begin(), lockedDoors.end(),
  119. [p](cell cell) {
  120. return cell.row == p.first && cell.column == p.second;
  121. }) != lockedDoors.end();
  122. }
  123. bool Grid::isUnlockedDoor(coordinates p) {
  124. return std::find_if(unlockedDoors.begin(), unlockedDoors.end(),
  125. [p](cell cell) {
  126. return cell.row == p.first && cell.column == p.second;
  127. }) != unlockedDoors.end();
  128. }
  129. bool Grid::isKey(coordinates p) {
  130. return std::find_if(keys.begin(), keys.end(),
  131. [p](cell cell) {
  132. return cell.row == p.first && cell.column == p.second;
  133. }) != keys.end();
  134. }
  135. bool Grid::isBox(coordinates p) {
  136. return std::find_if(boxes.begin(), boxes.end(),
  137. [p](cell cell) {
  138. return cell.row == p.first && cell.column == p.second;
  139. }) != boxes.end();
  140. }
  141. void Grid::applyOverwrites(std::string& str, std::vector<Configuration>& configuration) {
  142. for (auto& config : configuration) {
  143. if (!config.overwrite_) {
  144. continue;
  145. }
  146. size_t start_pos;
  147. if (config.type_ == ConfigType::Formula) {
  148. start_pos = str.find("formula " + config.identifier_);
  149. } else if (config.type_ == ConfigType::Label) {
  150. start_pos = str.find("label " + config.identifier_);
  151. } else if (config.type_ == ConfigType::Module) {
  152. auto iter = boost::find_nth(str, config.identifier_, config.index_);
  153. start_pos = std::distance(str.begin(), iter.begin());
  154. }
  155. else if (config.type_ == ConfigType::Constant) {
  156. start_pos = str.find(config.identifier_);
  157. if (start_pos == std::string::npos) {
  158. std::cout << "Couldn't find overwrite:" << config.expression_ << std::endl;
  159. }
  160. }
  161. size_t end_pos = str.find(';', start_pos) + 1;
  162. std::string expression = config.expression_;
  163. str.replace(start_pos, end_pos - start_pos , expression);
  164. }
  165. }
  166. void Grid::printToPrism(std::ostream& os, std::vector<Configuration>& configuration ,const prism::ModelType& modelType) {
  167. cells northRestriction;
  168. cells eastRestriction;
  169. cells southRestriction;
  170. cells westRestriction;
  171. cells walkable = floor;
  172. cells conditionallyWalkable;
  173. walkable.insert(walkable.end(), goals.begin(), goals.end());
  174. walkable.insert(walkable.end(), boxes.begin(), boxes.end());
  175. walkable.push_back(agent);
  176. walkable.insert(walkable.end(), adversaries.begin(), adversaries.end());
  177. walkable.insert(walkable.end(), lava.begin(), lava.end());
  178. conditionallyWalkable.insert(conditionallyWalkable.end(), keys.begin(), keys.end());
  179. conditionallyWalkable.insert(conditionallyWalkable.end(), lockedDoors.begin(), lockedDoors.end());
  180. conditionallyWalkable.insert(conditionallyWalkable.end(), unlockedDoors.begin(), unlockedDoors.end());
  181. for(auto const& c : walkable) {
  182. if(isBlocked(c.getNorth())) northRestriction.push_back(c);
  183. if(isBlocked(c.getEast())) eastRestriction.push_back(c);
  184. if(isBlocked(c.getSouth())) southRestriction.push_back(c);
  185. if(isBlocked(c.getWest())) westRestriction.push_back(c);
  186. }
  187. // TODO Add doors here (list of doors keys etc)
  188. // walkable.insert(walkable.end(), lockedDoors.begin(), lockedDoors.end());
  189. // walkable.insert(walkable.end(), unlockedDoors.begin(), unlockedDoors.end());
  190. for(auto const& c : conditionallyWalkable) {
  191. if(isBlocked(c.getNorth())) northRestriction.push_back(c);
  192. if(isBlocked(c.getEast())) eastRestriction.push_back(c);
  193. if(isBlocked(c.getSouth())) southRestriction.push_back(c);
  194. if(isBlocked(c.getWest())) westRestriction.push_back(c);
  195. }
  196. prism::PrismModulesPrinter printer(modelType, agentNameAndPositionMap.size(), configuration, gridOptions.enforceOneWays);
  197. printer.printModel(os, modelType);
  198. if(modelType == prism::ModelType::SMG) {
  199. printer.printGlobalMoveVariable(os, agentNameAndPositionMap.size());
  200. }
  201. for(auto const &backgroundTilesOfColor : backgroundTiles) {
  202. for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition) {
  203. printer.printBackgroundLabels(os, agentNameAndPosition->first, backgroundTilesOfColor);
  204. }
  205. }
  206. cells noTurnFloor;
  207. if(gridOptions.enforceOneWays) {
  208. for(auto const& c : floor) {
  209. cell east = c.getEast(allGridCells);
  210. cell south = c.getSouth(allGridCells);
  211. cell west = c.getWest(allGridCells);
  212. cell north = c.getNorth(allGridCells);
  213. if( (east.type == Type::Wall && west.type == Type::Wall) or
  214. (north.type == Type::Wall && south.type == Type::Wall) ) {
  215. noTurnFloor.push_back(c);
  216. }
  217. }
  218. }
  219. cells doors;
  220. doors.insert(doors.end(), lockedDoors.begin(), lockedDoors.end());
  221. doors.insert(doors.end(), unlockedDoors.begin(), unlockedDoors.end());
  222. for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition) {
  223. printer.printFormulas(os, agentNameAndPosition->first, northRestriction, eastRestriction, southRestriction, westRestriction, { slipperyNorth, slipperyEast, slipperySouth, slipperyWest }, lava, walls, noTurnFloor, slipperyNorth, slipperyEast, slipperySouth, slipperyWest, keys, doors);
  224. printer.printGoalLabel(os, agentNameAndPosition->first, goals);
  225. printer.printKeysLabels(os, agentNameAndPosition->first, keys);
  226. }
  227. std::vector<std::string> constants {"const double prop_zero = 0/9;",
  228. "const double prop_next_neighbour = 1/9;",
  229. "const double prop_slippery_move_forward = 7/9;",
  230. "const double prop_slippery_turn = 6/9;",
  231. "const double prop_next_neighbour_turn = 1/9;",
  232. "const double prop_direct_neighbour = 2/9;",
  233. "const double total_prop = 9;"};
  234. printer.printConstants(os, constants);
  235. std::vector<AgentName> agentNames;
  236. std::transform(agentNameAndPositionMap.begin(),
  237. agentNameAndPositionMap.end(),
  238. std::back_inserter(agentNames),
  239. [](const std::map<AgentNameAndPosition::first_type,AgentNameAndPosition::second_type>::value_type &pair){return pair.first;});
  240. if(modelType == prism::ModelType::SMG) {
  241. printer.printCrashLabel(os, agentNames);
  242. }
  243. size_t agentIndex = 0;
  244. printer.printInitStruct(os, agentNameAndPositionMap, keyNameAndPositionMap, lockedDoors, unlockedDoors, modelType);
  245. for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition, agentIndex++) {
  246. AgentName agentName = agentNameAndPosition->first;
  247. //std::cout << "Agent Name: " << agentName << std::endl;
  248. bool agentWithView = std::find(gridOptions.agentsWithView.begin(), gridOptions.agentsWithView.end(), agentName) != gridOptions.agentsWithView.end();
  249. bool agentWithProbabilisticBehaviour = std::find(gridOptions.agentsWithProbabilisticBehaviour.begin(), gridOptions.agentsWithProbabilisticBehaviour.end(), agentName) != gridOptions.agentsWithProbabilisticBehaviour.end();
  250. std::set<std::string> slipperyActions; // TODO AGENT POSITION INITIALIZATIN
  251. if(agentWithProbabilisticBehaviour) printer.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView, gridOptions.probabilitiesForActions);
  252. else printer.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView);
  253. for(auto const& c : slipperyNorth) {
  254. printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North);
  255. if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North);
  256. }
  257. for(auto const& c : slipperyEast) {
  258. printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East);
  259. if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East);
  260. }
  261. for(auto const& c : slipperySouth) {
  262. printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South);
  263. if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South);
  264. }
  265. for(auto const& c : slipperyWest) {
  266. printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West);
  267. if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West);
  268. }
  269. printer.printEndmodule(os);
  270. if(modelType == prism::ModelType::SMG) {
  271. if(agentWithProbabilisticBehaviour) printer.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, gridOptions.probabilitiesForActions, slipperyActions);
  272. else printer.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, {}, slipperyActions);
  273. }
  274. //if(!stateRewards.empty()) {
  275. printer.printRewards(os, agentName, stateRewards, lava, goals, backgroundTiles);
  276. //}
  277. }
  278. if (!configuration.empty()) {
  279. printer.printConfiguration(os, configuration);
  280. }
  281. // TODO CHANGE HANDLING
  282. std::string agentName = agentNames.at(0);
  283. for (auto const & key : keys) {
  284. os << "\n";
  285. printer.printKeyModule(os, key, maxBoundaries, agentName);
  286. printer.printEndmodule(os);
  287. }
  288. for (auto const& door : lockedDoors) {
  289. os << "\n";
  290. printer.printDoorModule(os, door, maxBoundaries, agentName);
  291. printer.printEndmodule(os);
  292. }
  293. }
  294. std::array<bool, 8> Grid::getWalkableDirOf8Neighborhood(cell c) /* const */ {
  295. return (std::array<bool, 8>)
  296. {
  297. !isBlocked(c.getNorth()),
  298. !isBlocked(c.getNorth(allGridCells).getEast()),
  299. !isBlocked(c.getEast()),
  300. !isBlocked(c.getSouth(allGridCells).getEast()),
  301. !isBlocked(c.getSouth()),
  302. !isBlocked(c.getSouth(allGridCells).getWest()),
  303. !isBlocked(c.getWest()),
  304. !isBlocked(c.getNorth(allGridCells).getWest())
  305. };
  306. }