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

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