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.

642 lines
45 KiB

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