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.

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