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.

654 lines
46 KiB

9 months ago
9 months ago
9 months ago
9 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. os << "\n" << actionStream.str();
  175. actionStream.str(std::string());
  176. if(agentNameAndPositionMap.size() > 1 && agentName == "Agent") printDoneActions(agentName);
  177. os << "endmodule\n\n";
  178. }
  179. void PrismModulesPrinter::printPortableObjectActionsForRobot(const std::string &a, const std::string &i) {
  180. actionStream << " [" << a << "_pickup_" << i << "] " << " !" << a << "IsCarrying & " << a << "CannotMove" << i << " -> (" << a << "Carrying" << i << "'=true);\n";
  181. actionStream << " [" << a << "_drop_" << i << "_north]\t" << a << "Carrying" << i << " & view" << a << "=3 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveNorthWall -> (" << a << "Carrying" << i << "'=false);\n";
  182. actionStream << " [" << a << "_drop_" << i << "_west] \t" << a << "Carrying" << i << " & view" << a << "=2 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveWestWall -> (" << a << "Carrying" << i << "'=false);\n";
  183. actionStream << " [" << a << "_drop_" << i << "_south]\t" << a << "Carrying" << i << " & view" << a << "=1 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveSouthWall -> (" << a << "Carrying" << i << "'=false);\n";
  184. actionStream << " [" << a << "_drop_" << i << "_east] \t" << a << "Carrying" << i << " & view" << a << "=0 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveEastWall -> (" << a << "Carrying" << i << "'=false);\n";
  185. actionStream << "\n";
  186. }
  187. void PrismModulesPrinter::printUnlockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier) {
  188. actionStream << " [" << agentName << "_open_" << identifier << "] " << agentName << "CannotMove" << identifier << " -> true;\n";
  189. actionStream << " [" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " -> true;\n";
  190. actionStream << "\n";
  191. }
  192. void PrismModulesPrinter::printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key) {
  193. actionStream << " [" << agentName << "_unlock_" << identifier << "] " << agentName << "CannotMove" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n";
  194. actionStream << " [" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n";
  195. actionStream << "\n";
  196. }
  197. void PrismModulesPrinter::printTurnActionsForRobot(const AgentName &a) {
  198. actionStream << printTurnGuard(a, "right", RIGHT, "true") << printTurnUpdate(a, {1.0, "(view"+a+"'=mod(view"+a+"+1,4))"}, RIGHT);
  199. actionStream << printTurnGuard(a, "left", LEFT, "view"+a+">0") << printTurnUpdate(a, {1.0, "(view"+a+"'=view"+a+"-1)"}, LEFT);
  200. actionStream << printTurnGuard(a, "left", LEFT, "view"+a+"=0") << printTurnUpdate(a, {1.0, "(view"+a+"'=3)"}, LEFT);
  201. }
  202. void PrismModulesPrinter::printMovementActionsForRobot(const AgentName &a) {
  203. actionStream << printMovementGuard(a, "North", 3) << printMovementUpdate(a, {1.0, northUpdate(a)});
  204. actionStream << printMovementGuard(a, "East", 0) << printMovementUpdate(a, {1.0, eastUpdate(a)});
  205. actionStream << printMovementGuard(a, "South", 1) << printMovementUpdate(a, {1.0, southUpdate(a)});
  206. actionStream << printMovementGuard(a, "West", 2) << printMovementUpdate(a, {1.0, westUpdate(a)});
  207. if(faultyBehaviour()) {
  208. std::string actionName = "[" + a + "_stuck]";
  209. agentNameActionMap.at(a).insert({FORWARD, actionName});
  210. actionStream << " " << actionName << " " << "previousAction" << a << "=" << std::to_string(FORWARD);
  211. actionStream << " & ((view" << a << "=0 & " << a << "CannotMoveEastWall) |";
  212. actionStream << " (view" << a << "=1 & " << a << "CannotMoveSouthWall) |";
  213. actionStream << " (view" << a << "=2 & " << a << "CannotMoveWestWall) |";
  214. actionStream << " (view" << a << "=3 & " << a << "CannotMoveNorthWall) ) -> true;\n";
  215. }
  216. }
  217. std::string PrismModulesPrinter::printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) {
  218. std::string actionName = "[" + a + "_move_" + direction + "]";
  219. agentNameActionMap.at(a).insert({FORWARD, actionName});
  220. std::string guard = " " + actionName + " " + viewVariable(a, viewDirection);
  221. if(slipperyBehaviour()) guard += " & !" + a + "IsOnSlippery";
  222. if(anyLava) guard += " & !" + a + "IsOnLava";
  223. if(anyGoals) guard += " & !" + a + "IsOnGoal";
  224. guard += " & !" + a + "CannotMove" + direction + "Wall";
  225. if(anyPortableObject()) guard += " & !" + a + "CannotMoveConditionally";
  226. guard += " -> ";
  227. return guard;
  228. }
  229. std::string PrismModulesPrinter::printMovementUpdate(const AgentName &a, const update &u) const {
  230. return updateToString(u) + ";\n";
  231. }
  232. std::string PrismModulesPrinter::printTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::string &cond) {
  233. std::string actionName = "[" + a + "_turn_" + direction + "]";
  234. agentNameActionMap.at(a).insert({actionId, actionName});
  235. std::string guard = " " + actionName;
  236. if(slipperyBehaviour()) guard += " !" + a + "IsOnSlippery & ";
  237. if(anyLava) guard += " !" + a + "IsOnLava &";
  238. guard += cond + " -> ";
  239. return guard;
  240. }
  241. std::string PrismModulesPrinter::printTurnUpdate(const AgentName &a, const update &u, const ActionId &actionId) const {
  242. return updateToString(u) + ";\n";
  243. }
  244. void PrismModulesPrinter::printSlipperyMovementActionsForRobot(const AgentName &a) {
  245. if(!slipperyTiles.at("North").empty()) {
  246. printSlipperyMovementActionsForNorth(a);
  247. printSlipperyTurnActionsForNorth(a);
  248. }
  249. if(!slipperyTiles.at("East").empty()) {
  250. printSlipperyMovementActionsForEast(a) ;
  251. printSlipperyTurnActionsForEast(a);
  252. }
  253. if(!slipperyTiles.at("South").empty()) {
  254. printSlipperyMovementActionsForSouth(a);
  255. printSlipperyTurnActionsForSouth(a);
  256. }
  257. if(!slipperyTiles.at("West").empty()) {
  258. printSlipperyMovementActionsForWest(a) ;
  259. printSlipperyTurnActionsForWest(a);
  260. }
  261. }
  262. void PrismModulesPrinter::printSlipperyMovementActionsForNorth(const AgentName &a) {
  263. 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";
  264. 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";
  265. actionStream << printSlipperyMovementGuard(a, "North", 3, {"!"+a+"CannotSlipNorth", a+"CannotSlipNorthEast", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, northUpdate(a)}, {(1 - probIntended), northUpdate(a)+"&"+westUpdate(a)} }) << ";\n";
  266. actionStream << printSlipperyMovementGuard(a, "North", 3, {"!"+a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, northUpdate(a)}, {(1 - probIntended), northUpdate(a)+"&"+eastUpdate(a)} }) << ";\n";
  267. actionStream << printSlipperyMovementGuard(a, "North", 3, { a+"CannotSlipNorth", a+"CannotSlipNorthEast", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)+"&"+westUpdate(a) } }) << ";\n";
  268. actionStream << printSlipperyMovementGuard(a, "North", 3, {"!"+a+"CannotSlipNorth", a+"CannotSlipNorthEast", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)} }) << ";\n";
  269. actionStream << printSlipperyMovementGuard(a, "North", 3, { a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)+"&"+eastUpdate(a)} }) << ";\n";
  270. actionStream << printSlipperyMovementGuard(a, "North", 3, { a+"CannotSlipNorth", a+"CannotSlipNorthEast", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", {}) << ";\n";
  271. actionStream << printSlipperyMovementGuard(a, "North", 2, {"!"+a+"CannotSlipWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, westUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  272. actionStream << printSlipperyMovementGuard(a, "North", 2, { a+"CannotSlipWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  273. actionStream << printSlipperyMovementGuard(a, "North", 2, {"!"+a+"CannotSlipWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, westUpdate(a) } }) << ";\n";
  274. actionStream << printSlipperyMovementGuard(a, "North", 2, { a+"CannotSlipWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", {}) << ";\n";
  275. actionStream << printSlipperyMovementGuard(a, "North", 0, {"!"+a+"CannotSlipEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, eastUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  276. actionStream << printSlipperyMovementGuard(a, "North", 0, { a+"CannotSlipEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", { {1, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  277. actionStream << printSlipperyMovementGuard(a, "North", 0, {"!"+a+"CannotSlipEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", { {1, eastUpdate(a) } }) << ";\n";
  278. actionStream << printSlipperyMovementGuard(a, "North", 0, { a+"CannotSlipEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", {}) << ";\n";
  279. actionStream << printSlipperyMovementGuard(a, "North", 1, {"!"+a+"CannotSlipSouth"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, southUpdate(a) }, {1 - probIntended, "true"} }) << ";\n";
  280. actionStream << printSlipperyMovementGuard(a, "North", 1, { a+"CannotSlipSouth"}) << printSlipperyMovementUpdate(a, "North", { {1, "true"} }) << ";\n";
  281. }
  282. void PrismModulesPrinter::printSlipperyMovementActionsForEast(const AgentName &a) {
  283. 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";
  284. 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";
  285. actionStream << printSlipperyMovementGuard(a, "East", 0, {"!"+a+"CannotSlipEast", a+"CannotSlipSouthEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a)}, {(1 - probIntended), eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  286. actionStream << printSlipperyMovementGuard(a, "East", 0, {"!"+a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a)}, {(1 - probIntended), eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  287. actionStream << printSlipperyMovementGuard(a, "East", 0, { a+"CannotSlipEast", a+"CannotSlipSouthEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+northUpdate(a) } }) << ";\n";
  288. actionStream << printSlipperyMovementGuard(a, "East", 0, {"!"+a+"CannotSlipEast", a+"CannotSlipSouthEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)} }) << ";\n";
  289. actionStream << printSlipperyMovementGuard(a, "East", 0, { a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  290. actionStream << printSlipperyMovementGuard(a, "East", 0, { a+"CannotSlipEast", a+"CannotSlipSouthEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", {}) << ";\n";
  291. actionStream << printSlipperyMovementGuard(a, "East", 3, {"!"+a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, northUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  292. actionStream << printSlipperyMovementGuard(a, "East", 3, { a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  293. actionStream << printSlipperyMovementGuard(a, "East", 3, {"!"+a+"CannotSlipNorth", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, northUpdate(a) } }) << ";\n";
  294. actionStream << printSlipperyMovementGuard(a, "East", 3, { a+"CannotSlipNorth", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", {}) << ";\n";
  295. actionStream << printSlipperyMovementGuard(a, "East", 1, {"!"+a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, southUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  296. actionStream << printSlipperyMovementGuard(a, "East", 1, { a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  297. actionStream << printSlipperyMovementGuard(a, "East", 1, {"!"+a+"CannotSlipSouth", a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, southUpdate(a) } }) << ";\n";
  298. actionStream << printSlipperyMovementGuard(a, "East", 1, { a+"CannotSlipSouth", a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", {}) << ";\n";
  299. actionStream << printSlipperyMovementGuard(a, "East", 2, {"!"+a+"CannotSlipEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a) }, {1 - probIntended, "true"} }) << ";\n";
  300. actionStream << printSlipperyMovementGuard(a, "East", 2, { a+"CannotSlipEast"}) << printSlipperyMovementUpdate(a, "East", { {1, "true"} }) << ";\n";
  301. }
  302. void PrismModulesPrinter::printSlipperyMovementActionsForSouth(const AgentName &a) {
  303. 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";
  304. 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";
  305. actionStream << printSlipperyMovementGuard(a, "South", 1, {"!"+a+"CannotSlipSouth", a+"CannotSlipSouthEast", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, southUpdate(a)}, {(1 - probIntended), southUpdate(a)+"&"+westUpdate(a)} }) << ";\n";
  306. actionStream << printSlipperyMovementGuard(a, "South", 1, {"!"+a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, southUpdate(a)}, {(1 - probIntended), southUpdate(a)+"&"+eastUpdate(a)} }) << ";\n";
  307. actionStream << printSlipperyMovementGuard(a, "South", 1, { a+"CannotSlipSouth", a+"CannotSlipSouthEast", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)+"&"+westUpdate(a) } }) << ";\n";
  308. actionStream << printSlipperyMovementGuard(a, "South", 1, {"!"+a+"CannotSlipSouth", a+"CannotSlipSouthEast", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)} }) << ";\n";
  309. actionStream << printSlipperyMovementGuard(a, "South", 1, { a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)+"&"+eastUpdate(a)} }) << ";\n";
  310. actionStream << printSlipperyMovementGuard(a, "South", 1, { a+"CannotSlipSouth", a+"CannotSlipSouthEast", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", {}) << ";\n";
  311. actionStream << printSlipperyMovementGuard(a, "South", 2, {"!"+a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, westUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  312. actionStream << printSlipperyMovementGuard(a, "South", 2, { a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  313. actionStream << printSlipperyMovementGuard(a, "South", 2, {"!"+a+"CannotSlipWest", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, westUpdate(a) } }) << ";\n";
  314. actionStream << printSlipperyMovementGuard(a, "South", 2, { a+"CannotSlipWest", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", {}) << ";\n";
  315. actionStream << printSlipperyMovementGuard(a, "South", 0, {"!"+a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, eastUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  316. actionStream << printSlipperyMovementGuard(a, "South", 0, { a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", { {1, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  317. actionStream << printSlipperyMovementGuard(a, "South", 0, {"!"+a+"CannotSlipEast", a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", { {1, eastUpdate(a) } }) << ";\n";
  318. actionStream << printSlipperyMovementGuard(a, "South", 0, { a+"CannotSlipEast", a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", {}) << ";\n";
  319. actionStream << printSlipperyMovementGuard(a, "South", 3, {"!"+a+"CannotSlipSouth"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, northUpdate(a) }, {1 - probIntended, "true"} }) << ";\n";
  320. actionStream << printSlipperyMovementGuard(a, "South", 3, { a+"CannotSlipSouth"}) << printSlipperyMovementUpdate(a, "South", { {1, "true"} }) << ";\n";
  321. }
  322. void PrismModulesPrinter::printSlipperyMovementActionsForWest(const AgentName &a) {
  323. 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";
  324. 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";
  325. actionStream << printSlipperyMovementGuard(a, "West", 2, {"!"+a+"CannotSlipWest", a+"CannotSlipSouthWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a)}, {(1 - probIntended), westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  326. actionStream << printSlipperyMovementGuard(a, "West", 2, {"!"+a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a)}, {(1 - probIntended), westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  327. actionStream << printSlipperyMovementGuard(a, "West", 2, { a+"CannotSlipWest", a+"CannotSlipSouthWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+northUpdate(a) } }) << ";\n";
  328. actionStream << printSlipperyMovementGuard(a, "West", 2, {"!"+a+"CannotSlipWest", a+"CannotSlipSouthWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)} }) << ";\n";
  329. actionStream << printSlipperyMovementGuard(a, "West", 2, { a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  330. actionStream << printSlipperyMovementGuard(a, "West", 2, { a+"CannotSlipWest", a+"CannotSlipSouthWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", {}) << ";\n";
  331. actionStream << printSlipperyMovementGuard(a, "West", 3, {"!"+a+"CannotSlipNorth", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, northUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  332. actionStream << printSlipperyMovementGuard(a, "West", 3, { a+"CannotSlipNorth", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  333. actionStream << printSlipperyMovementGuard(a, "West", 3, {"!"+a+"CannotSlipNorth", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, northUpdate(a) } }) << ";\n";
  334. actionStream << printSlipperyMovementGuard(a, "West", 3, { a+"CannotSlipNorth", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", {}) << ";\n";
  335. actionStream << printSlipperyMovementGuard(a, "West", 1, {"!"+a+"CannotSlipSouth", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, southUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  336. actionStream << printSlipperyMovementGuard(a, "West", 1, { a+"CannotSlipSouth", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  337. actionStream << printSlipperyMovementGuard(a, "West", 1, {"!"+a+"CannotSlipSouth", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, southUpdate(a) } }) << ";\n";
  338. actionStream << printSlipperyMovementGuard(a, "West", 1, { a+"CannotSlipSouth", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", {}) << ";\n";
  339. actionStream << printSlipperyMovementGuard(a, "West", 0, {"!"+a+"CannotSlipWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a) }, {1 - probIntended, "true"} }) << ";\n";
  340. actionStream << printSlipperyMovementGuard(a, "West", 0, { a+"CannotSlipWest"}) << printSlipperyMovementUpdate(a, "West", {{1, "true"}}) << ";\n";
  341. }
  342. void PrismModulesPrinter::printSlipperyTurnActionsForNorth(const AgentName &a) {
  343. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!"+a+"CannotSlipNorth"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, northUpdate(a)} }) << ";\n";
  344. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { a+"CannotSlipNorth"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << ";\n";
  345. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipNorth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"-1)"}, {1 - probIntended, northUpdate(a)} }) << ";\n";
  346. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipNorth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=3)"}, {1 - probIntended, northUpdate(a)} }) << ";\n";
  347. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipNorth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"-1)"} }) << ";\n";
  348. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipNorth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=3)"} }) << ";\n";
  349. }
  350. void PrismModulesPrinter::printSlipperyTurnActionsForEast(const AgentName &a) {
  351. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!"+a+"CannotSlipEast"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, eastUpdate(a)} }) << ";\n";
  352. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { a+"CannotSlipEast"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << ";\n";
  353. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipEast"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"-1)"}, {1 - probIntended, eastUpdate(a)} }) << ";\n";
  354. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipEast"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=3)"}, {1 - probIntended, eastUpdate(a)} }) << ";\n";
  355. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipEast"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"-1)"} }) << ";\n";
  356. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipEast"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=3)"} }) << ";\n";
  357. }
  358. void PrismModulesPrinter::printSlipperyTurnActionsForSouth(const AgentName &a) {
  359. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!"+a+"CannotSlipSouth"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, southUpdate(a)} }) << ";\n";
  360. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { a+"CannotSlipSouth"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << ";\n";
  361. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipSouth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"-1)"}, {1 - probIntended, southUpdate(a)} }) << ";\n";
  362. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipSouth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=3)"}, {1 - probIntended, southUpdate(a)} }) << ";\n";
  363. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipSouth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"-1)"} }) << ";\n";
  364. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipSouth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=3)"} }) << ";\n";
  365. }
  366. void PrismModulesPrinter::printSlipperyTurnActionsForWest(const AgentName &a) {
  367. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!"+a+"CannotSlipWest"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, westUpdate(a)} }) << ";\n";
  368. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { a+"CannotSlipWest"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << ";\n";
  369. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipWest"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"-1)"}, {1 - probIntended, westUpdate(a)} }) << ";\n";
  370. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipWest"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=3)"}, {1 - probIntended, westUpdate(a)} }) << ";\n";
  371. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipWest"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"-1)"} }) << ";\n";
  372. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipWest"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=3)"} }) << ";\n";
  373. }
  374. std::string PrismModulesPrinter::printSlipperyMovementGuard(const AgentName &a, const std::string &direction, const ViewDirection &viewDirection, const std::vector<std::string> &guards) {
  375. std::string actionName = "[" + a + "_move_" + viewDirectionToString.at(viewDirection) + "]";
  376. agentNameActionMap.at(a).insert({FORWARD, actionName});
  377. return " " + actionName + " " + viewVariable(a, viewDirection) + " & " + a + "IsOnSlippery" + direction + " & " + buildConjunction(a, guards) + " -> ";
  378. }
  379. std::string PrismModulesPrinter::printSlipperyMovementUpdate(const AgentName &a, const std::string &direction, const updates &u) const {
  380. return updatesToString(u);
  381. }
  382. std::string PrismModulesPrinter::printSlipperyTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::vector<std::string> &guards, const std::string &cond) {
  383. std::string actionName = "[" + a + "_turn_" + direction + "]";
  384. agentNameActionMap.at(a).insert({actionId, actionName});
  385. return " " + actionName + " " + a + "IsOnSlippery & " + buildConjunction(a, guards) + " & " + cond + " -> ";
  386. }
  387. std::string PrismModulesPrinter::printSlipperyTurnUpdate(const AgentName &a, const updates &u) {
  388. return updatesToString(u);
  389. }
  390. void PrismModulesPrinter::printFaultyMovementModule(const AgentName &a) {
  391. os << "\nmodule " << a << "FaultyBehaviour" << std::endl;
  392. os << " previousAction" << a << " : [0.." + std::to_string(NOFAULT) + "];\n";
  393. for(const auto [actionId, actionName] : agentNameActionMap.at(a)) {
  394. os << " " << actionName << faultyBehaviourGuard(a, actionId) << " -> " << faultyBehaviourUpdate(a, actionId) << ";\n";
  395. }
  396. os << "endmodule\n\n";
  397. }
  398. void PrismModulesPrinter::printMoveModule() {
  399. os << "\nmodule " << "Arbiter" << std::endl;
  400. os << " clock : [0.." << agentIndexMap.size() - 1 << "];\n";
  401. for(const auto [agentName, actions] : agentNameActionMap) {
  402. for(const auto [actionId, actionName] : actions) {
  403. os << " " << actionName << " " << moveGuard(agentName) << " -> " << moveUpdate(agentName) << ";\n";
  404. }
  405. }
  406. os << "endmodule\n\n";
  407. }
  408. void PrismModulesPrinter::printConfiguredActions(const AgentName &agentName) {
  409. for (auto& config : configuration) {
  410. if (config.type_ == ConfigType::Module && !config.overwrite_ && agentName == config.module_) {
  411. os << config.expression_ ;
  412. }
  413. }
  414. os << "\n";
  415. }
  416. void PrismModulesPrinter::printDoneActions(const AgentName &agentName) {
  417. os << " [" << agentName << "_done]" << agentName << "IsOnGoal & clock=0 -> true;\n";
  418. }
  419. void PrismModulesPrinter::printPlayerStruct(const AgentName &agentName) {
  420. os << "player " << agentName << "\n\t";
  421. bool first = true;
  422. for(const auto [actionId, actionName] : agentNameActionMap.at(agentName)) {
  423. if(first) first = false;
  424. else os << ", ";
  425. os << actionName;
  426. }
  427. if(agentName == "Agent") os << ", [Agent_done]";
  428. os << "\nendplayer\n";
  429. }
  430. void PrismModulesPrinter::printRewards(const AgentName &agentName, const std::map<coordinates, float> &stateRewards, const cells &lava, const cells &goals, const std::map<Color, cells> &backgroundTiles) {
  431. if(lava.size() != 0) {
  432. os << "rewards \"" << agentName << "SafetyNoBFS\"\n";
  433. os << "\t" <<agentName << "IsInLavaAndNotDone: -100;\n";
  434. os << "endrewards\n";
  435. }
  436. if (!goals.empty() || !lava.empty()) {
  437. os << "rewards \"" << agentName << "SafetyNoBFSAndGoal\"\n";
  438. if(goals.size() != 0) os << "\t" << agentName << "IsInGoalAndNotDone: 100;\n";
  439. if(lava.size() != 0) os << "\t" << agentName << "IsInLavaAndNotDone: -100;\n";
  440. os << "endrewards\n";
  441. }
  442. os << "rewards \"" << agentName << "Time\"\n";
  443. os << "\t!" << agentName << "IsInGoal : -1;\n";
  444. if(goals.size() != 0) os << "\t" << agentName << "IsInGoalAndNotDone: 100;\n";
  445. if(lava.size() != 0) os << "\t" << agentName << "IsInLavaAndNotDone: -100;\n";
  446. os << "endrewards\n";
  447. if(stateRewards.size() > 0) {
  448. os << "rewards \"" << agentName << "SafetyWithBFS\"\n";
  449. if(lava.size() != 0) os << "\t" << agentName << "IsInLavaAndNotDone: -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. if(stateRewards.size() > 0) {
  456. os << "rewards \"" << agentName << "SafetyWithBFSAndGoal\"\n";
  457. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  458. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  459. for(auto const [coordinates, reward] : stateRewards) {
  460. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  461. }
  462. os << "endrewards\n";
  463. }
  464. for(auto const entry : backgroundTiles)
  465. {
  466. std::cout << getColor(entry.first) << " ";
  467. for(auto const cell : entry.second){
  468. std::cout << cell.getCoordinates().first << " " << cell.getCoordinates().second << std::endl;
  469. }
  470. }
  471. if(backgroundTiles.size() > 0) {
  472. os << "rewards \"TaxiReward\"\n";
  473. os << "\t!AgentIsInGoal : -1;\n";
  474. std::string allPassengersPickedUp = "";
  475. bool first = true;
  476. for(auto const [color, cells] : backgroundTiles) {
  477. if(cells.size() == 0) continue;
  478. if(first) first = false; else allPassengersPickedUp += "&";
  479. std::string c = getColor(color);
  480. c.at(0) = std::toupper(c.at(0));
  481. std::string visitedLabel = agentName + "_picked_up_" + c;
  482. allPassengersPickedUp += visitedLabel;
  483. os << "[" << agentName << "_pickup_" << c << "] true : 100;\n";
  484. }
  485. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & " << allPassengersPickedUp << " : 100;\n";
  486. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & !(" << allPassengersPickedUp << ") : -100;\n";
  487. os << "endrewards";
  488. }
  489. }
  490. std::string PrismModulesPrinter::faultyBehaviourGuard(const AgentName &agentName, const ActionId &actionId) const {
  491. if(faultyBehaviour()) {
  492. if(actionId == NOFAULT) {
  493. return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + ") ";
  494. } else {
  495. return "(previousAction" + agentName + "=" + std::to_string(NOFAULT) + " | previousAction" + agentName + "=" + std::to_string(actionId) + ") ";
  496. }
  497. } else {
  498. return "";
  499. }
  500. }
  501. std::string PrismModulesPrinter::faultyBehaviourUpdate(const AgentName &agentName, const ActionId &actionId) const {
  502. if(actionId != NOFAULT) {
  503. return updatesToString({ {1 - faultyProbability, "(previousAction" + agentName + "'=" + std::to_string(NOFAULT) + ")"}, {faultyProbability, "(previousAction" + agentName + "'=" + std::to_string(actionId) + ")" } });
  504. } else {
  505. return "true";
  506. }
  507. }
  508. std::string PrismModulesPrinter::moveGuard(const AgentName &agentName) const {
  509. return "clock=" + std::to_string(agentIndexMap.at(agentName));
  510. }
  511. std::string PrismModulesPrinter::moveUpdate(const AgentName &agentName) const {
  512. size_t agentIndex = agentIndexMap.at(agentName);
  513. return (agentIndex == numberOfPlayer - 1) ? "(clock'=0) " : "(clock'=" + std::to_string(agentIndex + 1) + ") ";
  514. }
  515. std::string PrismModulesPrinter::updatesToString(const updates &updates) const {
  516. if(updates.empty()) return "true";
  517. std::string updatesString = "";
  518. bool first = true;
  519. for(auto const update : updates) {
  520. if(first) first = false;
  521. else updatesString += " + ";
  522. updatesString += updateToString(update);
  523. }
  524. return updatesString;
  525. }
  526. std::string PrismModulesPrinter::updateToString(const update &u) const {
  527. return std::to_string(u.first) + ": " + u.second;
  528. }
  529. std::string PrismModulesPrinter::viewVariable(const AgentName &agentName, const size_t &agentDirection) const {
  530. return "view" + agentName + "=" + std::to_string(agentDirection);
  531. }
  532. bool PrismModulesPrinter::anyPortableObject() const {
  533. return !keys.empty() || !boxes.empty() || !balls.empty();
  534. }
  535. bool PrismModulesPrinter::faultyBehaviour() const {
  536. return faultyProbability > 0.0f;
  537. }
  538. bool PrismModulesPrinter::slipperyBehaviour() const {
  539. return !slipperyTiles.at("North").empty() || !slipperyTiles.at("East").empty() || !slipperyTiles.at("South").empty() || !slipperyTiles.at("West").empty();
  540. }
  541. bool PrismModulesPrinter::isGame() const {
  542. return modelType == ModelType::SMG;
  543. }
  544. std::string PrismModulesPrinter::buildConjunction(const AgentName &a, std::vector<std::string> formulae) const {
  545. if(formulae.empty()) return "true";
  546. std::string conjunction = "";
  547. bool first = true;
  548. for(auto const formula : formulae) {
  549. if(first) first = false;
  550. else conjunction += " & ";
  551. conjunction += formula;
  552. }
  553. return conjunction;
  554. }
  555. }