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.

183 lines
8.8 KiB

9 months ago
  1. #include "Grid.h"
  2. #include <boost/algorithm/string/find.hpp>
  3. #include <algorithm>
  4. Grid::Grid(cells gridCells, cells background, const std::map<coordinates, float> &stateRewards, const float probIntended, const float faultyProbability)
  5. : allGridCells(gridCells), background(background), stateRewards(stateRewards), probIntended(probIntended), faultyProbability(faultyProbability)
  6. {
  7. cell max = allGridCells.at(allGridCells.size() - 1);
  8. maxBoundaries = std::make_pair(max.column - 1, max.row - 1);
  9. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(walls), [](cell c) { return c.type == Type::Wall; });
  10. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lava), [](cell c) { return c.type == Type::Lava; });
  11. 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
  12. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyNorth), [](cell c) { return c.type == Type::SlipperyNorth; });
  13. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyEast), [](cell c) { return c.type == Type::SlipperyEast; });
  14. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperySouth), [](cell c) { return c.type == Type::SlipperySouth; });
  15. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyWest), [](cell c) { return c.type == Type::SlipperyWest; });
  16. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyNorthWest), [](cell c) { return c.type == Type::SlipperyNorthWest; });
  17. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperyNorthEast), [](cell c) { return c.type == Type::SlipperyNorthEast; });
  18. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperySouthWest), [](cell c) { return c.type == Type::SlipperySouthWest; });
  19. std::copy_if(background.begin(), background.end(), std::back_inserter(slipperySouthEast), [](cell c) { return c.type == Type::SlipperySouthEast; });
  20. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(lockedDoors), [](cell c) { return c.type == Type::LockedDoor; });
  21. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(unlockedDoors), [](cell c) { return c.type == Type::Door; });
  22. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(goals), [](cell c) { return c.type == Type::Goal; });
  23. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(keys), [](cell c) { return c.type == Type::Key; });
  24. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(boxes), [](cell c) { return c.type == Type::Box; });
  25. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(balls), [](cell c) { return c.type == Type::Ball; });
  26. std::copy_if(gridCells.begin(), gridCells.end(), std::back_inserter(adversaries), [](cell c) { return c.type == Type::Adversary; });
  27. agent = *std::find_if(gridCells.begin(), gridCells.end(), [](cell c) { return c.type == Type::Agent; });
  28. floor.push_back(agent);
  29. agentNameAndPositionMap.insert({ "Agent", agent.getCoordinates() });
  30. for(auto const& adversary : adversaries) {
  31. std::string color = adversary.getColor();
  32. color.at(0) = std::toupper(color.at(0));
  33. try {
  34. auto success = agentNameAndPositionMap.insert({ color, adversary.getCoordinates() });
  35. floor.push_back(adversary);
  36. if(!success.second) {
  37. throw std::logic_error("Agent with " + color + " already present\n");
  38. }
  39. } catch(const std::logic_error& e) {
  40. std::cerr << "Expected agents colors to be different. Agent with color : '" << color << "' already present." << std::endl;
  41. throw;
  42. }
  43. }
  44. for(auto const& key : keys) {
  45. std::string color = key.getColor();
  46. try {
  47. auto success = keyNameAndPositionMap.insert({color, key.getCoordinates() });
  48. if (!success.second) {
  49. throw std::logic_error("Multiple keys with same color not supported " + color + "\n");
  50. }
  51. } catch(const std::logic_error& e) {
  52. std::cerr << "Expected key colors to be different. Key with color : '" << color << "' already present." << std::endl;
  53. throw;
  54. }
  55. }
  56. for(auto const& color : allColors) {
  57. cells cellsOfColor;
  58. std::copy_if(background.begin(), background.end(), std::back_inserter(cellsOfColor), [&color](cell c) {
  59. return c.type == Type::Floor && c.color == color;
  60. });
  61. if(cellsOfColor.size() > 0) {
  62. backgroundTiles.emplace(color, cellsOfColor);
  63. }
  64. }
  65. if(adversaries.empty()) {
  66. modelType = prism::ModelType::MDP;
  67. } else {
  68. modelType = prism::ModelType::SMG;
  69. }
  70. }
  71. std::ostream& operator<<(std::ostream& os, const Grid& grid) {
  72. int lastRow = 1;
  73. for(auto const& cell : grid.allGridCells) {
  74. if(lastRow != cell.row)
  75. os << std::endl;
  76. os << static_cast<char>(cell.type) << static_cast<char>(cell.color);
  77. lastRow = cell.row;
  78. }
  79. return os;
  80. }
  81. cells Grid::getGridCells() {
  82. return allGridCells;
  83. }
  84. bool Grid::isBlocked(coordinates p) {
  85. return isWall(p);
  86. }
  87. bool Grid::isWall(coordinates p) {
  88. return std::find_if(walls.begin(), walls.end(),
  89. [p](cell cell) {
  90. return cell.row == p.second && cell.column == p.first;
  91. }) != walls.end();
  92. }
  93. void Grid::applyOverwrites(std::string& str, std::vector<Configuration>& configuration) {
  94. for (auto& config : configuration) {
  95. if (!config.overwrite_) {
  96. continue;
  97. }
  98. for (auto& index : config.indexes_) {
  99. size_t start_pos;
  100. std::string search;
  101. if (config.type_ == ConfigType::Formula) {
  102. search = "formula " + config.identifier_;
  103. } else if (config.type_ == ConfigType::Label) {
  104. search = "label " + config.identifier_;
  105. } else if (config.type_ == ConfigType::Module) {
  106. search = config.identifier_;
  107. }
  108. else if (config.type_ == ConfigType::Constant) {
  109. search = config.identifier_;
  110. }
  111. auto iter = boost::find_nth(str, search, index);
  112. start_pos = std::distance(str.begin(), iter.begin());
  113. size_t end_pos = str.find(';', start_pos) + 1;
  114. if (end_pos != std::string::npos && end_pos != 0) {
  115. std::string expression = config.expression_;
  116. str.replace(start_pos, end_pos - start_pos , expression);
  117. }
  118. }
  119. }
  120. }
  121. void Grid::printToPrism(std::ostream& os, std::vector<Configuration>& configuration) {
  122. cells northRestriction, eastRestriction, southRestriction, westRestriction;
  123. cells walkable = floor;
  124. walkable.insert(walkable.end(), goals.begin(), goals.end());
  125. walkable.insert(walkable.end(), boxes.begin(), boxes.end());
  126. walkable.insert(walkable.end(), lava.begin(), lava.end());
  127. walkable.insert(walkable.end(), keys.begin(), keys.end());
  128. walkable.insert(walkable.end(), balls.begin(), balls.end());
  129. for(auto const& c : walkable) {
  130. if(isWall(c.getNorth())) northRestriction.push_back(c);
  131. if(isWall(c.getEast())) eastRestriction.push_back(c);
  132. if(isWall(c.getSouth())) southRestriction.push_back(c);
  133. if(isWall(c.getWest())) westRestriction.push_back(c);
  134. }
  135. std::map<std::string, cells> wallRestrictions = {{"North", northRestriction}, {"East", eastRestriction}, {"South", southRestriction}, {"West", westRestriction}};
  136. std::map<std::string, cells> slipperyTiles = {{"North", slipperyNorth}, {"East", slipperyEast}, {"South", slipperySouth}, {"West", slipperyWest}, {"NorthWest", slipperyNorthWest}, {"NorthEast", slipperyNorthEast},{"SouthWest", slipperySouthWest},{"SouthEast", slipperySouthEast}};
  137. std::vector<AgentName> agentNames;
  138. std::transform(agentNameAndPositionMap.begin(),
  139. agentNameAndPositionMap.end(),
  140. std::back_inserter(agentNames),
  141. [](const std::map<AgentNameAndPosition::first_type,AgentNameAndPosition::second_type>::value_type &pair){return pair.first;});
  142. std::string agentName = agentNames.at(0);
  143. prism::PrismFormulaPrinter formulas(os, wallRestrictions, walls, lockedDoors, unlockedDoors, keys, slipperyTiles, lava, goals, agentNameAndPositionMap, faultyProbability > 0.0);
  144. prism::PrismModulesPrinter modules(os, modelType, maxBoundaries, lockedDoors, unlockedDoors, keys, slipperyTiles, agentNameAndPositionMap, configuration, probIntended, faultyProbability, !lava.empty(), !goals.empty());
  145. modules.printModelType(modelType);
  146. for(const auto &agentName : agentNames) {
  147. formulas.print(agentName);
  148. }
  149. if(agentNameAndPositionMap.size() > 1) formulas.printCollisionFormula(agentName);
  150. formulas.printInitStruct();
  151. modules.print();
  152. //if(!stateRewards.empty()) {
  153. // modules.printRewards(os, agentName, stateRewards, lava, goals, backgroundTiles);
  154. //}
  155. //if (!configuration.empty()) {
  156. // modules.printConfiguration(os, configuration);
  157. //}
  158. }