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.

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