Advertisement
max2201111

bad propagation not so bad

Apr 27th, 2024
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.21 KB | Science | 0 0
  1. import chess
  2. from collections import deque
  3. import hashlib
  4.  
  5. def simplify_fen_string(fen):
  6.     parts = fen.split(' ')
  7.     simplified_fen = ' '.join(parts[:4])  # Only includes position information
  8.     return simplified_fen
  9.  
  10. def fen_hash(fen):
  11.     return hashlib.md5(fen.encode('utf-8')).hexdigest()
  12.  
  13. def evaluate_position(board, depth):
  14.     if board.is_checkmate():
  15.         return 1000 - depth if board.turn == chess.WHITE else -1000 + depth
  16.     if board.is_stalemate() or board.is_insufficient_material() or board.is_seventyfive_moves() or board.is_fivefold_repetition() or board.is_variant_draw():
  17.         return 0
  18.     return None
  19.  
  20. def is_white_turn(fen):
  21.     return 'w' in fen.split(' ')[1]
  22.  
  23. def propagate_results(AR):
  24.     changed = True
  25.     while changed:
  26.         changed = False
  27.         for fen in AR:
  28.             if 'children' in AR[fen] and any(AR[child]['result'] is not None for child in AR[fen]['children']):
  29.                 if is_white_turn(fen):
  30.                     candidates = [(child, AR[child]['result']) for child in AR[fen]['children'] if AR[child]['result'] is not None and AR[child]['result'] < 0]
  31.                     if candidates:
  32.                         fen_ch, min_negative_score = min(candidates, key=lambda x: x[1])
  33.                         new_result = 1001 + min_negative_score
  34.                         if AR[fen]['result'] != new_result:
  35.                             AR[fen]['result'] = new_result
  36.                             AR[fen]['sequence'] = [fen_ch]
  37.                             changed = True
  38.                 else:
  39.                     candidates = [(child, AR[child]['result']) for child in AR[fen]['children'] if AR[child]['result'] is not None and AR[child]['result'] > 0]
  40.                     if candidates:
  41.                         fen_ch, max_positive_score = max(candidates, key=lambda x: x[1])
  42.                         new_result = -1001 + max_positive_score
  43.                         if AR[fen]['result'] != new_result:
  44.                             AR[fen]['result'] = new_result
  45.                             AR[fen]['sequence'] = [fen_ch]
  46.                             changed = True
  47.  
  48. def generate_moves(initial_board):
  49.     queue = deque()
  50.     initial_fen = simplify_fen_string(initial_board.fen())
  51.     queue.append((initial_board.copy(), initial_fen, None, 0))
  52.     transposition_table = {}
  53.     node_count = 0
  54.     explored_depths = set()
  55.  
  56.     while queue:
  57.         current_board, current_fen, parent_fen, depth = queue.popleft()
  58.         fen_key = fen_hash(current_fen)
  59.  
  60.         if fen_key not in transposition_table:
  61.             result = evaluate_position(current_board, depth)
  62.             transposition_table[fen_key] = result
  63.  
  64.             AR[current_fen] = {
  65.                 'fen': current_fen,
  66.                 'parent': parent_fen,
  67.                 'color': chess.WHITE if current_board.turn == chess.WHITE else chess.BLACK,
  68.                 'children': [],
  69.                 'result': result,
  70.                 'depth': depth
  71.             }
  72.             if parent_fen:
  73.                 AR[parent_fen]['children'].append(current_fen)
  74.  
  75.             if depth not in explored_depths:
  76.                 print(f"New depth reached: {depth}")
  77.                 explored_depths.add(depth)
  78.  
  79.             if result is None:
  80.                 for move in current_board.legal_moves:
  81.                     current_board.push(move)
  82.                     new_fen = simplify_fen_string(current_board.fen())
  83.                     if fen_hash(new_fen) not in transposition_table:
  84.                         queue.append((current_board.copy(), new_fen, current_fen, depth + 1))
  85.                     current_board.pop()
  86.  
  87. # Initialize the chess board and the AR dictionary
  88. initial_fen = "1k6/3K1Q2/8/8/8/8/8/8 w - - 0 1"
  89. board = chess.Board(initial_fen)
  90. AR = {}
  91.  
  92. # Start generating moves and propagating results
  93. generate_moves(board)
  94. propagate_results(AR)  # Pass the AR dictionary as an argument
  95.  
  96. # Print current AR data
  97. def print_current_AR_data():
  98.     for key in sorted(AR, key=lambda k: AR[k]['depth']):
  99.         node = AR[key]
  100.         if node['depth'] <= 3:  # Display up to a certain depth
  101.             print(f"Depth {node['depth']} {key}: Result={node['result']} Children={len(node['children'])}")
  102.  
  103. print_current_AR_data()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement