Advertisement
max2201111

to je nejlepsi minimax good s T

May 8th, 2024
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.20 KB | Science | 0 0
  1. import chess
  2. import time
  3.  
  4. def ten_moves_rule(board):
  5.     """Custom rule to evaluate a draw condition based on the last ten moves, considering no captures or pawn moves."""
  6.     history = list(board.move_stack)
  7.     if len(history) < 10:
  8.         return False
  9.  
  10.     for move in history[-10:]:
  11.         if board.is_capture(move):
  12.             return False
  13.         if board.piece_type_at(move.from_square) == chess.PAWN:
  14.             return False
  15.     return True
  16.  
  17. def evaluate_board(board, depth):
  18.     """Evaluate the board state for minimax decision-making."""
  19.     if board.is_checkmate():
  20.         return -1000 + depth if board.turn == chess.WHITE else 1000 - depth
  21.     elif board.is_stalemate():
  22.         return 1
  23.     elif board.is_insufficient_material():
  24.         return 2
  25.     elif ten_moves_rule(board):
  26.         return 3
  27.     return 4  # Default heuristic if none of the above conditions are met
  28.  
  29. # def minimax(board, depth, alpha, beta, maximizing_player, depth2, printed_depths, position_count, memo, start_time, last_print_time,T):
  30. #     current_time = time.time()
  31. #     if current_time - last_print_time[0] >= 1:
  32. #         elapsed_hours, remainder = divmod(current_time - start_time, 3600)
  33. #         elapsed_minutes, elapsed_seconds = divmod(remainder, 60)
  34. #         print(f"\r{int(elapsed_hours):02d}h {int(elapsed_minutes):02d}m {int(elapsed_seconds):02d}s", end='', flush=True)
  35. #         last_print_time[0] = current_time
  36.  
  37. #     position_count[0] += 1
  38. #     if position_count[0] % 1000000 == 0:
  39. #         print(f"\nProzkoumano {position_count[0]} pozic.")
  40.  
  41. #     key = (board.fen(), maximizing_player, depth, alpha, beta)
  42. #     if key in memo:
  43. #         return memo[key]
  44.  
  45. #     if depth == 0 or board.is_game_over() or T:
  46. #         eval = evaluate_board(board, depth2)
  47. #         memo[key] = ([], eval)
  48. #         T = True
  49. #         return [], eval
  50.  
  51. #     best_eval = float('-inf') if maximizing_player else float('inf')
  52. #     best_sequence = []
  53.  
  54. #     for move in board.legal_moves:
  55. #         move_san = board.san(move)
  56. #         board.push(move)
  57. #         sequence, eval = minimax(board, depth - 1, alpha, beta, not maximizing_player, depth2 + 1, printed_depths, position_count, memo, start_time, last_print_time, T)
  58. #         board.pop()
  59.        
  60. #         if (maximizing_player and eval > best_eval) or (not maximizing_player and eval < best_eval):
  61. #             best_eval = eval
  62. #             best_sequence = [(move, move_san)] + sequence
  63.  
  64. #         if maximizing_player:
  65. #             alpha = max(alpha, eval)
  66. #         else:
  67. #             beta = min(beta, eval)
  68.  
  69. #         if beta <= alpha:
  70. #             break
  71.  
  72. #     memo[key] = (best_sequence, best_eval)
  73. #     if depth2 not in printed_depths:
  74. #         printed_depths.add(depth2)
  75. #         print(f"\nHloubka {depth2} prozkoumána, čas: {time.time() - start_time:.2f}s")
  76. #     return best_sequence, best_eval
  77.  
  78. # def minimax(board, depth, alpha, beta, maximizing_player, depth2, printed_depths, position_count, memo, start_time, last_print_time, T):
  79. #     current_time = time.time()
  80. #     if current_time - last_print_time[0] >= 1:
  81. #         elapsed_hours, remainder = divmod(current_time - start_time, 3600)
  82. #         elapsed_minutes, elapsed_seconds = divmod(remainder, 60)
  83. #         print(f"\r{int(elapsed_hours):02d}h {int(elapsed_minutes):02d}m {int(elapsed_seconds):02d}s", end='', flush=True)
  84. #         last_print_time[0] = current_time
  85.  
  86. #     position_count[0] += 1
  87. #     if position_count[0] % 1000000 == 0:
  88. #         print(f"\nProzkoumano {position_count[0]} pozic.")
  89.  
  90. #     key = (board.fen(), maximizing_player, depth, alpha, beta)
  91. #     if key in memo:
  92. #         return memo[key][0], memo[key][1], T
  93.  
  94. #     if depth == 0 or board.is_game_over():
  95. #         eval = evaluate_board(board, depth2)
  96. #         memo[key] = ([], eval)
  97. #         if abs(eval) == 1000 - depth2:  # Specificky kontroluje, zda je hodnocení rovno 1000 nebo -1000 s přihlédnutím k hloubce
  98. #             return [], eval, True
  99. #         return [], eval, T
  100.  
  101. #     best_eval = float('-inf') if maximizing_player else float('inf')
  102. #     best_sequence = []
  103.  
  104. #     for move in board.legal_moves:
  105. #         board.push(move)
  106. #         sequence, eval, T = minimax(board, depth - 1, alpha, beta, not maximizing_player, depth2 + 1, printed_depths, position_count, memo, start_time, last_print_time, T)
  107. #         board.pop()
  108.  
  109. #         if T:  # Pokud bylo T nastaveno na True v nějaké z rekurzí, předčasně ukončit všechny další
  110. #             return [], eval, True
  111.  
  112. #         if (maximizing_player and eval > best_eval) or (not maximizing_player and eval < best_eval):
  113. #             best_eval = eval
  114. #             best_sequence = [(move, board.san(move))] + sequence
  115.  
  116. #         if maximizing_player:
  117. #             alpha = max(alpha, eval)
  118. #         else:
  119. #             beta = min(beta, eval)
  120.  
  121. #         if beta <= alpha:
  122. #             break
  123.  
  124. #     memo[key] = (best_sequence, best_eval)
  125. #     if depth2 not in printed_depths:
  126. #         printed_depths.add(depth2)
  127. #         print(f"\nHloubka {depth2} prozkoumána, čas: {time.time() - start_time:.2f}s")
  128. #     return best_sequence, best_eval, T
  129.  
  130. # def minimax(board, depth, alpha, beta, maximizing_player, depth2, printed_depths, position_count, memo, start_time, last_print_time, T):
  131. #     current_time = time.time()
  132. #     if current_time - last_print_time[0] >= 1:
  133. #         elapsed_hours, remainder = divmod(current_time - start_time, 3600)
  134. #         elapsed_minutes, elapsed_seconds = divmod(remainder, 60)
  135. #         print(f"\r{int(elapsed_hours):02d}h {int(elapsed_minutes):02d}m {int(elapsed_seconds):02d}s", end='', flush=True)
  136. #         last_print_time[0] = current_time
  137.  
  138. #     position_count[0] += 1
  139. #     if position_count[0] % 1000000 == 0:
  140. #         print(f"\nProzkoumano {position_count[0]} pozic.")
  141.  
  142. #     key = (board.fen(), maximizing_player, depth, alpha, beta)
  143. #     if key in memo:
  144. #         return memo[key][0], memo[key][1], T
  145.  
  146. #     if depth == 0 or board.is_game_over():
  147. #         eval = evaluate_board(board, depth2)
  148. #         memo[key] = ([], eval)
  149. #         return [], eval, T
  150.  
  151. #     best_eval = float('-inf') if maximizing_player else float('inf')
  152. #     best_sequence = []
  153.  
  154. #     for move in board.legal_moves:
  155. #         board.push(move)
  156. #         sequence, eval, T = minimax(board, depth - 1, alpha, beta, not maximizing_player, depth2 + 1, printed_depths, position_count, memo, start_time, last_print_time, T)
  157. #         board.pop()
  158.  
  159. #         if (maximizing_player and eval > best_eval) or (not maximizing_player and eval < best_eval):
  160. #             best_eval = eval
  161. #             best_sequence = [(move, board.san(move))] + sequence  # Ujistěte se, že sekvence je správně aktualizována
  162.  
  163. #         if maximizing_player:
  164. #             alpha = max(alpha, eval)
  165. #         else:
  166. #             beta = min(beta, eval)
  167.  
  168. #         if beta <= alpha:
  169. #             break
  170.  
  171. #     memo[key] = (best_sequence, best_eval)
  172. #     if depth2 not in printed_depths:
  173. #         printed_depths.add(depth2)
  174. #         print(f"\nHloubka {depth2} prozkoumána, čas: {time.time() - start_time:.2f}s")
  175. #     return best_sequence, best_eval, T
  176.  
  177. def minimax(board, depth, alpha, beta, maximizing_player, depth2, printed_depths, position_count, memo, start_time, last_print_time, T):
  178.     current_time = time.time()
  179.     if current_time - last_print_time[0] >= 1:
  180.         elapsed_hours, remainder = divmod(current_time - start_time, 3600)
  181.         elapsed_minutes, elapsed_seconds = divmod(remainder, 60)
  182.         print(f"\r{int(elapsed_hours):02d}h {int(elapsed_minutes):02d}m {int(elapsed_seconds):02d}s", end='', flush=True)
  183.         last_print_time[0] = current_time
  184.  
  185.     position_count[0] += 1
  186.     if position_count[0] % 1000000 == 0:
  187.         print(f"\nProzkoumano {position_count[0]} pozic.")
  188.  
  189.     key = (board.fen(), maximizing_player, depth, alpha, beta)
  190.     if key in memo:
  191.         return memo[key][0], memo[key][1], T
  192.  
  193.     if depth == 0 or board.is_game_over():
  194.         eval = evaluate_board(board, depth2)
  195.         memo[key] = ([], eval)
  196.         T = T or abs(eval) >= 1000  # Nastavit T na True, pokud dosáhneme kritické eval hodnoty
  197.         return [], eval, T
  198.  
  199.     best_eval = float('-inf') if maximizing_player else float('inf')
  200.     best_sequence = []
  201.  
  202.     for move in board.legal_moves:
  203.         board.push(move)
  204.         sequence, eval, returned_T = minimax(board, depth - 1, alpha, beta, not maximizing_player, depth2 + 1, printed_depths, position_count, memo, start_time, last_print_time, T)
  205.         board.pop()
  206.        
  207.         # Nastavení T na True, pokud některá z rekurzí vrátí True
  208.         if returned_T:
  209.             T = True
  210.  
  211.         if (maximizing_player and eval > best_eval) or (not maximizing_player and eval < best_eval):
  212.             best_eval = eval
  213.             best_sequence = [(move, board.san(move))] + sequence
  214.  
  215.         # Předčasné ukončení prohledávání, pokud T je True
  216.         if T:
  217.             memo[key] = (best_sequence, best_eval)
  218.             return best_sequence, best_eval, T
  219.  
  220.         if maximizing_player:
  221.             alpha = max(alpha, eval)
  222.         else:
  223.             beta = min(beta, eval)
  224.  
  225.         if beta <= alpha:
  226.             break
  227.  
  228.     memo[key] = (best_sequence, best_eval)
  229.     if depth2 not in printed_depths:
  230.         printed_depths.add(depth2)
  231.         print(f"\nHloubka {depth2} prozkoumána, čas: {time.time() - start_time:.2f}s")
  232.     return best_sequence, best_eval, T
  233.  
  234.  
  235. start_time = time.time()
  236. position_count = [0]
  237. memo = {}
  238. last_print_time = [start_time]
  239. printed_depths = set()
  240. start_fen = "7k/8/3Q4/5K2/8/8/8/8 w - - 0 1"
  241.  
  242. #start_fen = "7K/8/k1P5/7p/8/8/8/8 w - - 0 1"
  243.  
  244.  
  245. board = chess.Board(start_fen)
  246.  
  247. print("Počáteční šachovnice:")
  248. print(board)
  249. print("Počáteční FEN:", board.fen(), "\n")
  250.  
  251. sequence, final_score, T = minimax(board, 19, float('-inf'), float('inf'), True, 0, printed_depths, position_count, memo, start_time, last_print_time,T=False)
  252. print("\n\nOptimal move sequence:")
  253. for move, san in sequence:
  254.     print("Move:", san)
  255.     board.push(move)
  256.     print("Board:\n", board)
  257.     print("FEN:", board.fen())
  258.     print("Evaluation:", evaluate_board(board, 0), "\n")
  259. print("Final evaluation score:", final_score)
  260.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement