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.

721 lines
36 KiB

  1. #include "PrismModulesPrinter.h"
  2. #include <map>
  3. #include <string>
  4. namespace prism {
  5. PrismModulesPrinter::PrismModulesPrinter(const ModelType &modelType, const size_t &numberOfPlayer, std::vector<Configuration> config, const bool enforceOneWays)
  6. : modelType(modelType), numberOfPlayer(numberOfPlayer), enforceOneWays(enforceOneWays), configuration(config) {
  7. }
  8. std::ostream& PrismModulesPrinter::printModel(std::ostream &os, const ModelType &modelType) {
  9. switch(modelType) {
  10. case(ModelType::MDP):
  11. os << "mdp";
  12. break;
  13. case(ModelType::SMG):
  14. os << "smg";
  15. break;
  16. }
  17. os << "\n\n";
  18. return os;
  19. }
  20. std::ostream& PrismModulesPrinter::printBackgroundLabels(std::ostream &os, const AgentName &agentName, const std::pair<Color, cells> &backgroundTiles) {
  21. if(backgroundTiles.second.size() == 0) return os;
  22. bool first = true;
  23. std::string color = getColor(backgroundTiles.first);
  24. color.at(0) = std::toupper(color.at(0));
  25. os << "formula " << agentName << "On" << color << " = ";
  26. for(auto const& cell : backgroundTiles.second) {
  27. if(first) first = false; else os << " | ";
  28. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  29. }
  30. os << ";\n";
  31. os << "label \"" << agentName << "On" << color << "\" = " << agentName << "On" << color << ";\n";
  32. return os;
  33. }
  34. std::ostream& PrismModulesPrinter::printRestrictionFormula(std::ostream& os, const AgentName &agentName, const std::string &direction, const cells &cells) {
  35. bool first = true;
  36. os << "formula " << agentName << "CannotMove" << direction << " = " ;
  37. for(auto const& cell : cells) {
  38. if(first) first = false;
  39. else os << " | ";
  40. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  41. }
  42. os << ";\n";
  43. return os;
  44. }
  45. std::ostream& PrismModulesPrinter::printIsOnSlipperyFormula(std::ostream& os, const AgentName &agentName, const std::vector<std::reference_wrapper<cells>> &slipperyCollection, const cells &slipperyNorth, const cells &slipperyEast, const cells &slipperySouth, const cells &slipperyWest) {
  46. if(std::find_if(slipperyCollection.cbegin(), slipperyCollection.cend(), [=](const std::reference_wrapper<cells>& c) { return !c.get().empty(); }) == slipperyCollection.cend()) {
  47. os << "formula " << agentName << "IsOnSlippery = false;\n";
  48. return os;
  49. }
  50. bool first = true;
  51. os << "formula " << agentName << "IsOnSlippery = ";
  52. for (const auto& slippery: slipperyCollection) {
  53. for(const auto& cell : slippery.get()) {
  54. if(first) first = false; else os << " | ";
  55. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  56. }
  57. }
  58. os << ";\n";
  59. if(enforceOneWays) {
  60. first = true;
  61. os << "formula " << agentName << "IsOnSlipperyNorth = ";
  62. for (const auto& cell: slipperyNorth) {
  63. if(first) first = false; else os << " | ";
  64. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  65. }
  66. os << ";\n";
  67. first = true;
  68. os << "formula " << agentName << "IsOnSlipperyEast = ";
  69. for (const auto& cell: slipperyEast) {
  70. if(first) first = false; else os << " | ";
  71. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  72. }
  73. os << ";\n";
  74. first = true;
  75. os << "formula " << agentName << "IsOnSlipperySouth = ";
  76. for (const auto& cell: slipperySouth) {
  77. if(first) first = false; else os << " | ";
  78. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  79. }
  80. os << ";\n";
  81. first = true;
  82. os << "formula " << agentName << "IsOnSlipperyWest = ";
  83. for (const auto& cell: slipperyWest) {
  84. if(first) first = false; else os << " | ";
  85. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  86. }
  87. os << ";\n";
  88. }
  89. return os;
  90. }
  91. std::ostream& PrismModulesPrinter::printIsInLavaFormula(std::ostream& os, const AgentName &agentName, const cells &lava) {
  92. if(lava.size() == 0) {
  93. os << "formula " << agentName << "IsInLava = false;\n";
  94. return os;
  95. }
  96. bool first = true;
  97. os << "formula " << agentName << "IsInLava = ";
  98. for(auto const& cell : lava) {
  99. if(first) first = false; else os << " | ";
  100. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  101. }
  102. os << ";\n";
  103. os << "formula " << agentName << "IsInLavaAndNotDone = " << agentName << "IsInLava & !" << agentName << "Done;\n";
  104. os << "label \"" << agentName << "IsInLavaAndNotDone\" = " << agentName << "IsInLava & !" << agentName << "Done;\n";
  105. return os;
  106. }
  107. std::ostream& PrismModulesPrinter::printTurningNotAllowedFormulas(std::ostream& os, const AgentName &agentName, const cells &noTurnFloor) {
  108. if( (!enforceOneWays or noTurnFloor.size() == 0) or (noTurnFloor.size() == 0) ) {
  109. os << "formula " << agentName << "CannotTurn = false;\n";
  110. return os;
  111. }
  112. bool first = true;
  113. os << "formula " << agentName << "CannotTurn = ";
  114. for(auto const& cell : noTurnFloor) {
  115. if(first) first = false; else os << " | ";
  116. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  117. }
  118. os << " | " << agentName << "IsOnSlippery;\n";
  119. return os;
  120. }
  121. std::ostream& PrismModulesPrinter::printIsFixedFormulas(std::ostream& os, const AgentName &agentName) {
  122. os << "formula " << agentName << "IsFixed = false;\n";
  123. os << "formula " << agentName << "SlipperyTurnLeftAllowed = true;\n";
  124. os << "formula " << agentName << "SlipperyTurnRightAllowed = true;\n";
  125. os << "formula " << agentName << "SlipperyMoveForwardAllowed = true;\n";
  126. os << "label \"FixedStates\" = " << agentName << "IsFixed | !" << agentName << "SlipperyTurnRightAllowed | !" << agentName << "SlipperyTurnLeftAllowed | !" << agentName << "SlipperyMoveForwardAllowed | " << agentName << "IsInGoal | " << agentName << "IsInLava";
  127. if(enforceOneWays) {
  128. os << " | " << agentName << "CannotTurn";
  129. }
  130. os << ";\n";
  131. //os << "label \"FixedStates\" = " << agentName << "IsFixed | " << agentName << "IsOnSlippery | " << agentName << "IsInGoal | " << agentName << "IsInLava;\n";
  132. return os;
  133. }
  134. std::ostream& PrismModulesPrinter::printWallFormula(std::ostream& os, const AgentName &agentName, const cells &walls) {
  135. os << "formula " << agentName << "IsOnWall = ";
  136. bool first = true;
  137. for(auto const& cell : walls) {
  138. if(first) first = false; else os << " | ";
  139. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  140. }
  141. os << ";\n";
  142. return os;
  143. }
  144. std::ostream& PrismModulesPrinter::printFormulas(std::ostream& os,
  145. const AgentName &agentName,
  146. const cells &restrictionNorth,
  147. const cells &restrictionEast,
  148. const cells &restrictionSouth,
  149. const cells &restrictionWest,
  150. const std::vector<std::reference_wrapper<cells>> &slipperyCollection,
  151. const cells &lava,
  152. const cells &walls,
  153. const cells &noTurnFloor,
  154. const cells &slipperyNorth,
  155. const cells &slipperyEast,
  156. const cells &slipperySouth,
  157. const cells &slipperyWest) {
  158. printRestrictionFormula(os, agentName, "North", restrictionNorth);
  159. printRestrictionFormula(os, agentName, "East ", restrictionEast);
  160. printRestrictionFormula(os, agentName, "South", restrictionSouth);
  161. printRestrictionFormula(os, agentName, "West ", restrictionWest);
  162. printIsOnSlipperyFormula(os, agentName, slipperyCollection, slipperyNorth, slipperyEast, slipperySouth, slipperyWest);
  163. printIsInLavaFormula(os, agentName, lava);
  164. printWallFormula(os, agentName, walls);
  165. printTurningNotAllowedFormulas(os, agentName, noTurnFloor);
  166. printIsFixedFormulas(os, agentName);
  167. os << "\n";
  168. return os;
  169. }
  170. std::ostream& PrismModulesPrinter::printGoalLabel(std::ostream& os, const AgentName &agentName, const cells &goals) {
  171. if(goals.size() == 0) {
  172. os << "formula " << agentName << "IsInGoal = false;\n";
  173. return os;
  174. }
  175. bool first = true;
  176. os << "formula " << agentName << "IsInGoal = ";
  177. for(auto const& cell : goals) {
  178. if(first) first = false; else os << " | ";
  179. os << "(x" << agentName << "=" << cell.column << "&y" << agentName << "=" << cell.row << ")";
  180. }
  181. os << ";\n";
  182. os << "formula " << agentName << "IsInGoalAndNotDone = " << agentName << "IsInGoal & !" << agentName << "Done;\n";
  183. os << "label \"" << agentName << "IsInGoalAndNotDone\" = " << agentName << "IsInGoal & !" << agentName << "Done;\n";
  184. return os;
  185. }
  186. std::ostream& PrismModulesPrinter::printCrashLabel(std::ostream &os, const std::vector<AgentName> agentNames) {
  187. os << "label crash = ";
  188. bool first = true;
  189. for(auto const& agentName : agentNames) {
  190. if(agentName == "Agent") continue;
  191. if(first) first = false; else os << " | ";
  192. os << "(xAgent=x" << agentName << ")&(yAgent=y" << agentName << ")";
  193. }
  194. os << ";\n\n";
  195. return os;
  196. }
  197. std::ostream& PrismModulesPrinter::printConfiguration(std::ostream& os, const std::vector<Configuration>& configurations) {
  198. os << "\n// Configuration\n";
  199. for (auto& configuration : configurations) {
  200. if (configuration.overwrite_ || configuration.type_ == ConfigType::Module) {
  201. continue;
  202. }
  203. os << configuration.expression_ << std::endl;
  204. }
  205. return os;
  206. }
  207. std::ostream& PrismModulesPrinter::printAvoidanceLabel(std::ostream &os, const std::vector<AgentName> agentNames, const int &distance) {
  208. os << "label avoidance = ";
  209. bool first = true;
  210. for(auto const& agentName : agentNames) {
  211. if(agentName == "Agent") continue;
  212. if(first) first = false; else os << " | ";
  213. os << "max(xAgent-x" << agentName << ",x" << agentName << "-xAgent)+";
  214. os << "max(yAgent-y" << agentName << ",y" << agentName << "-yAgent) ";
  215. }
  216. os << ";\n\n";
  217. return os;
  218. }
  219. // TODO this does not account for multiple agents yet, i.e. key can be picked up multiple times
  220. std::ostream& PrismModulesPrinter::printKeysLabels(std::ostream& os, const AgentName &agentName, const cells &keys) {
  221. if(keys.size() == 0) return os;
  222. for(auto const& key : keys) {
  223. int xKey = key.getCoordinates().first;
  224. int yKey = key.getCoordinates().second;
  225. std::string keyColor = key.getColor();
  226. os << "label \"" << agentName << "PickedUp" << keyColor << "Key\" = " << agentName << "_has_" << keyColor << "_key = true;\n";
  227. os << "formula " << agentName << "CanPickUp" << keyColor << "Key = ";
  228. os << "((x" << agentName << "-1 = " << xKey << "&y" << agentName << " = " << yKey << "&view" << agentName << " = 1) |";
  229. os << " (x" << agentName << "+1 = " << xKey << "&y" << agentName << " = " << yKey << "&view" << agentName << " = 3) |";
  230. os << " (x" << agentName << " = " << xKey << "&y" << agentName << "-1 = " << yKey << "&view" << agentName << " = 0) |";
  231. os << " (x" << agentName << " = " << xKey << "&y" << agentName << "+1 = " << yKey << "&view" << agentName << " = 2) ) &";
  232. os << "!" << agentName << "_has_" << keyColor << "_key;";
  233. }
  234. os << "\n";
  235. return os;
  236. }
  237. std::ostream& PrismModulesPrinter::printBooleansForKeys(std::ostream &os, const AgentName &agentName, const cells &keys) {
  238. for(auto const& key : keys) {
  239. os << "\t" << agentName << "_has_"<< key.getColor() << "_key : bool;\n";//init false;\n";
  240. }
  241. os << "\n";
  242. return os;
  243. }
  244. std::ostream& PrismModulesPrinter::printBooleansForBackground(std::ostream &os, const AgentName &agentName, const std::map<Color, cells> &backgroundTiles) {
  245. for(auto const& [color, cells] : backgroundTiles) {
  246. if(cells.size() == 0) continue;
  247. std::string c = getColor(color);
  248. c.at(0) = std::toupper(c.at(0));
  249. os << "\t" << agentName << "_picked_up_" << c << " : bool init false;\n";
  250. }
  251. os << "\n";
  252. return os;
  253. }
  254. std::ostream& PrismModulesPrinter::printActionsForKeys(std::ostream &os, const AgentName &agentName, const cells &keys) {
  255. for(auto const& key : keys) {
  256. std::string keyColor = key.getColor();
  257. os << "\t[pickup_" << keyColor << "_key] " << agentName << "CanPickUp" << keyColor << "Key -> ";
  258. os << "(" << agentName << "_has_" << keyColor << "_key'=true);";
  259. }
  260. os << "\n";
  261. return os;
  262. }
  263. std::ostream& PrismModulesPrinter::printActionsForBackground(std::ostream &os, const AgentName &agentName, const std::map<Color, cells> &backgroundTiles) {
  264. for(auto const& [color, cells] : backgroundTiles) {
  265. if(cells.size() == 0) continue;
  266. std::string c = getColor(color);
  267. c.at(0) = std::toupper(c.at(0));
  268. os << "\t[" << agentName << "_pickup_" << c << "] " << agentName << "On" << c << " & !" << agentName << "_picked_up_" << c << " -> ";
  269. os << "(" << agentName << "_picked_up_" << c << "'=true);\n";
  270. }
  271. os << "\n";
  272. return os;
  273. }
  274. std::ostream& PrismModulesPrinter::printInitStruct(std::ostream &os, const AgentName &agentName, const cells &keys) {
  275. os << "init\n";
  276. os << "\t(!AgentIsInGoal & !AgentIsInLava & !AgentDone & !AgentIsOnWall)";
  277. if(enforceOneWays) {
  278. os << " & ( !AgentCannotTurn ) ";
  279. } else {
  280. os << " & ( !AgentIsOnSlippery ) ";
  281. }
  282. for (auto const& key : keys) {
  283. os << " & ( !" << agentName << "_has_" << key.getColor() << "_key )";
  284. }
  285. os << "\nendinit\n\n";
  286. return os;
  287. }
  288. 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) {
  289. os << "module " << agentName << "\n";
  290. os << "\tx" << agentName << " : [1.." << boundaries.second << "];\n";
  291. os << "\ty" << agentName << " : [1.." << boundaries.first << "];\n";
  292. printBooleansForKeys(os, agentName, keys);
  293. printBooleansForBackground(os, agentName, backgroundTiles);
  294. os << "\t" << agentName << "Done : bool;\n";
  295. if(agentWithView) {
  296. os << "\tview" << agentName << " : [0..3];\n";
  297. os << "\n";
  298. 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";
  299. 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";
  300. 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";
  301. if(enforceOneWays) {
  302. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 0 & " << agentName << "CannotMoveEast -> true;\n";
  303. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 1 & " << agentName << "CannotMoveSouth -> true;\n";
  304. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 2 & " << agentName << "CannotMoveWest -> true;\n";
  305. os << "\t[" << agentName << "_stuck] !" << agentName << "IsFixed & " << agentName << "CannotTurn & view" << agentName << " = 3 & " << agentName << "CannotMoveNorth -> true;\n";
  306. }
  307. } else {
  308. os << "\t[" << agentName << "_turns] " << " !" << agentName << "CannotTurn & " << " !" << agentName << "IsFixed & " << moveGuard(agentIndex) << " true -> (x" << agentName << "'=x" << agentName << ")" << moveUpdate(agentIndex) << ";\n";
  309. }
  310. printActionsForKeys(os, agentName, keys);
  311. printActionsForBackground(os, agentName, backgroundTiles);
  312. os << "\n";
  313. printMovementActions(os, agentName, agentIndex, agentWithView);
  314. for(auto const& probability : probabilities) {
  315. printMovementActions(os, agentName, agentIndex, agentWithView, probability);
  316. }
  317. printDoneActions(os, agentName, agentIndex);
  318. printConfiguredActions(os, agentName);
  319. os << "\n";
  320. return os;
  321. }
  322. std::ostream& PrismModulesPrinter::printConfiguredActions(std::ostream &os, const AgentName &agentName) {
  323. os << "\t//Configuration \n";
  324. for (auto& config : configuration) {
  325. if (config.type_ == ConfigType::Module && !config.overwrite_ && agentName == config.module_) {
  326. os << config.expression_ ;
  327. }
  328. }
  329. os << "\n";
  330. return os;
  331. }
  332. std::ostream& PrismModulesPrinter::printMovementActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex, const bool agentWithView, const float &probability) {
  333. if(probability >= 1) {
  334. 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";
  335. 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";
  336. 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";
  337. 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";
  338. } else {
  339. std::string probabilityString = std::to_string(probability);
  340. std::string percentageString = std::to_string((int)(100 * probability));
  341. std::string complementProbabilityString = std::to_string(1 - probability);
  342. os << "\t[" << agentName << "_move_north_" << percentageString << "] ";
  343. os << moveGuard(agentIndex) << viewVariable(agentName, 3, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveNorth -> ";
  344. os << probabilityString << ": (y" << agentName << "'=y" << agentName << "-1)" << moveUpdate(agentIndex) << " + ";
  345. os << complementProbabilityString << ": (y" << agentName << "'=y" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  346. os << "\t[" << agentName << "_move_east_" << percentageString << "] ";
  347. os << moveGuard(agentIndex) << viewVariable(agentName, 0, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveEast -> ";
  348. os << probabilityString << ": (x" << agentName << "'=x" << agentName << "+1)" << moveUpdate(agentIndex) << " + ";
  349. os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  350. os << "\t[" << agentName << "_move_south_" << percentageString << "] ";
  351. os << moveGuard(agentIndex) << viewVariable(agentName, 1, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveSouth -> ";
  352. os << probabilityString << ": (y" << agentName << "'=y" << agentName << "+1)" << moveUpdate(agentIndex) << " + ";
  353. os << complementProbabilityString << ": (y" << agentName << "'=y" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  354. os << "\t[" << agentName << "_move_west_" << percentageString << "] ";
  355. os << moveGuard(agentIndex) << viewVariable(agentName, 2, agentWithView) << " !" << agentName << "IsFixed & " << " !" << agentName << "IsOnSlippery & !" << agentName << "IsInLava & !" << agentName << "CannotMoveWest -> ";
  356. os << probabilityString << ": (x" << agentName << "'=x" << agentName << "-1)" << moveUpdate(agentIndex) << " + ";
  357. os << complementProbabilityString << ": (x" << agentName << "'=x" << agentName << ") " << moveUpdate(agentIndex) << ";\n";
  358. }
  359. return os;
  360. }
  361. std::ostream& PrismModulesPrinter::printDoneActions(std::ostream &os, const AgentName &agentName, const size_t &agentIndex) {
  362. os << "\t[" << agentName << "_done]" << moveGuard(agentIndex) << agentName << "IsInGoal | " << agentName << "IsInLava -> (" << agentName << "Done'=true);\n";
  363. return os;
  364. }
  365. 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) {
  366. constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 9;
  367. std::array<std::string, ALL_POSS_DIRECTIONS> positionTransition = {
  368. /* north */ "(y" + agentName + "'=y" + agentName + "-1)",
  369. /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)",
  370. /* east */ "(x" + agentName + "'=x" + agentName + "+1)",
  371. /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)",
  372. /* south */ "(y" + agentName + "'=y" + agentName + "+1)",
  373. /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)",
  374. /* west */ "(x" + agentName + "'=x" + agentName + "-1)",
  375. /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)",
  376. /* own position */ "(x" + agentName + "'=x" + agentName + ") & (y" + agentName + "'=y" + agentName + ")"
  377. };
  378. // view transition appdx in form (guard, update part)
  379. // IMPORTANT: No mod() usage for turn left due to bug in mod() function for decrement
  380. std::array<std::pair<std::string, std::string>, 3> viewTransition = {
  381. /* turn to right */ std::make_pair(" & " + agentName + "SlipperyTurnRightAllowed ", " & (view" + agentName + "'=mod(view" + agentName + " + 1, 4))"),
  382. /* turn to left */ std::make_pair(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + ">0", " & (view" + agentName + "'=view" + agentName + " - 1)"),
  383. /* turn to left */ std::make_pair(" & " + agentName + "SlipperyTurnLeftAllowed & view" + agentName + "=0", " & (view" + agentName + "'=3)")
  384. };
  385. // direction specifics
  386. std::string actionName;
  387. std::size_t remainPosIndex = 8;
  388. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir; // { n, ne, w, se, s, sw, w, nw, CURRENT POS }
  389. switch (orientation)
  390. {
  391. case SlipperyType::North:
  392. actionName = "\t[" + agentName + "turn_at_slip_north]";
  393. prob_piece_dir = { 0, 0, 0, 1, 1, 1, 0, 0, 0 /* <- R */ };
  394. break;
  395. case SlipperyType::South:
  396. actionName = "\t[" + agentName + "turn_at_slip_south]";
  397. prob_piece_dir = { 1, 1, 0, 0, 0, 0, 0, 1, 0 /* <- R */ };
  398. break;
  399. case SlipperyType::East:
  400. actionName = "\t[" + agentName + "turn_at_slip_east]";
  401. prob_piece_dir = { 0, 0, 0, 0, 0, 1, 1, 1, 0 /* <- R */ };
  402. break;
  403. case SlipperyType::West:
  404. actionName = "\t[" + agentName + "turn_at_slip_west]";
  405. prob_piece_dir = { 0, 1, 1, 1, 0, 0, 0, 0, 0 /* <- R */ };
  406. break;
  407. }
  408. slipperyActions.insert(actionName);
  409. // override probability to 0 if corresp. direction is blocked
  410. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS - 1; i++) {
  411. if (!neighborhood.at(i))
  412. prob_piece_dir.at(i) = 0;
  413. }
  414. // determine residual probability (R) by replacing 0 with (1 - overall sum)
  415. prob_piece_dir.at(remainPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0);
  416. // <DEBUG_AREA>
  417. {
  418. assert(prob_piece_dir.at(remainPosIndex) <= 9 && prob_piece_dir.at(remainPosIndex) >= 6 && "Value not in Range!");
  419. assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!");
  420. }
  421. // </DEBUG_AREA>
  422. // generic output (for every view transition)
  423. for (std::size_t v = 0; v < viewTransition.size(); v++) {
  424. os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << viewTransition.at(v).first;
  425. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  426. os << (i == 0 ? " -> " : " + ") << prob_piece_dir.at(i) << "/" << PROB_PIECES << " : " << positionTransition.at(i) << viewTransition.at(v).second << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  427. }
  428. }
  429. return os;
  430. }
  431. 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) {
  432. constexpr std::size_t PROB_PIECES = 9, ALL_POSS_DIRECTIONS = 8;
  433. std::array<std::string, ALL_POSS_DIRECTIONS> positionTransition = {
  434. /* north */ "(y" + agentName + "'=y" + agentName + "-1)",
  435. /* north east */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "-1)",
  436. /* east */ "(x" + agentName + "'=x" + agentName + "+1)",
  437. /* east south */ "(x" + agentName + "'=x" + agentName + "+1) & (y" + agentName + "'=y" + agentName + "+1)",
  438. /* south */ "(y" + agentName + "'=y" + agentName + "+1)",
  439. /* south west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "+1)",
  440. /* west */ "(x" + agentName + "'=x" + agentName + "-1)",
  441. /* north west */ "(x" + agentName + "'=x" + agentName + "-1) & (y" + agentName + "'=y" + agentName + "-1)"
  442. };
  443. // direction specifics
  444. std::size_t straightPosIndex;
  445. std::string actionName, specialTransition; // if straight ahead is blocked
  446. std::array<std::size_t, ALL_POSS_DIRECTIONS> prob_piece_dir; // { n, ne, w, se, s, sw, w, nw }
  447. switch (orientation)
  448. {
  449. case SlipperyType::North:
  450. actionName = "\t[" + agentName + "move_on_slip_north]";
  451. prob_piece_dir = { 0, 0, 1, 2, 0 /* <- R */, 2, 1, 0 };
  452. straightPosIndex = 4;
  453. specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)");
  454. break;
  455. case SlipperyType::South:
  456. actionName = "\t[" + agentName + "move_on_slip_south]";
  457. prob_piece_dir = { 0 /* <- R */, 2, 1, 0, 0, 0, 1, 2 };
  458. straightPosIndex = 0; // always north
  459. specialTransition = "(y" + agentName + "'=y" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)");
  460. break;
  461. case SlipperyType::East:
  462. actionName = "\t[" + agentName + "move_on_slip_east]";
  463. prob_piece_dir = { 1, 0, 0, 0, 1, 2, 0 /* <- R */, 2 };
  464. straightPosIndex = 6;
  465. specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "-1)");
  466. break;
  467. case SlipperyType::West:
  468. actionName = "\t[" + agentName + "move_on_slip_west]";
  469. prob_piece_dir = { 1, 2, 0 /* <- R */, 2, 1, 0, 0, 0 };
  470. straightPosIndex = 2;
  471. specialTransition = "(x" + agentName + "'=x" + agentName + (!neighborhood.at(straightPosIndex) ? ")" : "+1)");
  472. break;
  473. }
  474. slipperyActions.insert(actionName);
  475. // override probability to 0 if corresp. direction is blocked
  476. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  477. if (!neighborhood.at(i))
  478. prob_piece_dir.at(i) = 0;
  479. }
  480. // determine residual probability (R) by replacing 0 with (1 - overall sum)
  481. if(enforceOneWays) {
  482. prob_piece_dir = {0,0,0,0,0,0,0,0};
  483. }
  484. prob_piece_dir.at(straightPosIndex) = PROB_PIECES - std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0);
  485. // <DEBUG_AREA>
  486. {
  487. assert(prob_piece_dir.at(straightPosIndex) <= 9 && prob_piece_dir.at(straightPosIndex) >= 3 && "Value not in Range!");
  488. assert(std::accumulate(prob_piece_dir.begin(), prob_piece_dir.end(), 0) == PROB_PIECES && "Does not sum up to 1!");
  489. 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!"));
  490. 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!"));
  491. 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!"));
  492. 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!"));
  493. }
  494. // </DEBUG_AREA>
  495. // special case: straight forward is blocked (then remain in same position)
  496. positionTransition.at(straightPosIndex) = specialTransition;
  497. // generic output (for every view and every possible view direction)
  498. os << actionName << moveGuard(agentIndex) << " x" << agentName << "=" << c.second << " & y" << agentName << "=" << c.first << " & " << agentName << "SlipperyMoveForwardAllowed ";
  499. for (std::size_t i = 0; i < ALL_POSS_DIRECTIONS; i++) {
  500. os << (i == 0 ? " -> " : " + ") << prob_piece_dir.at(i) << "/" << PROB_PIECES << " : " << positionTransition.at(i) << moveUpdate(agentIndex) << (i == ALL_POSS_DIRECTIONS - 1 ? ";\n" : "\n");
  501. }
  502. return os;
  503. }
  504. std::ostream& PrismModulesPrinter::printEndmodule(std::ostream &os) {
  505. os << "endmodule\n";
  506. os << "\n";
  507. return os;
  508. }
  509. std::ostream& PrismModulesPrinter::printPlayerStruct(std::ostream &os, const AgentName &agentName, const bool agentWithView, const std::vector<float> &probabilities, const std::set<std::string> &slipperyActions) {
  510. os << "player " << agentName << "\n\t";
  511. bool first = true;
  512. std::list<std::string> allActions = { "_move_north", "_move_east", "_move_south", "_move_west" };
  513. std::list<std::string> movementActions = allActions;
  514. for(auto const& probability : probabilities) {
  515. std::string percentageString = std::to_string((int)(100 * probability));
  516. for(auto const& movement : movementActions) {
  517. allActions.push_back(movement + "_" + percentageString);
  518. }
  519. }
  520. if(agentWithView) {
  521. allActions.push_back("_turn_left");
  522. allActions.push_back("_turn_right");
  523. } else {
  524. allActions.push_back("_turns");
  525. }
  526. for(auto const& action : allActions) {
  527. if(first) first = false; else os << ", ";
  528. os << "[" << agentName << action << "]";
  529. }
  530. for(auto const& action : slipperyActions) {
  531. os << ", " << action;
  532. }
  533. os << "\nendplayer\n";
  534. return os;
  535. }
  536. std::ostream& PrismModulesPrinter::printGlobalMoveVariable(std::ostream &os, const size_t &numberOfPlayer) {
  537. os << "\nglobal move : [0.." << std::to_string(numberOfPlayer - 1) << "] init 0;\n\n";
  538. return os;
  539. }
  540. 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) {
  541. if(lava.size() != 0) {
  542. os << "rewards \"SafetyNoBFS\"\n";
  543. os << "\tAgentIsInLavaAndNotDone: -100;\n";
  544. os << "endrewards\n";
  545. }
  546. if (!goals.empty() || !lava.empty()) {
  547. os << "rewards \"SafetyNoBFSAndGoal\"\n";
  548. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  549. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  550. os << "endrewards\n";
  551. }
  552. os << "rewards \"Time\"\n";
  553. os << "\t!AgentIsInGoal : -1;\n";
  554. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  555. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  556. os << "endrewards\n";
  557. if(stateRewards.size() > 0) {
  558. os << "rewards \"SafetyWithBFS\"\n";
  559. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  560. for(auto const [coordinates, reward] : stateRewards) {
  561. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  562. }
  563. os << "endrewards\n";
  564. }
  565. if(stateRewards.size() > 0) {
  566. os << "rewards \"SafetyWithBFSAndGoal\"\n";
  567. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone: 100;\n";
  568. if(lava.size() != 0) os << "\tAgentIsInLavaAndNotDone: -100;\n";
  569. for(auto const [coordinates, reward] : stateRewards) {
  570. os << "\txAgent=" << coordinates.first << "&yAgent=" << coordinates.second << " : " << reward << ";\n";
  571. }
  572. os << "endrewards\n";
  573. }
  574. for(auto const entry : backgroundTiles)
  575. {
  576. std::cout << getColor(entry.first) << " ";
  577. for(auto const cell : entry.second){
  578. std::cout << cell.getCoordinates().first << " " << cell.getCoordinates().second << std::endl;
  579. }
  580. }
  581. if(backgroundTiles.size() > 0) {
  582. os << "rewards \"TaxiReward\"\n";
  583. os << "\t!AgentIsInGoal : -1;\n";
  584. std::string allPassengersPickedUp = "";
  585. bool first = true;
  586. for(auto const [color, cells] : backgroundTiles) {
  587. if(cells.size() == 0) continue;
  588. if(first) first = false; else allPassengersPickedUp += "&";
  589. std::string c = getColor(color);
  590. c.at(0) = std::toupper(c.at(0));
  591. std::string visitedLabel = agentName + "_picked_up_" + c;
  592. allPassengersPickedUp += visitedLabel;
  593. os << "[" << agentName << "_pickup_" << c << "] true : 100;\n";
  594. }
  595. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & " << allPassengersPickedUp << " : 100;\n";
  596. if(goals.size() != 0) os << "\tAgentIsInGoalAndNotDone & !(" << allPassengersPickedUp << ") : -100;\n";
  597. os << "endrewards";
  598. }
  599. return os;
  600. }
  601. std::string PrismModulesPrinter::moveGuard(const size_t &agentIndex) {
  602. return isGame() ? " move=" + std::to_string(agentIndex) + " & " : " ";
  603. }
  604. std::string PrismModulesPrinter::moveUpdate(const size_t &agentIndex) {
  605. return isGame() ?
  606. (agentIndex == numberOfPlayer - 1) ?
  607. " & (move'=0) " :
  608. " & (move'=" + std::to_string(agentIndex + 1) + ") " :
  609. "";
  610. }
  611. std::string PrismModulesPrinter::viewVariable(const AgentName &agentName, const size_t &agentDirection, const bool agentWithView) {
  612. return agentWithView ? " view" + agentName + "=" + std::to_string(agentDirection) + " & " : " ";
  613. }
  614. bool PrismModulesPrinter::isGame() const {
  615. return modelType == ModelType::SMG;
  616. }
  617. }