Python Forum
Python trivial endgame engine is not working as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python trivial endgame engine is not working as expected
#1
I have detected where roughly is a problem. It is in this function which gets processed for node "1" only, see the output section below: (I'm trying to put the node 6109 into A[1]['sequence'] but I cannot) Can someone help to me fix this function def evaluate_and_partition(A, node_id=1):

def evaluate_and_partition(A, node_id=1): node = A[node_id] # Debugging line to track the function calls print(f"Evaluating Node ID: {node_id}") # Return previously calculated result if this node has been processed if node['processed']: print(f"Returning processed Node ID: {node_id} with result: {node['result']}, moves to mate: {node['moves_to_mate']}") return node['result'], node['moves_to_mate'] # Initialize variables to store the best result and path found so far optimal_result = -float('inf') # Assume worst case scenario optimal_moves_to_mate = None # Initialize as None, to be updated optimal_child_id = None # Keep track of which child leads to the optimal result # Iterate through each child of the current node for child_id in node['children']: print(child_id) # Recursively evaluate each child child_result, child_moves_to_mate = evaluate_and_partition(A, child_id) # Check if the current child's result improves upon the best result found so far if child_result is not None and ( child_result > optimal_result or (child_result == optimal_result and optimal_moves_to_mate is not None and child_moves_to_mate is not None and child_moves_to_mate < optimal_moves_to_mate)): # Update optimal values based on the current child's results optimal_result = child_result optimal_moves_to_mate = child_moves_to_mate optimal_child_id = child_id # After evaluating all children, update the current node with the best result and path found node['result'] = optimal_result if optimal_moves_to_mate is not None: # If a path to victory or draw is found, increment moves to mate as we move up the tree node['moves_to_mate'] = optimal_moves_to_mate + 1 else: # If no path improves the position, moves to mate remains None node['moves_to_mate'] = None node['sequence'] = [optimal_child_id] if optimal_child_id is not None else [] node['processed'] = True # Mark the node as processed # Return the best result and path found for the current node return node['result'], node['moves_to_mate']
END OF THE WRONG FUNCTION, the code follows

import chess def initialize_game_tree(initial_fen): """Initializes the game tree with the root node based on the initial FEN.""" return { 1: { 'fen': initial_fen, 'moves_to_mate': None, 'parent': None, 'color': chess.WHITE if initial_fen.split(' ')[1] == 'w' else chess.BLACK, 'result': None, 'processed': False, 'sequence': [], 'children': [] } } def add_descendants(node_id, depth, A): """Recursively adds descendant nodes to the game tree up to a specified depth.""" if depth == 0: return board = chess.Board(A[node_id]['fen']) for move in board.legal_moves: board.push(move) new_node_id = len(A) + 1 A[new_node_id] = { 'fen': board.fen(), 'moves_to_mate': None, 'parent': node_id, 'color': chess.WHITE if board.turn else chess.BLACK, 'result': None, 'processed': False, 'sequence': [], 'children': [] } A[node_id]['children'].append(new_node_id) add_descendants(new_node_id, depth - 1, A) board.pop() def evaluate_and_partition(A, node_id=1): node = A[node_id] # Debugging line to track the function calls print(f"Evaluating Node ID: {node_id}") # Return previously calculated result if this node has been processed if node['processed']: print(f"Returning processed Node ID: {node_id} with result: {node['result']}, moves to mate: {node['moves_to_mate']}") return node['result'], node['moves_to_mate'] # Initialize variables to store the best result and path found so far optimal_result = -float('inf') # Assume worst case scenario optimal_moves_to_mate = None # Initialize as None, to be updated optimal_child_id = None # Keep track of which child leads to the optimal result # Iterate through each child of the current node for child_id in node['children']: print(child_id) # Recursively evaluate each child child_result, child_moves_to_mate = evaluate_and_partition(A, child_id) # Check if the current child's result improves upon the best result found so far if child_result is not None and ( child_result > optimal_result or (child_result == optimal_result and optimal_moves_to_mate is not None and child_moves_to_mate is not None and child_moves_to_mate < optimal_moves_to_mate)): # Update optimal values based on the current child's results optimal_result = child_result optimal_moves_to_mate = child_moves_to_mate optimal_child_id = child_id # After evaluating all children, update the current node with the best result and path found node['result'] = optimal_result if optimal_moves_to_mate is not None: # If a path to victory or draw is found, increment moves to mate as we move up the tree node['moves_to_mate'] = optimal_moves_to_mate + 1 else: # If no path improves the position, moves to mate remains None node['moves_to_mate'] = None node['sequence'] = [optimal_child_id] if optimal_child_id is not None else [] node['processed'] = True # Mark the node as processed # Return the best result and path found for the current node return node['result'], node['moves_to_mate'] # Main execution block initial_fen = "4k3/8/8/3K2Q1/8/8/8/8 w - - 6 4" A = initialize_game_tree(initial_fen) add_descendants(1, 5, A) # Adjust the depth as needed evaluate_terminal_positions(A) evaluate_and_partition(A, 1) # Print the root node to see the analysis result print(A[1]) # Function to print boards for children of a given node def print_boards_for_children(A, parent_key): children_keys = A[parent_key].get('children', []) for key in children_keys: if key in A: fen = A[key]['fen'] board = chess.Board(fen) print(f"Board for child {key}:\n{board}\n") # Display the initial board and boards for first-level children print(chess.Board(initial_fen)) print_boards_for_children(A, 1)
The output is like this, the node 6109 below is my target to be included to A[1]['sequence']

Output:
Evaluating Node ID: 1 Returning processed Node ID: 1 with result: None, moves to mate: None {'fen': '4k3/8/8/3K2Q1/8/8/8/8 w - - 6 4', 'moves_to_mate': None, 'parent': None, 'color': True, 'result': None, 'processed': True, 'sequence': [], 'children': [2, 3697, 6109, 8685, 9041, 20864, 32046, 34967, 44777, 50093, 60983, 70685, 83639, 94018, 111578, 126490, 142939, 161014, 178208, 196205, 197783, 202648, 209235, 216611, 225572, 236906, 248111]} . . . . k . . . . . . . . . . . . . . . . . . . . . . K . . Q . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Board for child 2: . . . . k . Q . . . . . . . . . . . . . . . . . . . . K . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Board for child 3697: . . . Q k . . . . . . . . . . . . . . . . . . . . . . K . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Board for child 6109: . . . . k . . . . . . . . . Q . . . . . . . . . . . . K . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
buran write Feb-24-2024, 08:29 PM:
please, use python tags when post code, not output
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Endgame engine with non-standard figures doesn't behave correctly max248 0 840 Dec-07-2024, 12:44 PM
Last Post: max248
  "Random" module is not working as expected Genericgamemaker 1 1,608 Jul-25-2024, 04:46 PM
Last Post: Gribouillis
  a chess endgame problem, almost solved but not quite max22 0 996 Mar-31-2024, 06:14 PM
Last Post: max22
  optimum chess endgame with D=3 pieces doesn't give an exact moves_to_mate variable max22 1 1,331 Mar-21-2024, 09:31 PM
Last Post: max22
  A new endgame problem, almost solved but not quite 2 max22 0 1,130 Mar-07-2024, 07:12 PM
Last Post: max22
  Simple conditional not working as expected return2sender 8 3,383 Aug-27-2023, 10:39 PM
Last Post: return2sender
  Custom method to handle exceptions not working as expected gradlon93 3 2,833 Dec-22-2022, 07:12 PM
Last Post: deanhystad
Exclamation My code is not working as I expected and I don't know why! Marinho 4 2,824 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  set and sorted, not working how expected! wtr 2 2,630 Jan-07-2022, 04:53 PM
Last Post: bowlofred
  Search Engine nman52 1 87,473 Dec-16-2020, 11:46 AM
Last Post: Larz60+

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.