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.

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