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.

1001 lines
49 KiB

  1. #include "PrismModulesPrinter.h"
  2. #include <map>
  3. #include <string>
  4. namespace prism {
  5. PrismModulesPrinter::PrismModulesPrinter(const ModelType &modelType, const size_t &numberOfPlayer, std::vector<Configuration> config, const bool enforceOneWays)
  6. : modelType(modelType), numberOfPlayer(numberOfPlayer), enforceOneWays(enforceOneWays), configuration(config), viewDirectionMapping({{0, "East"}, {1, "South"}, {2, "West"}, {3, "North"}}) {
  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 &grid_cells, const cells &keys, const cells &doors) {
  35. bool first = true;
  36. os << "formula " << agentName << "CannotMove" << direction << " = " ;
  37. for(auto const& cell : grid_cells) {
  38. if(first) first = false;
  39. else os << " | ";
  40. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  41. }
  42. if (!keys.empty()) {
  43. os << " | " << agentName << "CannotMove" << direction << "BecauseOfKey";
  44. }
  45. if (!doors.empty()) {
  46. os << " | " << agentName << "CannotMove" << direction << "BecauseOfDoor";
  47. }
  48. os << ";\n";
  49. return os;
  50. }
  51. std::ostream& PrismModulesPrinter::printKeyRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &keys) {
  52. bool first = true;
  53. os << "formula " << agentName << "CannotMove" << direction << "BecauseOfKey" << " = ";
  54. for (auto const& key : keys) {
  55. if(first) first = false;
  56. else os << " | ";
  57. std::string keyColor = key.getColor();
  58. std::string xKey = "xKey" + keyColor;
  59. std::string yKey = "yKey" + keyColor;
  60. coordinates coords;
  61. os << "(!" << agentName << "_has_" << keyColor << "_key & ";
  62. if (direction == "North") {
  63. os << " x" << agentName << " = " << xKey << "&y" << agentName << "-1 = " << yKey << ")";
  64. } else if (direction == "South") {
  65. os << " x" << agentName << " = " << xKey << "&y" << agentName << "+1 = " << yKey << ")";
  66. } else if (direction == "East") {
  67. os << " x" << agentName << "+1 = " << xKey << "&y" << agentName << " = " << yKey << ")";
  68. } else if (direction == "West") {
  69. os << " x" << agentName << "-1 = " << xKey << "&y" << agentName << " = " << yKey << ")";
  70. } else {
  71. os << "Invalid Direction! in Key Restriction";
  72. }
  73. }
  74. os << ";\n";
  75. return os;
  76. }
  77. std::ostream& PrismModulesPrinter::printDoorRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &doors) {
  78. bool first = true;
  79. os << "formula " << agentName << "CannotMove" << direction << "BecauseOfDoor" << " = ";
  80. for (auto const& door : doors) {
  81. if (first) first = false;
  82. else os << " | ";
  83. std::string doorColor = door.getColor();
  84. size_t y = door.getCoordinates().first;
  85. size_t x = door.getCoordinates().second;
  86. os << "(!Door" << doorColor << "open & ";
  87. if (direction == "North") {
  88. os << " x" << agentName << " = " << x << "&y" << agentName << "-1 = " << y << ")";
  89. } else if (direction == "South") {
  90. os << " x" << agentName << " = " << x << "&y" << agentName << "+1 = " << y << ")";
  91. } else if (direction == "East") {
  92. os << " x" << agentName << "+1 = " << x << "&y" << agentName << " = " << y << ")";
  93. } else if (direction == "West") {
  94. os << " x" << agentName << "-1 = " << y << "&y" << agentName << " = " << y << ")";
  95. } else {
  96. os << "Invalid Direction! in Key Restriction";
  97. }
  98. }
  99. os << ";\n";
  100. return os;
  101. }
  102. 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) {
  103. if(std::find_if(slipperyCollection.cbegin(), slipperyCollection.cend(), [=](const std::reference_wrapper<cells>& c) { return !c.get().empty(); }) == slipperyCollection.cend()) {
  104. os << "formula " << agentName << "IsOnSlippery = false;\n";
  105. return os;
  106. }
  107. bool first = true;
  108. if (!slipperyNorth.empty()) {
  109. os << "formula " << agentName << "IsOnSlipperyNorth = ";
  110. for (const auto& cell : slipperyNorth) {
  111. if(first) first = false; else os << " | ";
  112. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  113. }
  114. os << ";\n";
  115. }
  116. if (!slipperyEast.empty()) {
  117. first = true;
  118. os << "formula " << agentName << "IsOnSlipperyEast = ";
  119. for (const auto& cell : slipperyEast) {
  120. if(first) first = false; else os << " | ";
  121. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  122. }
  123. os << ";\n";
  124. }
  125. if (!slipperySouth.empty()) {
  126. first = true;
  127. os << "formula " << agentName << "IsOnSlipperySouth = ";
  128. for (const auto& cell : slipperySouth) {
  129. if(first) first = false; else os << " | ";
  130. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  131. }
  132. os << ";\n";
  133. }
  134. if (!slipperyWest.empty()) {
  135. first = true;
  136. os << "formula " << agentName << "IsOnSlipperyWest = ";
  137. for (const auto& cell : slipperyWest) {
  138. if(first) first = false; else os << " | ";
  139. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  140. }
  141. os << ";\n";
  142. }
  143. first = true;
  144. os << "formula " << agentName << "IsOnSlippery = ";
  145. for (const auto& slippery: slipperyCollection) {
  146. for(const auto& cell : slippery.get()) {
  147. if(first) first = false; else os << " | ";
  148. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  149. }
  150. }
  151. os << ";\n";
  152. if(enforceOneWays) {
  153. first = true;
  154. os << "formula " << agentName << "IsOnSlipperyNorth = ";
  155. for (const auto& cell: slipperyNorth) {
  156. if(first) first = false; else os << " | ";
  157. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  158. }
  159. os << ";\n";
  160. first = true;
  161. os << "formula " << agentName << "IsOnSlipperyEast = ";
  162. for (const auto& cell: slipperyEast) {
  163. if(first) first = false; else os << " | ";
  164. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  165. }
  166. os << ";\n";
  167. first = true;
  168. os << "formula " << agentName << "IsOnSlipperySouth = ";
  169. for (const auto& cell: slipperySouth) {
  170. if(first) first = false; else os << " | ";
  171. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  172. }
  173. os << ";\n";
  174. first = true;
  175. os << "formula " << agentName << "IsOnSlipperyWest = ";
  176. for (const auto& cell: slipperyWest) {
  177. if(first) first = false; else os << " | ";
  178. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  179. }
  180. os << ";\n";
  181. }
  182. return os;
  183. }
  184. std::ostream& PrismModulesPrinter::printIsInLavaFormula(std::ostream& os, const AgentName &agentName, const cells &lava) {
  185. if(lava.size() == 0) {
  186. os << "formula " << agentName << "IsInLava = false;\n";
  187. return os;
  188. }
  189. bool first = true;
  190. os << "formula " << agentName << "IsInLava = ";
  191. for(auto const& cell : lava) {
  192. if(first) first = false; else os << " | ";
  193. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  194. }
  195. os << ";\n";
  196. os << "formula " << agentName << "IsInLavaAndNotDone = " << agentName << "IsInLava & !" << agentName << "Done;\n";
  197. os << "label \"" << agentName << "IsInLavaAndNotDone\" = " << agentName << "IsInLava & !" << agentName << "Done;\n";
  198. return os;
  199. }
  200. std::ostream& PrismModulesPrinter::printTurningNotAllowedFormulas(std::ostream& os, const AgentName &agentName, const cells &noTurnFloor) {
  201. if( (!enforceOneWays or noTurnFloor.size() == 0) or (noTurnFloor.size() == 0) ) {
  202. os << "formula " << agentName << "CannotTurn = false;\n";
  203. return os;
  204. }
  205. bool first = true;
  206. os << "formula " << agentName << "CannotTurn = ";
  207. for(auto const& cell : noTurnFloor) {
  208. if(first) first = false; else os << " | ";
  209. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  210. }
  211. os << " | " << agentName << "IsOnSlippery;\n";
  212. return os;
  213. }
  214. std::ostream& PrismModulesPrinter::printIsFixedFormulas(std::ostream& os, const AgentName &agentName) {
  215. os << "formula " << agentName << "IsFixed = false;\n";
  216. os << "formula " << agentName << "SlipperyTurnLeftAllowed = true;\n";
  217. os << "formula " << agentName << "SlipperyTurnRightAllowed = true;\n";
  218. os << "formula " << agentName << "SlipperyMoveForwardAllowed = true;\n";
  219. os << "label \"FixedStates" << agentName << "\" = " << agentName << "IsFixed | !" << agentName << "SlipperyTurnRightAllowed | !" << agentName << "SlipperyTurnLeftAllowed | !" << agentName << "SlipperyMoveForwardAllowed | " << agentName << "IsInGoal | " << agentName << "IsInLava";
  220. if(enforceOneWays) {
  221. os << " | " << agentName << "CannotTurn";
  222. }
  223. os << ";\n";
  224. //os << "label \"FixedStates\" = " << agentName << "IsFixed | " << agentName << "IsOnSlippery | " << agentName << "IsInGoal | " << agentName << "IsInLava;\n";
  225. return os;
  226. }
  227. std::ostream& PrismModulesPrinter::printWallFormula(std::ostream& os, const AgentName &agentName, const cells &walls) {
  228. os << "formula " << agentName << "IsOnWall = ";
  229. bool first = true;
  230. for(auto const& cell : walls) {
  231. if(first) first = false; else os << " | ";
  232. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  233. }
  234. os << ";\n";
  235. return os;
  236. }
  237. std::ostream& PrismModulesPrinter::printFormulas(std::ostream& os,
  238. const AgentName &agentName,
  239. const cells &restrictionNorth,
  240. const cells &restrictionEast,
  241. const cells &restrictionSouth,
  242. const cells &restrictionWest,
  243. const std::vector<std::reference_wrapper<cells>> &slipperyCollection,
  244. const cells &lava,
  245. const cells &walls,
  246. const cells &noTurnFloor,
  247. const cells &slipperyNorth,
  248. const cells &slipperyEast,
  249. const cells &slipperySouth,
  250. const cells &slipperyWest,
  251. const cells &keys,
  252. const cells &doors) {
  253. printRestrictionFormula(os, agentName, "North", restrictionNorth, keys, doors);
  254. printRestrictionFormula(os, agentName, "East", restrictionEast, keys, doors);
  255. printRestrictionFormula(os, agentName, "South", restrictionSouth, keys, doors);
  256. printRestrictionFormula(os, agentName, "West", restrictionWest, keys, doors);
  257. if (!keys.empty()) {
  258. printKeyRestrictionFormula(os, agentName, "North", keys);
  259. printKeyRestrictionFormula(os, agentName, "East", keys);
  260. printKeyRestrictionFormula(os, agentName, "South", keys);
  261. printKeyRestrictionFormula(os, agentName, "West", keys);
  262. }
  263. if (!doors.empty()) {
  264. printDoorRestrictionFormula(os, agentName, "North", doors);
  265. printDoorRestrictionFormula(os, agentName, "East", doors);
  266. printDoorRestrictionFormula(os, agentName, "South", doors);
  267. printDoorRestrictionFormula(os, agentName, "West", doors);
  268. }
  269. printIsOnSlipperyFormula(os, agentName, slipperyCollection, slipperyNorth, slipperyEast, slipperySouth, slipperyWest);
  270. printIsInLavaFormula(os, agentName, lava);
  271. printWallFormula(os, agentName, walls);
  272. printTurningNotAllowedFormulas(os, agentName, noTurnFloor);
  273. printIsFixedFormulas(os, agentName);
  274. os << "\n";
  275. return os;
  276. }
  277. std::ostream& PrismModulesPrinter::printGoalLabel(std::ostream& os, const AgentName &agentName, const cells &goals) {
  278. if(goals.size() == 0) {
  279. os << "formula " << agentName << "IsInGoal = false;\n";
  280. return os;
  281. }
  282. bool first = true;
  283. os << "formula " << agentName << "IsInGoal = ";
  284. for(auto const& cell : goals) {
  285. if(first) first = false; else os << " | ";
  286. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  287. }
  288. os << ";\n";
  289. os << "formula " << agentName << "IsInGoalAndNotDone = " << agentName << "IsInGoal & !" << agentName << "Done;\n";
  290. os << "label \"" << agentName << "IsInGoalAndNotDone\" = " << agentName << "IsInGoal & !" << agentName << "Done;\n";
  291. return os;
  292. }
  293. std::ostream& PrismModulesPrinter::printCrashLabel(std::ostream &os, const std::vector<AgentName> agentNames) {
  294. os << "label crash = ";
  295. bool first = true;
  296. for(auto const& agentName : agentNames) {
  297. if(agentName == "Agent") continue;
  298. if(first) first = false; else os << " | ";
  299. os << "(xAgent=x" << agentName << ")&(yAgent=y" << agentName << ")";
  300. }
  301. os << ";\n\n";
  302. return os;
  303. }
  304. std::ostream& PrismModulesPrinter::printConfiguration(std::ostream& os, const std::vector<Configuration>& configurations) {
  305. for (auto& configuration : configurations) {
  306. if (configuration.overwrite_ || configuration.type_ == ConfigType::Module) {
  307. continue;
  308. }
  309. os << configuration.expression_ << std::endl;
  310. }
  311. return os;
  312. }
  313. std::ostream& PrismModulesPrinter::printAvoidanceLabel(std::ostream &os, const std::vector<AgentName> agentNames, const int &distance) {
  314. os << "label avoidance = ";
  315. bool first = true;
  316. for(auto const& agentName : agentNames) {
  317. if(agentName == "Agent") continue;
  318. if(first) first = false; else os << " | ";
  319. os << "max(xAgent-x" << agentName << ",x" << agentName << "-xAgent)+";
  320. os << "max(yAgent-y" << agentName << ",y" << agentName << "-yAgent) ";
  321. }
  322. os << ";\n\n";
  323. return os;
  324. }
  325. // TODO this does not account for multiple agents yet, i.e. key can be picked up multiple times
  326. std::ostream& PrismModulesPrinter::printKeysLabels(std::ostream& os, const AgentName &agentName, const cells &keys) {
  327. if(keys.size() == 0) return os;
  328. for(auto const& key : keys) {
  329. std::string keyColor = key.getColor();
  330. std::string xKey = "xKey" + keyColor;
  331. std::string yKey = "yKey" + keyColor;
  332. os << "label \"" << agentName << "PickedUp" << keyColor << "Key\" = " << agentName << "_has_" << keyColor << "_key = true;\n";
  333. os << "formula " << agentName << "CanPickUp" << keyColor << "Key = ";
  334. os << "((x" << agentName << "-1 = " << xKey << "&y" << agentName << " = " << yKey << "&view" << agentName << " = 2) |";
  335. os << " (x" << agentName << "+1 = " << xKey << "&y" << agentName << " = " << yKey << "&view" << agentName << " = 0) |";
  336. os << " (x" << agentName << " = " << xKey << "&y" << agentName << "-1 = " << yKey << "&view" << agentName << " = 3) |";
  337. os << " (x" << agentName << " = " << xKey << "&y" << agentName << "+1 = " << yKey << "&view" << agentName << " = 1) ) &";
  338. os << "!" << agentName << "_has_" << keyColor << "_key;";
  339. }
  340. os << "\n";
  341. return os;
  342. }
  343. std::ostream& PrismModulesPrinter::printBooleansForKeys(std::ostream &os, const AgentName &agentName, const cells &keys) {
  344. for(auto const& key : keys) {
  345. os << "\t" << agentName << "_has_"<< key.getColor() << "_key : bool;\n";//init false;\n";
  346. }
  347. os << "\n";
  348. return os;
  349. }
  350. std::ostream& PrismModulesPrinter::printBooleansForBackground(std::ostream &os, const AgentName &agentName, const std::map<Color, cells> &backgroundTiles) {
  351. for(auto const& [color, cells] : backgroundTiles) {
  352. if(cells.size() == 0) continue;
  353. std::string c = getColor(color);
  354. c.at(0) = std::toupper(c.at(0));
  355. os << "\t" << agentName << "_picked_up_" << c << " : bool init false;\n";
  356. }
  357. os << "\n";
  358. return os;
  359. }
  360. std::ostream& PrismModulesPrinter::printActionsForKeys(std::ostream &os, const AgentName &agentName, const cells &keys) {
  361. for(auto const& key : keys) { // TODO ADD Drop action and enforce that pickup only possible if pockets empty (nothing picked up already)
  362. os << "\n";
  363. std::string keyColor = key.getColor();
  364. os << "\t[pickup_" << keyColor << "_key]\t" << pickupGuard(agentName, keyColor) << "-> ";
  365. os << "(" << agentName << "_has_" << keyColor << "_key'=true) & (" << agentName << "_is_carrying_object'=true);\n";
  366. os << "\n";
  367. os << "\t[drop_" << keyColor << "_key_north]\t" << dropGuard(agentName, keyColor, 3) << "-> ";
  368. os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n";
  369. os << "\t[drop_" << keyColor << "_key_west]\t" << dropGuard(agentName, keyColor, 2) << "-> ";
  370. os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n";
  371. os << "\t[drop_" << keyColor << "_key_south]\t" << dropGuard(agentName, keyColor, 1) << "-> ";
  372. os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n";
  373. os << "\t[drop_" << keyColor << "_key_east]\t" << dropGuard(agentName, keyColor, 0) << "-> ";
  374. os << "(" << agentName << "_has_" << keyColor << "_key'=false) & (" << agentName << "_is_carrying_object'=false);\n";
  375. }
  376. return os;
  377. }
  378. std::string PrismModulesPrinter::pickupGuard(const AgentName &agentName, const std::string keyColor ) {
  379. return "!" + agentName + "_is_carrying_object &\t" + agentName + "CanPickUp" + keyColor + "Key ";
  380. }
  381. std::string PrismModulesPrinter::dropGuard(const AgentName &agentName, const std::string keyColor, size_t view) {
  382. return viewVariable(agentName, view, true) + "\t!" + agentName + "CannotMove" + viewDirectionMapping.at(view) + "\t&\t" + agentName + "_has_" + keyColor + "_key\t";
  383. }
  384. std::ostream& PrismModulesPrinter::printActionsForBackground(std::ostream &os, const AgentName &agentName, const std::map<Color, cells> &backgroundTiles) {
  385. for(auto const& [color, cells] : backgroundTiles) {
  386. if(cells.size() == 0) continue;
  387. std::string c = getColor(color);
  388. c.at(0) = std::toupper(c.at(0));
  389. os << "\t[" << agentName << "_pickup_" << c << "] " << agentName << "On" << c << " & !" << agentName << "_picked_up_" << c << " -> ";
  390. os << "(" << agentName << "_picked_up_" << c << "'=true);\n";
  391. }
  392. os << "\n";
  393. return os;
  394. }
  395. std::ostream& PrismModulesPrinter::printInitStruct(std::ostream &os, const AgentNameAndPositionMap &agents, const KeyNameAndPositionMap &keys, const cells &lockedDoors, const cells &unlockedDoors, prism::ModelType modelType) {
  396. os << "init\n";
  397. os << "\t";
  398. bool first = true;
  399. for (auto const& agent : agents) {
  400. if (first) first = false;
  401. else os << " & ";
  402. os << "(!" << agent.first << "IsInGoal & !" << agent.first << "IsInLava & !" << agent.first << "Done & !" << agent.first << "IsOnWall & ";
  403. os << "x" << agent.first << "=" << agent.second.second << " & y" << agent.first << "=" << agent.second.first << ")";
  404. os << " & !" << agent.first << "_is_carrying_object";
  405. if(enforceOneWays) {
  406. os << " & ( !AgentCannotTurn ) ";
  407. } else {
  408. os << " & ( !AgentIsOnSlippery ) ";
  409. }
  410. for (auto const& key : keys) {
  411. os << " & ( !" << agent.first << "_has_" << key.first << "_key )";
  412. }
  413. }
  414. for (auto const& key : keys) {
  415. os << " & ( xKey" << key.first << "="<< key.second.second<< ")";
  416. os << " & ( yKey" << key.first << "=" << key.second.first << ")";
  417. }
  418. for (auto const& locked : lockedDoors) {
  419. os << " & (Door" << locked.getColor() << "locked & !Door" << locked.getColor() << "open)";
  420. }
  421. for (auto const& unlocked : unlockedDoors) {
  422. os << " & (!Door" << unlocked.getColor() << "locked & !Door" << unlocked.getColor() << "open)";
  423. }
  424. if (modelType == ModelType::SMG) {
  425. os << " & move=0";
  426. }
  427. os << "\nendinit\n\n";
  428. return os;
  429. }
  430. 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) {
  431. os << "module " << agentName << "\n";
  432. os << "\tx" << agentName << " : [1.." << boundaries.second << "];\n";
  433. os << "\ty" << agentName << " : [1.." << boundaries.first << "];\n";
  434. os << "\t" << agentName << "_is_carrying_object : bool;\n";
  435. printBooleansForKeys(os, agentName, keys);
  436. printBooleansForBackground(os, agentName, backgroundTiles);
  437. os << "\t" << agentName << "Done : bool;\n";
  438. if(agentWithView) {
  439. os << "\tview" << agentName << " : [0..3];\n";
  440. os << "\n";
  441. 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";
  442. 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";
  443. 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";
  444. if(enforceOneWays) {
  445. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 0 & " << agentName << "CannotMoveEast -> true;\n";
  446. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 1 & " << agentName << "CannotMoveSouth -> true;\n";
  447. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 2 & " << agentName << "CannotMoveWest -> true;\n";
  448. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 3 & " << agentName << "CannotMoveNorth -> true;\n";
  449. }
  450. } else {
  451. os << "\t[" << agentName << "_turns] " << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << moveGuard(agentIndex) << " true -> (x" << agentName << "'=x" << agentName << ")" << moveUpdate(agentIndex) << ";\n";
  452. }
  453. printActionsForKeys(os, agentName, keys);
  454. printActionsForBackground(os, agentName, backgroundTiles);
  455. os << "\n";
  456. printMovementActions(os, agentName, agentIndex, agentWithView);
  457. for(auto const& probability : probabilities) {
  458. printMovementActions(os, agentName, agentIndex, agentWithView, probability);
  459. }
  460. printDoneActions(os, agentName, agentIndex);
  461. printConfiguredActions(os, agentName);
  462. os << "\n";
  463. return os;
  464. }
  465. std::ostream& PrismModulesPrinter::printKeyModule(std::ostream &os, const cell &key, const coordinates &boundaries, AgentName agentName) {
  466. std::string keyIdentifier = "Key" + key.getColor();
  467. os << "module " << keyIdentifier << "\n";
  468. os << "\tx" << keyIdentifier << " : [-1.." << boundaries.second << "];\n";
  469. os << "\ty" << keyIdentifier << " : [-1.." << boundaries.first << "];\n";
  470. os << "\n";
  471. printKeyActions(os, key ,keyIdentifier, agentName);
  472. os << "\n";
  473. return os;
  474. }
  475. std::ostream& PrismModulesPrinter::printKeyActions(std::ostream &os, const cell &key ,const std::string &keyIdentifier, AgentName agentName) {
  476. std::string keyColor = key.getColor();
  477. os << "\t[pickup_" << keyColor << "key]\t" << pickupGuard(agentName, keyColor) << "-> ";
  478. os << "(xKey" << keyColor << "'=-1) & (yKey" << keyColor << "'=-1)" << ";\n";
  479. os << "\t[drop_" << keyColor << "_key_north]\t" << dropGuard(agentName, keyColor, 3) << "-> ";
  480. os << "(xKey" << keyColor << "'=x" << agentName << ") & (yKey" << keyColor << "'=y" <<agentName << "-1) ;\n";
  481. os << "\t[drop_" << keyColor << "_key_west]\t" << dropGuard(agentName, keyColor, 2) << "-> ";
  482. os << "(xKey" << keyColor << "'=x" << agentName << "-1) & (yKey" << keyColor << "'=y" <<agentName << ") ;\n";
  483. os << "\t[drop_" << keyColor << "_key_south]\t" << dropGuard(agentName, keyColor, 1) << "-> ";
  484. os << "(xKey" << keyColor << "'=x" << agentName << ") & (yKey" << keyColor << "'=y" <<agentName << "+1) ;\n";
  485. os << "\t[drop_" << keyColor << "_key_east]\t" << dropGuard(agentName, keyColor, 0) << "-> ";
  486. os << "(xKey" << keyColor << "'=x" << agentName << "+1) & (yKey" << keyColor << "'=y" <<agentName << ") ;\n";
  487. return os;
  488. }
  489. std::ostream& PrismModulesPrinter::printDoorModule(std::ostream &os, const cell &door, const coordinates &boundaries, AgentName agent) {
  490. std::string doorIdentifier = "Door" + door.getColor();
  491. os << "module " << doorIdentifier << "\n";
  492. os << "\t" << doorIdentifier << "locked : bool;\n";
  493. os << "\t" << doorIdentifier << "open : bool;\n";
  494. printDoorActions(os, door, doorIdentifier, agent);
  495. return os;
  496. }
  497. std::ostream& PrismModulesPrinter::printDoorActions(std::ostream &os, const cell &door ,const std::string &doorIdentifier, AgentName agentName) {
  498. os << "\t[" << "unlock_" << doorIdentifier << "]\t" << unlockGuard(agentName, door) << " -> ";
  499. os << "(" << doorIdentifier << "locked'=false) & (" << doorIdentifier << "open'=true) ;\n";
  500. os << "\t[" << "toggle_" << doorIdentifier << "]\t" << toggleGuard(agentName, door) << " -> ";
  501. os << "(" << doorIdentifier << "open'=!" << doorIdentifier << "open) ;\n";
  502. return os;
  503. }
  504. std::string PrismModulesPrinter::unlockGuard(const AgentName &agentName, const cell& door) {
  505. std::string doorColor = door.getColor();
  506. std::string ret;
  507. ret += agentName + "_has_" + doorColor + "_key & ";
  508. ret += "((" + viewVariable(agentName, 0, true) + "x" + agentName + "+ 1 = " + std::to_string(door.column) + " & y" + agentName + "= " + std::to_string(door.row) + ")";
  509. ret += " | (" + viewVariable(agentName, 1, true) + "x" + agentName + " = " + std::to_string(door.column) + " & y" + agentName + " + 1 = " + std::to_string(door.row) + ")";
  510. ret += " | (" + viewVariable(agentName, 2, true) + "x" + agentName + "- 1 = " + std::to_string(door.column) + " & y" + agentName + "= " + std::to_string(door.row) + ")";
  511. ret += " | (" + viewVariable(agentName, 3, true) + "x" + agentName + " = " + std::to_string(door.column) + " & y" + agentName + " - 1 = " + std::to_string(door.row) + "))";
  512. return ret;
  513. }
  514. std::string PrismModulesPrinter::toggleGuard(const AgentName &agentName, const cell& door) {
  515. std::string doorColor = door.getColor();
  516. std::string ret;
  517. ret += "!Door" + doorColor + "locked & (";
  518. ret += "(" + viewVariable(agentName, 0, true) + "x" + agentName + "+ 1 = " + std::to_string(door.column) + " & y" + agentName + "= " + std::to_string(door.row) + ")";
  519. ret += " | (" + viewVariable(agentName, 1, true) + "x" + agentName + " = " + std::to_string(door.column) + " & y" + agentName + " + 1 = " + std::to_string(door.row) + ")";
  520. ret += " | (" + viewVariable(agentName, 2, true) + "x" + agentName + "- 1 = " + std::to_string(door.column) + " & y" + agentName + "= " + std::to_string(door.row) + ")";
  521. ret += " | (" + viewVariable(agentName, 3, true) + "x" + agentName + " = " + std::to_string(door.column) + " & y" + agentName + " - 1 = " + std::to_string(door.row) + "))";
  522. return ret;
  523. }
  524. std::ostream& PrismModulesPrinter::printConfiguredActions(std::ostream &os, const AgentName &agentName) {
  525. for (auto& config : configuration) {
  526. if (config.type_ == ConfigType::Module && !config.overwrite_ && agentName == config.module_) {
  527. os << config.expression_ ;
  528. }
  529. }
  530. os << "\n";
  531. return os;
  532. }
  533. std::ostream& PrismModulesPrinter::printMovementActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const bool agentWithView, const float &probability) {
  534. if(probability >= 1) {
  535. 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";
  536. 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";
  537. 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";
  538. 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";
  539. } else {
  540. std::string probabilityString = std::to_string(probability);
  541. std::string percentageString = std::to_string((int)(100 * probability));
  542. std::string complementProbabilityString = std::to_string(1 - probability);
  543. os << "\t[" << agentName << "_move_north_" << percentageString << "] ";
  544. os << moveGuard(agentIndex) << viewVariable(agentName, 3, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveNorth -> ";
  545. os << probabilityString << ": (y" << agentName << "'=y" << agentName << "-1)" << moveUpdate(agentIndex) << " + ";
  546. os << complementProbabilityString << ": (y" << agentName << "'=y" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  547. os << "\t[" << agentName << "_move_east_" << percentageString << "] ";
  548. os << moveGuard(agentIndex) << viewVariable(agentName, 0, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveEast -> ";
  549. os << probabilityString << ": (x" << agentName << "'=x" << agentName << "+1)" << moveUpdate(agentIndex) << " + ";
  550. os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  551. os << "\t[" << agentName << "_move_south_" << percentageString << "] ";
  552. os << moveGuard(agentIndex) << viewVariable(agentName, 1, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveSouth -> ";
  553. os << probabilityString << ": (y" << agentName << "'=y" << agentName << "+1)" << moveUpdate(agentIndex) << " + ";
  554. os << complementProbabilityString << ": (y" << agentName << "'=y" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  555. os << "\t[" << agentName << "_move_west_" << percentageString << "] ";
  556. os << moveGuard(agentIndex) << viewVariable(agentName, 2, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveWest -> ";
  557. os << probabilityString << ": (x" << agentName << "'=x" << agentName << "-1)" << moveUpdate(agentIndex) << " + ";
  558. os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  559. }
  560. return os;
  561. }
  562. std::ostream& PrismModulesPrinter::printDoneActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex) {
  563. os << "\t[" << agentName << "_done]" << moveGuard(agentIndex) << agentName << "IsInGoal | " << agentName << "IsInLava -> (" << agentName << "Done'=true);\n";
  564. return os;
  565. }
  566. std::ostream& PrismModulesPrinter::printSlipperyTurn(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, std::set<std::string> &slipperyActions, const std::array<bool, 8>& neighborhood, SlipperyType orientation) {
  567. constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 9;
  568. std::array<std::string, ALL_POSS_DIRECTIONS> positionTransition = {
  569. /* north */ "(y" + agentName + "'=y" + agentName + "-1)",
  570. /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)",
  571. /* east */ "(x" + agentName + "'=x" + agentName + "+1)",
  572. /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)",
  573. /* south */ "(y" + agentName + "'=y" + agentName + "+1)",
  574. /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)",
  575. /* west */ "(x" + agentName + "'=x" + agentName + "-1)",
  576. /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)",
  577. /* own position */ "(x" + agentName + "'=x" + agentName + ") & (y" + agentName + "'=y" + agentName + ")"
  578. };
  579. // view transition appdx in form (guard, update part)
  580. // IMPORTANT: No mod() usage for turn left due to bug in mod() function for decrement
  581. std::array<std::tuple<std::string, std::string, std::string>, 3> viewTransition = {
  582. std::make_tuple(" & " + agentName + "SlipperyTurnRightAllowed ", " & (view" + agentName + "'=mod(view" + agentName + " + 1, 4))", "_right]"),
  583. std::make_tuple(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + ">0", " & (view" + agentName + "'=view" + agentName + " - 1)", "_left]"),
  584. std::make_tuple(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + "=0", " & (view" + agentName + "'=3)", "_left]")
  585. };
  586. // direction specifics
  587. std::string actionName;
  588. std::string positionGuard;
  589. std::size_t remainPosIndex = 8;
  590. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir; // { n, ne, w, se, s, sw, w, nw, CURRENT POS }
  591. switch (orientation)
  592. {
  593. case SlipperyType::North:
  594. actionName = "\t[" + agentName + "turn_at_slip_north";
  595. positionGuard = "\t" + agentName + "IsOnSlipperyNorth";
  596. prob_piece_dir = { 0, 0, 0, 1, 1, 1, 0, 0, 0 /* <- R */ };
  597. break;
  598. case SlipperyType::South:
  599. actionName = "\t[" + agentName + "turn_at_slip_south";
  600. positionGuard = "\t" + agentName + "IsOnSlipperySouth";
  601. prob_piece_dir = { 1, 1, 0, 0, 0, 0, 0, 1, 0 /* <- R */ };
  602. break;
  603. case SlipperyType::East:
  604. actionName = "\t[" + agentName + "turn_at_slip_east";
  605. positionGuard = "\t" + agentName + "IsOnSlipperyEast";
  606. prob_piece_dir = { 0, 0, 0, 0, 0, 1, 1, 1, 0 /* <- R */ };
  607. break;
  608. case SlipperyType::West:
  609. actionName = "\t[" + agentName + "turn_at_slip_west";
  610. positionGuard = "\t" + agentName + "IsOnSlipperyWest";
  611. prob_piece_dir = { 0, 1, 1, 1, 0, 0, 0, 0, 0 /* <- R */ };
  612. break;
  613. }
  614. slipperyActions.insert(actionName + "_left]");
  615. slipperyActions.insert(actionName + "_right]");
  616. // override probability to 0 if corresp. direction is blocked
  617. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS - 1; i++) {
  618. if (!neighborhood.at(i))
  619. prob_piece_dir.at(i) = 0;
  620. }
  621. // determine residual probability (R) by replacing 0 with (1 - overall sum)
  622. prob_piece_dir.at(remainPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0);
  623. // <DEBUG_AREA>
  624. {
  625. assert(prob_piece_dir.at(remainPosIndex) <= 9 && prob_piece_dir.at(remainPosIndex) >= 6 && "Value not in Range!");
  626. assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!");
  627. }
  628. // </DEBUG_AREA>
  629. // generic output (for every view transition)
  630. for (std::size_t v = 0; v < viewTransition.size(); v++) {
  631. os << actionName << std::get<2>(viewTransition.at(v)) << moveGuard(agentIndex) << positionGuard << std::get<0>(viewTransition.at(v));
  632. // os << actionName << std::get<2>(viewTransition.at(v)) << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << std::get<0>(viewTransition.at(v));
  633. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  634. os << (i == 0 ? " -> " : " + ") << prob_piece_dir.at(i) << "/" << PROB_PIECES << " : " << positionTransition.at(i) << std::get<1>(viewTransition.at(v)) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  635. }
  636. }
  637. return os;
  638. }
  639. std::ostream& PrismModulesPrinter::printSlipperyMove(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, std::set<std::string> &slipperyActions, const std::array<bool, 8>& neighborhood, SlipperyType orientation) {
  640. constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 8;
  641. std::array<std::string, ALL_POSS_DIRECTIONS> positionTransition = {
  642. /* north */ "(y" + agentName + "'=y" + agentName + "-1)",
  643. /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)",
  644. /* east */ "(x" + agentName + "'=x" + agentName + "+1)",
  645. /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)",
  646. /* south */ "(y" + agentName + "'=y" + agentName + "+1)",
  647. /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)",
  648. /* west */ "(x" + agentName + "'=x" + agentName + "-1)",
  649. /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)"
  650. };
  651. // direction specifics
  652. std::size_t straightPosIndex;
  653. std::string actionName, specialTransition; // if straight ahead is blocked
  654. std::string positionGuard;
  655. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir; // { n, ne, w, se, s, sw, w, nw }
  656. switch (orientation)
  657. {
  658. case SlipperyType::North:
  659. actionName = "\t[" + agentName + "move_on_slip_north]";
  660. positionGuard = "\t" + agentName + "IsOnSlipperyNorth";
  661. prob_piece_dir = { 0, 0, 1, 2, 0 /* <- R */, 2, 1, 0 };
  662. straightPosIndex = 4;
  663. specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)");
  664. break;
  665. case SlipperyType::South:
  666. actionName = "\t[" + agentName + "move_on_slip_south]";
  667. positionGuard = "\t" + agentName + "IsOnSlipperySouth";
  668. prob_piece_dir = { 0 /* <- R */, 2, 1, 0, 0, 0, 1, 2 };
  669. straightPosIndex = 0; // always north
  670. specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)");
  671. break;
  672. case SlipperyType::East:
  673. actionName = "\t[" + agentName + "move_on_slip_east]";
  674. positionGuard = "\t" + agentName + "IsOnSlipperyEast";
  675. prob_piece_dir = { 1, 0, 0, 0, 1, 2, 0 /* <- R */, 2 };
  676. straightPosIndex = 6;
  677. specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)");
  678. break;
  679. case SlipperyType::West:
  680. actionName = "\t[" + agentName + "move_on_slip_west]";
  681. positionGuard = "\t" + agentName + "IsOnSlipperyWest";
  682. prob_piece_dir = { 1, 2, 0 /* <- R */, 2, 1, 0, 0, 0 };
  683. straightPosIndex = 2;
  684. specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)");
  685. break;
  686. }
  687. slipperyActions.insert(actionName);
  688. // override probability to 0 if corresp. direction is blocked
  689. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  690. if (!neighborhood.at(i))
  691. prob_piece_dir.at(i) = 0;
  692. }
  693. // determine residual probability (R) by replacing 0 with (1 - overall sum)
  694. if(enforceOneWays) {
  695. prob_piece_dir = {0,0,0,0,0,0,0,0};
  696. }
  697. prob_piece_dir.at(straightPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0);
  698. // <DEBUG_AREA>
  699. {
  700. assert(prob_piece_dir.at(straightPosIndex) <= 9 && prob_piece_dir.at(straightPosIndex) >= 3 && "Value not in Range!");
  701. assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!");
  702. 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!"));
  703. 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!"));
  704. 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!"));
  705. 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!"));
  706. }
  707. // </DEBUG_AREA>
  708. // special case: straight forward is blocked (then remain in same position)
  709. positionTransition.at(straightPosIndex) = specialTransition;
  710. // generic output (for every view and every possible view direction)
  711. os << actionName << moveGuard(agentIndex) << positionGuard << " & " << agentName << "SlipperyMoveForwardAllowed ";
  712. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  713. os << (i == 0 ? " -> " : " + ") << prob_piece_dir.at(i) << "/" << PROB_PIECES << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  714. }
  715. return os;
  716. }
  717. std::ostream& PrismModulesPrinter::printEndmodule(std::ostream &os) {
  718. os << "endmodule\n";
  719. os << "\n";
  720. return os;
  721. }
  722. std::ostream& PrismModulesPrinter::printPlayerStruct(std::ostream &os, const AgentName &agentName, const bool agentWithView, const std::vector<float> &probabilities, const std::set<std::string> &slipperyActions) {
  723. os << "player " << agentName << "\n\t";
  724. bool first = true;
  725. std::list<std::string> allActions = { "_move_north", "_move_east", "_move_south", "_move_west" };
  726. std::list<std::string> movementActions = allActions;
  727. for(auto const& probability : probabilities) {
  728. std::string percentageString = std::to_string((int)(100 * probability));
  729. for(auto const& movement : movementActions) {
  730. allActions.push_back(movement + "_" + percentageString);
  731. }
  732. }
  733. if(agentWithView) {
  734. allActions.push_back("_turn_left");
  735. allActions.push_back("_turn_right");
  736. } else {
  737. allActions.push_back("_turns");
  738. }
  739. for(auto const& action : allActions) {
  740. if(first) first = false; else os << ", ";
  741. os << "[" << agentName << action << "]";
  742. }
  743. for(auto const& action : slipperyActions) {
  744. os << ", " << action;
  745. }
  746. os << ", [" << agentName << "_done]";
  747. os << "\nendplayer\n";
  748. return os;
  749. }
  750. std::ostream& PrismModulesPrinter::printGlobalMoveVariable(std::ostream &os, const size_t &numberOfPlayer) {
  751. os << "\nglobal move : [0.." << std::to_string(numberOfPlayer - 1) << "];\n\n";
  752. return os;
  753. }
  754. 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) {
  755. if(lava.size() != 0) {
  756. os << "rewards \"" << agentName << "SafetyNoBFS\"\n";
  757. os << "\t" <<agentName << "IsInLavaAndNotDone: -100;\n";
  758. os << "endrewards\n";
  759. }
  760. if (!goals.empty() || !lava.empty()) {
  761. os << "rewards \"" << agentName << "SafetyNoBFSAndGoal\"\n";
  762. if(goals.size() != 0) os << "\t" << agentName << "IsInGoalAndNotDone: 100;\n";
  763. if(lava.size() != 0) os << "\t" << agentName << "IsInLavaAndNotDone: -100;\n";
  764. os << "endrewards\n";
  765. }
  766. os << "rewards \"" << agentName << "Time\"\n";
  767. os << "\t!" << agentName << "IsInGoal : -1;\n";
  768. if(goals.size() != 0) os << "\t" << agentName << "IsInGoalAndNotDone: 100;\n";
  769. if(lava.size() != 0) os << "\t" << agentName << "IsInLavaAndNotDone: -100;\n";
  770. os << "endrewards\n";
  771. if(stateRewards.size() > 0) {
  772. os << "rewards \"" << agentName << "SafetyWithBFS\"\n";
  773. if(lava.size() != 0) os << "\t" << agentName << "IsInLavaAndNotDone: -100;\n";
  774. for(auto const [coordinates, reward] : stateRewards) {
  775. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  776. }
  777. os << "endrewards\n";
  778. }
  779. if(stateRewards.size() > 0) {
  780. os << "rewards \"" << agentName << "SafetyWithBFSAndGoal\"\n";
  781. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  782. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  783. for(auto const [coordinates, reward] : stateRewards) {
  784. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  785. }
  786. os << "endrewards\n";
  787. }
  788. for(auto const entry : backgroundTiles)
  789. {
  790. std::cout << getColor(entry.first) << " ";
  791. for(auto const cell : entry.second){
  792. std::cout << cell.getCoordinates().first << " " << cell.getCoordinates().second << std::endl;
  793. }
  794. }
  795. if(backgroundTiles.size() > 0) {
  796. os << "rewards \"TaxiReward\"\n";
  797. os << "\t!AgentIsInGoal : -1;\n";
  798. std::string allPassengersPickedUp = "";
  799. bool first = true;
  800. for(auto const [color, cells] : backgroundTiles) {
  801. if(cells.size() == 0) continue;
  802. if(first) first = false; else allPassengersPickedUp += "&";
  803. std::string c = getColor(color);
  804. c.at(0) = std::toupper(c.at(0));
  805. std::string visitedLabel = agentName + "_picked_up_" + c;
  806. allPassengersPickedUp += visitedLabel;
  807. os << "[" << agentName << "_pickup_" << c << "] true : 100;\n";
  808. }
  809. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & " << allPassengersPickedUp << " : 100;\n";
  810. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & !(" << allPassengersPickedUp << ") : -100;\n";
  811. os << "endrewards";
  812. }
  813. return os;
  814. }
  815. std::string PrismModulesPrinter::moveGuard(const size_t &agentIndex) {
  816. return isGame() ? " move=" + std::to_string(agentIndex) + " & " : " ";
  817. }
  818. std::string PrismModulesPrinter::moveUpdate(const size_t &agentIndex) {
  819. return isGame() ?
  820. (agentIndex == numberOfPlayer - 1) ?
  821. " & (move'=0) " :
  822. " & (move'=" + std::to_string(agentIndex + 1) + ") " :
  823. "";
  824. }
  825. std::string PrismModulesPrinter::viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView) {
  826. return agentWithView ? " view" + agentName + "=" + std::to_string(agentDirection) + " & " : " ";
  827. }
  828. bool PrismModulesPrinter::isGame() const {
  829. return modelType == ModelType::SMG;
  830. }
  831. }