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.

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