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.

244 lines
13 KiB

  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 float 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) { return c.type == Type::Wall; });
  17. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lava), [](cell c) { return c.type == Type::Lava; });
  18. 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
  19. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyNorth), [](cell c) { return c.type == Type::SlipperyNorth; });
  20. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyEast), [](cell c) { return c.type == Type::SlipperyEast; });
  21. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperySouth), [](cell c) { return c.type == Type::SlipperySouth; });
  22. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyWest), [](cell c) { return c.type == Type::SlipperyWest; });
  23. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lockedDoors), [](cell c) { return c.type == Type::LockedDoor; });
  24. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(unlockedDoors), [](cell c) { return c.type == Type::Door; });
  25. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(goals), [](cell c) { return c.type == Type::Goal; });
  26. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(keys), [](cell c) { return c.type == Type::Key; });
  27. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(boxes), [](cell c) { return c.type == Type::Box; });
  28. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(balls), [](cell c) { return c.type == Type::Ball; });
  29. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(adversaries), [](cell c) { return c.type == Type::Adversary; });
  30. agent = *std::find_if(gridCells.begin(), gridCells.end(), [](cell c) { return c.type == Type::Agent; });
  31. floor.push_back(agent);
  32. agentNameAndPositionMap.insert({ "Agent", agent.getCoordinates() });
  33. for(auto const& adversary : adversaries) {
  34. std::string color = adversary.getColor();
  35. color.at(0) = std::toupper(color.at(0));
  36. try {
  37. if(gridOptions.agentsToBeConsidered.size() != 0 && std::find(gridOptions.agentsToBeConsidered.begin(), gridOptions.agentsToBeConsidered.end(), color) == gridOptions.agentsToBeConsidered.end()) continue;
  38. auto success = agentNameAndPositionMap.insert({ color, adversary.getCoordinates() });
  39. floor.push_back(adversary);
  40. if(!success.second) {
  41. throw std::logic_error("Agent with " + color + " already present\n");
  42. }
  43. } catch(const std::logic_error& e) {
  44. std::cerr << "Expected agents colors to be different. Agent with color : '" << color << "' already present." << std::endl;
  45. throw;
  46. }
  47. }
  48. for(auto const& key : keys) {
  49. std::string color = key.getColor();
  50. try {
  51. auto success = keyNameAndPositionMap.insert({color, key.getCoordinates() });
  52. if (!success.second) {
  53. throw std::logic_error("Multiple keys with same color not supported " + color + "\n");
  54. }
  55. } catch(const std::logic_error& e) {
  56. std::cerr << "Expected key colors to be different. Key with color : '" << color << "' already present." << std::endl;
  57. throw;
  58. }
  59. }
  60. for(auto const& color : allColors) {
  61. cells cellsOfColor;
  62. std::copy_if(background.begin(), background.end(), std::back_inserter(cellsOfColor), [&color](cell c) {
  63. return c.type == Type::Floor && c.color == color;
  64. });
  65. if(cellsOfColor.size() > 0) {
  66. backgroundTiles.emplace(color, cellsOfColor);
  67. }
  68. }
  69. }
  70. std::ostream& operator<<(std::ostream& os, const Grid& grid) {
  71. int lastRow = 1;
  72. for(auto const& cell : grid.allGridCells) {
  73. if(lastRow != cell.row)
  74. os << std::endl;
  75. os << static_cast<char>(cell.type) << static_cast<char>(cell.color);
  76. lastRow = cell.row;
  77. }
  78. return os;
  79. }
  80. cells Grid::getGridCells() {
  81. return allGridCells;
  82. }
  83. bool Grid::isBlocked(coordinates p) {
  84. return isWall(p);
  85. }
  86. bool Grid::isWall(coordinates p) {
  87. return std::find_if(walls.begin(), walls.end(),
  88. [p](cell cell) {
  89. return cell.row == p.second && cell.column == p.first;
  90. }) != walls.end();
  91. }
  92. void Grid::applyOverwrites(std::string& str, std::vector<Configuration>& configuration) {
  93. for (auto& config : configuration) {
  94. if (!config.overwrite_) {
  95. continue;
  96. }
  97. size_t start_pos;
  98. if (config.type_ == ConfigType::Formula) {
  99. start_pos = str.find("formula " + config.identifier_);
  100. } else if (config.type_ == ConfigType::Label) {
  101. start_pos = str.find("label " + config.identifier_);
  102. } else if (config.type_ == ConfigType::Module) {
  103. auto iter = boost::find_nth(str, config.identifier_, config.index_);
  104. start_pos = std::distance(str.begin(), iter.begin());
  105. }
  106. else if (config.type_ == ConfigType::Constant) {
  107. start_pos = str.find(config.identifier_);
  108. if (start_pos == std::string::npos) {
  109. std::cout << "Couldn't find overwrite:" << config.expression_ << std::endl;
  110. }
  111. }
  112. size_t end_pos = str.find(';', start_pos) + 1;
  113. std::string expression = config.expression_;
  114. str.replace(start_pos, end_pos - start_pos , expression);
  115. }
  116. }
  117. void Grid::printToPrism(std::ostream& os, std::vector<Configuration>& configuration ,const prism::ModelType& modelType) {
  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(), adversaries.begin(), adversaries.end());
  123. walkable.insert(walkable.end(), lava.begin(), lava.end());
  124. walkable.insert(walkable.end(), keys.begin(), keys.end());
  125. walkable.insert(walkable.end(), balls.begin(), balls.end());
  126. for(auto const& c : walkable) {
  127. std::cout << "checking: " << c << " south of c: " << c.getSouth().first << " " << c.getSouth().second << std::endl;
  128. if(isWall(c.getNorth())) northRestriction.push_back(c);
  129. if(isWall(c.getEast())) eastRestriction.push_back(c);
  130. if(isWall(c.getSouth())) southRestriction.push_back(c);
  131. if(isWall(c.getWest())) westRestriction.push_back(c);
  132. }
  133. std::map<std::string, cells> wallRestrictions = {{"North", northRestriction}, {"East", eastRestriction}, {"South", southRestriction}, {"West", westRestriction}};
  134. std::map<std::string, cells> slipperyTiles = {{"North", slipperyNorth}, {"East", slipperyEast}, {"South", slipperySouth}, {"West", slipperyWest}};
  135. std::vector<AgentName> agentNames;
  136. std::transform(agentNameAndPositionMap.begin(),
  137. agentNameAndPositionMap.end(),
  138. std::back_inserter(agentNames),
  139. [](const std::map<AgentNameAndPosition::first_type,AgentNameAndPosition::second_type>::value_type &pair){return pair.first;});
  140. std::string agentName = agentNames.at(0);
  141. prism::PrismFormulaPrinter formulas(os, wallRestrictions, boxes, balls, lockedDoors, unlockedDoors, keys, slipperyTiles, lava, goals);
  142. prism::PrismModulesPrinter modules(os, modelType, maxBoundaries, boxes, balls, lockedDoors, unlockedDoors, keys, slipperyTiles, agentNameAndPositionMap, configuration, faultyProbability);
  143. modules.printModelType(modelType);
  144. for(const auto &agentName : agentNames) {
  145. formulas.print(agentName);
  146. }
  147. if(modelType == prism::ModelType::SMG) modules.printGlobalMoveVariable(os, agentNameAndPositionMap.size());
  148. std::vector<std::string> constants {"const double prop_zero = 0/9;",
  149. "const double prop_intended = 6/9;",
  150. "const double prop_turn_intended = 6/9;",
  151. "const double prop_displacement = 3/9;",
  152. "const double prop_turn_displacement = 3/9;",
  153. "const int width = " + std::to_string(maxBoundaries.first) + ";",
  154. "const int height = " + std::to_string(maxBoundaries.second) + ";"
  155. };
  156. modules.printConstants(os, constants);
  157. modules.print();
  158. //modules.printInitStruct(os, agentNameAndPositionMap, keyNameAndPositionMap, lockedDoors, unlockedDoors, modelType);
  159. //size_t agentIndex = 0;
  160. //for(auto agentNameAndPosition = agentNameAndPositionMap.begin(); agentNameAndPosition != agentNameAndPositionMap.end(); ++agentNameAndPosition, agentIndex++) {
  161. // AgentName agentName = agentNameAndPosition->first;
  162. // //std::cout << "Agent Name: " << agentName << std::endl;
  163. // bool agentWithView = std::find(gridOptions.agentsWithView.begin(), gridOptions.agentsWithView.end(), agentName) != gridOptions.agentsWithView.end();
  164. // bool agentWithProbabilisticBehaviour = std::find(gridOptions.agentsWithProbabilisticBehaviour.begin(), gridOptions.agentsWithProbabilisticBehaviour.end(), agentName) != gridOptions.agentsWithProbabilisticBehaviour.end();
  165. // std::set<std::string> slipperyActions; // TODO AGENT POSITION INITIALIZATIN
  166. // if(agentWithProbabilisticBehaviour) modules.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView, gridOptions.probabilitiesForActions);
  167. // else modules.printModule(os, agentName, agentIndex, maxBoundaries, agentNameAndPosition->second, keys, backgroundTiles, agentWithView, {} ,faultyProbability);
  168. // for(auto const& c : slipperyNorth) {
  169. // modules.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North);
  170. // if(!gridOptions.enforceOneWays) modules.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::North);
  171. // }
  172. // for(auto const& c : slipperyEast) {
  173. // modules.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East);
  174. // if(!gridOptions.enforceOneWays) modules.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::East);
  175. // }
  176. // for(auto const& c : slipperySouth) {
  177. // modules.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South);
  178. // if(!gridOptions.enforceOneWays) modules.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::South);
  179. // }
  180. // for(auto const& c : slipperyWest) {
  181. // modules.printSlipperyMove(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West);
  182. // if(!gridOptions.enforceOneWays) modules.printSlipperyTurn(os, agentName, agentIndex, c.getCoordinates(), slipperyActions, getWalkableDirOf8Neighborhood(c), prism::PrismModulesPrinter::SlipperyType::West);
  183. // }
  184. // modules.printEndmodule(os);
  185. // if(modelType == prism::ModelType::SMG) {
  186. // if(agentWithProbabilisticBehaviour) modules.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, gridOptions.probabilitiesForActions, slipperyActions);
  187. // else modules.printPlayerStruct(os, agentNameAndPosition->first, agentWithView, {}, slipperyActions);
  188. // }
  189. //if(!stateRewards.empty()) {
  190. // modules.printRewards(os, agentName, stateRewards, lava, goals, backgroundTiles);
  191. //}
  192. //}
  193. if (!configuration.empty()) {
  194. modules.printConfiguration(os, configuration);
  195. }
  196. }
  197. std::array<bool, 8> Grid::getWalkableDirOf8Neighborhood(cell c) /* const */ {
  198. return (std::array<bool, 8>)
  199. {
  200. !isBlocked(c.getNorth()),
  201. !isBlocked(c.getNorth(allGridCells).getEast()),
  202. !isBlocked(c.getEast()),
  203. !isBlocked(c.getSouth(allGridCells).getEast()),
  204. !isBlocked(c.getSouth()),
  205. !isBlocked(c.getSouth(allGridCells).getWest()),
  206. !isBlocked(c.getWest()),
  207. !isBlocked(c.getNorth(allGridCells).getWest())
  208. };
  209. }