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.

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