The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

243 lines
11 KiB

2 months ago
  1. #include "PrismFormulaPrinter.h"
  2. #include <map>
  3. #include <string>
  4. #include <algorithm>
  5. std::string oneOffToString(const int &offset) {
  6. return offset != 0 ? ( offset == 1 ? "+1" : "-1" ) : "";
  7. }
  8. std::string vectorToDisjunction(const std::vector<std::string> &formulae) {
  9. bool first = true;
  10. std::string disjunction = "";
  11. for(const auto &formula : formulae) {
  12. if(first) first = false;
  13. else disjunction += " | ";
  14. disjunction += formula;
  15. }
  16. return disjunction;
  17. }
  18. std::string cellToConjunction(const AgentName &agentName, const cell &c) {
  19. return "col" + agentName + "=" + std::to_string(c.column) + "&row" + agentName + "=" + std::to_string(c.row);
  20. }
  21. std::string cellToConjunctionWithOffset(const AgentName &agentName, const cell &c, const std::string &xOffset, const std::string &yOffset){
  22. return "col" + agentName + xOffset + "=" + std::to_string(c.column) + "&row" + agentName + yOffset + "=" + std::to_string(c.row);
  23. }
  24. std::string coordinatesToConjunction(const AgentName &agentName, const coordinates &c, const ViewDirection viewDirection) {
  25. return "col" + agentName + "=" + std::to_string(c.first) + "&row" + agentName + "=" + std::to_string(c.second) + "&view" + agentName + "=" + std::to_string(viewDirection);
  26. }
  27. std::string objectPositionToConjunction(const AgentName &agentName, const std::string &identifier, const std::pair<int, int> &relativePosition) {
  28. std::string xOffset = oneOffToString(relativePosition.first);
  29. std::string yOffset = oneOffToString(relativePosition.second);
  30. return "col" + agentName + xOffset + "=col" + identifier + "&row" + agentName + yOffset + "=row" + identifier;
  31. }
  32. std::string objectPositionToConjunction(const AgentName &agentName, const std::string &identifier, const std::pair<int, int> &relativePosition, const ViewDirection viewDirection) {
  33. std::string xOffset = oneOffToString(relativePosition.first);
  34. std::string yOffset = oneOffToString(relativePosition.second);
  35. return "col" + agentName + xOffset + "=col" + identifier + "&row" + agentName + yOffset + "=row" + identifier + "&view" + agentName + "=" + std::to_string(viewDirection);
  36. }
  37. std::map<ViewDirection, coordinates> getAdjacentCells(const cell &c) {
  38. return {{1, c.getNorth()}, {2, c.getEast()}, {3, c.getSouth()}, {0, c.getWest()}};
  39. }
  40. std::map<ViewDirection, std::pair<int, int>> getRelativeAdjacentCells() {
  41. return { {1, {0,+1}}, {2, {-1,0}}, {3, {0,-1}}, {0, {+1,0}} };
  42. }
  43. std::map<std::string, std::pair<int, int>> getRelativeSurroundingCells() {
  44. return { {"NorthWest", {-1,-1}}, {"North", { 0,-1}}, {"NorthEast", {+1,-1}},
  45. {"West", {-1, 0}}, {"East", {+1, 0}},
  46. {"SouthWest", {-1,+1}}, {"South", { 0,+1}}, {"SouthEast", {+1,+1}} };
  47. }
  48. namespace prism {
  49. PrismFormulaPrinter::PrismFormulaPrinter(std::ostream &os, const std::map<std::string, cells> &restrictions, const cells &walls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map<std::string, cells> &slipperyTiles, const cells &lava, const cells &goals, const AgentNameAndPositionMap &agentNameAndPositionMap, const bool faulty)
  50. : os(os), restrictions(restrictions), walls(walls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), lava(lava), goals(goals), agentNameAndPositionMap(agentNameAndPositionMap), faulty(faulty)
  51. { }
  52. void PrismFormulaPrinter::print(const AgentName &agentName) {
  53. conditionalMovementRestrictions.clear();
  54. for(const auto& [direction, cells] : restrictions) {
  55. printRestrictionFormula(agentName, direction, cells);
  56. }
  57. if(slipperyBehaviour()) {
  58. for(const auto& [direction, cells] : slipperyTiles) {
  59. printIsOnFormula(agentName, "Slippery", cells, direction);
  60. }
  61. std::vector<std::string> allSlipperyDirections;
  62. for(const auto &[slipperyType, _] : slipperyTiles) {
  63. allSlipperyDirections.push_back(agentName + "IsOnSlippery" + slipperyType);
  64. }
  65. os << buildFormula(agentName + "IsOnSlippery", vectorToDisjunction(allSlipperyDirections));
  66. for(const auto& [direction, relativePosition] : getRelativeSurroundingCells()) {
  67. printSlipRestrictionFormula(agentName, direction);
  68. }
  69. } else {
  70. os << buildFormula(agentName + "IsOnSlippery", "false");
  71. }
  72. if(!lava.empty()) printIsOnFormula(agentName, "Lava", lava);
  73. if(!goals.empty()) printIsOnFormula(agentName, "Goal", goals);
  74. for(const auto& key : keys) {
  75. std::string identifier = capitalize(key.getColor()) + key.getType();
  76. printRelativeIsInFrontOfFormulaWithCondition(agentName, identifier, "!" + identifier + "PickedUp");
  77. portableObjects.push_back(agentName + "Carrying" + identifier);
  78. }
  79. for(const auto& door : unlockedDoors) {
  80. std::string identifier = capitalize(door.getColor()) + door.getType();
  81. printRestrictionFormulaWithCondition(agentName, identifier, getAdjacentCells(door), "!" + identifier + "Open");
  82. printIsNextToFormula(agentName, identifier, getAdjacentCells(door));
  83. }
  84. for(const auto& door : lockedDoors) {
  85. std::string identifier = capitalize(door.getColor()) + door.getType();
  86. printRestrictionFormulaWithCondition(agentName, identifier, getAdjacentCells(door), "!" + identifier + "Open");
  87. printIsNextToFormula(agentName, identifier, getAdjacentCells(door));
  88. }
  89. if(conditionalMovementRestrictions.size() > 0) {
  90. os << buildFormula(agentName + "CannotMoveConditionally", vectorToDisjunction(conditionalMovementRestrictions));
  91. }
  92. if(portableObjects.size() > 0) {
  93. os << buildFormula(agentName + "IsCarrying", vectorToDisjunction(portableObjects));
  94. }
  95. }
  96. void PrismFormulaPrinter::printRestrictionFormula(const AgentName &agentName, const std::string &direction, const cells &grid_cells) {
  97. os << buildFormula(agentName + "CannotMove" + direction + "Wall", buildDisjunction(agentName, grid_cells));
  98. }
  99. void PrismFormulaPrinter::printIsOnFormula(const AgentName &agentName, const std::string &type, const cells &grid_cells, const std::string &direction) {
  100. os << buildFormula(agentName + "IsOn" + type + direction, buildDisjunction(agentName, grid_cells));
  101. }
  102. void PrismFormulaPrinter::printIsNextToFormula(const AgentName &agentName, const std::string &type, const std::map<ViewDirection, coordinates> &coordinates) {
  103. os << buildFormula(agentName + "IsNextTo" + type, buildDisjunction(agentName, coordinates));
  104. }
  105. void PrismFormulaPrinter::printRestrictionFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::map<ViewDirection, coordinates> &coordinates, const std::string &condition) {
  106. os << buildFormula(agentName + "CannotMove" + reason, "(" + buildDisjunction(agentName, coordinates) + ") & " + condition);
  107. conditionalMovementRestrictions.push_back(agentName + "CannotMove" + reason);
  108. }
  109. void PrismFormulaPrinter::printRelativeIsInFrontOfFormulaWithCondition(const AgentName &agentName, const std::string &reason, const std::string &condition) {
  110. os << buildFormula(agentName + "IsInFrontOf" + reason, "(" + buildDisjunction(agentName, reason) + ") & " + condition);
  111. }
  112. void PrismFormulaPrinter::printSlipRestrictionFormula(const AgentName &agentName, const std::string &direction) {
  113. std::pair<int, int> slipCell = getRelativeSurroundingCells().at(direction);
  114. bool semicolon = anyPortableObject() ? false : true;
  115. os << buildFormula(agentName + "CannotSlip" + direction, buildDisjunction(agentName, walls, slipCell), semicolon);
  116. if(!semicolon) os << ";\n";
  117. }
  118. void PrismFormulaPrinter::printCollisionFormula(const AgentName &agentName) {
  119. if(!agentNameAndPositionMap.empty()) {
  120. os << "formula collision = ";
  121. bool first = true;
  122. for(auto const [name, coordinates] : agentNameAndPositionMap) {
  123. if(name == agentName) continue;
  124. if(first) first = false;
  125. else os << " | ";
  126. os << "(col"+agentName+"=col"+name+"&row"+agentName+"=row"+name+")";
  127. }
  128. os << ";\n";
  129. printCollisionLabel();
  130. }
  131. }
  132. void PrismFormulaPrinter::printCollisionLabel() {
  133. if(!agentNameAndPositionMap.empty()) {
  134. os << "label \"collision\" = collision;\n";
  135. }
  136. }
  137. void PrismFormulaPrinter::printInitStruct() {
  138. os << "init\n true\n";
  139. //bool first = true;
  140. //for(auto const [a, coordinates] : agentNameAndPositionMap) {
  141. // if(first) first = false;
  142. // else os << " & ";
  143. // os << "(col"+a+"="+std::to_string(coordinates.first)+"&row"+a+"="+std::to_string(coordinates.second)+" & ";
  144. // os << "(view"+a+"=0|view"+a+"=1|view"+a+"=2|view"+a+"=3) ";
  145. // if(faulty) os << " & previousAction"+a+"="+std::to_string(NOFAULT);
  146. // os << ")";
  147. //}
  148. //for(auto const key : keys) {
  149. // std::string identifier = capitalize(key.getColor()) + key.getType();
  150. // os << " & (col"+identifier+"="+std::to_string(key.column)+"&row"+identifier+"="+std::to_string(key.row)+"&"+identifier+"PickedUp=false) ";
  151. //}
  152. os << "endinit\n\n";
  153. }
  154. std::string PrismFormulaPrinter::buildFormula(const std::string &formulaName, const std::string &formula, const bool semicolon) {
  155. return "formula " + formulaName + " = " + formula + (semicolon ? ";\n": "");
  156. }
  157. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::map<ViewDirection, coordinates> &cells) {
  158. if(cells.size() == 0) return "false";
  159. bool first = true;
  160. std::string disjunction = "";
  161. for(const auto [viewDirection, coordinates] : cells) {
  162. if(first) first = false;
  163. else disjunction += " | ";
  164. disjunction += "(" + coordinatesToConjunction(agentName, coordinates, viewDirection) + ")";
  165. }
  166. return disjunction;
  167. }
  168. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells) {
  169. if(cells.size() == 0) return "false";
  170. bool first = true;
  171. std::string disjunction = "";
  172. for(auto const cell : cells) {
  173. if(first) first = false;
  174. else disjunction += " | ";
  175. disjunction += "(" + cellToConjunction(agentName, cell) + ")";
  176. }
  177. return disjunction;
  178. }
  179. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const std::string &reason) {
  180. std::string disjunction = "";
  181. bool first = true;
  182. for(auto const [viewDirection, relativePosition] : getRelativeAdjacentCells()) {
  183. if(first) first = false;
  184. else disjunction += " | ";
  185. disjunction += "(" + objectPositionToConjunction(agentName, reason, relativePosition, viewDirection) + ")";
  186. }
  187. return disjunction;
  188. }
  189. std::string PrismFormulaPrinter::buildDisjunction(const AgentName &agentName, const cells &cells, const std::pair<int, int> &offset) {
  190. std::string disjunction = "";
  191. bool first = true;
  192. std::string xOffset = oneOffToString(offset.first);
  193. std::string yOffset = oneOffToString(offset.second);
  194. for(auto const cell : cells) {
  195. if(first) first = false;
  196. else disjunction += " | ";
  197. disjunction += "(" + cellToConjunctionWithOffset(agentName, cell, xOffset, yOffset) + ")";
  198. }
  199. return disjunction;
  200. }
  201. bool PrismFormulaPrinter::slipperyBehaviour() const {
  202. return !slipperyTiles.at("North").empty() || !slipperyTiles.at("East").empty() || !slipperyTiles.at("South").empty() || !slipperyTiles.at("West").empty();
  203. }
  204. bool PrismFormulaPrinter::anyPortableObject() const {
  205. return !keys.empty();
  206. }
  207. }