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.

344 lines
16 KiB

10 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, const double faultyProbability)
  12. : allGridCells(gridCells), background(background), gridOptions(gridOptions), stateRewards(stateRewards), faultyProbability(faultyProbability)
  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_intended = 6/9;",
  229. "const double prop_turn_intended = 6/9;",
  230. "const double prop_displacement = 3/9;",
  231. "const double prop_turn_displacement = 3/9;",
  232. "const int width = " + std::to_string(maxBoundaries.first) + ";",
  233. "const int height = " + std::to_string(maxBoundaries.second) + ";"
  234. };
  235. printer.printConstants(os, constants);
  236. std::vector<AgentName> agentNames;
  237. std::transform(agentNameAndPositionMap.begin(),
  238. agentNameAndPositionMap.end(),
  239. std::back_inserter(agentNames),
  240. [](const std::map<AgentNameAndPosition::first_type,AgentNameAndPosition::second_type>::value_type &pair){return pair.first;});
  241. if(modelType == prism::ModelType::SMG) {
  242. printer.printCrashLabel(os, agentNames);
  243. }
  244. size_t agentIndex = 0;
  245. printer.printInitStruct(os, agentNameAndPositionMap, keyNameAndPositionMap, lockedDoors, unlockedDoors, modelType);
  246. for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition, agentIndex++) {
  247. AgentName agentName = agentNameAndPosition->first;
  248. //std::cout << "Agent Name: " << agentName << std::endl;
  249. bool agentWithView = std::find(gridOptions.agentsWithView.begin(), gridOptions.agentsWithView.end(), agentName) != gridOptions.agentsWithView.end();
  250. bool agentWithProbabilisticBehaviour = std::find(gridOptions.agentsWithProbabilisticBehaviour.begin(), gridOptions.agentsWithProbabilisticBehaviour.end(), agentName) != gridOptions.agentsWithProbabilisticBehaviour.end();
  251. std::set<std::string> slipperyActions; // TODO AGENT POSITION INITIALIZATIN
  252. if(agentWithProbabilisticBehaviour) printer.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView, gridOptions.probabilitiesForActions);
  253. else printer.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView, {} ,faultyProbability);
  254. for(auto const& c : slipperyNorth) {
  255. printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North);
  256. if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North);
  257. }
  258. for(auto const& c : slipperyEast) {
  259. printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East);
  260. if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East);
  261. }
  262. for(auto const& c : slipperySouth) {
  263. printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South);
  264. if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South);
  265. }
  266. for(auto const& c : slipperyWest) {
  267. printer.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West);
  268. if(!gridOptions.enforceOneWays) printer.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West);
  269. }
  270. printer.printEndmodule(os);
  271. if(modelType == prism::ModelType::SMG) {
  272. if(agentWithProbabilisticBehaviour) printer.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, gridOptions.probabilitiesForActions, slipperyActions);
  273. else printer.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, {}, slipperyActions);
  274. }
  275. //if(!stateRewards.empty()) {
  276. printer.printRewards(os, agentName, stateRewards, lava, goals, backgroundTiles);
  277. //}
  278. }
  279. if (!configuration.empty()) {
  280. printer.printConfiguration(os, configuration);
  281. }
  282. // TODO CHANGE HANDLING
  283. std::string agentName = agentNames.at(0);
  284. for (auto const & key : keys) {
  285. os << "\n";
  286. printer.printKeyModule(os, key, maxBoundaries, agentName);
  287. printer.printEndmodule(os);
  288. }
  289. for (auto const& door : lockedDoors) {
  290. os << "\n";
  291. printer.printDoorModule(os, door, maxBoundaries, agentName);
  292. printer.printEndmodule(os);
  293. }
  294. }
  295. std::array<bool, 8> Grid::getWalkableDirOf8Neighborhood(cell c) /* const */ {
  296. return (std::array<bool, 8>)
  297. {
  298. !isBlocked(c.getNorth()),
  299. !isBlocked(c.getNorth(allGridCells).getEast()),
  300. !isBlocked(c.getEast()),
  301. !isBlocked(c.getSouth(allGridCells).getEast()),
  302. !isBlocked(c.getSouth()),
  303. !isBlocked(c.getSouth(allGridCells).getWest()),
  304. !isBlocked(c.getWest()),
  305. !isBlocked(c.getNorth(allGridCells).getWest())
  306. };
  307. }