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.

666 lines
46 KiB

11 months ago
11 months ago
11 months ago
11 months ago
  1. #include "PrismModulesPrinter.h"
  2. #include <map>
  3. #include <string>
  4. std::string northUpdate(const AgentName &a) { return "(row"+a+"'=row"+a+"-1)"; }
  5. std::string southUpdate(const AgentName &a) { return "(row"+a+"'=row"+a+"+1)"; }
  6. std::string eastUpdate(const AgentName &a) { return "(col"+a+"'=col"+a+"+1)"; }
  7. std::string westUpdate(const AgentName &a) { return "(col"+a+"'=col"+a+"-1)"; }
  8. namespace prism {
  9. PrismModulesPrinter::PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &boxes, const cells &balls, const cells &lockedDoors, const cells &unlockedDoors, const cells &keys, const std::map<std::string, cells> &slipperyTiles, const AgentNameAndPositionMap &agentNameAndPositionMap, std::vector<Configuration> config, const float probIntended, const float faultyProbability, const bool anyLava, const bool anyGoals)
  10. : os(os), modelType(modelType), maxBoundaries(maxBoundaries), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), slipperyTiles(slipperyTiles), agentNameAndPositionMap(agentNameAndPositionMap), configuration(config), probIntended(probIntended), faultyProbability(faultyProbability), anyLava(anyLava), anyGoals(anyGoals) {
  11. numberOfPlayer = agentNameAndPositionMap.size();
  12. size_t index = 0;
  13. for(auto begin = agentNameAndPositionMap.begin(); begin != agentNameAndPositionMap.end(); begin++, index++) {
  14. agentIndexMap[begin->first] = index;
  15. }
  16. }
  17. void PrismModulesPrinter::printModelType(const ModelType &modelType) {
  18. switch(modelType) {
  19. case(ModelType::MDP):
  20. os << "mdp";
  21. break;
  22. case(ModelType::SMG):
  23. os << "smg";
  24. break;
  25. }
  26. os << "\n\n";
  27. }
  28. std::ostream& PrismModulesPrinter::print() {
  29. for(const auto [agentName, initialPosition] : agentNameAndPositionMap) {
  30. agentNameActionMap[agentName] = {};
  31. }
  32. for(const auto &key : keys) {
  33. printPortableObjectModule(key);
  34. }
  35. for(const auto &ball : balls) {
  36. printPortableObjectModule(ball);
  37. }
  38. for(const auto &box : boxes) {
  39. printPortableObjectModule(box);
  40. }
  41. for(const auto &door : unlockedDoors) {
  42. printDoorModule(door, true);
  43. }
  44. for(const auto &door : lockedDoors) {
  45. printDoorModule(door, false);
  46. }
  47. for(const auto [agentName, initialPosition] : agentNameAndPositionMap) {
  48. printRobotModule(agentName, initialPosition);
  49. }
  50. if(agentNameAndPositionMap.size() > 1) {
  51. printMoveModule();
  52. }
  53. if(faultyBehaviour()) {
  54. for(const auto [agentName, initialPosition] : agentNameAndPositionMap) {
  55. printFaultyMovementModule(agentName);
  56. }
  57. }
  58. if(agentNameAndPositionMap.size() > 1) {
  59. for(const auto [agentName, index] : agentIndexMap) {
  60. printPlayerStruct(agentName);
  61. }
  62. }
  63. if (!configuration.empty()) {
  64. printConfiguration(configuration);
  65. }
  66. return os;
  67. }
  68. void PrismModulesPrinter::printConfiguration(const std::vector<Configuration>& configurations) {
  69. for (auto& configuration : configurations) {
  70. if (configuration.overwrite_ || configuration.type_ == ConfigType::Module) {
  71. continue;
  72. }
  73. os << configuration.expression_ << std::endl;
  74. }
  75. }
  76. void PrismModulesPrinter::printConstants(const std::vector<std::string> &constants) {
  77. for (auto& constant : constants) {
  78. os << constant << std::endl;
  79. }
  80. }
  81. void PrismModulesPrinter::printPortableObjectModule(const cell &object) {
  82. std::string identifier = capitalize(object.getColor()) + object.getType();
  83. os << "\nmodule " << identifier << std::endl;
  84. os << " col" << identifier << " : [-1.." << maxBoundaries.first << "];\n";
  85. os << " row" << identifier << " : [-1.." << maxBoundaries.second << "];\n";
  86. os << " " << identifier << "PickedUp : bool;\n";
  87. os << "\n";
  88. for(const auto [name, position] : agentNameAndPositionMap) {
  89. printPortableObjectActions(name, identifier);
  90. }
  91. os << "endmodule\n\n";
  92. }
  93. void PrismModulesPrinter::printPortableObjectActions(const std::string &agentName, const std::string &identifier) {
  94. std::string actionName = "[" + agentName + "_pickup_" + identifier + "]";
  95. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  96. os << " " << actionName << " true -> (col" << identifier << "'=-1) & (row" << identifier << "'=-1) & (" << identifier << "PickedUp'=true);\n";
  97. actionName = "[" + agentName + "_drop_" + identifier + "_north]";
  98. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  99. os << " " << actionName << " " << " true -> (col" << identifier << "'=col" << agentName << ") & (row" << identifier << "'=row" << agentName << "-1) & (" << identifier << "PickedUp'=false);\n";
  100. actionName = "[" + agentName + "_drop_" + identifier + "_west]";
  101. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  102. os << " " << actionName << " " << " true -> (col" << identifier << "'=col" << agentName << "-1) & (row" << identifier << "'=row" << agentName << ") & (" << identifier << "PickedUp'=false);\n";
  103. actionName = "[" + agentName + "_drop_" + identifier + "_south]";
  104. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  105. os << " " << actionName << " " << " true -> (col" << identifier << "'=col" << agentName << ") & (row" << identifier << "'=row" << agentName << "+1) & (" << identifier << "PickedUp'=false);\n";
  106. actionName = "[" + agentName + "_drop_" + identifier + "_east]";
  107. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  108. os << " " << actionName << " " << " true -> (col" << identifier << "'=col" << agentName << "+1) & (row" << identifier << "'=row" << agentName << ") & (" << identifier << "PickedUp'=false);\n";
  109. }
  110. void PrismModulesPrinter::printDoorModule(const cell &door, const bool &opened) {
  111. std::string identifier = capitalize(door.getColor()) + door.getType();
  112. os << "\nmodule " << identifier << std::endl;
  113. os << " " << identifier << "Open : bool;\n";
  114. os << "\n";
  115. if(opened) {
  116. for(const auto [name, position] : agentNameAndPositionMap) {
  117. printUnlockedDoorActions(name, identifier);
  118. }
  119. } else {
  120. for(const auto [name, position] : agentNameAndPositionMap) {
  121. printLockedDoorActions(name, identifier);
  122. }
  123. }
  124. os << "endmodule\n\n";
  125. }
  126. void PrismModulesPrinter::printLockedDoorActions(const std::string &agentName, const std::string &identifier) {
  127. std::string actionName = "[" + agentName + "_unlock_" + identifier + "]";
  128. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  129. os << " " << actionName << " !" << identifier << "Open -> (" << identifier << "Open'=true);\n";
  130. actionName = "[" + agentName + "_close_" + identifier + "]";
  131. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  132. os << " " << actionName << " " << identifier << "Open -> (" << identifier << "Open'=false);\n";
  133. }
  134. void PrismModulesPrinter::printUnlockedDoorActions(const std::string &agentName, const std::string &identifier) {
  135. std::string actionName = "[" + agentName + "_open_" + identifier + "]";
  136. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  137. os << " !" << identifier << "Open -> (" << identifier << "Open'=true);\n";
  138. actionName = "[" + agentName + "_close_" + identifier + "]";
  139. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  140. os << " " << agentName << " " << identifier << "Open -> (" << identifier << "Open'=false);\n";
  141. }
  142. void PrismModulesPrinter::printRobotModule(const AgentName &agentName, const coordinates &initialPosition) {
  143. os << "\nmodule " << agentName << std::endl;
  144. os << " col" << agentName << " : [1.." << maxBoundaries.first << "];\n";
  145. os << " row" << agentName << " : [1.." << maxBoundaries.second << "];\n";
  146. os << " view" << agentName << " : [0..3];\n";
  147. printTurnActionsForRobot(agentName);
  148. printMovementActionsForRobot(agentName);
  149. if(slipperyBehaviour()) printSlipperyMovementActionsForRobot(agentName);
  150. for(const auto &door : unlockedDoors) {
  151. std::string identifier = capitalize(door.getColor()) + door.getType();
  152. printUnlockedDoorActionsForRobot(agentName, identifier);
  153. }
  154. for(const auto &door : lockedDoors) {
  155. std::string identifier = capitalize(door.getColor()) + door.getType();
  156. std::string key = capitalize(door.getColor()) + "Key";
  157. printLockedDoorActionsForRobot(agentName, identifier, key);
  158. }
  159. for(const auto &key : keys) {
  160. std::string identifier = capitalize(key.getColor()) + key.getType();
  161. os << " " << agentName << "Carrying" << identifier << " : bool;\n";
  162. printPortableObjectActionsForRobot(agentName, identifier);
  163. }
  164. for(const auto &ball : balls) {
  165. std::string identifier = capitalize(ball.getColor()) + ball.getType();
  166. os << " " << agentName << "Carrying" << identifier << " : bool;\n";
  167. printPortableObjectActionsForRobot(agentName, identifier);
  168. }
  169. for(const auto &box : boxes) {
  170. std::string identifier = capitalize(box.getColor()) + box.getType();
  171. os << " " << agentName << "Carrying" << identifier << " : bool;\n";
  172. printPortableObjectActionsForRobot(agentName, identifier);
  173. }
  174. printNonMovementActionsForRobot(agentName);
  175. os << "\n" << actionStream.str();
  176. actionStream.str(std::string());
  177. if(agentNameAndPositionMap.size() > 1 && agentName == "Agent") printDoneActions(agentName);
  178. os << "endmodule\n\n";
  179. }
  180. void PrismModulesPrinter::printPortableObjectActionsForRobot(const std::string &a, const std::string &i) {
  181. actionStream << " [" << a << "_pickup_" << i << "] " << " !" << a << "IsCarrying & " << a << "CannotMove" << i << " -> (" << a << "Carrying" << i << "'=true);\n";
  182. actionStream << " [" << a << "_drop_" << i << "_north]\t" << a << "Carrying" << i << " & view" << a << "=3 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveNorthWall -> (" << a << "Carrying" << i << "'=false);\n";
  183. actionStream << " [" << a << "_drop_" << i << "_west] \t" << a << "Carrying" << i << " & view" << a << "=2 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveWestWall -> (" << a << "Carrying" << i << "'=false);\n";
  184. actionStream << " [" << a << "_drop_" << i << "_south]\t" << a << "Carrying" << i << " & view" << a << "=1 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveSouthWall -> (" << a << "Carrying" << i << "'=false);\n";
  185. actionStream << " [" << a << "_drop_" << i << "_east] \t" << a << "Carrying" << i << " & view" << a << "=0 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveEastWall -> (" << a << "Carrying" << i << "'=false);\n";
  186. actionStream << "\n";
  187. }
  188. void PrismModulesPrinter::printUnlockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier) {
  189. actionStream << " [" << agentName << "_open_" << identifier << "] " << agentName << "CannotMove" << identifier << " -> true;\n";
  190. actionStream << " [" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " -> true;\n";
  191. actionStream << "\n";
  192. }
  193. void PrismModulesPrinter::printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key) {
  194. actionStream << " [" << agentName << "_unlock_" << identifier << "] " << agentName << "CannotMove" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n";
  195. actionStream << " [" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n";
  196. actionStream << "\n";
  197. }
  198. void PrismModulesPrinter::printTurnActionsForRobot(const AgentName &a) {
  199. actionStream << printTurnGuard(a, "right", RIGHT, "true") << printTurnUpdate(a, {1.0, "(view"+a+"'=mod(view"+a+"+1,4))"}, RIGHT);
  200. actionStream << printTurnGuard(a, "left", LEFT, "view"+a+">0") << printTurnUpdate(a, {1.0, "(view"+a+"'=view"+a+"-1)"}, LEFT);
  201. actionStream << printTurnGuard(a, "left", LEFT, "view"+a+"=0") << printTurnUpdate(a, {1.0, "(view"+a+"'=3)"}, LEFT);
  202. }
  203. void PrismModulesPrinter::printMovementActionsForRobot(const AgentName &a) {
  204. actionStream << printMovementGuard(a, "North", 3) << printMovementUpdate(a, {1.0, northUpdate(a)});
  205. actionStream << printMovementGuard(a, "East", 0) << printMovementUpdate(a, {1.0, eastUpdate(a)});
  206. actionStream << printMovementGuard(a, "South", 1) << printMovementUpdate(a, {1.0, southUpdate(a)});
  207. actionStream << printMovementGuard(a, "West", 2) << printMovementUpdate(a, {1.0, westUpdate(a)});
  208. if(faultyBehaviour()) {
  209. std::string actionName = "[" + a + "_stuck]";
  210. agentNameActionMap.at(a).insert({FORWARD, actionName});
  211. actionStream << " " << actionName << " " << "previousAction" << a << "=" << std::to_string(FORWARD);
  212. actionStream << " & ((view" << a << "=0 & " << a << "CannotMoveEastWall) |";
  213. actionStream << " (view" << a << "=1 & " << a << "CannotMoveSouthWall) |";
  214. actionStream << " (view" << a << "=2 & " << a << "CannotMoveWestWall) |";
  215. actionStream << " (view" << a << "=3 & " << a << "CannotMoveNorthWall) ) -> true;\n";
  216. }
  217. }
  218. std::string PrismModulesPrinter::printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) {
  219. std::string actionName = "[" + a + "_move_" + direction + "]";
  220. agentNameActionMap.at(a).insert({FORWARD, actionName});
  221. std::string guard = " " + actionName + " " + viewVariable(a, viewDirection);
  222. if(slipperyBehaviour()) guard += " & !" + a + "IsOnSlippery";
  223. if(anyLava) guard += " & !" + a + "IsOnLava";
  224. if(anyGoals) guard += " & !" + a + "IsOnGoal";
  225. guard += " & !" + a + "CannotMove" + direction + "Wall";
  226. if(anyPortableObject()) guard += " & !" + a + "CannotMoveConditionally";
  227. guard += " -> ";
  228. return guard;
  229. }
  230. std::string PrismModulesPrinter::printMovementUpdate(const AgentName &a, const update &u) const {
  231. return updateToString(u) + ";\n";
  232. }
  233. std::string PrismModulesPrinter::printTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::string &cond) {
  234. std::string actionName = "[" + a + "_turn_" + direction + "]";
  235. agentNameActionMap.at(a).insert({actionId, actionName});
  236. std::string guard = " " + actionName;
  237. if(slipperyBehaviour()) guard += " !" + a + "IsOnSlippery & ";
  238. if(anyLava) guard += " !" + a + "IsOnLava &";
  239. guard += cond + " -> ";
  240. return guard;
  241. }
  242. std::string PrismModulesPrinter::printTurnUpdate(const AgentName &a, const update &u, const ActionId &actionId) const {
  243. return updateToString(u) + ";\n";
  244. }
  245. void PrismModulesPrinter::printNonMovementActionsForRobot(const AgentName &agentName) {
  246. for(auto const [actionId, action] : nonMovementActions) {
  247. std::string actionName = "[" + agentName + "_" + action + "]";
  248. agentNameActionMap.at(agentName).insert({actionId, actionName});
  249. actionStream << " " << actionName << " true -> true;\n";
  250. }
  251. }
  252. void PrismModulesPrinter::printSlipperyMovementActionsForRobot(const AgentName &a) {
  253. if(!slipperyTiles.at("North").empty()) {
  254. printSlipperyMovementActionsForNorth(a);
  255. printSlipperyTurnActionsForNorth(a);
  256. }
  257. if(!slipperyTiles.at("East").empty()) {
  258. printSlipperyMovementActionsForEast(a) ;
  259. printSlipperyTurnActionsForEast(a);
  260. }
  261. if(!slipperyTiles.at("South").empty()) {
  262. printSlipperyMovementActionsForSouth(a);
  263. printSlipperyTurnActionsForSouth(a);
  264. }
  265. if(!slipperyTiles.at("West").empty()) {
  266. printSlipperyMovementActionsForWest(a) ;
  267. printSlipperyTurnActionsForWest(a);
  268. }
  269. }
  270. void PrismModulesPrinter::printSlipperyMovementActionsForNorth(const AgentName &a) {
  271. actionStream << printSlipperyMovementGuard(a, "North", 3, {"!"+a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, northUpdate(a)}, {(1 - probIntended) * 1/2, northUpdate(a)+"&"+eastUpdate(a)}, {(1 - probIntended) * 1/2, northUpdate(a)+"&"+westUpdate(a)} }) << ";\n";
  272. actionStream << printSlipperyMovementGuard(a, "North", 3, { a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1/2, northUpdate(a)+"&"+eastUpdate(a)}, {1/2, northUpdate(a)+"&"+westUpdate(a)} }) << ";\n";
  273. actionStream << printSlipperyMovementGuard(a, "North", 3, {"!"+a+"CannotSlipNorth", a+"CannotSlipNorthEast", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, northUpdate(a)}, {(1 - probIntended), northUpdate(a)+"&"+westUpdate(a)} }) << ";\n";
  274. actionStream << printSlipperyMovementGuard(a, "North", 3, {"!"+a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, northUpdate(a)}, {(1 - probIntended), northUpdate(a)+"&"+eastUpdate(a)} }) << ";\n";
  275. actionStream << printSlipperyMovementGuard(a, "North", 3, { a+"CannotSlipNorth", a+"CannotSlipNorthEast", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)+"&"+westUpdate(a) } }) << ";\n";
  276. actionStream << printSlipperyMovementGuard(a, "North", 3, {"!"+a+"CannotSlipNorth", a+"CannotSlipNorthEast", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)} }) << ";\n";
  277. actionStream << printSlipperyMovementGuard(a, "North", 3, { a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)+"&"+eastUpdate(a)} }) << ";\n";
  278. actionStream << printSlipperyMovementGuard(a, "North", 3, { a+"CannotSlipNorth", a+"CannotSlipNorthEast", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", {}) << ";\n";
  279. actionStream << printSlipperyMovementGuard(a, "North", 2, {"!"+a+"CannotSlipWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, westUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  280. actionStream << printSlipperyMovementGuard(a, "North", 2, { a+"CannotSlipWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  281. actionStream << printSlipperyMovementGuard(a, "North", 2, {"!"+a+"CannotSlipWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, westUpdate(a) } }) << ";\n";
  282. actionStream << printSlipperyMovementGuard(a, "North", 2, { a+"CannotSlipWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", {}) << ";\n";
  283. actionStream << printSlipperyMovementGuard(a, "North", 0, {"!"+a+"CannotSlipEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, eastUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  284. actionStream << printSlipperyMovementGuard(a, "North", 0, { a+"CannotSlipEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", { {1, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  285. actionStream << printSlipperyMovementGuard(a, "North", 0, {"!"+a+"CannotSlipEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", { {1, eastUpdate(a) } }) << ";\n";
  286. actionStream << printSlipperyMovementGuard(a, "North", 0, { a+"CannotSlipEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", {}) << ";\n";
  287. actionStream << printSlipperyMovementGuard(a, "North", 1, {"!"+a+"CannotSlipSouth"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, southUpdate(a) }, {1 - probIntended, "true"} }) << ";\n";
  288. actionStream << printSlipperyMovementGuard(a, "North", 1, { a+"CannotSlipSouth"}) << printSlipperyMovementUpdate(a, "North", { {1, "true"} }) << ";\n";
  289. }
  290. void PrismModulesPrinter::printSlipperyMovementActionsForEast(const AgentName &a) {
  291. actionStream << printSlipperyMovementGuard(a, "East", 0, {"!"+a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a)}, {(1 - probIntended) * 1/2, eastUpdate(a)+"&"+southUpdate(a)}, {(1 - probIntended) * 1/2, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  292. actionStream << printSlipperyMovementGuard(a, "East", 0, { a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1/2, eastUpdate(a)+"&"+southUpdate(a)}, {1/2, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  293. actionStream << printSlipperyMovementGuard(a, "East", 0, {"!"+a+"CannotSlipEast", a+"CannotSlipSouthEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a)}, {(1 - probIntended), eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  294. actionStream << printSlipperyMovementGuard(a, "East", 0, {"!"+a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a)}, {(1 - probIntended), eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  295. actionStream << printSlipperyMovementGuard(a, "East", 0, { a+"CannotSlipEast", a+"CannotSlipSouthEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+northUpdate(a) } }) << ";\n";
  296. actionStream << printSlipperyMovementGuard(a, "East", 0, {"!"+a+"CannotSlipEast", a+"CannotSlipSouthEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)} }) << ";\n";
  297. actionStream << printSlipperyMovementGuard(a, "East", 0, { a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  298. actionStream << printSlipperyMovementGuard(a, "East", 0, { a+"CannotSlipEast", a+"CannotSlipSouthEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", {}) << ";\n";
  299. actionStream << printSlipperyMovementGuard(a, "East", 3, {"!"+a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, northUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  300. actionStream << printSlipperyMovementGuard(a, "East", 3, { a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  301. actionStream << printSlipperyMovementGuard(a, "East", 3, {"!"+a+"CannotSlipNorth", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, northUpdate(a) } }) << ";\n";
  302. actionStream << printSlipperyMovementGuard(a, "East", 3, { a+"CannotSlipNorth", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", {}) << ";\n";
  303. actionStream << printSlipperyMovementGuard(a, "East", 1, {"!"+a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, southUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  304. actionStream << printSlipperyMovementGuard(a, "East", 1, { a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  305. actionStream << printSlipperyMovementGuard(a, "East", 1, {"!"+a+"CannotSlipSouth", a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, southUpdate(a) } }) << ";\n";
  306. actionStream << printSlipperyMovementGuard(a, "East", 1, { a+"CannotSlipSouth", a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", {}) << ";\n";
  307. actionStream << printSlipperyMovementGuard(a, "East", 2, {"!"+a+"CannotSlipEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a) }, {1 - probIntended, "true"} }) << ";\n";
  308. actionStream << printSlipperyMovementGuard(a, "East", 2, { a+"CannotSlipEast"}) << printSlipperyMovementUpdate(a, "East", { {1, "true"} }) << ";\n";
  309. }
  310. void PrismModulesPrinter::printSlipperyMovementActionsForSouth(const AgentName &a) {
  311. actionStream << printSlipperyMovementGuard(a, "South", 1, {"!"+a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, southUpdate(a)}, {(1 - probIntended) * 1/2, southUpdate(a)+"&"+eastUpdate(a)}, {(1 - probIntended) * 1/2, southUpdate(a)+"&"+westUpdate(a)} }) << ";\n";
  312. actionStream << printSlipperyMovementGuard(a, "South", 1, { a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1/2, southUpdate(a)+"&"+eastUpdate(a)}, {1/2, southUpdate(a)+"&"+westUpdate(a)} }) << ";\n";
  313. actionStream << printSlipperyMovementGuard(a, "South", 1, {"!"+a+"CannotSlipSouth", a+"CannotSlipSouthEast", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, southUpdate(a)}, {(1 - probIntended), southUpdate(a)+"&"+westUpdate(a)} }) << ";\n";
  314. actionStream << printSlipperyMovementGuard(a, "South", 1, {"!"+a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, southUpdate(a)}, {(1 - probIntended), southUpdate(a)+"&"+eastUpdate(a)} }) << ";\n";
  315. actionStream << printSlipperyMovementGuard(a, "South", 1, { a+"CannotSlipSouth", a+"CannotSlipSouthEast", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)+"&"+westUpdate(a) } }) << ";\n";
  316. actionStream << printSlipperyMovementGuard(a, "South", 1, {"!"+a+"CannotSlipSouth", a+"CannotSlipSouthEast", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)} }) << ";\n";
  317. actionStream << printSlipperyMovementGuard(a, "South", 1, { a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)+"&"+eastUpdate(a)} }) << ";\n";
  318. actionStream << printSlipperyMovementGuard(a, "South", 1, { a+"CannotSlipSouth", a+"CannotSlipSouthEast", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", {}) << ";\n";
  319. actionStream << printSlipperyMovementGuard(a, "South", 2, {"!"+a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, westUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  320. actionStream << printSlipperyMovementGuard(a, "South", 2, { a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  321. actionStream << printSlipperyMovementGuard(a, "South", 2, {"!"+a+"CannotSlipWest", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, westUpdate(a) } }) << ";\n";
  322. actionStream << printSlipperyMovementGuard(a, "South", 2, { a+"CannotSlipWest", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", {}) << ";\n";
  323. actionStream << printSlipperyMovementGuard(a, "South", 0, {"!"+a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, eastUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  324. actionStream << printSlipperyMovementGuard(a, "South", 0, { a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", { {1, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  325. actionStream << printSlipperyMovementGuard(a, "South", 0, {"!"+a+"CannotSlipEast", a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", { {1, eastUpdate(a) } }) << ";\n";
  326. actionStream << printSlipperyMovementGuard(a, "South", 0, { a+"CannotSlipEast", a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", {}) << ";\n";
  327. actionStream << printSlipperyMovementGuard(a, "South", 3, {"!"+a+"CannotSlipSouth"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, northUpdate(a) }, {1 - probIntended, "true"} }) << ";\n";
  328. actionStream << printSlipperyMovementGuard(a, "South", 3, { a+"CannotSlipSouth"}) << printSlipperyMovementUpdate(a, "South", { {1, "true"} }) << ";\n";
  329. }
  330. void PrismModulesPrinter::printSlipperyMovementActionsForWest(const AgentName &a) {
  331. actionStream << printSlipperyMovementGuard(a, "West", 2, {"!"+a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a)}, {(1 - probIntended) * 1/2, westUpdate(a)+"&"+southUpdate(a)}, {(1 - probIntended) * 1/2, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  332. actionStream << printSlipperyMovementGuard(a, "West", 2, { a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1/2, westUpdate(a)+"&"+southUpdate(a)}, {1/2, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  333. actionStream << printSlipperyMovementGuard(a, "West", 2, {"!"+a+"CannotSlipWest", a+"CannotSlipSouthWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a)}, {(1 - probIntended), westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  334. actionStream << printSlipperyMovementGuard(a, "West", 2, {"!"+a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a)}, {(1 - probIntended), westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  335. actionStream << printSlipperyMovementGuard(a, "West", 2, { a+"CannotSlipWest", a+"CannotSlipSouthWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+northUpdate(a) } }) << ";\n";
  336. actionStream << printSlipperyMovementGuard(a, "West", 2, {"!"+a+"CannotSlipWest", a+"CannotSlipSouthWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)} }) << ";\n";
  337. actionStream << printSlipperyMovementGuard(a, "West", 2, { a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  338. actionStream << printSlipperyMovementGuard(a, "West", 2, { a+"CannotSlipWest", a+"CannotSlipSouthWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", {}) << ";\n";
  339. actionStream << printSlipperyMovementGuard(a, "West", 3, {"!"+a+"CannotSlipNorth", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, northUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  340. actionStream << printSlipperyMovementGuard(a, "West", 3, { a+"CannotSlipNorth", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  341. actionStream << printSlipperyMovementGuard(a, "West", 3, {"!"+a+"CannotSlipNorth", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, northUpdate(a) } }) << ";\n";
  342. actionStream << printSlipperyMovementGuard(a, "West", 3, { a+"CannotSlipNorth", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", {}) << ";\n";
  343. actionStream << printSlipperyMovementGuard(a, "West", 1, {"!"+a+"CannotSlipSouth", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, southUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  344. actionStream << printSlipperyMovementGuard(a, "West", 1, { a+"CannotSlipSouth", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  345. actionStream << printSlipperyMovementGuard(a, "West", 1, {"!"+a+"CannotSlipSouth", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, southUpdate(a) } }) << ";\n";
  346. actionStream << printSlipperyMovementGuard(a, "West", 1, { a+"CannotSlipSouth", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", {}) << ";\n";
  347. actionStream << printSlipperyMovementGuard(a, "West", 0, {"!"+a+"CannotSlipWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a) }, {1 - probIntended, "true"} }) << ";\n";
  348. actionStream << printSlipperyMovementGuard(a, "West", 0, { a+"CannotSlipWest"}) << printSlipperyMovementUpdate(a, "West", {{1, "true"}}) << ";\n";
  349. }
  350. void PrismModulesPrinter::printSlipperyTurnActionsForNorth(const AgentName &a) {
  351. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!"+a+"CannotSlipNorth"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, northUpdate(a)} }) << ";\n";
  352. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { a+"CannotSlipNorth"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << ";\n";
  353. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipNorth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"-1)"}, {1 - probIntended, northUpdate(a)} }) << ";\n";
  354. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipNorth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=3)"}, {1 - probIntended, northUpdate(a)} }) << ";\n";
  355. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipNorth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"-1)"} }) << ";\n";
  356. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipNorth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=3)"} }) << ";\n";
  357. }
  358. void PrismModulesPrinter::printSlipperyTurnActionsForEast(const AgentName &a) {
  359. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!"+a+"CannotSlipEast"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, eastUpdate(a)} }) << ";\n";
  360. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { a+"CannotSlipEast"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << ";\n";
  361. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipEast"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"-1)"}, {1 - probIntended, eastUpdate(a)} }) << ";\n";
  362. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipEast"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=3)"}, {1 - probIntended, eastUpdate(a)} }) << ";\n";
  363. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipEast"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"-1)"} }) << ";\n";
  364. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipEast"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=3)"} }) << ";\n";
  365. }
  366. void PrismModulesPrinter::printSlipperyTurnActionsForSouth(const AgentName &a) {
  367. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!"+a+"CannotSlipSouth"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, southUpdate(a)} }) << ";\n";
  368. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { a+"CannotSlipSouth"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << ";\n";
  369. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipSouth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"-1)"}, {1 - probIntended, southUpdate(a)} }) << ";\n";
  370. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipSouth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=3)"}, {1 - probIntended, southUpdate(a)} }) << ";\n";
  371. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipSouth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"-1)"} }) << ";\n";
  372. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipSouth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=3)"} }) << ";\n";
  373. }
  374. void PrismModulesPrinter::printSlipperyTurnActionsForWest(const AgentName &a) {
  375. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!"+a+"CannotSlipWest"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, westUpdate(a)} }) << ";\n";
  376. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { a+"CannotSlipWest"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << ";\n";
  377. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipWest"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"-1)"}, {1 - probIntended, westUpdate(a)} }) << ";\n";
  378. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipWest"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=3)"}, {1 - probIntended, westUpdate(a)} }) << ";\n";
  379. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipWest"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"-1)"} }) << ";\n";
  380. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipWest"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=3)"} }) << ";\n";
  381. }
  382. std::string PrismModulesPrinter::printSlipperyMovementGuard(const AgentName &a, const std::string &direction, const ViewDirection &viewDirection, const std::vector<std::string> &guards) {
  383. std::string actionName = "[" + a + "_move_" + viewDirectionToString.at(viewDirection) + "]";
  384. agentNameActionMap.at(a).insert({FORWARD, actionName});
  385. return " " + actionName + " " + viewVariable(a, viewDirection) + " & " + a + "IsOnSlippery" + direction + " & " + buildConjunction(a, guards) + " -> ";
  386. }
  387. std::string PrismModulesPrinter::printSlipperyMovementUpdate(const AgentName &a, const std::string &direction, const updates &u) const {
  388. return updatesToString(u);
  389. }
  390. std::string PrismModulesPrinter::printSlipperyTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::vector<std::string> &guards, const std::string &cond) {
  391. std::string actionName = "[" + a + "_turn_" + direction + "]";
  392. agentNameActionMap.at(a).insert({actionId, actionName});
  393. return " " + actionName + " " + a + "IsOnSlippery & " + buildConjunction(a, guards) + " & " + cond + " -> ";
  394. }
  395. std::string PrismModulesPrinter::printSlipperyTurnUpdate(const AgentName &a, const updates &u) {
  396. return updatesToString(u);
  397. }
  398. void PrismModulesPrinter::printFaultyMovementModule(const AgentName &a) {
  399. os << "\nmodule " << a << "FaultyBehaviour" << std::endl;
  400. os << " previousAction" << a << " : [0.." + std::to_string(NOFAULT) + "];\n";
  401. std::set<size_t> exclude = {PICKUP, DROP, TOGGLE, DONE};
  402. for(const auto [actionId, actionName] : agentNameActionMap.at(a)) {
  403. if(exclude.count(actionId) > 0) continue;
  404. os << " " << actionName << faultyBehaviourGuard(a, actionId) << " -> " << faultyBehaviourUpdate(a, actionId) << ";\n";
  405. }
  406. os << "endmodule\n\n";
  407. }
  408. void PrismModulesPrinter::printMoveModule() {
  409. os << "\nmodule " << "Arbiter" << std::endl;
  410. os << " clock : [0.." << agentIndexMap.size() - 1 << "];\n";
  411. for(const auto [agentName, actions] : agentNameActionMap) {
  412. for(const auto [actionId, actionName] : actions) {
  413. os << " " << actionName << " " << moveGuard(agentName) << " -> " << moveUpdate(agentName) << ";\n";
  414. }
  415. }
  416. os << "endmodule\n\n";
  417. }
  418. void PrismModulesPrinter::printConfiguredActions(const AgentName &agentName) {
  419. for (auto& config : configuration) {
  420. if (config.type_ == ConfigType::Module && !config.overwrite_ && agentName == config.module_) {
  421. os << config.expression_ ;
  422. }
  423. }
  424. os << "\n";
  425. }
  426. void PrismModulesPrinter::printDoneActions(const AgentName &agentName) {
  427. os << " [" << agentName << "_on_goal]" << agentName << "IsOnGoal & clock=0 -> true;\n";
  428. }
  429. void PrismModulesPrinter::printPlayerStruct(const AgentName &agentName) {
  430. os << "player " << agentName << "\n\t";
  431. bool first = true;
  432. for(const auto [actionId, actionName] : agentNameActionMap.at(agentName)) {
  433. if(first) first = false;
  434. else os << ", ";
  435. os << actionName;
  436. }
  437. if(agentName == "Agent") os << ", [Agent_on_goal]";
  438. os << "\nendplayer\n";
  439. }
  440. void PrismModulesPrinter::printRewards(const AgentName &agentName, const std::map<coordinates, float> &stateRewards, const cells &lava, const cells &goals, const std::map<Color, cells> &backgroundTiles) {
  441. if(lava.size() != 0) {
  442. os << "rewards \"" << agentName << "SafetyNoBFS\"\n";
  443. os << "\t" <<agentName << "IsInLavaAndNotDone: -100;\n";
  444. os << "endrewards\n";
  445. }
  446. if (!goals.empty() || !lava.empty()) {
  447. os << "rewards \"" << agentName << "SafetyNoBFSAndGoal\"\n";
  448. if(goals.size() != 0) os << "\t" << agentName << "IsInGoalAndNotDone: 100;\n";
  449. if(lava.size() != 0) os << "\t" << agentName << "IsInLavaAndNotDone: -100;\n";
  450. os << "endrewards\n";
  451. }
  452. os << "rewards \"" << agentName << "Time\"\n";
  453. os << "\t!" << agentName << "IsInGoal : -1;\n";
  454. if(goals.size() != 0) os << "\t" << agentName << "IsInGoalAndNotDone: 100;\n";
  455. if(lava.size() != 0) os << "\t" << agentName << "IsInLavaAndNotDone: -100;\n";
  456. os << "endrewards\n";
  457. if(stateRewards.size() > 0) {
  458. os << "rewards \"" << agentName << "SafetyWithBFS\"\n";
  459. if(lava.size() != 0) os << "\t" << agentName << "IsInLavaAndNotDone: -100;\n";
  460. for(auto const [coordinates, reward] : stateRewards) {
  461. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  462. }
  463. os << "endrewards\n";
  464. }
  465. if(stateRewards.size() > 0) {
  466. os << "rewards \"" << agentName << "SafetyWithBFSAndGoal\"\n";
  467. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  468. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  469. for(auto const [coordinates, reward] : stateRewards) {
  470. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  471. }
  472. os << "endrewards\n";
  473. }
  474. for(auto const entry : backgroundTiles)
  475. {
  476. std::cout << getColor(entry.first) << " ";
  477. for(auto const cell : entry.second){
  478. std::cout << cell.getCoordinates().first << " " << cell.getCoordinates().second << std::endl;
  479. }
  480. }
  481. if(backgroundTiles.size() > 0) {
  482. os << "rewards \"TaxiReward\"\n";
  483. os << "\t!AgentIsInGoal : -1;\n";
  484. std::string allPassengersPickedUp = "";
  485. bool first = true;
  486. for(auto const [color, cells] : backgroundTiles) {
  487. if(cells.size() == 0) continue;
  488. if(first) first = false; else allPassengersPickedUp += "&";
  489. std::string c = getColor(color);
  490. c.at(0) = std::toupper(c.at(0));
  491. std::string visitedLabel = agentName + "_picked_up_" + c;
  492. allPassengersPickedUp += visitedLabel;
  493. os << "[" << agentName << "_pickup_" << c << "] true : 100;\n";
  494. }
  495. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & " << allPassengersPickedUp << " : 100;\n";
  496. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & !(" << allPassengersPickedUp << ") : -100;\n";
  497. os << "endrewards";
  498. }
  499. }
  500. std::string PrismModulesPrinter::faultyBehaviourGuard(const AgentName &agentName, const ActionId &actionId) const {
  501. if(faultyBehaviour()) {
  502. if(actionId == NOFAULT) {
  503. return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + ") ";
  504. } else {
  505. return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + " | previousAction" + agentName + "=" + std::to_string(actionId) + ") ";
  506. }
  507. } else {
  508. return "";
  509. }
  510. }
  511. std::string PrismModulesPrinter::faultyBehaviourUpdate(const AgentName &agentName, const ActionId &actionId) const {
  512. if(actionId != NOFAULT) {
  513. return updatesToString({ {1 - faultyProbability, "(previousAction" + agentName + "'=" + std::to_string(NOFAULT) + ")"}, {faultyProbability, "(previousAction" + agentName + "'=" + std::to_string(actionId) + ")" } });
  514. } else {
  515. return "true";
  516. }
  517. }
  518. std::string PrismModulesPrinter::moveGuard(const AgentName &agentName) const {
  519. return "clock=" + std::to_string(agentIndexMap.at(agentName));
  520. }
  521. std::string PrismModulesPrinter::moveUpdate(const AgentName &agentName) const {
  522. size_t agentIndex = agentIndexMap.at(agentName);
  523. return (agentIndex == numberOfPlayer - 1) ? "(clock'=0) " : "(clock'=" + std::to_string(agentIndex + 1) + ") ";
  524. }
  525. std::string PrismModulesPrinter::updatesToString(const updates &updates) const {
  526. if(updates.empty()) return "true";
  527. std::string updatesString = "";
  528. bool first = true;
  529. for(auto const update : updates) {
  530. if(first) first = false;
  531. else updatesString += " + ";
  532. updatesString += updateToString(update);
  533. }
  534. return updatesString;
  535. }
  536. std::string PrismModulesPrinter::updateToString(const update &u) const {
  537. return std::to_string(u.first) + ": " + u.second;
  538. }
  539. std::string PrismModulesPrinter::viewVariable(const AgentName &agentName, const size_t &agentDirection) const {
  540. return "view" + agentName + "=" + std::to_string(agentDirection);
  541. }
  542. bool PrismModulesPrinter::anyPortableObject() const {
  543. return !keys.empty() || !boxes.empty() || !balls.empty();
  544. }
  545. bool PrismModulesPrinter::faultyBehaviour() const {
  546. return faultyProbability > 0.0f;
  547. }
  548. bool PrismModulesPrinter::slipperyBehaviour() const {
  549. return !slipperyTiles.at("North").empty() || !slipperyTiles.at("East").empty() || !slipperyTiles.at("South").empty() || !slipperyTiles.at("West").empty();
  550. }
  551. bool PrismModulesPrinter::isGame() const {
  552. return modelType == ModelType::SMG;
  553. }
  554. std::string PrismModulesPrinter::buildConjunction(const AgentName &a, std::vector<std::string> formulae) const {
  555. if(formulae.empty()) return "true";
  556. std::string conjunction = "";
  557. bool first = true;
  558. for(auto const formula : formulae) {
  559. if(first) first = false;
  560. else conjunction += " & ";
  561. conjunction += formula;
  562. }
  563. return conjunction;
  564. }
  565. }