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.

652 lines
46 KiB

11 months ago
11 months ago
11 months ago
11 months ago
  1. #include "PrismModulesPrinter.h"
  2. #include <map>
  3. #include <string>
  4. std::string northUpdate(const AgentName &a) { return "(row"+a+"'=row"+a+"-1)"; }
  5. std::string southUpdate(const AgentName &a) { return "(row"+a+"'=row"+a+"+1)"; }
  6. std::string eastUpdate(const AgentName &a) { return "(col"+a+"'=col"+a+"+1)"; }
  7. std::string westUpdate(const AgentName &a) { return "(col"+a+"'=col"+a+"-1)"; }
  8. namespace prism {
  9. PrismModulesPrinter::PrismModulesPrinter(std::ostream& os, const ModelType &modelType, const coordinates &maxBoundaries, const cells &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), 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 &door : unlockedDoors) {
  36. printDoorModule(door, true);
  37. }
  38. for(const auto &door : lockedDoors) {
  39. printDoorModule(door, false);
  40. }
  41. for(const auto [agentName, initialPosition] : agentNameAndPositionMap) {
  42. printRobotModule(agentName, initialPosition);
  43. }
  44. if(agentNameAndPositionMap.size() > 1) {
  45. printMoveModule();
  46. }
  47. if(faultyBehaviour()) {
  48. for(const auto [agentName, initialPosition] : agentNameAndPositionMap) {
  49. printFaultyMovementModule(agentName);
  50. }
  51. }
  52. if(agentNameAndPositionMap.size() > 1) {
  53. for(const auto [agentName, index] : agentIndexMap) {
  54. printPlayerStruct(agentName);
  55. }
  56. }
  57. if (!configuration.empty()) {
  58. printConfiguration(configuration);
  59. }
  60. return os;
  61. }
  62. void PrismModulesPrinter::printConfiguration(const std::vector<Configuration>& configurations) {
  63. for (auto& configuration : configurations) {
  64. if (configuration.overwrite_ || configuration.type_ == ConfigType::Module) {
  65. continue;
  66. }
  67. os << configuration.expression_ << std::endl;
  68. }
  69. }
  70. void PrismModulesPrinter::printConstants(const std::vector<std::string> &constants) {
  71. for (auto& constant : constants) {
  72. os << constant << std::endl;
  73. }
  74. }
  75. void PrismModulesPrinter::printPortableObjectModule(const cell &object) {
  76. std::string identifier = capitalize(object.getColor()) + object.getType();
  77. os << "\nmodule " << identifier << std::endl;
  78. os << " col" << identifier << " : [-1.." << maxBoundaries.first << "];\n";
  79. os << " row" << identifier << " : [-1.." << maxBoundaries.second << "];\n";
  80. os << " " << identifier << "PickedUp : bool;\n";
  81. os << "\n";
  82. for(const auto [name, position] : agentNameAndPositionMap) {
  83. printPortableObjectActions(name, identifier);
  84. }
  85. os << "endmodule\n\n";
  86. }
  87. void PrismModulesPrinter::printPortableObjectActions(const std::string &agentName, const std::string &identifier, const bool canBeDroped) {
  88. std::string actionName = "[" + agentName + "_pickup_" + identifier + "]";
  89. agentNameActionMap.at(agentName).insert({PICKUP, actionName});
  90. os << " " << actionName << " true -> (col" << identifier << "'=-1) & (row" << identifier << "'=-1) & (" << identifier << "PickedUp'=true);\n";
  91. if(canBeDroped) {
  92. actionName = "[" + agentName + "_drop_" + identifier + "_north]";
  93. agentNameActionMap.at(agentName).insert({DROP, actionName});
  94. os << " " << actionName << " " << " true -> (col" << identifier << "'=col" << agentName << ") & (row" << identifier << "'=row" << agentName << "-1) & (" << identifier << "PickedUp'=false);\n";
  95. actionName = "[" + agentName + "_drop_" + identifier + "_west]";
  96. agentNameActionMap.at(agentName).insert({DROP, actionName});
  97. os << " " << actionName << " " << " true -> (col" << identifier << "'=col" << agentName << "-1) & (row" << identifier << "'=row" << agentName << ") & (" << identifier << "PickedUp'=false);\n";
  98. actionName = "[" + agentName + "_drop_" + identifier + "_south]";
  99. agentNameActionMap.at(agentName).insert({DROP, actionName});
  100. os << " " << actionName << " " << " true -> (col" << identifier << "'=col" << agentName << ") & (row" << identifier << "'=row" << agentName << "+1) & (" << identifier << "PickedUp'=false);\n";
  101. actionName = "[" + agentName + "_drop_" + identifier + "_east]";
  102. agentNameActionMap.at(agentName).insert({DROP, actionName});
  103. os << " " << actionName << " " << " true -> (col" << identifier << "'=col" << agentName << "+1) & (row" << identifier << "'=row" << agentName << ") & (" << identifier << "PickedUp'=false);\n";
  104. }
  105. }
  106. void PrismModulesPrinter::printDoorModule(const cell &door, const bool &opened) {
  107. std::string identifier = capitalize(door.getColor()) + door.getType();
  108. os << "\nmodule " << identifier << std::endl;
  109. os << " " << identifier << "Open : bool;\n";
  110. os << "\n";
  111. if(opened) {
  112. for(const auto [name, position] : agentNameAndPositionMap) {
  113. printUnlockedDoorActions(name, identifier);
  114. }
  115. } else {
  116. for(const auto [name, position] : agentNameAndPositionMap) {
  117. printLockedDoorActions(name, identifier);
  118. }
  119. }
  120. os << "endmodule\n\n";
  121. }
  122. void PrismModulesPrinter::printLockedDoorActions(const std::string &agentName, const std::string &identifier) {
  123. std::string actionName = "[" + agentName + "_unlock_" + identifier + "]";
  124. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  125. os << " " << actionName << " !" << identifier << "Open -> (" << identifier << "Open'=true);\n";
  126. actionName = "[" + agentName + "_close_" + identifier + "]";
  127. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  128. os << " " << actionName << " " << identifier << "Open -> (" << identifier << "Open'=false);\n";
  129. }
  130. void PrismModulesPrinter::printUnlockedDoorActions(const std::string &agentName, const std::string &identifier) {
  131. std::string actionName = "[" + agentName + "_open_" + identifier + "]";
  132. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  133. os << " !" << identifier << "Open -> (" << identifier << "Open'=true);\n";
  134. actionName = "[" + agentName + "_close_" + identifier + "]";
  135. agentNameActionMap.at(agentName).insert({NOFAULT, actionName});
  136. os << " " << agentName << " " << identifier << "Open -> (" << identifier << "Open'=false);\n";
  137. }
  138. void PrismModulesPrinter::printRobotModule(const AgentName &agentName, const coordinates &initialPosition) {
  139. os << "\nmodule " << agentName << std::endl;
  140. os << " col" << agentName << " : [1.." << maxBoundaries.first << "];\n";
  141. os << " row" << agentName << " : [1.." << maxBoundaries.second << "];\n";
  142. os << " view" << agentName << " : [0..3];\n";
  143. printTurnActionsForRobot(agentName);
  144. printMovementActionsForRobot(agentName);
  145. if(slipperyBehaviour()) printSlipperyMovementActionsForRobot(agentName);
  146. for(const auto &door : unlockedDoors) {
  147. std::string identifier = capitalize(door.getColor()) + door.getType();
  148. printUnlockedDoorActionsForRobot(agentName, identifier);
  149. }
  150. for(const auto &door : lockedDoors) {
  151. std::string identifier = capitalize(door.getColor()) + door.getType();
  152. std::string key = capitalize(door.getColor()) + "Key";
  153. printLockedDoorActionsForRobot(agentName, identifier, key);
  154. }
  155. for(const auto &key : keys) {
  156. std::string identifier = capitalize(key.getColor()) + key.getType();
  157. os << " " << agentName << "Carrying" << identifier << " : bool;\n";
  158. printPortableObjectActionsForRobot(agentName, identifier);
  159. }
  160. printNonMovementActionsForRobot(agentName);
  161. os << "\n" << actionStream.str();
  162. actionStream.str(std::string());
  163. if(agentNameAndPositionMap.size() > 1 && agentName == "Agent") printDoneActions(agentName);
  164. os << "endmodule\n\n";
  165. }
  166. void PrismModulesPrinter::printPortableObjectActionsForRobot(const std::string &a, const std::string &i, const bool canBeDroped) {
  167. actionStream << " [" << a << "_pickup_" << i << "] " << " !" << a << "IsCarrying & " << a << "IsInFrontOf" << i << " -> (" << a << "Carrying" << i << "'=true);\n";
  168. if(canBeDroped) {
  169. actionStream << " [" << a << "_drop_" << i << "_north]\t" << a << "Carrying" << i << " & view" << a << "=3 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveNorthWall -> (" << a << "Carrying" << i << "'=false);\n";
  170. actionStream << " [" << a << "_drop_" << i << "_west] \t" << a << "Carrying" << i << " & view" << a << "=2 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveWestWall -> (" << a << "Carrying" << i << "'=false);\n";
  171. actionStream << " [" << a << "_drop_" << i << "_south]\t" << a << "Carrying" << i << " & view" << a << "=1 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveSouthWall -> (" << a << "Carrying" << i << "'=false);\n";
  172. actionStream << " [" << a << "_drop_" << i << "_east] \t" << a << "Carrying" << i << " & view" << a << "=0 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveEastWall -> (" << a << "Carrying" << i << "'=false);\n";
  173. actionStream << "\n";
  174. }
  175. }
  176. void PrismModulesPrinter::printUnlockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier) {
  177. actionStream << " [" << agentName << "_open_" << identifier << "] " << agentName << "CannotMove" << identifier << " -> true;\n";
  178. actionStream << " [" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " -> true;\n";
  179. actionStream << "\n";
  180. }
  181. void PrismModulesPrinter::printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key) {
  182. actionStream << " [" << agentName << "_unlock_" << identifier << "] " << agentName << "CannotMove" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n";
  183. actionStream << " [" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n";
  184. actionStream << "\n";
  185. }
  186. void PrismModulesPrinter::printTurnActionsForRobot(const AgentName &a) {
  187. actionStream << printTurnGuard(a, "right", RIGHT, "true") << printTurnUpdate(a, {1.0, "(view"+a+"'=mod(view"+a+"+1,4))"}, RIGHT);
  188. actionStream << printTurnGuard(a, "left", LEFT, "view"+a+">0") << printTurnUpdate(a, {1.0, "(view"+a+"'=view"+a+"-1)"}, LEFT);
  189. actionStream << printTurnGuard(a, "left", LEFT, "view"+a+"=0") << printTurnUpdate(a, {1.0, "(view"+a+"'=3)"}, LEFT);
  190. }
  191. void PrismModulesPrinter::printMovementActionsForRobot(const AgentName &a) {
  192. actionStream << printMovementGuard(a, "North", 3) << printMovementUpdate(a, {1.0, northUpdate(a)});
  193. actionStream << printMovementGuard(a, "East", 0) << printMovementUpdate(a, {1.0, eastUpdate(a)});
  194. actionStream << printMovementGuard(a, "South", 1) << printMovementUpdate(a, {1.0, southUpdate(a)});
  195. actionStream << printMovementGuard(a, "West", 2) << printMovementUpdate(a, {1.0, westUpdate(a)});
  196. if(faultyBehaviour()) {
  197. std::string actionName = "[" + a + "_stuck]";
  198. agentNameActionMap.at(a).insert({FORWARD, actionName});
  199. actionStream << " " << actionName << " " << "previousAction" << a << "=" << std::to_string(FORWARD);
  200. actionStream << " & ((view" << a << "=0 & " << a << "CannotMoveEastWall) |";
  201. actionStream << " (view" << a << "=1 & " << a << "CannotMoveSouthWall) |";
  202. actionStream << " (view" << a << "=2 & " << a << "CannotMoveWestWall) |";
  203. actionStream << " (view" << a << "=3 & " << a << "CannotMoveNorthWall) ) -> true;\n";
  204. }
  205. }
  206. std::string PrismModulesPrinter::printMovementGuard(const AgentName &a, const std::string &direction, const size_t &viewDirection) {
  207. std::string actionName = "[" + a + "_move_" + direction + "]";
  208. agentNameActionMap.at(a).insert({FORWARD, actionName});
  209. std::string guard = " " + actionName + " " + viewVariable(a, viewDirection);
  210. if(slipperyBehaviour()) guard += " & !" + a + "IsOnSlippery";
  211. if(anyLava) guard += " & !" + a + "IsOnLava";
  212. if(anyGoals) guard += " & !" + a + "IsOnGoal";
  213. guard += " & !" + a + "CannotMove" + direction + "Wall";
  214. if(anyPortableObject()) guard += " & !" + a + "CannotMoveConditionally";
  215. guard += " -> ";
  216. return guard;
  217. }
  218. std::string PrismModulesPrinter::printMovementUpdate(const AgentName &a, const update &u) const {
  219. return updateToString(u) + ";\n";
  220. }
  221. std::string PrismModulesPrinter::printTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::string &cond) {
  222. std::string actionName = "[" + a + "_turn_" + direction + "]";
  223. agentNameActionMap.at(a).insert({actionId, actionName});
  224. std::string guard = " " + actionName;
  225. if(slipperyBehaviour()) guard += " !" + a + "IsOnSlippery & ";
  226. if(anyLava) guard += " !" + a + "IsOnLava &";
  227. guard += cond + " -> ";
  228. return guard;
  229. }
  230. std::string PrismModulesPrinter::printTurnUpdate(const AgentName &a, const update &u, const ActionId &actionId) const {
  231. return updateToString(u) + ";\n";
  232. }
  233. void PrismModulesPrinter::printNonMovementActionsForRobot(const AgentName &agentName) {
  234. for(auto const [actionId, action] : nonMovementActions) {
  235. std::string actionName = "[" + agentName + "_" + action + "]";
  236. agentNameActionMap.at(agentName).insert({actionId, actionName});
  237. actionStream << " " << actionName << " true -> true;\n";
  238. }
  239. }
  240. void PrismModulesPrinter::printSlipperyMovementActionsForRobot(const AgentName &a) {
  241. if(!slipperyTiles.at("North").empty()) {
  242. printSlipperyMovementActionsForNorth(a);
  243. printSlipperyTurnActionsForNorth(a);
  244. }
  245. if(!slipperyTiles.at("East").empty()) {
  246. printSlipperyMovementActionsForEast(a) ;
  247. printSlipperyTurnActionsForEast(a);
  248. }
  249. if(!slipperyTiles.at("South").empty()) {
  250. printSlipperyMovementActionsForSouth(a);
  251. printSlipperyTurnActionsForSouth(a);
  252. }
  253. if(!slipperyTiles.at("West").empty()) {
  254. printSlipperyMovementActionsForWest(a) ;
  255. printSlipperyTurnActionsForWest(a);
  256. }
  257. }
  258. void PrismModulesPrinter::printSlipperyMovementActionsForNorth(const AgentName &a) {
  259. 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";
  260. 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";
  261. actionStream << printSlipperyMovementGuard(a, "North", 3, {"!"+a+"CannotSlipNorth", a+"CannotSlipNorthEast", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, northUpdate(a)}, {(1 - probIntended), northUpdate(a)+"&"+westUpdate(a)} }) << ";\n";
  262. actionStream << printSlipperyMovementGuard(a, "North", 3, {"!"+a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, northUpdate(a)}, {(1 - probIntended), northUpdate(a)+"&"+eastUpdate(a)} }) << ";\n";
  263. actionStream << printSlipperyMovementGuard(a, "North", 3, { a+"CannotSlipNorth", a+"CannotSlipNorthEast", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)+"&"+westUpdate(a) } }) << ";\n";
  264. actionStream << printSlipperyMovementGuard(a, "North", 3, {"!"+a+"CannotSlipNorth", a+"CannotSlipNorthEast", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)} }) << ";\n";
  265. actionStream << printSlipperyMovementGuard(a, "North", 3, { a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, northUpdate(a)+"&"+eastUpdate(a)} }) << ";\n";
  266. actionStream << printSlipperyMovementGuard(a, "North", 3, { a+"CannotSlipNorth", a+"CannotSlipNorthEast", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", {}) << ";\n";
  267. actionStream << printSlipperyMovementGuard(a, "North", 2, {"!"+a+"CannotSlipWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, westUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  268. actionStream << printSlipperyMovementGuard(a, "North", 2, { a+"CannotSlipWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  269. actionStream << printSlipperyMovementGuard(a, "North", 2, {"!"+a+"CannotSlipWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", { {1, westUpdate(a) } }) << ";\n";
  270. actionStream << printSlipperyMovementGuard(a, "North", 2, { a+"CannotSlipWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "North", {}) << ";\n";
  271. actionStream << printSlipperyMovementGuard(a, "North", 0, {"!"+a+"CannotSlipEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, eastUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  272. actionStream << printSlipperyMovementGuard(a, "North", 0, { a+"CannotSlipEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", { {1, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  273. actionStream << printSlipperyMovementGuard(a, "North", 0, {"!"+a+"CannotSlipEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", { {1, eastUpdate(a) } }) << ";\n";
  274. actionStream << printSlipperyMovementGuard(a, "North", 0, { a+"CannotSlipEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "North", {}) << ";\n";
  275. actionStream << printSlipperyMovementGuard(a, "North", 1, {"!"+a+"CannotSlipSouth"}) << printSlipperyMovementUpdate(a, "North", { {probIntended, southUpdate(a) }, {1 - probIntended, "true"} }) << ";\n";
  276. actionStream << printSlipperyMovementGuard(a, "North", 1, { a+"CannotSlipSouth"}) << printSlipperyMovementUpdate(a, "North", { {1, "true"} }) << ";\n";
  277. }
  278. void PrismModulesPrinter::printSlipperyMovementActionsForEast(const AgentName &a) {
  279. 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";
  280. 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";
  281. actionStream << printSlipperyMovementGuard(a, "East", 0, {"!"+a+"CannotSlipEast", a+"CannotSlipSouthEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a)}, {(1 - probIntended), eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  282. actionStream << printSlipperyMovementGuard(a, "East", 0, {"!"+a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a)}, {(1 - probIntended), eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  283. actionStream << printSlipperyMovementGuard(a, "East", 0, { a+"CannotSlipEast", a+"CannotSlipSouthEast", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+northUpdate(a) } }) << ";\n";
  284. actionStream << printSlipperyMovementGuard(a, "East", 0, {"!"+a+"CannotSlipEast", a+"CannotSlipSouthEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)} }) << ";\n";
  285. actionStream << printSlipperyMovementGuard(a, "East", 0, { a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  286. actionStream << printSlipperyMovementGuard(a, "East", 0, { a+"CannotSlipEast", a+"CannotSlipSouthEast", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", {}) << ";\n";
  287. actionStream << printSlipperyMovementGuard(a, "East", 3, {"!"+a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, northUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  288. actionStream << printSlipperyMovementGuard(a, "East", 3, { a+"CannotSlipNorth", "!"+a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  289. actionStream << printSlipperyMovementGuard(a, "East", 3, {"!"+a+"CannotSlipNorth", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, northUpdate(a) } }) << ";\n";
  290. actionStream << printSlipperyMovementGuard(a, "East", 3, { a+"CannotSlipNorth", a+"CannotSlipNorthEast"}) << printSlipperyMovementUpdate(a, "East", {}) << ";\n";
  291. actionStream << printSlipperyMovementGuard(a, "East", 1, {"!"+a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, southUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  292. actionStream << printSlipperyMovementGuard(a, "East", 1, { a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  293. actionStream << printSlipperyMovementGuard(a, "East", 1, {"!"+a+"CannotSlipSouth", a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", { {1, southUpdate(a) } }) << ";\n";
  294. actionStream << printSlipperyMovementGuard(a, "East", 1, { a+"CannotSlipSouth", a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "East", {}) << ";\n";
  295. actionStream << printSlipperyMovementGuard(a, "East", 2, {"!"+a+"CannotSlipEast"}) << printSlipperyMovementUpdate(a, "East", { {probIntended, eastUpdate(a) }, {1 - probIntended, "true"} }) << ";\n";
  296. actionStream << printSlipperyMovementGuard(a, "East", 2, { a+"CannotSlipEast"}) << printSlipperyMovementUpdate(a, "East", { {1, "true"} }) << ";\n";
  297. }
  298. void PrismModulesPrinter::printSlipperyMovementActionsForSouth(const AgentName &a) {
  299. 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";
  300. 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";
  301. actionStream << printSlipperyMovementGuard(a, "South", 1, {"!"+a+"CannotSlipSouth", a+"CannotSlipSouthEast", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, southUpdate(a)}, {(1 - probIntended), southUpdate(a)+"&"+westUpdate(a)} }) << ";\n";
  302. actionStream << printSlipperyMovementGuard(a, "South", 1, {"!"+a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, southUpdate(a)}, {(1 - probIntended), southUpdate(a)+"&"+eastUpdate(a)} }) << ";\n";
  303. actionStream << printSlipperyMovementGuard(a, "South", 1, { a+"CannotSlipSouth", a+"CannotSlipSouthEast", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)+"&"+westUpdate(a) } }) << ";\n";
  304. actionStream << printSlipperyMovementGuard(a, "South", 1, {"!"+a+"CannotSlipSouth", a+"CannotSlipSouthEast", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)} }) << ";\n";
  305. actionStream << printSlipperyMovementGuard(a, "South", 1, { a+"CannotSlipSouth", "!"+a+"CannotSlipSouthEast", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, southUpdate(a)+"&"+eastUpdate(a)} }) << ";\n";
  306. actionStream << printSlipperyMovementGuard(a, "South", 1, { a+"CannotSlipSouth", a+"CannotSlipSouthEast", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", {}) << ";\n";
  307. actionStream << printSlipperyMovementGuard(a, "South", 2, {"!"+a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, westUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  308. actionStream << printSlipperyMovementGuard(a, "South", 2, { a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  309. actionStream << printSlipperyMovementGuard(a, "South", 2, {"!"+a+"CannotSlipWest", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", { {1, westUpdate(a) } }) << ";\n";
  310. actionStream << printSlipperyMovementGuard(a, "South", 2, { a+"CannotSlipWest", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "South", {}) << ";\n";
  311. actionStream << printSlipperyMovementGuard(a, "South", 0, {"!"+a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, eastUpdate(a) }, {1 - probIntended, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  312. actionStream << printSlipperyMovementGuard(a, "South", 0, { a+"CannotSlipEast", "!"+a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", { {1, eastUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  313. actionStream << printSlipperyMovementGuard(a, "South", 0, {"!"+a+"CannotSlipEast", a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", { {1, eastUpdate(a) } }) << ";\n";
  314. actionStream << printSlipperyMovementGuard(a, "South", 0, { a+"CannotSlipEast", a+"CannotSlipSouthEast"}) << printSlipperyMovementUpdate(a, "South", {}) << ";\n";
  315. actionStream << printSlipperyMovementGuard(a, "South", 3, {"!"+a+"CannotSlipSouth"}) << printSlipperyMovementUpdate(a, "South", { {probIntended, northUpdate(a) }, {1 - probIntended, "true"} }) << ";\n";
  316. actionStream << printSlipperyMovementGuard(a, "South", 3, { a+"CannotSlipSouth"}) << printSlipperyMovementUpdate(a, "South", { {1, "true"} }) << ";\n";
  317. }
  318. void PrismModulesPrinter::printSlipperyMovementActionsForWest(const AgentName &a) {
  319. 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";
  320. 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";
  321. actionStream << printSlipperyMovementGuard(a, "West", 2, {"!"+a+"CannotSlipWest", a+"CannotSlipSouthWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a)}, {(1 - probIntended), westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  322. actionStream << printSlipperyMovementGuard(a, "West", 2, {"!"+a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a)}, {(1 - probIntended), westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  323. actionStream << printSlipperyMovementGuard(a, "West", 2, { a+"CannotSlipWest", a+"CannotSlipSouthWest", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+northUpdate(a) } }) << ";\n";
  324. actionStream << printSlipperyMovementGuard(a, "West", 2, {"!"+a+"CannotSlipWest", a+"CannotSlipSouthWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)} }) << ";\n";
  325. actionStream << printSlipperyMovementGuard(a, "West", 2, { a+"CannotSlipWest", "!"+a+"CannotSlipSouthWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  326. actionStream << printSlipperyMovementGuard(a, "West", 2, { a+"CannotSlipWest", a+"CannotSlipSouthWest", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", {}) << ";\n";
  327. actionStream << printSlipperyMovementGuard(a, "West", 3, {"!"+a+"CannotSlipNorth", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, northUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  328. actionStream << printSlipperyMovementGuard(a, "West", 3, { a+"CannotSlipNorth", "!"+a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+northUpdate(a)} }) << ";\n";
  329. actionStream << printSlipperyMovementGuard(a, "West", 3, {"!"+a+"CannotSlipNorth", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, northUpdate(a) } }) << ";\n";
  330. actionStream << printSlipperyMovementGuard(a, "West", 3, { a+"CannotSlipNorth", a+"CannotSlipNorthWest"}) << printSlipperyMovementUpdate(a, "West", {}) << ";\n";
  331. actionStream << printSlipperyMovementGuard(a, "West", 1, {"!"+a+"CannotSlipSouth", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, southUpdate(a) }, {1 - probIntended, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  332. actionStream << printSlipperyMovementGuard(a, "West", 1, { a+"CannotSlipSouth", "!"+a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, westUpdate(a)+"&"+southUpdate(a)} }) << ";\n";
  333. actionStream << printSlipperyMovementGuard(a, "West", 1, {"!"+a+"CannotSlipSouth", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", { {1, southUpdate(a) } }) << ";\n";
  334. actionStream << printSlipperyMovementGuard(a, "West", 1, { a+"CannotSlipSouth", a+"CannotSlipSouthWest"}) << printSlipperyMovementUpdate(a, "West", {}) << ";\n";
  335. actionStream << printSlipperyMovementGuard(a, "West", 0, {"!"+a+"CannotSlipWest"}) << printSlipperyMovementUpdate(a, "West", { {probIntended, westUpdate(a) }, {1 - probIntended, "true"} }) << ";\n";
  336. actionStream << printSlipperyMovementGuard(a, "West", 0, { a+"CannotSlipWest"}) << printSlipperyMovementUpdate(a, "West", {{1, "true"}}) << ";\n";
  337. }
  338. void PrismModulesPrinter::printSlipperyTurnActionsForNorth(const AgentName &a) {
  339. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!"+a+"CannotSlipNorth"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, northUpdate(a)} }) << ";\n";
  340. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { a+"CannotSlipNorth"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << ";\n";
  341. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipNorth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"-1)"}, {1 - probIntended, northUpdate(a)} }) << ";\n";
  342. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipNorth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=3)"}, {1 - probIntended, northUpdate(a)} }) << ";\n";
  343. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipNorth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"-1)"} }) << ";\n";
  344. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipNorth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=3)"} }) << ";\n";
  345. }
  346. void PrismModulesPrinter::printSlipperyTurnActionsForEast(const AgentName &a) {
  347. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!"+a+"CannotSlipEast"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, eastUpdate(a)} }) << ";\n";
  348. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { a+"CannotSlipEast"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << ";\n";
  349. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipEast"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"-1)"}, {1 - probIntended, eastUpdate(a)} }) << ";\n";
  350. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipEast"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=3)"}, {1 - probIntended, eastUpdate(a)} }) << ";\n";
  351. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipEast"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"-1)"} }) << ";\n";
  352. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipEast"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=3)"} }) << ";\n";
  353. }
  354. void PrismModulesPrinter::printSlipperyTurnActionsForSouth(const AgentName &a) {
  355. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!"+a+"CannotSlipSouth"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, southUpdate(a)} }) << ";\n";
  356. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { a+"CannotSlipSouth"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << ";\n";
  357. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipSouth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"-1)"}, {1 - probIntended, southUpdate(a)} }) << ";\n";
  358. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipSouth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=3)"}, {1 - probIntended, southUpdate(a)} }) << ";\n";
  359. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipSouth"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"-1)"} }) << ";\n";
  360. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipSouth"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=3)"} }) << ";\n";
  361. }
  362. void PrismModulesPrinter::printSlipperyTurnActionsForWest(const AgentName &a) {
  363. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, {"!"+a+"CannotSlipWest"}, "true") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=mod(view"+a+"+1,4))"}, { 1 - probIntended, westUpdate(a)} }) << ";\n";
  364. actionStream << printSlipperyTurnGuard(a, "right", RIGHT, { a+"CannotSlipWest"}, "true") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=mod(view"+a+"+1,4))"} }) << ";\n";
  365. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipWest"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=view"+a+"-1)"}, {1 - probIntended, westUpdate(a)} }) << ";\n";
  366. actionStream << printSlipperyTurnGuard(a, "left", LEFT, {"!"+a+"CannotSlipWest"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {probIntended, "(view"+a+"'=3)"}, {1 - probIntended, westUpdate(a)} }) << ";\n";
  367. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipWest"}, "view"+a+">0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=view"+a+"-1)"} }) << ";\n";
  368. actionStream << printSlipperyTurnGuard(a, "left", LEFT, { a+"CannotSlipWest"}, "view"+a+"=0") << printSlipperyTurnUpdate(a, { {1, "(view"+a+"'=3)"} }) << ";\n";
  369. }
  370. std::string PrismModulesPrinter::printSlipperyMovementGuard(const AgentName &a, const std::string &direction, const ViewDirection &viewDirection, const std::vector<std::string> &guards) {
  371. std::string actionName = "[" + a + "_move_" + viewDirectionToString.at(viewDirection) + "]";
  372. agentNameActionMap.at(a).insert({FORWARD, actionName});
  373. return " " + actionName + " " + viewVariable(a, viewDirection) + " & " + a + "IsOnSlippery" + direction + " & " + buildConjunction(a, guards) + " -> ";
  374. }
  375. std::string PrismModulesPrinter::printSlipperyMovementUpdate(const AgentName &a, const std::string &direction, const updates &u) const {
  376. return updatesToString(u);
  377. }
  378. std::string PrismModulesPrinter::printSlipperyTurnGuard(const AgentName &a, const std::string &direction, const ActionId &actionId, const std::vector<std::string> &guards, const std::string &cond) {
  379. std::string actionName = "[" + a + "_turn_" + direction + "]";
  380. agentNameActionMap.at(a).insert({actionId, actionName});
  381. return " " + actionName + " " + a + "IsOnSlippery & " + buildConjunction(a, guards) + " & " + cond + " -> ";
  382. }
  383. std::string PrismModulesPrinter::printSlipperyTurnUpdate(const AgentName &a, const updates &u) {
  384. return updatesToString(u);
  385. }
  386. void PrismModulesPrinter::printFaultyMovementModule(const AgentName &a) {
  387. os << "\nmodule " << a << "FaultyBehaviour" << std::endl;
  388. os << " previousAction" << a << " : [0.." + std::to_string(NOFAULT) + "];\n";
  389. std::set<size_t> exclude = {PICKUP, DROP, TOGGLE, DONE};
  390. for(const auto [actionId, actionName] : agentNameActionMap.at(a)) {
  391. if(exclude.count(actionId) > 0) continue;
  392. os << " " << actionName << faultyBehaviourGuard(a, actionId) << " -> " << faultyBehaviourUpdate(a, actionId) << ";\n";
  393. }
  394. os << "endmodule\n\n";
  395. }
  396. void PrismModulesPrinter::printMoveModule() {
  397. os << "\nmodule " << "Arbiter" << std::endl;
  398. os << " clock : [0.." << agentIndexMap.size() - 1 << "];\n";
  399. for(const auto [agentName, actions] : agentNameActionMap) {
  400. for(const auto [actionId, actionName] : actions) {
  401. os << " " << actionName << " " << moveGuard(agentName) << " -> " << moveUpdate(agentName) << ";\n";
  402. }
  403. }
  404. os << "endmodule\n\n";
  405. }
  406. void PrismModulesPrinter::printConfiguredActions(const AgentName &agentName) {
  407. for (auto& config : configuration) {
  408. if (config.type_ == ConfigType::Module && !config.overwrite_ && agentName == config.module_) {
  409. os << config.expression_ ;
  410. }
  411. }
  412. os << "\n";
  413. }
  414. void PrismModulesPrinter::printDoneActions(const AgentName &agentName) {
  415. os << " [" << agentName << "_on_goal]" << agentName << "IsOnGoal & clock=0 -> true;\n";
  416. }
  417. void PrismModulesPrinter::printPlayerStruct(const AgentName &agentName) {
  418. os << "player " << agentName << "\n\t";
  419. bool first = true;
  420. for(const auto [actionId, actionName] : agentNameActionMap.at(agentName)) {
  421. if(first) first = false;
  422. else os << ", ";
  423. os << actionName;
  424. }
  425. if(agentName == "Agent") os << ", [Agent_on_goal]";
  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();
  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. }