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.

832 lines
51 KiB

11 months ago
  1. #include "PrismModulesPrinter.h"
  2. #include <map>
  3. #include <string>
  4. namespace prism {
  5. 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 AgentNameAndPositionMap &agentNameAndPositionMap, std::vector<Configuration> config, const bool enforceOneWays)
  6. : os(os), modelType(modelType), maxBoundaries(maxBoundaries), boxes(boxes), balls(balls), lockedDoors(lockedDoors), unlockedDoors(unlockedDoors), keys(keys), agentNameAndPositionMap(agentNameAndPositionMap), enforceOneWays(enforceOneWays), configuration(config), viewDirectionMapping({{0, "East"}, {1, "South"}, {2, "West"}, {3, "North"}}) {
  7. numberOfPlayer = agentNameAndPositionMap.size();
  8. }
  9. std::ostream& PrismModulesPrinter::printModelType(const ModelType &modelType) {
  10. switch(modelType) {
  11. case(ModelType::MDP):
  12. os << "mdp";
  13. break;
  14. case(ModelType::SMG):
  15. os << "smg";
  16. break;
  17. }
  18. os << "\n\n";
  19. return os;
  20. }
  21. std::ostream& PrismModulesPrinter::print() {
  22. for(const auto &key : keys) {
  23. printPortableObjectModule(key);
  24. }
  25. for(const auto &ball : balls) {
  26. printPortableObjectModule(ball);
  27. }
  28. for(const auto &box : boxes) {
  29. printPortableObjectModule(box);
  30. }
  31. for(const auto &door : unlockedDoors) {
  32. printDoorModule(door, true);
  33. }
  34. for(const auto &door : lockedDoors) {
  35. printDoorModule(door, false);
  36. }
  37. for(const auto [agentName, initialPosition] : agentNameAndPositionMap) {
  38. printRobotModule(agentName, initialPosition);
  39. }
  40. return os;
  41. }
  42. std::ostream& PrismModulesPrinter::printConfiguration(std::ostream& os, const std::vector<Configuration>& configurations) {
  43. for (auto& configuration : configurations) {
  44. if (configuration.overwrite_ || configuration.type_ == ConfigType::Module) {
  45. continue;
  46. }
  47. os << configuration.expression_ << std::endl;
  48. }
  49. return os;
  50. }
  51. std::ostream& PrismModulesPrinter::printConstants(std::ostream &os, const std::vector<std::string> &constants) {
  52. for (auto& constant : constants) {
  53. os << constant << std::endl;
  54. }
  55. return os;
  56. }
  57. std::ostream& PrismModulesPrinter::printBooleansForKeys(std::ostream &os, const AgentName &agentName, const cells &keys) {
  58. for(auto const& key : keys) {
  59. os << "\t" << agentName << "_has_"<< key.getColor() << "_key : bool;\n";//init false;\n";
  60. }
  61. os << "\n";
  62. return os;
  63. }
  64. std::ostream& PrismModulesPrinter::printBooleansForBackground(std::ostream &os, const AgentName &agentName, const std::map<Color, cells> &backgroundTiles) {
  65. for(auto const& [color, cells] : backgroundTiles) {
  66. if(cells.size() == 0) continue;
  67. std::string c = getColor(color);
  68. c.at(0) = std::toupper(c.at(0));
  69. os << "\t" << agentName << "_picked_up_" << c << " : bool init false;\n";
  70. }
  71. os << "\n";
  72. return os;
  73. }
  74. std::ostream& PrismModulesPrinter::printActionsForKeys(std::ostream &os, const AgentName &agentName, const cells &keys) {
  75. for(auto const& key : keys) { // TODO ADD Drop action and enforce that pickup only possible if pockets empty (nothing picked up already)
  76. os << "\n";
  77. std::string keyColor = key.getColor();
  78. os << "\t[pickup_" << keyColor << "_key]\t" << pickupGuard(agentName, keyColor) << "-> ";
  79. os << "(" << agentName << "_has_" << keyColor << "_key'=true) & (" << agentName << "_is_carrying_object'=true);\n";
  80. os << "\n";
  81. os << "\t[drop_" << keyColor << "_key_north]\t" << dropGuard(agentName, keyColor, 3) << "-> ";
  82. os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n";
  83. os << "\t[drop_" << keyColor << "_key_west]\t" << dropGuard(agentName, keyColor, 2) << "-> ";
  84. os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n";
  85. os << "\t[drop_" << keyColor << "_key_south]\t" << dropGuard(agentName, keyColor, 1) << "-> ";
  86. os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n";
  87. os << "\t[drop_" << keyColor << "_key_east]\t" << dropGuard(agentName, keyColor, 0) << "-> ";
  88. os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n";
  89. }
  90. return os;
  91. }
  92. std::string PrismModulesPrinter::pickupGuard(const AgentName &agentName, const std::string keyColor ) {
  93. return "!" + agentName + "_is_carrying_object &\t" + agentName + "CanPickUp" + keyColor + "Key ";
  94. }
  95. std::string PrismModulesPrinter::dropGuard(const AgentName &agentName, const std::string keyColor, size_t view) {
  96. return viewVariable(agentName, view, true) + "\t!" + agentName + "CannotMove" + viewDirectionMapping.at(view) + "\t&\t" + agentName + "_has_" + keyColor + "_key\t";
  97. }
  98. std::ostream& PrismModulesPrinter::printActionsForBackground(std::ostream &os, const AgentName &agentName, const std::map<Color, cells> &backgroundTiles) {
  99. for(auto const& [color, cells] : backgroundTiles) {
  100. if(cells.size() == 0) continue;
  101. std::string c = getColor(color);
  102. c.at(0) = std::toupper(c.at(0));
  103. os << "\t[" << agentName << "_pickup_" << c << "] " << agentName << "On" << c << " & !" << agentName << "_picked_up_" << c << " -> ";
  104. os << "(" << agentName << "_picked_up_" << c << "'=true);\n";
  105. }
  106. os << "\n";
  107. return os;
  108. }
  109. std::ostream& PrismModulesPrinter::printInitStruct(std::ostream &os, const AgentNameAndPositionMap &agents, const KeyNameAndPositionMap &keys, const cells &lockedDoors, const cells &unlockedDoors, prism::ModelType modelType) {
  110. os << "init\n";
  111. os << "\t";
  112. bool first = true;
  113. for (auto const& agent : agents) {
  114. if (first) first = false;
  115. else os << " & ";
  116. os << "(!" << agent.first << "IsInGoal & !" << agent.first << "IsInLava & !" << agent.first << "Done & !" << agent.first << "IsOnWall & ";
  117. os << "x" << agent.first << "=" << agent.second.second << " & y" << agent.first << "=" << agent.second.first << ")";
  118. os << " & !" << agent.first << "_is_carrying_object";
  119. if(enforceOneWays) {
  120. os << " & ( !AgentCannotTurn ) ";
  121. } else {
  122. // os << " & ( !AgentIsOnSlippery ) ";
  123. }
  124. for (auto const& key : keys) {
  125. os << " & ( !" << agent.first << "_has_" << key.first << "_key )";
  126. }
  127. }
  128. for (auto const& key : keys) {
  129. os << " & ( xKey" << key.first << "="<< key.second.second<< ")";
  130. os << " & ( yKey" << key.first << "=" << key.second.first << ")";
  131. }
  132. for (auto const& locked : lockedDoors) {
  133. os << " & (Door" << locked.getColor() << "locked & !Door" << locked.getColor() << "open)";
  134. }
  135. for (auto const& unlocked : unlockedDoors) {
  136. os << " & (!Door" << unlocked.getColor() << "locked & !Door" << unlocked.getColor() << "open)";
  137. }
  138. if (modelType == ModelType::SMG) {
  139. os << " & move=0";
  140. }
  141. os << "\nendinit\n\n";
  142. return os;
  143. }
  144. std::ostream& PrismModulesPrinter::printModule(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &boundaries, const coordinates& initialPosition, const cells &keys, const std::map<Color, cells> &backgroundTiles, const bool agentWithView, const std::vector<float> &probabilities, const double faultyProbability) {
  145. os << "module " << agentName << "\n";
  146. os << "\tx" << agentName << " : [1.." << boundaries.second << "];\n";
  147. os << "\ty" << agentName << " : [1.." << boundaries.first << "];\n";
  148. os << "\t" << agentName << "_is_carrying_object : bool;\n";
  149. printBooleansForKeys(os, agentName, keys);
  150. printBooleansForBackground(os, agentName, backgroundTiles);
  151. os << "\t" << agentName << "Done : bool;\n";
  152. if(agentWithView) {
  153. os << "\tview" << agentName << " : [0..3];\n";
  154. os << "\n";
  155. if (faultyProbability != 0.0) {
  156. os << "\t[" << agentName << "_turn_right] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " + 1, 4))" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " + 2, 4))" << ";\n";
  157. os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << ">1 -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " - 1, 4))" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=mod(view" << agentName << " - 2, 4))" << ";\n";
  158. os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << "=0 -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=3)" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=2)" << ";\n";
  159. os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << "=1 -> " << 100 - faultyProbability << "/100:" << "(view" << agentName << "'=0)" << moveUpdate(agentIndex) << "\n + " << faultyProbability << "/100:" << "(view" << agentName << "'=3)" << ";\n";
  160. } else {
  161. os << "\t[" << agentName << "_turn_right] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery -> (view" << agentName << "'=mod(view" << agentName << " + 1, 4)) " << moveUpdate(agentIndex) << ";\n";
  162. os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << ">0 -> (view" << agentName << "'=view" << agentName << " - 1) " << moveUpdate(agentIndex) << ";\n";
  163. os << "\t[" << agentName << "_turn_left] " << moveGuard(agentIndex) << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << " !" << agentName << "IsInGoal & !" << agentName << "IsInLava & !" << agentName << "IsOnSlippery & view" << agentName << "=0 -> (view" << agentName << "'=3) " << moveUpdate(agentIndex) << ";\n";
  164. }
  165. if(enforceOneWays) {
  166. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 0 & " << agentName << "CannotMoveEast -> true;\n";
  167. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 1 & " << agentName << "CannotMoveSouth -> true;\n";
  168. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 2 & " << agentName << "CannotMoveWest -> true;\n";
  169. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 3 & " << agentName << "CannotMoveNorth -> true;\n";
  170. }
  171. } else {
  172. os << "\t[" << agentName << "_turns] " << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << moveGuard(agentIndex) << " true -> (x" << agentName << "'=x" << agentName << ")" << moveUpdate(agentIndex) << ";\n";
  173. }
  174. printActionsForKeys(os, agentName, keys);
  175. printActionsForBackground(os, agentName, backgroundTiles);
  176. os << "\n";
  177. printMovementActions(os, agentName, agentIndex, agentWithView, 1.0, faultyProbability);
  178. for(auto const& probability : probabilities) {
  179. printMovementActions(os, agentName, agentIndex, agentWithView, probability);
  180. }
  181. printDoneActions(os, agentName, agentIndex);
  182. printConfiguredActions(os, agentName);
  183. os << "\n";
  184. return os;
  185. }
  186. void PrismModulesPrinter::printPortableObjectModule(const cell &object) {
  187. std::string identifier = capitalize(object.getColor()) + object.getType();
  188. os << "\nmodule " << identifier << std::endl;
  189. os << "\tx" << identifier << " : [-1.." << maxBoundaries.second << "] init " << object.column << ";\n";
  190. os << "\ty" << identifier << " : [-1.." << maxBoundaries.first << "] init " << object.row << ";\n";
  191. os << "\t" << identifier << "PickedUp : bool;\n";
  192. os << "\n";
  193. for(const auto [name, position] : agentNameAndPositionMap) {
  194. printPortableObjectActions(name, identifier);
  195. }
  196. os << "endmodule\n\n";
  197. }
  198. void PrismModulesPrinter::printPortableObjectActions(const std::string &agentName, const std::string &identifier) {
  199. os << "\t[" << agentName << "_pickup_" << identifier << "]\ttrue -> (x" << identifier << "'=-1) & (y" << identifier << "'=-1) & (" << identifier << "PickedUp'=true);\n";
  200. os << "\t[" << agentName << "_drop_" << identifier << "_north]\ttrue -> (x" << identifier << "'=x" << agentName << ") & (y" << identifier << "'=y" << agentName << "-1) & (" << identifier << "PickedUp'=false);\n";
  201. os << "\t[" << agentName << "_drop_" << identifier << "_west]\ttrue -> (x" << identifier << "'=x" << agentName << "-1) & (y" << identifier << "'=y" << agentName << ") & (" << identifier << "PickedUp'=false);\n";
  202. os << "\t[" << agentName << "_drop_" << identifier << "_south]\ttrue -> (x" << identifier << "'=x" << agentName << ") & (y" << identifier << "'=y" << agentName << "+1) & (" << identifier << "PickedUp'=false);\n";
  203. os << "\t[" << agentName << "_drop_" << identifier << "_east]\ttrue -> (x" << identifier << "'=x" << agentName << "+1) & (y" << identifier << "'=y" << agentName << ") & (" << identifier << "PickedUp'=false);\n";
  204. }
  205. void PrismModulesPrinter::printDoorModule(const cell &door, const bool &opened) {
  206. std::string identifier = capitalize(door.getColor()) + door.getType();
  207. os << "\nmodule " << identifier << std::endl;
  208. os << "\t" << identifier << "Open : bool init false;\n";
  209. os << "\n";
  210. if(opened) {
  211. for(const auto [name, position] : agentNameAndPositionMap) {
  212. printUnlockedDoorActions(name, identifier);
  213. }
  214. } else {
  215. for(const auto [name, position] : agentNameAndPositionMap) {
  216. printLockedDoorActions(name, identifier);
  217. }
  218. }
  219. os << "endmodule\n\n";
  220. }
  221. void PrismModulesPrinter::printLockedDoorActions(const std::string &agentName, const std::string &identifier) {
  222. os << "\t[" << agentName << "_unlock_" << identifier << "] !" << identifier << "Open -> (" << identifier << "Open'=true);\n";
  223. os << "\t[" << agentName << "_close_" << identifier << "] " << identifier << "Open -> (" << identifier << "Open'=false);\n";
  224. }
  225. void PrismModulesPrinter::printUnlockedDoorActions(const std::string &agentName, const std::string &identifier) {
  226. os << "\t[" << agentName << "_open_" << identifier << "] !" << identifier << "Open -> (" << identifier << "Open'=true);\n";
  227. os << "\t[" << agentName << "_close_" << identifier << "] " << identifier << "Open -> (" << identifier << "Open'=false);\n";
  228. }
  229. void PrismModulesPrinter::printRobotModule(const AgentName &agentName, const coordinates &initialPosition) {
  230. os << "\nmodule " << agentName << std::endl;
  231. os << "\tx" << agentName << " : [0.." << maxBoundaries.second << "] init " << initialPosition.second << ";\n";
  232. os << "\ty" << agentName << " : [0.." << maxBoundaries.first << "] init " << initialPosition.first << ";\n";
  233. os << "\tview" << agentName << " : [0..3] init 1;\n";
  234. for(const auto &door : unlockedDoors) {
  235. std::string identifier = capitalize(door.getColor()) + door.getType();
  236. printUnlockedDoorActionsForRobot(agentName, identifier);
  237. }
  238. for(const auto &door : lockedDoors) {
  239. std::string identifier = capitalize(door.getColor()) + door.getType();
  240. std::string key = capitalize(door.getColor()) + "Key";
  241. printLockedDoorActionsForRobot(agentName, identifier, key);
  242. }
  243. for(const auto &key : keys) {
  244. std::string identifier = capitalize(key.getColor()) + key.getType();
  245. os << "\t" << agentName << "Carrying" << identifier << " : bool init false;\n";
  246. printPortableObjectActionsForRobot(agentName, identifier);
  247. }
  248. os << "\n" << actionStream.str();
  249. os << "endmodule\n\n";
  250. }
  251. void PrismModulesPrinter::printPortableObjectActionsForRobot(const std::string &a, const std::string &i) {
  252. actionStream << "\t[" << a << "_pickup_" << i << "] " << a << "CannotMove" << i << " -> (" << a << "Carrying" << i << "'=true);\n";
  253. actionStream << "\t[" << a << "_drop_" << i << "_north]\t" << a << "Carrying" << i << " & view" << a << "=3 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveNorthWall -> (" << a << "Carrying" << i << "'=false);\n";
  254. actionStream << "\t[" << a << "_drop_" << i << "_west] \t" << a << "Carrying" << i << " & view" << a << "=2 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveWestWall -> (" << a << "Carrying" << i << "'=false);\n";
  255. actionStream << "\t[" << a << "_drop_" << i << "_south]\t" << a << "Carrying" << i << " & view" << a << "=1 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveSouthWall -> (" << a << "Carrying" << i << "'=false);\n";
  256. actionStream << "\t[" << a << "_drop_" << i << "_east] \t" << a << "Carrying" << i << " & view" << a << "=0 & !" << a << "CannotMoveConditionally & !" << a << "CannotMoveEastWall -> (" << a << "Carrying" << i << "'=false);\n";
  257. actionStream << "\n";
  258. }
  259. void PrismModulesPrinter::printUnlockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier) {
  260. actionStream << "\t[" << agentName << "_open_" << identifier << "] " << agentName << "CannotMove" << identifier << " -> true;\n";
  261. actionStream << "\t[" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " -> true;\n";
  262. actionStream << "\n";
  263. }
  264. void PrismModulesPrinter::printLockedDoorActionsForRobot(const std::string &agentName, const std::string &identifier, const std::string &key) {
  265. actionStream << "\t[" << agentName << "_unlock_" << identifier << "] " << agentName << "CannotMove" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n";
  266. actionStream << "\t[" << agentName << "_close_" << identifier << "] " << agentName << "IsNextTo" << identifier << " & " << agentName << "Carrying" << key << " -> true;\n";
  267. actionStream << "\n";
  268. }
  269. std::ostream& PrismModulesPrinter::printConfiguredActions(std::ostream &os, const AgentName &agentName) {
  270. for (auto& config : configuration) {
  271. if (config.type_ == ConfigType::Module && !config.overwrite_ && agentName == config.module_) {
  272. os << config.expression_ ;
  273. }
  274. }
  275. os << "\n";
  276. return os;
  277. }
  278. std::ostream& PrismModulesPrinter::printMovementActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const bool agentWithView, const float &probability, const double &stickyProbability) {
  279. if(stickyProbability != 0.0) {
  280. os << "\t[" << agentName << "_move_north]" << moveGuard(agentIndex) << viewVariable(agentName, 3, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveNorth -> " << (100 - stickyProbability) << "/100:" << "(y" << agentName << "'=y" << agentName << "-1)" << moveUpdate(agentIndex) << "\n+ " << (stickyProbability) << "/100:" << "(y" << agentName << "'=max(y" << agentName << "-2, 1 ))" << moveUpdate(agentIndex) << ";\n";
  281. os << "\t[" << agentName << "_move_east] " << moveGuard(agentIndex) << viewVariable(agentName, 0, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveEast -> " << (100 - stickyProbability) << "/100:" << "(x" << agentName << "'=x" << agentName << "+1)" << moveUpdate(agentIndex) << "\n+ " << (stickyProbability) << "/100:" << "(x" << agentName << "'=min(x" << agentName << "+2, width))" << moveUpdate(agentIndex) << ";\n";
  282. os << "\t[" << agentName << "_move_south]" << moveGuard(agentIndex) << viewVariable(agentName, 1, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveSouth -> " << (100 - stickyProbability) << "/100:" << "(y" << agentName << "'=y" << agentName << "+1)" << moveUpdate(agentIndex) << "\n+ " << (stickyProbability) << "/100:" << "(y" << agentName << "'=min(y" << agentName << "+2, height))" << moveUpdate(agentIndex) << ";\n";
  283. os << "\t[" << agentName << "_move_west] " << moveGuard(agentIndex) << viewVariable(agentName, 2, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveWest -> " << (100 - stickyProbability) << "/100:" << "(x" << agentName << "'=x" << agentName << "-1)" << moveUpdate(agentIndex) << "\n+ " << (stickyProbability) << "/100:" << "(x" << agentName << "'=max(x" << agentName << "-1, 1))" << moveUpdate(agentIndex) << ";\n";
  284. }
  285. else if(probability >= 1) {
  286. os << "\t[" << agentName << "_move_north]" << moveGuard(agentIndex) << viewVariable(agentName, 3, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveNorth -> (y" << agentName << "'=y" << agentName << "-1)" << moveUpdate(agentIndex) << ";\n";
  287. os << "\t[" << agentName << "_move_east] " << moveGuard(agentIndex) << viewVariable(agentName, 0, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveEast -> (x" << agentName << "'=x" << agentName << "+1)" << moveUpdate(agentIndex) << ";\n";
  288. os << "\t[" << agentName << "_move_south]" << moveGuard(agentIndex) << viewVariable(agentName, 1, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveSouth -> (y" << agentName << "'=y" << agentName << "+1)" << moveUpdate(agentIndex) << ";\n";
  289. os << "\t[" << agentName << "_move_west] " << moveGuard(agentIndex) << viewVariable(agentName, 2, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava &!" << agentName << "IsInGoal & !" << agentName << "CannotMoveWest -> (x" << agentName << "'=x" << agentName << "-1)" << moveUpdate(agentIndex) << ";\n";
  290. } else {
  291. std::string probabilityString = std::to_string(probability);
  292. std::string percentageString = std::to_string((int)(100 * probability));
  293. std::string complementProbabilityString = std::to_string(1 - probability);
  294. os << "\t[" << agentName << "_move_north_" << percentageString << "] ";
  295. os << moveGuard(agentIndex) << viewVariable(agentName, 3, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveNorth -> ";
  296. os << probabilityString << ": (y" << agentName << "'=y" << agentName << "-1)" << moveUpdate(agentIndex) << " + ";
  297. os << complementProbabilityString << ": (y" << agentName << "'=y" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  298. os << "\t[" << agentName << "_move_east_" << percentageString << "] ";
  299. os << moveGuard(agentIndex) << viewVariable(agentName, 0, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveEast -> ";
  300. os << probabilityString << ": (x" << agentName << "'=x" << agentName << "+1)" << moveUpdate(agentIndex) << " + ";
  301. os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  302. os << "\t[" << agentName << "_move_south_" << percentageString << "] ";
  303. os << moveGuard(agentIndex) << viewVariable(agentName, 1, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveSouth -> ";
  304. os << probabilityString << ": (y" << agentName << "'=y" << agentName << "+1)" << moveUpdate(agentIndex) << " + ";
  305. os << complementProbabilityString << ": (y" << agentName << "'=y" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  306. os << "\t[" << agentName << "_move_west_" << percentageString << "] ";
  307. os << moveGuard(agentIndex) << viewVariable(agentName, 2, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveWest -> ";
  308. os << probabilityString << ": (x" << agentName << "'=x" << agentName << "-1)" << moveUpdate(agentIndex) << " + ";
  309. os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  310. }
  311. return os;
  312. }
  313. std::ostream& PrismModulesPrinter::printDoneActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex) {
  314. os << "\t[" << agentName << "_done]" << moveGuard(agentIndex) << agentName << "IsInGoal | " << agentName << "IsInLava -> (" << agentName << "Done'=true);\n";
  315. return os;
  316. }
  317. std::ostream& PrismModulesPrinter::printSlipperyTurn(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &c, std::set<std::string> &slipperyActions, const std::array<bool, 8>& neighborhood, SlipperyType orientation) {
  318. constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 9;
  319. std::array<std::string, ALL_POSS_DIRECTIONS> positionTransition = {
  320. /* north */ "(y" + agentName + "'=y" + agentName + "-1)",
  321. /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)",
  322. /* east */ "(x" + agentName + "'=x" + agentName + "+1)",
  323. /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)",
  324. /* south */ "(y" + agentName + "'=y" + agentName + "+1)",
  325. /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)",
  326. /* west */ "(x" + agentName + "'=x" + agentName + "-1)",
  327. /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)",
  328. /* own position */ "(x" + agentName + "'=x" + agentName + ") & (y" + agentName + "'=y" + agentName + ")"
  329. };
  330. // view transition appdx in form (guard, update part)
  331. // IMPORTANT: No mod() usage for turn left due to bug in mod() function for decrement
  332. std::array<std::tuple<std::string, std::string, std::string>, 3> viewTransition = {
  333. std::make_tuple(" & " + agentName + "SlipperyTurnRightAllowed ", " & (view" + agentName + "'=mod(view" + agentName + " + 1, 4))", "_right]"),
  334. std::make_tuple(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + ">0", " & (view" + agentName + "'=view" + agentName + " - 1)", "_left]"),
  335. std::make_tuple(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + "=0", " & (view" + agentName + "'=3)", "_left]")
  336. };
  337. // direction specifics
  338. std::string actionName;
  339. std::string positionGuard;
  340. std::size_t remainPosIndex = 8;
  341. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir; // { n, ne, e, se, s, sw, w, nw, CURRENT POS }
  342. std::array<std::string, ALL_POSS_DIRECTIONS> prob_piece_dir_constants;
  343. switch (orientation)
  344. {
  345. case SlipperyType::North:
  346. actionName = "\t[" + agentName + "turn_at_slip_north";
  347. positionGuard = "\t" + agentName + "IsOnSlipperyNorth";
  348. prob_piece_dir = { 0, 0, 0, 0, 1, 0, 0, 0, 0 /* <- R */ };
  349. prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_turn_displacement" /* <- R */, "prop_zero", "prop_zero", "prop_zero","prop_zero" };
  350. break;
  351. case SlipperyType::South:
  352. actionName = "\t[" + agentName + "turn_at_slip_south";
  353. positionGuard = "\t" + agentName + "IsOnSlipperySouth";
  354. prob_piece_dir = { 1, 0, 0, 0, 0, 0, 0, 0, 0 /* <- R */ };
  355. prob_piece_dir_constants = { "prop_turn_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" };
  356. break;
  357. case SlipperyType::East:
  358. actionName = "\t[" + agentName + "turn_at_slip_east";
  359. positionGuard = "\t" + agentName + "IsOnSlipperyEast";
  360. prob_piece_dir = { 0, 0, 0, 0, 0, 0, 1, 0, 0 /* <- R */ };
  361. prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_turn_displacement", "prop_zero", "prop_zero" };
  362. break;
  363. case SlipperyType::West:
  364. actionName = "\t[" + agentName + "turn_at_slip_west";
  365. positionGuard = "\t" + agentName + "IsOnSlipperyWest";
  366. prob_piece_dir = { 0, 0, 1, 0, 0, 0, 0, 0, 0 /* <- R */ };
  367. prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_turn_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" };
  368. break;
  369. }
  370. slipperyActions.insert(actionName + "_left]");
  371. slipperyActions.insert(actionName + "_right]");
  372. // override probability to 0 if corresp. direction is blocked
  373. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS - 1; i++) {
  374. if (!neighborhood.at(i))
  375. prob_piece_dir.at(i) = 0;
  376. }
  377. // determine residual probability (R) by replacing 0 with (1 - overall sum)
  378. prob_piece_dir.at(remainPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0);
  379. prob_piece_dir_constants.at(remainPosIndex) = "prop_turn_intended";
  380. // <DEBUG_AREA>
  381. {
  382. assert(prob_piece_dir.at(remainPosIndex) <= 9 && prob_piece_dir.at(remainPosIndex) >= 6 && "Value not in Range!");
  383. assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!");
  384. }
  385. // </DEBUG_AREA>
  386. // generic output (for every view transition)
  387. for (std::size_t v = 0; v < viewTransition.size(); v++) {
  388. os << actionName << std::get<2>(viewTransition.at(v)) << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << std::get<0>(viewTransition.at(v));
  389. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  390. if (i == remainPosIndex) {
  391. os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants.at(i) << " : " << positionTransition.at(i) << std::get<1>(viewTransition.at(v)) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  392. } else {
  393. os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  394. }
  395. }
  396. }
  397. return os;
  398. }
  399. std::ostream& PrismModulesPrinter::printSlipperyMove(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const coordinates &c, std::set<std::string> &slipperyActions, const std::array<bool, 8>& neighborhood, SlipperyType orientation) {
  400. constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 8;
  401. std::array<std::string, ALL_POSS_DIRECTIONS> positionTransition = {
  402. /* north */ "(y" + agentName + "'=y" + agentName + "-1)",
  403. /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)",
  404. /* east */ "(x" + agentName + "'=x" + agentName + "+1)",
  405. /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)",
  406. /* south */ "(y" + agentName + "'=y" + agentName + "+1)",
  407. /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)",
  408. /* west */ "(x" + agentName + "'=x" + agentName + "-1)",
  409. /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)"
  410. };
  411. // direction specifics
  412. std::size_t straightPosIndex, straightPosIndex_east, straightPosIndex_south, straightPosIndex_west, straightPosIndex_north;
  413. std::string actionName, specialTransition; // if straight ahead is blocked
  414. std::string positionGuard;
  415. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir; // { n, ne, e, se, s, sw, w, nw }
  416. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir_agent_north; // { n, ne, e, se, s, sw, w, nw }
  417. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir_agent_east; // { n, ne, e, se, s, sw, w, nw }
  418. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir_agent_south; // { n, ne, e, se, s, sw, w, nw }
  419. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir_agent_west; // { n, ne, e, se, s, sw, w, nw }
  420. std::array<std::string, ALL_POSS_DIRECTIONS> prob_piece_dir_constants;
  421. std::array<std::string, ALL_POSS_DIRECTIONS> prob_piece_dir_constants_agent_north;
  422. std::array<std::string, ALL_POSS_DIRECTIONS> prob_piece_dir_constants_agent_east;
  423. std::array<std::string, ALL_POSS_DIRECTIONS> prob_piece_dir_constants_agent_south;
  424. std::array<std::string, ALL_POSS_DIRECTIONS> prob_piece_dir_constants_agent_west;
  425. switch (orientation)
  426. {
  427. case SlipperyType::North:
  428. actionName = "\t[" + agentName + "move_on_slip_north]";
  429. positionGuard = "\t" + agentName + "IsOnSlipperyNorth";
  430. prob_piece_dir = { 0, 0, 1, 2, 0 /* <- R */, 2, 1, 0 };
  431. prob_piece_dir_agent_south = { 0 , 0, 0, 1, 0 /*s <- R */, 1, 0, 0};
  432. prob_piece_dir_agent_east = { 0, 0, 0 /*e <- R */, 2, 0, 0, 0, 0 };
  433. prob_piece_dir_agent_north = { 0 /*n <- R */, 0, 0, 0, 2 , 0, 0, 0 };
  434. prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 2, 0 /* <- R */, 0 };
  435. prob_piece_dir_constants = { "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero" };
  436. prob_piece_dir_constants_agent_north = { "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_zero", "prop_zero" };
  437. prob_piece_dir_constants_agent_east = { "prop_zero", "prop_zero", "prop_zero", "prop_displacement", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" };
  438. prob_piece_dir_constants_agent_south = { "prop_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ;
  439. prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement", "prop_zero", "prop_zero" } ;
  440. straightPosIndex = 4;
  441. straightPosIndex_east = 2;
  442. straightPosIndex_south = 4;
  443. straightPosIndex_west = 6;
  444. straightPosIndex_north = 0;
  445. specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)");
  446. break;
  447. case SlipperyType::South:
  448. actionName = "\t[" + agentName + "move_on_slip_south]";
  449. positionGuard = "\t" + agentName + "IsOnSlipperySouth";
  450. prob_piece_dir = { 0 /* <- R */, 2, 1, 0, 0, 0, 1, 2 };
  451. // { n, ne, e, se, s, sw, w, nw }
  452. prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1};
  453. prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 };
  454. prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 };
  455. prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 };
  456. prob_piece_dir_constants = { "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement" };
  457. prob_piece_dir_constants_agent_north = { "prop_zero", "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_displacement * 1/2" };
  458. prob_piece_dir_constants_agent_east = { "prop_zero", "prop_displacement", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" };
  459. prob_piece_dir_constants_agent_south = { "prop_displacement", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ;
  460. prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_displacement" } ;
  461. straightPosIndex = 0; // always north
  462. straightPosIndex_east = 2;
  463. straightPosIndex_south = 4;
  464. straightPosIndex_west = 6;
  465. straightPosIndex_north = 0;
  466. specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)");
  467. break;
  468. case SlipperyType::East:
  469. actionName = "\t[" + agentName + "move_on_slip_east]";
  470. positionGuard = "\t" + agentName + "IsOnSlipperyEast";
  471. // { n, ne, e, se, s, sw, w, nw }
  472. prob_piece_dir = { 1, 0, 0, 0, 1, 2, 0 /* <- R */, 2 };
  473. // TODO
  474. prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1};
  475. prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 };
  476. prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 };
  477. prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 };
  478. prob_piece_dir_constants = { "prop_displacement * 1/2", "prop_zero", "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement" };
  479. prob_piece_dir_constants_agent_north = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2" };
  480. prob_piece_dir_constants_agent_east = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_displacement", "prop_zero" };
  481. prob_piece_dir_constants_agent_south = { "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero" } ;
  482. prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_zero", "prop_zero", "prop_zero" /* <- R */, "prop_displacement * 1/2", "prop_zero", "prop_displacement * 1/2" } ;
  483. straightPosIndex = 6;
  484. straightPosIndex_east = 2;
  485. straightPosIndex_south = 4;
  486. straightPosIndex_west = 6;
  487. straightPosIndex_north = 0;
  488. specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)");
  489. break;
  490. case SlipperyType::West:
  491. actionName = "\t[" + agentName + "move_on_slip_west]";
  492. positionGuard = "\t" + agentName + "IsOnSlipperyWest";
  493. prob_piece_dir = { 1, 2, 0 /* <- R */, 2, 1, 0, 0, 0 };
  494. // TODO
  495. // { n, ne, e, se, s, sw, w, nw }
  496. prob_piece_dir_agent_north = { 0 /*n <- R */, 1, 0, 0, 0, 0, 0, 1};
  497. prob_piece_dir_agent_east = { 0, 2, 0 /*e <- R */, 0, 0, 0, 0, 0 };
  498. prob_piece_dir_agent_south = { 2, 0, 0, 0, 0 /*s <- R */, 0, 0, 0 };
  499. prob_piece_dir_agent_west = { 0, 0, 0, 0, 0, 0, 0 /* <- R */, 2 };
  500. prob_piece_dir_constants = {"prop_displacement * 1/2", "prop_displacement", "prop_zero" /* <- R */, "prop_displacement", "prop_displacement * 1/2", "prop_zero","prop_zero", "prop_zero" };
  501. prob_piece_dir_constants_agent_north = { "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" };
  502. prob_piece_dir_constants_agent_east = { "prop_zero", "prop_displacement * 1/2", "prop_zero", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" };
  503. prob_piece_dir_constants_agent_south = { "prop_zero", "prop_zero", "prop_displacement * 1/2", "prop_displacement * 1/2", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ;
  504. prob_piece_dir_constants_agent_west ={ "prop_zero", "prop_zero", "prop_displacement", "prop_zero", "prop_zero" /* <- R */, "prop_zero", "prop_zero", "prop_zero" } ;
  505. straightPosIndex = 2;
  506. straightPosIndex_east = 2;
  507. straightPosIndex_south = 4;
  508. straightPosIndex_west = 6;
  509. straightPosIndex_north = 0;
  510. specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)");
  511. break;
  512. }
  513. slipperyActions.insert(actionName);
  514. // override probability to 0 if corresp. direction is blocked
  515. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  516. if (!neighborhood.at(i))
  517. prob_piece_dir.at(i) = 0;
  518. }
  519. // determine residual probability (R) by replacing 0 with (1 - overall sum)
  520. if(enforceOneWays) {
  521. prob_piece_dir = {0,0,0,0,0,0,0,0};
  522. prob_piece_dir_constants = {"zero","zero","zero","zero","zero","zero","zero","zero"};
  523. }
  524. prob_piece_dir.at(straightPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0);
  525. prob_piece_dir_constants.at(straightPosIndex) = "prop_intended";
  526. prob_piece_dir_constants_agent_east.at(straightPosIndex_east) = "prop_intended";
  527. prob_piece_dir_constants_agent_south.at(straightPosIndex_south) = "prop_intended";
  528. prob_piece_dir_constants_agent_west.at(straightPosIndex_west) = "prop_intended";
  529. prob_piece_dir_constants_agent_north.at(straightPosIndex_north) = "prop_intended";
  530. // <DEBUG_AREA>
  531. {
  532. assert(prob_piece_dir.at(straightPosIndex) <= 9 && prob_piece_dir.at(straightPosIndex) >= 3 && "Value not in Range!");
  533. assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!");
  534. assert(orientation != SlipperyType::North || (prob_piece_dir.at(7) == 0 && prob_piece_dir.at(0) == 0 && prob_piece_dir.at(1) == 0 && "Slippery up should be impossible!"));
  535. assert(orientation != SlipperyType::South || (prob_piece_dir.at(3) == 0 && prob_piece_dir.at(4) == 0 && prob_piece_dir.at(5) == 0 && "Slippery down should be impossible!"));
  536. assert(orientation != SlipperyType::East || (prob_piece_dir.at(1) == 0 && prob_piece_dir.at(2) == 0 && prob_piece_dir.at(3) == 0 && "Slippery right should be impossible!"));
  537. assert(orientation != SlipperyType::West || (prob_piece_dir.at(5) == 0 && prob_piece_dir.at(6) == 0 && prob_piece_dir.at(7) == 0 && "Slippery left should be impossible!"));
  538. }
  539. // </DEBUG_AREA>
  540. // special case: straight forward is blocked (then remain in same position)
  541. positionTransition.at(straightPosIndex) = specialTransition;
  542. // generic output (for every view and every possible view direction)
  543. size_t current_dir = 0; // East
  544. os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir;
  545. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  546. os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_east.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  547. }
  548. current_dir = 1; // South
  549. os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir;
  550. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  551. os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_south.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  552. }
  553. current_dir = 2; // West
  554. os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir;
  555. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  556. os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_west.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  557. }
  558. current_dir = 3; // North
  559. os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed " << "& " << "view" << agentName << "=" << current_dir;
  560. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  561. os << (i == 0 ? " -> " : " + ") << prob_piece_dir_constants_agent_north.at(i) << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  562. }
  563. return os;
  564. }
  565. std::ostream& PrismModulesPrinter::printEndmodule(std::ostream &os) {
  566. os << "endmodule\n";
  567. os << "\n";
  568. return os;
  569. }
  570. std::ostream& PrismModulesPrinter::printPlayerStruct(std::ostream &os, const AgentName &agentName, const bool agentWithView, const std::vector<float> &probabilities, const std::set<std::string> &slipperyActions) {
  571. os << "player " << agentName << "\n\t";
  572. bool first = true;
  573. std::list<std::string> allActions = { "_move_north", "_move_east", "_move_south", "_move_west" };
  574. std::list<std::string> movementActions = allActions;
  575. for(auto const& probability : probabilities) {
  576. std::string percentageString = std::to_string((int)(100 * probability));
  577. for(auto const& movement : movementActions) {
  578. allActions.push_back(movement + "_" + percentageString);
  579. }
  580. }
  581. if(agentWithView) {
  582. allActions.push_back("_turn_left");
  583. allActions.push_back("_turn_right");
  584. } else {
  585. allActions.push_back("_turns");
  586. }
  587. for(auto const& action : allActions) {
  588. if(first) first = false; else os << ", ";
  589. os << "[" << agentName << action << "]";
  590. }
  591. for(auto const& action : slipperyActions) {
  592. os << ", " << action;
  593. }
  594. os << ", [" << agentName << "_done]";
  595. os << "\nendplayer\n";
  596. return os;
  597. }
  598. std::ostream& PrismModulesPrinter::printGlobalMoveVariable(std::ostream &os, const size_t &numberOfPlayer) {
  599. os << "\nglobal move : [0.." << std::to_string(numberOfPlayer - 1) << "];\n\n";
  600. return os;
  601. }
  602. std::ostream& PrismModulesPrinter::printRewards(std::ostream &os, const AgentName &agentName, const std::map<coordinates, float> &stateRewards, const cells &lava, const cells &goals, const std::map<Color, cells> &backgroundTiles) {
  603. if(lava.size() != 0) {
  604. os << "rewards \"" << agentName << "SafetyNoBFS\"\n";
  605. os << "\t" <<agentName << "IsInLavaAndNotDone: -100;\n";
  606. os << "endrewards\n";
  607. }
  608. if (!goals.empty() || !lava.empty()) {
  609. os << "rewards \"" << agentName << "SafetyNoBFSAndGoal\"\n";
  610. if(goals.size() != 0) os << "\t" << agentName << "IsInGoalAndNotDone: 100;\n";
  611. if(lava.size() != 0) os << "\t" << agentName << "IsInLavaAndNotDone: -100;\n";
  612. os << "endrewards\n";
  613. }
  614. os << "rewards \"" << agentName << "Time\"\n";
  615. os << "\t!" << agentName << "IsInGoal : -1;\n";
  616. if(goals.size() != 0) os << "\t" << agentName << "IsInGoalAndNotDone: 100;\n";
  617. if(lava.size() != 0) os << "\t" << agentName << "IsInLavaAndNotDone: -100;\n";
  618. os << "endrewards\n";
  619. if(stateRewards.size() > 0) {
  620. os << "rewards \"" << agentName << "SafetyWithBFS\"\n";
  621. if(lava.size() != 0) os << "\t" << agentName << "IsInLavaAndNotDone: -100;\n";
  622. for(auto const [coordinates, reward] : stateRewards) {
  623. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  624. }
  625. os << "endrewards\n";
  626. }
  627. if(stateRewards.size() > 0) {
  628. os << "rewards \"" << agentName << "SafetyWithBFSAndGoal\"\n";
  629. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  630. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  631. for(auto const [coordinates, reward] : stateRewards) {
  632. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  633. }
  634. os << "endrewards\n";
  635. }
  636. for(auto const entry : backgroundTiles)
  637. {
  638. std::cout << getColor(entry.first) << " ";
  639. for(auto const cell : entry.second){
  640. std::cout << cell.getCoordinates().first << " " << cell.getCoordinates().second << std::endl;
  641. }
  642. }
  643. if(backgroundTiles.size() > 0) {
  644. os << "rewards \"TaxiReward\"\n";
  645. os << "\t!AgentIsInGoal : -1;\n";
  646. std::string allPassengersPickedUp = "";
  647. bool first = true;
  648. for(auto const [color, cells] : backgroundTiles) {
  649. if(cells.size() == 0) continue;
  650. if(first) first = false; else allPassengersPickedUp += "&";
  651. std::string c = getColor(color);
  652. c.at(0) = std::toupper(c.at(0));
  653. std::string visitedLabel = agentName + "_picked_up_" + c;
  654. allPassengersPickedUp += visitedLabel;
  655. os << "[" << agentName << "_pickup_" << c << "] true : 100;\n";
  656. }
  657. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & " << allPassengersPickedUp << " : 100;\n";
  658. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & !(" << allPassengersPickedUp << ") : -100;\n";
  659. os << "endrewards";
  660. }
  661. return os;
  662. }
  663. std::string PrismModulesPrinter::moveGuard(const size_t &agentIndex) {
  664. return isGame() ? " move=" + std::to_string(agentIndex) + " & " : " ";
  665. }
  666. std::string PrismModulesPrinter::moveUpdate(const size_t &agentIndex) {
  667. return isGame() ?
  668. (agentIndex == numberOfPlayer - 1) ?
  669. " & (move'=0) " :
  670. " & (move'=" + std::to_string(agentIndex + 1) + ") " :
  671. "";
  672. }
  673. std::string PrismModulesPrinter::viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView) {
  674. return agentWithView ? " view" + agentName + "=" + std::to_string(agentDirection) + " & " : " ";
  675. }
  676. bool PrismModulesPrinter::isGame() const {
  677. return modelType == ModelType::SMG;
  678. }
  679. }