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.

689 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;\n";//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, const cells &keys) {
  265. os << "init\n";
  266. os << "\t(!AgentIsInGoal & !AgentIsInLava & !AgentDone & !AgentIsOnWall)";
  267. if(enforceOneWays) {
  268. os << " & ( !AgentCannotTurn ) ";
  269. } else {
  270. os << " & ( !AgentIsOnSlippery ) ";
  271. }
  272. for (auto const& key : keys) {
  273. os << " & ( !" << agentName << "_has_" << key.getColor() << "_key )";
  274. }
  275. os << "\nendinit\n\n";
  276. return os;
  277. }
  278. 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) {
  279. os << "module " << agentName << "\n";
  280. os << "\tx" << agentName << " : [1.." << boundaries.second << "];\n";
  281. os << "\ty" << agentName << " : [1.." << boundaries.first << "];\n";
  282. printBooleansForKeys(os, agentName, keys);
  283. printBooleansForBackground(os, agentName, backgroundTiles);
  284. os << "\t" << agentName << "Done : bool;\n";
  285. if(agentWithView) {
  286. os << "\tview" << agentName << " : [0..3];\n";
  287. os << "\n";
  288. 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";
  289. 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";
  290. 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";
  291. if(enforceOneWays) {
  292. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 0 & " << agentName << "CannotMoveEast -> true;\n";
  293. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 1 & " << agentName << "CannotMoveSouth -> true;\n";
  294. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 2 & " << agentName << "CannotMoveWest -> true;\n";
  295. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 3 & " << agentName << "CannotMoveNorth -> true;\n";
  296. }
  297. } else {
  298. os << "\t[" << agentName << "_turns] " << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << moveGuard(agentIndex) << " true -> (x" << agentName << "'=x" << agentName << ")" << moveUpdate(agentIndex) << ";\n";
  299. }
  300. printActionsForKeys(os, agentName, keys);
  301. printActionsForBackground(os, agentName, backgroundTiles);
  302. os << "\n";
  303. printMovementActions(os, agentName, agentIndex, agentWithView);
  304. for(auto const& probability : probabilities) {
  305. printMovementActions(os, agentName, agentIndex, agentWithView, probability);
  306. }
  307. printDoneActions(os, agentName, agentIndex);
  308. os << "\n";
  309. return os;
  310. }
  311. std::ostream& PrismModulesPrinter::printMovementActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const bool agentWithView, const float &probability) {
  312. if(probability >= 1) {
  313. 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";
  314. 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";
  315. 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";
  316. 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";
  317. } else {
  318. std::string probabilityString = std::to_string(probability);
  319. std::string percentageString = std::to_string((int)(100 * probability));
  320. std::string complementProbabilityString = std::to_string(1 - probability);
  321. os << "\t[" << agentName << "_move_north_" << percentageString << "] ";
  322. os << moveGuard(agentIndex) << viewVariable(agentName, 3, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveNorth -> ";
  323. os << probabilityString << ": (y" << agentName << "'=y" << agentName << "-1)" << moveUpdate(agentIndex) << " + ";
  324. os << complementProbabilityString << ": (y" << agentName << "'=y" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  325. os << "\t[" << agentName << "_move_east_" << percentageString << "] ";
  326. os << moveGuard(agentIndex) << viewVariable(agentName, 0, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveEast -> ";
  327. os << probabilityString << ": (x" << agentName << "'=x" << agentName << "+1)" << moveUpdate(agentIndex) << " + ";
  328. os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  329. os << "\t[" << agentName << "_move_south_" << percentageString << "] ";
  330. os << moveGuard(agentIndex) << viewVariable(agentName, 1, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveSouth -> ";
  331. os << probabilityString << ": (y" << agentName << "'=y" << agentName << "+1)" << moveUpdate(agentIndex) << " + ";
  332. os << complementProbabilityString << ": (y" << agentName << "'=y" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  333. os << "\t[" << agentName << "_move_west_" << percentageString << "] ";
  334. os << moveGuard(agentIndex) << viewVariable(agentName, 2, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveWest -> ";
  335. os << probabilityString << ": (x" << agentName << "'=x" << agentName << "-1)" << moveUpdate(agentIndex) << " + ";
  336. os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  337. }
  338. return os;
  339. }
  340. std::ostream& PrismModulesPrinter::printDoneActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex) {
  341. os << "\t[" << agentName << "_done]" << moveGuard(agentIndex) << agentName << "IsInGoal | " << agentName << "IsInLava -> (" << agentName << "Done'=true);\n";
  342. return os;
  343. }
  344. 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) {
  345. constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 9;
  346. std::array<std::string, ALL_POSS_DIRECTIONS> positionTransition = {
  347. /* north */ "(y" + agentName + "'=y" + agentName + "-1)",
  348. /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)",
  349. /* east */ "(x" + agentName + "'=x" + agentName + "+1)",
  350. /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)",
  351. /* south */ "(y" + agentName + "'=y" + agentName + "+1)",
  352. /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)",
  353. /* west */ "(x" + agentName + "'=x" + agentName + "-1)",
  354. /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)",
  355. /* own position */ "(x" + agentName + "'=x" + agentName + ") & (y" + agentName + "'=y" + agentName + ")"
  356. };
  357. // view transition appdx in form (guard, update part)
  358. // IMPORTANT: No mod() usage for turn left due to bug in mod() function for decrement
  359. std::array<std::pair<std::string, std::string>, 3> viewTransition = {
  360. /* turn to right */ std::make_pair(" & " + agentName + "SlipperyTurnRightAllowed ", " & (view" + agentName + "'=mod(view" + agentName + " + 1, 4))"),
  361. /* turn to left */ std::make_pair(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + ">0", " & (view" + agentName + "'=view" + agentName + " - 1)"),
  362. /* turn to left */ std::make_pair(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + "=0", " & (view" + agentName + "'=3)")
  363. };
  364. // direction specifics
  365. std::string actionName;
  366. std::size_t remainPosIndex = 8;
  367. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir; // { n, ne, w, se, s, sw, w, nw, CURRENT POS }
  368. switch (orientation)
  369. {
  370. case SlipperyType::North:
  371. actionName = "\t[" + agentName + "turn_at_slip_north]";
  372. prob_piece_dir = { 0, 0, 0, 1, 1, 1, 0, 0, 0 /* <- R */ };
  373. break;
  374. case SlipperyType::South:
  375. actionName = "\t[" + agentName + "turn_at_slip_south]";
  376. prob_piece_dir = { 1, 1, 0, 0, 0, 0, 0, 1, 0 /* <- R */ };
  377. break;
  378. case SlipperyType::East:
  379. actionName = "\t[" + agentName + "turn_at_slip_east]";
  380. prob_piece_dir = { 0, 0, 0, 0, 0, 1, 1, 1, 0 /* <- R */ };
  381. break;
  382. case SlipperyType::West:
  383. actionName = "\t[" + agentName + "turn_at_slip_west]";
  384. prob_piece_dir = { 0, 1, 1, 1, 0, 0, 0, 0, 0 /* <- R */ };
  385. break;
  386. }
  387. slipperyActions.insert(actionName);
  388. // override probability to 0 if corresp. direction is blocked
  389. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS - 1; i++) {
  390. if (!neighborhood.at(i))
  391. prob_piece_dir.at(i) = 0;
  392. }
  393. // determine residual probability (R) by replacing 0 with (1 - overall sum)
  394. prob_piece_dir.at(remainPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0);
  395. // <DEBUG_AREA>
  396. {
  397. assert(prob_piece_dir.at(remainPosIndex) <= 9 && prob_piece_dir.at(remainPosIndex) >= 6 && "Value not in Range!");
  398. assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!");
  399. }
  400. // </DEBUG_AREA>
  401. // generic output (for every view transition)
  402. for (std::size_t v = 0; v < viewTransition.size(); v++) {
  403. os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << viewTransition.at(v).first;
  404. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  405. 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");
  406. }
  407. }
  408. return os;
  409. }
  410. 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) {
  411. constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 8;
  412. std::array<std::string, ALL_POSS_DIRECTIONS> positionTransition = {
  413. /* north */ "(y" + agentName + "'=y" + agentName + "-1)",
  414. /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)",
  415. /* east */ "(x" + agentName + "'=x" + agentName + "+1)",
  416. /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)",
  417. /* south */ "(y" + agentName + "'=y" + agentName + "+1)",
  418. /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)",
  419. /* west */ "(x" + agentName + "'=x" + agentName + "-1)",
  420. /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)"
  421. };
  422. // direction specifics
  423. std::size_t straightPosIndex;
  424. std::string actionName, specialTransition; // if straight ahead is blocked
  425. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir; // { n, ne, w, se, s, sw, w, nw }
  426. switch (orientation)
  427. {
  428. case SlipperyType::North:
  429. actionName = "\t[" + agentName + "move_on_slip_north]";
  430. prob_piece_dir = { 0, 0, 1, 2, 0 /* <- R */, 2, 1, 0 };
  431. straightPosIndex = 4;
  432. specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)");
  433. break;
  434. case SlipperyType::South:
  435. actionName = "\t[" + agentName + "move_on_slip_south]";
  436. prob_piece_dir = { 0 /* <- R */, 2, 1, 0, 0, 0, 1, 2 };
  437. straightPosIndex = 0; // always north
  438. specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)");
  439. break;
  440. case SlipperyType::East:
  441. actionName = "\t[" + agentName + "move_on_slip_east]";
  442. prob_piece_dir = { 1, 0, 0, 0, 1, 2, 0 /* <- R */, 2 };
  443. straightPosIndex = 6;
  444. specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)");
  445. break;
  446. case SlipperyType::West:
  447. actionName = "\t[" + agentName + "move_on_slip_west]";
  448. prob_piece_dir = { 1, 2, 0 /* <- R */, 2, 1, 0, 0, 0 };
  449. straightPosIndex = 2;
  450. specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)");
  451. break;
  452. }
  453. slipperyActions.insert(actionName);
  454. // override probability to 0 if corresp. direction is blocked
  455. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  456. if (!neighborhood.at(i))
  457. prob_piece_dir.at(i) = 0;
  458. }
  459. // determine residual probability (R) by replacing 0 with (1 - overall sum)
  460. if(enforceOneWays) {
  461. prob_piece_dir = {0,0,0,0,0,0,0,0};
  462. }
  463. prob_piece_dir.at(straightPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0);
  464. // <DEBUG_AREA>
  465. {
  466. assert(prob_piece_dir.at(straightPosIndex) <= 9 && prob_piece_dir.at(straightPosIndex) >= 3 && "Value not in Range!");
  467. assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!");
  468. 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!"));
  469. 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!"));
  470. 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!"));
  471. 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!"));
  472. }
  473. // </DEBUG_AREA>
  474. // special case: straight forward is blocked (then remain in same position)
  475. positionTransition.at(straightPosIndex) = specialTransition;
  476. // generic output (for every view and every possible view direction)
  477. os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed ";
  478. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  479. os << (i == 0 ? " -> " : " + ") << prob_piece_dir.at(i) << "/" << PROB_PIECES << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  480. }
  481. return os;
  482. }
  483. std::ostream& PrismModulesPrinter::printEndmodule(std::ostream &os) {
  484. os << "endmodule\n";
  485. os << "\n";
  486. return os;
  487. }
  488. std::ostream& PrismModulesPrinter::printPlayerStruct(std::ostream &os, const AgentName &agentName, const bool agentWithView, const std::vector<float> &probabilities, const std::set<std::string> &slipperyActions) {
  489. os << "player " << agentName << "\n\t";
  490. bool first = true;
  491. std::list<std::string> allActions = { "_move_north", "_move_east", "_move_south", "_move_west" };
  492. std::list<std::string> movementActions = allActions;
  493. for(auto const& probability : probabilities) {
  494. std::string percentageString = std::to_string((int)(100 * probability));
  495. for(auto const& movement : movementActions) {
  496. allActions.push_back(movement + "_" + percentageString);
  497. }
  498. }
  499. if(agentWithView) {
  500. allActions.push_back("_turn_left");
  501. allActions.push_back("_turn_right");
  502. } else {
  503. allActions.push_back("_turns");
  504. }
  505. for(auto const& action : allActions) {
  506. if(first) first = false; else os << ", ";
  507. os << "[" << agentName << action << "]";
  508. }
  509. for(auto const& action : slipperyActions) {
  510. os << ", " << action;
  511. }
  512. os << "\nendplayer\n";
  513. return os;
  514. }
  515. std::ostream& PrismModulesPrinter::printGlobalMoveVariable(std::ostream &os, const size_t &numberOfPlayer) {
  516. os << "\nglobal move : [0.." << std::to_string(numberOfPlayer - 1) << "] init 0;\n\n";
  517. return os;
  518. }
  519. 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) {
  520. if(lava.size() != 0) {
  521. os << "rewards \"SafetyNoBFS\"\n";
  522. os << "\tAgentIsInLavaAndNotDone: -100;\n";
  523. os << "endrewards\n";
  524. }
  525. if (!goals.empty() || !lava.empty()) {
  526. os << "rewards \"SafetyNoBFSAndGoal\"\n";
  527. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  528. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  529. os << "endrewards\n";
  530. }
  531. os << "rewards \"Time\"\n";
  532. os << "\t!AgentIsInGoal : -1;\n";
  533. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  534. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  535. os << "endrewards\n";
  536. if(stateRewards.size() > 0) {
  537. os << "rewards \"SafetyWithBFS\"\n";
  538. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  539. for(auto const [coordinates, reward] : stateRewards) {
  540. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  541. }
  542. os << "endrewards\n";
  543. }
  544. if(stateRewards.size() > 0) {
  545. os << "rewards \"SafetyWithBFSAndGoal\"\n";
  546. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  547. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  548. for(auto const [coordinates, reward] : stateRewards) {
  549. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  550. }
  551. os << "endrewards\n";
  552. }
  553. for(auto const entry : backgroundTiles)
  554. {
  555. std::cout << getColor(entry.first) << " ";
  556. for(auto const cell : entry.second){
  557. std::cout << cell.getCoordinates().first << " " << cell.getCoordinates().second << std::endl;
  558. }
  559. }
  560. if(backgroundTiles.size() > 0) {
  561. os << "rewards \"TaxiReward\"\n";
  562. os << "\t!AgentIsInGoal : -1;\n";
  563. std::string allPassengersPickedUp = "";
  564. bool first = true;
  565. for(auto const [color, cells] : backgroundTiles) {
  566. if(cells.size() == 0) continue;
  567. if(first) first = false; else allPassengersPickedUp += "&";
  568. std::string c = getColor(color);
  569. c.at(0) = std::toupper(c.at(0));
  570. std::string visitedLabel = agentName + "_picked_up_" + c;
  571. allPassengersPickedUp += visitedLabel;
  572. os << "[" << agentName << "_pickup_" << c << "] true : 100;\n";
  573. }
  574. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & " << allPassengersPickedUp << " : 100;\n";
  575. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & !(" << allPassengersPickedUp << ") : -100;\n";
  576. os << "endrewards";
  577. }
  578. return os;
  579. }
  580. std::string PrismModulesPrinter::moveGuard(const size_t &agentIndex) {
  581. return isGame() ? " move=" + std::to_string(agentIndex) + " & " : " ";
  582. }
  583. std::string PrismModulesPrinter::moveUpdate(const size_t &agentIndex) {
  584. return isGame() ?
  585. (agentIndex == numberOfPlayer - 1) ?
  586. " & (move'=0) " :
  587. " & (move'=" + std::to_string(agentIndex + 1) + ") " :
  588. "";
  589. }
  590. std::string PrismModulesPrinter::viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView) {
  591. return agentWithView ? " view" + agentName + "=" + std::to_string(agentDirection) + " & " : " ";
  592. }
  593. bool PrismModulesPrinter::isGame() const {
  594. return modelType == ModelType::SMG;
  595. }
  596. }