Advertisement
Guest User

Laborator 6 - Minimax

a guest
Mar 31st, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. Two player Zero-sum games
  2. --------------------------
  3.  
  4. score_1 + score_2 = 0
  5.  
  6. X si 0:
  7. - 1 castiga: score_1 = 1, score_2 = -1
  8. - 2 castiga: score_1 = -1, score_2 = 1
  9. - remiza: score_1 = score_2 = 0
  10.  
  11.  
  12.  
  13. Minimax
  14. --------
  15.  
  16. int maxi(State state) {
  17. if (is_final(state))
  18. return score(state); // 1 daca castiga jucatorul 1, -1 daca castiga jucatorul 2, 0 la remiza
  19.  
  20. int val = -INF;
  21. for (new_state: next_states(state)) {
  22. int new_state_score = mini(new_state);
  23. val = max(val, new_state_score);
  24. }
  25.  
  26. return val;
  27. }
  28.  
  29. int mini(State state) {
  30. if (is_final(state))
  31. return score(state); // 1 daca castiga jucatorul 1, -1 daca castiga jucatorul 2, 0 la remiza
  32.  
  33. int val = INF;
  34. for (new_state: next_states(state)) {
  35. int new_state_score = maxi(new_state);
  36. val = min(val, new_state_score);
  37. }
  38.  
  39. return val;
  40. }
  41.  
  42.  
  43. Negamax
  44. --------
  45.  
  46. int negamax(State state, int player, int depth) {
  47. if (is_final(state) || depth == 0)
  48. return score(state, player);
  49.  
  50. int val = -INF;
  51. Move best_move = null;
  52.  
  53. for (move: all_moves) {
  54. State new_state = apply_move(state, move);
  55. int new_state_score = -negamax(new_state, -player, depth - 1);
  56. if (new_state_score >= val) {
  57. val = new_state_score;
  58. best_move = move;
  59. }
  60. }
  61.  
  62. return val;
  63. }
  64.  
  65. int score(state, player) {
  66. if (is_final(state)) {
  67. // return INF daca player a castigat, -INF daca a pierdut, 0 la egal
  68. }
  69.  
  70. // returnam un scor euristic care sa fie cu atat mai mare cu cat player e mai in avantaj
  71. }
  72.  
  73. main:
  74. negamax(state, player, 7);
  75.  
  76. Euristica
  77. ----------
  78. Pentru sah: score_1 = nr_piese_1 - nr_piese_2
  79. score_2 = nr_piese_2 - nr_piese_1
  80.  
  81. - o euristica mai buna ar fi sa acordam puncte pieselor: regina - 10p, tura - 5p, ... pion - 1p
  82.  
  83. Nim
  84. ----
  85.  
  86. 1 0 0 -> castiga celelalt jucator (cel care nu e la mutare)
  87. 2 0 0 -> castiga jucatorul la mutare
  88.  
  89. 2 5 3
  90.  
  91. -> nu implementati euristica (return 0 daca starea nu e finala)
  92.  
  93. -> de implementat: apply_move si is_final + minimax (urmariti TODO-urile)
  94.  
  95.  
  96. Reversi
  97. --------
  98.  
  99. Euristica: score_player = nr_bile_player - nr_bile_adversar
  100.  
  101. Common pitfalls:
  102. ----------------
  103. pair<Move, int> negamax(...) {
  104.  
  105. }
  106.  
  107. // aveti grija la ce intoarceti in Move (sa fie mutare valida) ... aveti grija la functiile de max/min cu -INF sau INF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement