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.

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