Apr-01-2022, 02:34 PM
Hi, I am working on alphabeta pruning and I am trying to print out the index of the pruned nodes. When I used this input 2 4 13 11 1 3 3 7 3 3 I got print out 3 6 7 5. However it is supposed to be 3 6 7 10 11. 5 is the parent's index of 10 and 11. My codes stuck at level 2 of node and it did not go all the way to children node for level 3. I wonder how do I change it from parent node in level 2 to children node in level 3? Any advice is appreciated. Thanks
def alphaBetaPrune(game_state, ALPHA, BETA): if game_state.depth == 4: return game_state.value if game_state.Node_type == 'MAX': i = 0 # for each each_game of node do for each_game in game_state.leafNode: # calling the alphaBetaPrune recursively until it reach the DEPTH ALPHA = max(ALPHA, alphaBetaPrune(each_game, ALPHA, BETA)) if BETA <= ALPHA: if (i + 1) < 2: SharedQueue = SimpleQueue() SharedQueue.put(each_game.head.leafNode[i + 1]) while not SharedQueue.empty(): curNode = SharedQueue.get_nowait() if len(curNode.leafNode) == 0: print(curNode.index, "", end='') else: for child in curNode.leafNode: SharedQueue.put(child) i += 1 return ALPHA else: i = 0 # Repeat for each each_game for each_game in game_state.leafNode: BETA = min(BETA, alphaBetaPrune(each_game, ALPHA, BETA)) if BETA <= ALPHA: if (i + 1) < 2: SharedQueue = SimpleQueue() SharedQueue.put(each_game.head.leafNode[i + 1]) while not SharedQueue.empty(): curNode = SharedQueue.get_nowait() if len(curNode.leafNode) == 0: print(curNode.index, "", end='') else: for child in curNode.leafNode: SharedQueue.put(child) break i += 1 return BETA def somefunction(): m_input = input().split() input_list = [] for num in m_input: input_list.append(int(num)) root = Node() new = root.leafNode level2_1 = Node(root) new.append(level2_1) level2_2 = Node(root) new.append(level2_2) level2_3 = Node(root) new.append(level2_3) tree_struct = [] tree_struct.extend([level2_1, level2_2, level2_3]) SharedQueue = SimpleQueue() for element in tree_struct: SharedQueue.put(element) while len(input_list) > 0: curNode = SharedQueue.get() if curNode.leafNode is not None: level = 4 while level > 2: leafNode = Node(curNode) if leafNode.depth == 4: leafNode.value = input_list.pop(0) curNode.leafNode.append(leafNode) SharedQueue.put(leafNode) level = level - 1 return root