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.

681 lines
35 KiB

  1. #include "PrismModulesPrinter.h"
  2. #include <map>
  3. #include <string>
  4. namespace prism {
  5. PrismModulesPrinter::PrismModulesPrinter(const ModelType &modelType, const size_t &numberOfPlayer, const bool enforceOneWays)
  6. : modelType(modelType), numberOfPlayer(numberOfPlayer), enforceOneWays(enforceOneWays) {
  7. }
  8. std::ostream& PrismModulesPrinter::printModel(std::ostream &os, const ModelType &modelType) {
  9. switch(modelType) {
  10. case(ModelType::MDP):
  11. os << "mdp";
  12. break;
  13. case(ModelType::SMG):
  14. os << "smg";
  15. break;
  16. }
  17. os << "\n\n";
  18. return os;
  19. }
  20. std::ostream& PrismModulesPrinter::printBackgroundLabels(std::ostream &os, const AgentName &agentName, const std::pair<Color, cells> &backgroundTiles) {
  21. if(backgroundTiles.second.size() == 0) return os;
  22. bool first = true;
  23. std::string color = getColor(backgroundTiles.first);
  24. color.at(0) = std::toupper(color.at(0));
  25. os << "formula " << agentName << "On" << color << " = ";
  26. for(auto const& cell : backgroundTiles.second) {
  27. if(first) first = false; else os << " | ";
  28. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  29. }
  30. os << ";\n";
  31. os << "label \"" << agentName << "On" << color << "\" = " << agentName << "On" << color << ";\n";
  32. return os;
  33. }
  34. std::ostream& PrismModulesPrinter::printRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &cells) {
  35. bool first = true;
  36. os << "formula " << agentName << "CannotMove" << direction << " = " ;
  37. for(auto const& cell : cells) {
  38. if(first) first = false;
  39. else os << " | ";
  40. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  41. }
  42. os << ";\n";
  43. return os;
  44. }
  45. std::ostream& PrismModulesPrinter::printIsOnSlipperyFormula(std::ostream& os, const AgentName &agentName, const std::vector<std::reference_wrapper<cells>> &slipperyCollection, const cells &slipperyNorth, const cells &slipperyEast, const cells &slipperySouth, const cells &slipperyWest) {
  46. if(std::find_if(slipperyCollection.cbegin(), slipperyCollection.cend(), [=](const std::reference_wrapper<cells>& c) { return !c.get().empty(); }) == slipperyCollection.cend()) {
  47. os << "formula " << agentName << "IsOnSlippery = false;\n";
  48. return os;
  49. }
  50. bool first = true;
  51. os << "formula " << agentName << "IsOnSlippery = ";
  52. for (const auto& slippery: slipperyCollection) {
  53. for(const auto& cell : slippery.get()) {
  54. if(first) first = false; else os << " | ";
  55. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  56. }
  57. }
  58. os << ";\n";
  59. if(enforceOneWays) {
  60. first = true;
  61. os << "formula " << agentName << "IsOnSlipperyNorth = ";
  62. for (const auto& cell: slipperyNorth) {
  63. if(first) first = false; else os << " | ";
  64. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  65. }
  66. os << ";\n";
  67. first = true;
  68. os << "formula " << agentName << "IsOnSlipperyEast = ";
  69. for (const auto& cell: slipperyEast) {
  70. if(first) first = false; else os << " | ";
  71. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  72. }
  73. os << ";\n";
  74. first = true;
  75. os << "formula " << agentName << "IsOnSlipperySouth = ";
  76. for (const auto& cell: slipperySouth) {
  77. if(first) first = false; else os << " | ";
  78. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  79. }
  80. os << ";\n";
  81. first = true;
  82. os << "formula " << agentName << "IsOnSlipperyWest = ";
  83. for (const auto& cell: slipperyWest) {
  84. if(first) first = false; else os << " | ";
  85. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  86. }
  87. os << ";\n";
  88. }
  89. return os;
  90. }
  91. std::ostream& PrismModulesPrinter::printIsInLavaFormula(std::ostream& os, const AgentName &agentName, const cells &lava) {
  92. if(lava.size() == 0) {
  93. os << "formula " << agentName << "IsInLava = false;\n";
  94. return os;
  95. }
  96. bool first = true;
  97. os << "formula " << agentName << "IsInLava = ";
  98. for(auto const& cell : lava) {
  99. if(first) first = false; else os << " | ";
  100. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  101. }
  102. os << ";\n";
  103. os << "formula " << agentName << "IsInLavaAndNotDone = " << agentName << "IsInLava & !" << agentName << "Done;\n";
  104. os << "label \"" << agentName << "IsInLavaAndNotDone\" = " << agentName << "IsInLava & !" << agentName << "Done;\n";
  105. return os;
  106. }
  107. std::ostream& PrismModulesPrinter::printTurningNotAllowedFormulas(std::ostream& os, const AgentName &agentName, const cells &noTurnFloor) {
  108. if( (!enforceOneWays or noTurnFloor.size() == 0) or (noTurnFloor.size() == 0) ) {
  109. os << "formula " << agentName << "CannotTurn = false;\n";
  110. return os;
  111. }
  112. bool first = true;
  113. os << "formula " << agentName << "CannotTurn = ";
  114. for(auto const& cell : noTurnFloor) {
  115. if(first) first = false; else os << " | ";
  116. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  117. }
  118. os << " | " << agentName << "IsOnSlippery;\n";
  119. return os;
  120. }
  121. std::ostream& PrismModulesPrinter::printIsFixedFormulas(std::ostream& os, const AgentName &agentName) {
  122. os << "formula " << agentName << "IsFixed = false;\n";
  123. os << "formula " << agentName << "SlipperyTurnLeftAllowed = true;\n";
  124. os << "formula " << agentName << "SlipperyTurnRightAllowed = true;\n";
  125. os << "formula " << agentName << "SlipperyMoveForwardAllowed = true;\n";
  126. os << "label \"FixedStates\" = " << agentName << "IsFixed | !" << agentName << "SlipperyTurnRightAllowed | !" << agentName << "SlipperyTurnLeftAllowed | !" << agentName << "SlipperyMoveForwardAllowed | " << agentName << "IsInGoal | " << agentName << "IsInLava";
  127. if(enforceOneWays) {
  128. os << " | " << agentName << "CannotTurn";
  129. }
  130. os << ";\n";
  131. //os << "label \"FixedStates\" = " << agentName << "IsFixed | " << agentName << "IsOnSlippery | " << agentName << "IsInGoal | " << agentName << "IsInLava;\n";
  132. return os;
  133. }
  134. std::ostream& PrismModulesPrinter::printWallFormula(std::ostream& os, const AgentName &agentName, const cells &walls) {
  135. os << "formula " << agentName << "IsOnWall = ";
  136. bool first = true;
  137. for(auto const& cell : walls) {
  138. if(first) first = false; else os << " | ";
  139. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  140. }
  141. os << ";\n";
  142. return os;
  143. }
  144. std::ostream& PrismModulesPrinter::printFormulas(std::ostream& os,
  145. const AgentName &agentName,
  146. const cells &restrictionNorth,
  147. const cells &restrictionEast,
  148. const cells &restrictionSouth,
  149. const cells &restrictionWest,
  150. const std::vector<std::reference_wrapper<cells>> &slipperyCollection,
  151. const cells &lava,
  152. const cells &walls,
  153. const cells &noTurnFloor,
  154. const cells &slipperyNorth,
  155. const cells &slipperyEast,
  156. const cells &slipperySouth,
  157. const cells &slipperyWest) {
  158. printRestrictionFormula(os, agentName, "North", restrictionNorth);
  159. printRestrictionFormula(os, agentName, "East ", restrictionEast);
  160. printRestrictionFormula(os, agentName, "South", restrictionSouth);
  161. printRestrictionFormula(os, agentName, "West ", restrictionWest);
  162. printIsOnSlipperyFormula(os, agentName, slipperyCollection, slipperyNorth, slipperyEast, slipperySouth, slipperyWest);
  163. printIsInLavaFormula(os, agentName, lava);
  164. printWallFormula(os, agentName, walls);
  165. printTurningNotAllowedFormulas(os, agentName, noTurnFloor);
  166. printIsFixedFormulas(os, agentName);
  167. os << "\n";
  168. return os;
  169. }
  170. std::ostream& PrismModulesPrinter::printGoalLabel(std::ostream& os, const AgentName &agentName, const cells &goals) {
  171. if(goals.size() == 0) {
  172. os << "formula " << agentName << "IsInGoal = false;\n";
  173. return os;
  174. }
  175. bool first = true;
  176. os << "formula " << agentName << "IsInGoal = ";
  177. for(auto const& cell : goals) {
  178. if(first) first = false; else os << " | ";
  179. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  180. }
  181. os << ";\n";
  182. os << "formula " << agentName << "IsInGoalAndNotDone = " << agentName << "IsInGoal & !" << agentName << "Done;\n";
  183. os << "label \"" << agentName << "IsInGoalAndNotDone\" = " << agentName << "IsInGoal & !" << agentName << "Done;\n";
  184. return os;
  185. }
  186. std::ostream& PrismModulesPrinter::printCrashLabel(std::ostream &os, const std::vector<AgentName> agentNames) {
  187. os << "label crash = ";
  188. bool first = true;
  189. for(auto const& agentName : agentNames) {
  190. if(agentName == "Agent") continue;
  191. if(first) first = false; else os << " | ";
  192. os << "(xAgent=x" << agentName << ")&(yAgent=y" << agentName << ")";
  193. }
  194. os << ";\n\n";
  195. return os;
  196. }
  197. std::ostream& PrismModulesPrinter::printAvoidanceLabel(std::ostream &os, const std::vector<AgentName> agentNames, const int &distance) {
  198. os << "label avoidance = ";
  199. bool first = true;
  200. for(auto const& agentName : agentNames) {
  201. if(agentName == "Agent") continue;
  202. if(first) first = false; else os << " | ";
  203. os << "max(xAgent-x" << agentName << ",x" << agentName << "-xAgent)+";
  204. os << "max(yAgent-y" << agentName << ",y" << agentName << "-yAgent) ";
  205. }
  206. os << ";\n\n";
  207. return os;
  208. }
  209. // TODO this does not account for multiple agents yet, i.e. key can be picked up multiple times
  210. std::ostream& PrismModulesPrinter::printKeysLabels(std::ostream& os, const AgentName &agentName, const cells &keys) {
  211. if(keys.size() == 0) return os;
  212. for(auto const& key : keys) {
  213. int xKey = key.getCoordinates().first;
  214. int yKey = key.getCoordinates().second;
  215. std::string keyColor = key.getColor();
  216. os << "label \"" << agentName << "PickedUp" << keyColor << "Key\" = " << agentName << "_has_" << keyColor << "_key = true;\n";
  217. os << "formula " << agentName << "CanPickUp" << keyColor << "Key = ";
  218. os << "((x" << agentName << "-1 = " << xKey << "&y" << agentName << " = " << yKey << "&view" << agentName << " = 1) |";
  219. os << " (x" << agentName << "+1 = " << xKey << "&y" << agentName << " = " << yKey << "&view" << agentName << " = 3) |";
  220. os << " (x" << agentName << " = " << xKey << "&y" << agentName << "-1 = " << yKey << "&view" << agentName << " = 0) |";
  221. os << " (x" << agentName << " = " << xKey << "&y" << agentName << "+1 = " << yKey << "&view" << agentName << " = 2) ) &";
  222. os << "!" << agentName << "_has_" << keyColor << "_key;";
  223. }
  224. os << "\n";
  225. return os;
  226. }
  227. std::ostream& PrismModulesPrinter::printBooleansForKeys(std::ostream &os, const AgentName &agentName, const cells &keys) {
  228. for(auto const& key : keys) {
  229. os << "\t" << agentName << "_has_"<< key.getColor() << "_key : bool init false;\n";
  230. }
  231. os << "\n";
  232. return os;
  233. }
  234. std::ostream& PrismModulesPrinter::printBooleansForBackground(std::ostream &os, const AgentName &agentName, const std::map<Color, cells> &backgroundTiles) {
  235. for(auto const& [color, cells] : backgroundTiles) {
  236. if(cells.size() == 0) continue;
  237. std::string c = getColor(color);
  238. c.at(0) = std::toupper(c.at(0));
  239. os << "\t" << agentName << "_picked_up_" << c << " : bool init false;\n";
  240. }
  241. os << "\n";
  242. return os;
  243. }
  244. std::ostream& PrismModulesPrinter::printActionsForKeys(std::ostream &os, const AgentName &agentName, const cells &keys) {
  245. for(auto const& key : keys) {
  246. std::string keyColor = key.getColor();
  247. os << "\t[pickup_" << keyColor << "_key] " << agentName << "CanPickUp" << keyColor << "Key -> ";
  248. os << "(" << agentName << "_has_" << keyColor << "_key'=true);";
  249. }
  250. os << "\n";
  251. return os;
  252. }
  253. std::ostream& PrismModulesPrinter::printActionsForBackground(std::ostream &os, const AgentName &agentName, const std::map<Color, cells> &backgroundTiles) {
  254. for(auto const& [color, cells] : backgroundTiles) {
  255. if(cells.size() == 0) continue;
  256. std::string c = getColor(color);
  257. c.at(0) = std::toupper(c.at(0));
  258. os << "\t[" << agentName << "_pickup_" << c << "] " << agentName << "On" << c << " & !" << agentName << "_picked_up_" << c << " -> ";
  259. os << "(" << agentName << "_picked_up_" << c << "'=true);\n";
  260. }
  261. os << "\n";
  262. return os;
  263. }
  264. std::ostream& PrismModulesPrinter::printInitStruct(std::ostream &os, const AgentName &agentName) {
  265. os << "init\n";
  266. os << "\t(!AgentIsInGoal & !AgentIsInLava & !AgentDone & !AgentIsOnWall)";
  267. if(enforceOneWays) {
  268. os << " & ( !AgentCannotTurn ) ";
  269. } else {
  270. os << " & ( !AgentIsOnSlippery ) ";
  271. }
  272. os << "\nendinit\n\n";
  273. return os;
  274. }
  275. 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) {
  276. os << "module " << agentName << "\n";
  277. os << "\tx" << agentName << " : [1.." << boundaries.second << "];\n";
  278. os << "\ty" << agentName << " : [1.." << boundaries.first << "];\n";
  279. printBooleansForKeys(os, agentName, keys);
  280. printBooleansForBackground(os, agentName, backgroundTiles);
  281. os << "\t" << agentName << "Done : bool;\n";
  282. if(agentWithView) {
  283. os << "\tview" << agentName << " : [0..3];\n";
  284. os << "\n";
  285. 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";
  286. 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";
  287. 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";
  288. if(enforceOneWays) {
  289. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 0 & " << agentName << "CannotMoveEast -> true;\n";
  290. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 1 & " << agentName << "CannotMoveSouth -> true;\n";
  291. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 2 & " << agentName << "CannotMoveWest -> true;\n";
  292. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 3 & " << agentName << "CannotMoveNorth -> true;\n";
  293. }
  294. } else {
  295. os << "\t[" << agentName << "_turns] " << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << moveGuard(agentIndex) << " true -> (x" << agentName << "'=x" << agentName << ")" << moveUpdate(agentIndex) << ";\n";
  296. }
  297. printActionsForKeys(os, agentName, keys);
  298. printActionsForBackground(os, agentName, backgroundTiles);
  299. os << "\n";
  300. printMovementActions(os, agentName, agentIndex, agentWithView);
  301. for(auto const& probability : probabilities) {
  302. printMovementActions(os, agentName, agentIndex, agentWithView, probability);
  303. }
  304. printDoneActions(os, agentName, agentIndex);
  305. os << "\n";
  306. return os;
  307. }
  308. std::ostream& PrismModulesPrinter::printMovementActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const bool agentWithView, const float &probability) {
  309. if(probability >= 1) {
  310. 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";
  311. 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";
  312. 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";
  313. 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";
  314. } else {
  315. std::string probabilityString = std::to_string(probability);
  316. std::string percentageString = std::to_string((int)(100 * probability));
  317. std::string complementProbabilityString = std::to_string(1 - probability);
  318. os << "\t[" << agentName << "_move_north_" << percentageString << "] ";
  319. os << moveGuard(agentIndex) << viewVariable(agentName, 3, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveNorth -> ";
  320. os << probabilityString << ": (y" << agentName << "'=y" << agentName << "-1)" << moveUpdate(agentIndex) << " + ";
  321. os << complementProbabilityString << ": (y" << agentName << "'=y" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  322. os << "\t[" << agentName << "_move_east_" << percentageString << "] ";
  323. os << moveGuard(agentIndex) << viewVariable(agentName, 0, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveEast -> ";
  324. os << probabilityString << ": (x" << agentName << "'=x" << agentName << "+1)" << moveUpdate(agentIndex) << " + ";
  325. os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  326. os << "\t[" << agentName << "_move_south_" << percentageString << "] ";
  327. os << moveGuard(agentIndex) << viewVariable(agentName, 1, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveSouth -> ";
  328. os << probabilityString << ": (y" << agentName << "'=y" << agentName << "+1)" << moveUpdate(agentIndex) << " + ";
  329. os << complementProbabilityString << ": (y" << agentName << "'=y" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  330. os << "\t[" << agentName << "_move_west_" << percentageString << "] ";
  331. os << moveGuard(agentIndex) << viewVariable(agentName, 2, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveWest -> ";
  332. os << probabilityString << ": (x" << agentName << "'=x" << agentName << "-1)" << moveUpdate(agentIndex) << " + ";
  333. os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  334. }
  335. return os;
  336. }
  337. std::ostream& PrismModulesPrinter::printDoneActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex) {
  338. os << "\t[" << agentName << "_done]" << moveGuard(agentIndex) << agentName << "IsInGoal | " << agentName << "IsInLava -> (" << agentName << "Done'=true);\n";
  339. return os;
  340. }
  341. 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) {
  342. constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 9;
  343. std::array<std::string, ALL_POSS_DIRECTIONS> positionTransition = {
  344. /* north */ "(y" + agentName + "'=y" + agentName + "-1)",
  345. /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)",
  346. /* east */ "(x" + agentName + "'=x" + agentName + "+1)",
  347. /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)",
  348. /* south */ "(y" + agentName + "'=y" + agentName + "+1)",
  349. /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)",
  350. /* west */ "(x" + agentName + "'=x" + agentName + "-1)",
  351. /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)",
  352. /* own position */ "(x" + agentName + "'=x" + agentName + ") & (y" + agentName + "'=y" + agentName + ")"
  353. };
  354. // view transition appdx in form (guard, update part)
  355. // IMPORTANT: No mod() usage for turn left due to bug in mod() function for decrement
  356. std::array<std::pair<std::string, std::string>, 3> viewTransition = {
  357. /* turn to right */ std::make_pair(" & " + agentName + "SlipperyTurnRightAllowed ", " & (view" + agentName + "'=mod(view" + agentName + " + 1, 4))"),
  358. /* turn to left */ std::make_pair(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + ">0", " & (view" + agentName + "'=view" + agentName + " - 1)"),
  359. /* turn to left */ std::make_pair(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + "=0", " & (view" + agentName + "'=3)")
  360. };
  361. // direction specifics
  362. std::string actionName;
  363. std::size_t remainPosIndex = 8;
  364. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir; // { n, ne, w, se, s, sw, w, nw, CURRENT POS }
  365. switch (orientation)
  366. {
  367. case SlipperyType::North:
  368. actionName = "\t[" + agentName + "turn_at_slip_north]";
  369. prob_piece_dir = { 0, 0, 0, 1, 1, 1, 0, 0, 0 /* <- R */ };
  370. break;
  371. case SlipperyType::South:
  372. actionName = "\t[" + agentName + "turn_at_slip_south]";
  373. prob_piece_dir = { 1, 1, 0, 0, 0, 0, 0, 1, 0 /* <- R */ };
  374. break;
  375. case SlipperyType::East:
  376. actionName = "\t[" + agentName + "turn_at_slip_east]";
  377. prob_piece_dir = { 0, 0, 0, 0, 0, 1, 1, 1, 0 /* <- R */ };
  378. break;
  379. case SlipperyType::West:
  380. actionName = "\t[" + agentName + "turn_at_slip_west]";
  381. prob_piece_dir = { 0, 1, 1, 1, 0, 0, 0, 0, 0 /* <- R */ };
  382. break;
  383. }
  384. slipperyActions.insert(actionName);
  385. // override probability to 0 if corresp. direction is blocked
  386. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS - 1; i++) {
  387. if (!neighborhood.at(i))
  388. prob_piece_dir.at(i) = 0;
  389. }
  390. // determine residual probability (R) by replacing 0 with (1 - overall sum)
  391. prob_piece_dir.at(remainPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0);
  392. // <DEBUG_AREA>
  393. {
  394. assert(prob_piece_dir.at(remainPosIndex) <= 9 && prob_piece_dir.at(remainPosIndex) >= 6 && "Value not in Range!");
  395. assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!");
  396. }
  397. // </DEBUG_AREA>
  398. // generic output (for every view transition)
  399. for (std::size_t v = 0; v < viewTransition.size(); v++) {
  400. os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << viewTransition.at(v).first;
  401. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  402. os << (i == 0 ? " -> " : " + ") << prob_piece_dir.at(i) << "/" << PROB_PIECES << " : " << positionTransition.at(i) << viewTransition.at(v).second << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  403. }
  404. }
  405. return os;
  406. }
  407. 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) {
  408. constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 8;
  409. std::array<std::string, ALL_POSS_DIRECTIONS> positionTransition = {
  410. /* north */ "(y" + agentName + "'=y" + agentName + "-1)",
  411. /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)",
  412. /* east */ "(x" + agentName + "'=x" + agentName + "+1)",
  413. /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)",
  414. /* south */ "(y" + agentName + "'=y" + agentName + "+1)",
  415. /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)",
  416. /* west */ "(x" + agentName + "'=x" + agentName + "-1)",
  417. /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)"
  418. };
  419. // direction specifics
  420. std::size_t straightPosIndex;
  421. std::string actionName, specialTransition; // if straight ahead is blocked
  422. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir; // { n, ne, w, se, s, sw, w, nw }
  423. switch (orientation)
  424. {
  425. case SlipperyType::North:
  426. actionName = "\t[" + agentName + "move_on_slip_north]";
  427. prob_piece_dir = { 0, 0, 1, 2, 0 /* <- R */, 2, 1, 0 };
  428. straightPosIndex = 4;
  429. specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)");
  430. break;
  431. case SlipperyType::South:
  432. actionName = "\t[" + agentName + "move_on_slip_south]";
  433. prob_piece_dir = { 0 /* <- R */, 2, 1, 0, 0, 0, 1, 2 };
  434. straightPosIndex = 0; // always north
  435. specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)");
  436. break;
  437. case SlipperyType::East:
  438. actionName = "\t[" + agentName + "move_on_slip_east]";
  439. prob_piece_dir = { 1, 0, 0, 0, 1, 2, 0 /* <- R */, 2 };
  440. straightPosIndex = 6;
  441. specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)");
  442. break;
  443. case SlipperyType::West:
  444. actionName = "\t[" + agentName + "move_on_slip_west]";
  445. prob_piece_dir = { 1, 2, 0 /* <- R */, 2, 1, 0, 0, 0 };
  446. straightPosIndex = 2;
  447. specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)");
  448. break;
  449. }
  450. slipperyActions.insert(actionName);
  451. // override probability to 0 if corresp. direction is blocked
  452. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  453. if (!neighborhood.at(i))
  454. prob_piece_dir.at(i) = 0;
  455. }
  456. // determine residual probability (R) by replacing 0 with (1 - overall sum)
  457. if(enforceOneWays) {
  458. prob_piece_dir = {0,0,0,0,0,0,0,0};
  459. }
  460. prob_piece_dir.at(straightPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0);
  461. // <DEBUG_AREA>
  462. {
  463. assert(prob_piece_dir.at(straightPosIndex) <= 9 && prob_piece_dir.at(straightPosIndex) >= 3 && "Value not in Range!");
  464. assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!");
  465. 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!"));
  466. 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!"));
  467. 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!"));
  468. 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!"));
  469. }
  470. // </DEBUG_AREA>
  471. // special case: straight forward is blocked (then remain in same position)
  472. positionTransition.at(straightPosIndex) = specialTransition;
  473. // generic output (for every view and every possible view direction)
  474. os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed ";
  475. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  476. os << (i == 0 ? " -> " : " + ") << prob_piece_dir.at(i) << "/" << PROB_PIECES << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  477. }
  478. return os;
  479. }
  480. std::ostream& PrismModulesPrinter::printEndmodule(std::ostream &os) {
  481. os << "endmodule\n";
  482. os << "\n";
  483. return os;
  484. }
  485. std::ostream& PrismModulesPrinter::printPlayerStruct(std::ostream &os, const AgentName &agentName, const bool agentWithView, const std::vector<float> &probabilities, const std::set<std::string> &slipperyActions) {
  486. os << "player " << agentName << "\n\t";
  487. bool first = true;
  488. std::list<std::string> allActions = { "_move_north", "_move_east", "_move_south", "_move_west" };
  489. std::list<std::string> movementActions = allActions;
  490. for(auto const& probability : probabilities) {
  491. std::string percentageString = std::to_string((int)(100 * probability));
  492. for(auto const& movement : movementActions) {
  493. allActions.push_back(movement + "_" + percentageString);
  494. }
  495. }
  496. if(agentWithView) {
  497. allActions.push_back("_turn_left");
  498. allActions.push_back("_turn_right");
  499. } else {
  500. allActions.push_back("_turns");
  501. }
  502. for(auto const& action : allActions) {
  503. if(first) first = false; else os << ", ";
  504. os << "[" << agentName << action << "]";
  505. }
  506. for(auto const& action : slipperyActions) {
  507. os << ", " << action;
  508. }
  509. os << "\nendplayer\n";
  510. return os;
  511. }
  512. std::ostream& PrismModulesPrinter::printGlobalMoveVariable(std::ostream &os, const size_t &numberOfPlayer) {
  513. os << "\nglobal move : [0.." << std::to_string(numberOfPlayer - 1) << "] init 0;\n\n";
  514. return os;
  515. }
  516. 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) {
  517. if(lava.size() != 0) {
  518. os << "rewards \"SafetyNoBFS\"\n";
  519. os << "\tAgentIsInLavaAndNotDone: -100;\n";
  520. os << "endrewards\n";
  521. }
  522. os << "rewards \"SafetyNoBFSAndGoal\"\n";
  523. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  524. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  525. os << "endrewards\n";
  526. os << "rewards \"Time\"\n";
  527. os << "\t!AgentIsInGoal : -1;\n";
  528. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  529. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  530. os << "endrewards\n";
  531. if(stateRewards.size() > 0) {
  532. os << "rewards \"SafetyWithBFS\"\n";
  533. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  534. for(auto const [coordinates, reward] : stateRewards) {
  535. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  536. }
  537. os << "endrewards\n";
  538. }
  539. if(stateRewards.size() > 0) {
  540. os << "rewards \"SafetyWithBFSAndGoal\"\n";
  541. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  542. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  543. for(auto const [coordinates, reward] : stateRewards) {
  544. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  545. }
  546. os << "endrewards\n";
  547. }
  548. for(auto const entry : backgroundTiles)
  549. {
  550. std::cout << getColor(entry.first) << " ";
  551. for(auto const cell : entry.second){
  552. std::cout << cell.getCoordinates().first << " " << cell.getCoordinates().second << std::endl;
  553. }
  554. }
  555. if(backgroundTiles.size() > 0) {
  556. os << "rewards \"TaxiReward\"\n";
  557. os << "\t!AgentIsInGoal : -1;\n";
  558. std::string allPassengersPickedUp = "";
  559. bool first = true;
  560. for(auto const [color, cells] : backgroundTiles) {
  561. if(cells.size() == 0) continue;
  562. if(first) first = false; else allPassengersPickedUp += "&";
  563. std::string c = getColor(color);
  564. c.at(0) = std::toupper(c.at(0));
  565. std::string visitedLabel = agentName + "_picked_up_" + c;
  566. allPassengersPickedUp += visitedLabel;
  567. os << "[" << agentName << "_pickup_" << c << "] true : 100;\n";
  568. }
  569. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & " << allPassengersPickedUp << " : 100;\n";
  570. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & !(" << allPassengersPickedUp << ") : -100;\n";
  571. os << "endrewards";
  572. }
  573. return os;
  574. }
  575. std::string PrismModulesPrinter::moveGuard(const size_t &agentIndex) {
  576. return isGame() ? " move=" + std::to_string(agentIndex) + " & " : " ";
  577. }
  578. std::string PrismModulesPrinter::moveUpdate(const size_t &agentIndex) {
  579. return isGame() ?
  580. (agentIndex == numberOfPlayer - 1) ?
  581. " & (move'=0) " :
  582. " & (move'=" + std::to_string(agentIndex + 1) + ") " :
  583. "";
  584. }
  585. std::string PrismModulesPrinter::viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView) {
  586. return agentWithView ? " view" + agentName + "=" + std::to_string(agentDirection) + " & " : " ";
  587. }
  588. bool PrismModulesPrinter::isGame() const {
  589. return modelType == ModelType::SMG;
  590. }
  591. }