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

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