My UCS is creating like a small order output issue. Its putting thee out before thou even though thou costs less because thee is made in the heap first. Any thoughts?
def suggest_ucs(self, prefix):
"""Get word suggestions using UCS"""
ucs_suggestions = []
heap = []
code
start_node = self.get_start_node(prefix) # traverse to the correct starting node
cost = 0
heapq.heappush(heap, (cost, prefix, start_node)) # add first node to the heap
# Run UCS on the heap
while heap:
print(sorted(heap, key=lambda x: x[0])) # Sort by cost
print()
# unpack the tuple, taking taking the node with the least cost first
cost, current_prefix, node = heapq.heappop(heap)
if node: # only run the search when there is children
# add all the children based on the cost
for child in node.children.values():
# print(f"{node}: cost: {cost}")
new_prefix = current_prefix + child.letter
child_cost = cost + (1 / child.frequency)
heapq.heappush(heap, (child_cost, new_prefix, child))
if child.is_word:
ucs_suggestions.append(new_prefix) # add the suggestions when the node completes a word
return ucs_suggestions
2025-02-18 17:07:20.569 Python[25204:2737183] +[IMKInputSession subclass]: chose IMKInputSession_Modern
t
[(0, 't', <autocomplete.Node object at 0x1007ee3f0>)]
[(0.1, 'th', <autocomplete.Node object at 0x1007ee870>)]
[(0.35, 'the', <autocomplete.Node object at 0x1007ee8a0>), (0.43333333333333335, 'tho', <autocomplete.Node object at 0x1007ee930>), (0.6, 'tha', <autocomplete.Node object at 0x1007ee9f0>), (1.1, 'thr', <autocomplete.Node object at 0x1007eeab0>)]
[(0.43333333333333335, 'tho', <autocomplete.Node object at 0x1007ee930>), (0.6, 'tha', <autocomplete.Node object at 0x1007ee9f0>), (1.1, 'thr', <autocomplete.Node object at 0x1007eeab0>), (1.35, 'ther', <autocomplete.Node object at 0x1007ee8d0>), (1.35, 'thei', <autocomplete.Node object at 0x1007eea50>), (1.35, 'thee', <autocomplete.Node object at 0x1007eeba0>)]
Assistant
[(0.6, 'tha', <autocomplete.Node object at 0x1007ee9f0>), (0.7666666666666666, 'thou', <autocomplete.Node object at 0x1007ee960>), (1.1, 'thr', <autocomplete.Node object at 0x1007eeab0>), (1.35, 'ther', <autocomplete.Node object at 0x1007ee8d0>), (1.35, 'thei', <autocomplete.Node object at 0x1007eea50>), (1.35, 'thee', <autocomplete.Node object at 0x1007eeba0>)]
[(0.7666666666666666, 'thou', <autocomplete.Node object at 0x1007ee960>), (1.1, 'thr', <autocomplete.Node object at 0x1007eeab0>), (1.35, 'thee', <autocomplete.Node object at 0x1007eeba0>), (1.35, 'ther', <autocomplete.Node object at 0x1007ee8d0>), (1.35, 'thei', <autocomplete.Node object at 0x1007eea50>), (1.6, 'that', <autocomplete.Node object at 0x1007eea20>), (1.6, 'thag', <autocomplete.Node object at 0x1007eec00>)]
[(1.1, 'thr', <autocomplete.Node object at 0x1007eeab0>), (1.2666666666666666, 'thoug', <autocomplete.Node object at 0x1007ee990>), (1.35, 'thei', <autocomplete.Node object at 0x1007eea50>), (1.35, 'ther', <autocomplete.Node object at 0x1007ee8d0>), (1.35, 'thee', <autocomplete.Node object at 0x1007eeba0>), (1.6, 'thag', <autocomplete.Node object at 0x1007eec00>), (1.6, 'that', <autocomplete.Node object at 0x1007eea20>)]
[(1.2666666666666666, 'thoug', <autocomplete.Node object at 0x1007ee990>), (1.35, 'thei', <autocomplete.Node object at 0x1007eea50>), (1.35, 'thee', <autocomplete.Node object at 0x1007eeba0>), (1.35, 'ther', <autocomplete.Node object at 0x1007ee8d0>), (1.6, 'thag', <autocomplete.Node object at 0x1007eec00>), (1.6, 'that', <autocomplete.Node object at 0x1007eea20>), (2.1, 'thro', <autocomplete.Node object at 0x1007eeae0>)]
[(1.35, 'thee', <autocomplete.Node object at 0x1007eeba0>), (1.35, 'thei', <autocomplete.Node object at 0x1007eea50>), (1.35, 'ther', <autocomplete.Node object at 0x1007ee8d0>), (1.6, 'that', <autocomplete.Node object at 0x1007eea20>), (1.6, 'thag', <autocomplete.Node object at 0x1007eec00>), (1.7666666666666666, 'though', <autocomplete.Node object at 0x1007ee9c0>), (2.1, 'thro', <autocomplete.Node object at 0x1007eeae0>)]
[(1.35, 'thei', <autocomplete.Node object at 0x1007eea50>), (1.35, 'ther', <autocomplete.Node object at 0x1007ee8d0>), (1.6, 'that', <autocomplete.Node object at 0x1007eea20>), (1.6, 'thag', <autocomplete.Node object at 0x1007eec00>), (1.7666666666666666, 'though', <autocomplete.Node object at 0x1007ee9c0>), (2.1, 'thro', <autocomplete.Node object at 0x1007eeae0>)]
[(1.35, 'ther', <autocomplete.Node object at 0x1007ee8d0>), (1.6, 'thag', <autocomplete.Node object at 0x1007eec00>), (1.6, 'that', <autocomplete.Node object at 0x1007eea20>), (1.7666666666666666, 'though', <autocomplete.Node object at 0x1007ee9c0>), (2.1, 'thro', <autocomplete.Node object at 0x1007eeae0>), (2.35, 'their', <autocomplete.Node object at 0x1007eea80>)]
[(1.6, 'thag', <autocomplete.Node object at 0x1007eec00>), (1.6, 'that', <autocomplete.Node object at 0x1007eea20>), (1.7666666666666666, 'though', <autocomplete.Node object at 0x1007ee9c0>), (2.1, 'thro', <autocomplete.Node object at 0x1007eeae0>), (2.35, 'their', <autocomplete.Node object at 0x1007eea80>), (2.35, 'there', <autocomplete.Node object at 0x1007ee900>)]
[(1.6, 'that', <autocomplete.Node object at 0x1007eea20>), (1.7666666666666666, 'though', <autocomplete.Node object at 0x1007ee9c0>), (2.1, 'thro', <autocomplete.Node object at 0x1007eeae0>), (2.35, 'there', <autocomplete.Node object at 0x1007ee900>), (2.35, 'their', <autocomplete.Node object at 0x1007eea80>)]
[(1.7666666666666666, 'though', <autocomplete.Node object at 0x1007ee9c0>), (2.1, 'thro', <autocomplete.Node object at 0x1007eeae0>), (2.35, 'there', <autocomplete.Node object at 0x1007ee900>), (2.35, 'their', <autocomplete.Node object at 0x1007eea80>)]
[(2.1, 'thro', <autocomplete.Node object at 0x1007eeae0>), (2.35, 'their', <autocomplete.Node object at 0x1007eea80>), (2.35, 'there', <autocomplete.Node object at 0x1007ee900>), (2.7666666666666666, 'thought', <autocomplete.Node object at 0x1007eebd0>)]
[(2.35, 'their', <autocomplete.Node object at 0x1007eea80>), (2.35, 'there', <autocomplete.Node object at 0x1007ee900>), (2.7666666666666666, 'thought', <autocomplete.Node object at 0x1007eebd0>), (3.1, 'throu', <autocomplete.Node object at 0x1007eeb10>)]
[(2.35, 'there', <autocomplete.Node object at 0x1007ee900>), (2.7666666666666666, 'thought', <autocomplete.Node object at 0x1007eebd0>), (3.1, 'throu', <autocomplete.Node object at 0x1007eeb10>)]
[(2.7666666666666666, 'thought', <autocomplete.Node object at 0x1007eebd0>), (3.1, 'throu', <autocomplete.Node object at 0x1007eeb10>)]
[(3.1, 'throu', <autocomplete.Node object at 0x1007eeb10>)]
[(4.1, 'throug', <autocomplete.Node object at 0x1007eeb40>)]
[(5.1, 'through', <autocomplete.Node object at 0x1007eeb70>)]
Your issue with the UCS (Uniform Cost Search) algorithm seems to stem from the way you are managing and pushing nodes to the heap based on their cost. Specifically, the problem arises from the fact that nodes are comparable based solely on their cumulative cost (the value you are pushing into the heap).
In your case, "thee" is being pushed to the heap before "thou", even though "thee" has a higher cost due to it being constructed later in the search (i.e., "though" might have been generated first).
Here's a basic breakdown of what might be causing the problem and how you might fix it:
Cost Calculation: Ensure you are correctly calculating the cost for each new prefix based on the frequency of each node. You are currently computing child_cost as cost + (1 / child.frequency). Double-check how frequency is being assigned to ensure it correctly reflects the relative costs of producing each word.
Processing Order: The order in which nodes are added to the heap is critical for UCS. Once you've pushed all children into the heap, they will be sorted based on the cost you provided. If "thee" has a higher cost as a result of the accumulation but enters the heap later than "thou", you will see that behavior where it's processed first.
Heapq Implementation: The heapq library you are using maintains the heap's order based on the first element of the tuples you push in. If two nodes happen to have the same cost, the node that was pushed into the heap first will be processed first, which is happening in your case.
To fix your issue:
You can include a secondary criterion to the tuple to differentiate nodes with the same cost. For instance, you could add a timestamp or an incrementing counter that gets updated each time a node with the same cost is pushed onto the heap. This would ensure consistent processing:
python
counter = 0 # A counter to keep track of the order of node insertions
...
heapq.heappush(heap, (child_cost, new_prefix, child, counter))
counter += 1
At the time of popping elements from the heap, unpack the additional element to maintain the order.
Here's a cleaned-up version of your heap push operations:
python
counter = 0
while heap:
...
cost, current_prefix, node, order = heapq.heappop(heap)
if node: # Only run the search when there are children
for child in node.children.values():
...
child_cost = cost + (1 / child.frequency)
heapq.heappush(heap, (child_cost, new_prefix, child, counter))
counter += 1
With this change, nodes that have the same cost will be processed in the order they were added, preventing your issue of "thee" appearing before "thou" if it has a higher cost.