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.

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