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.

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