Skip to content

i found another typo and problem greedy_best_first #8770

Description

@JadeKim042386

What would you like to share?

1. Typo

self.target.pos_y and self.target.pos_x are reversed.

successors.append(
Node(
pos_x,
pos_y,
self.target.pos_y,
self.target.pos_x,
parent.g_cost+1,
parent,
)
)

parameter of Node is below

def__init__(
self,
pos_x: int,
pos_y: int,
goal_x: int,
goal_y: int,
g_cost: float,
parent: Node|None,
):

The reason why it worked well is that self.target.pos_y(6) and self.target.pos_x(6) are the same.

2. Class equality

__eq__ is not implement in Node class.

classNode:
"""
>>> k = Node(0, 0, 4, 5, 0, None)
>>> k.calculate_heuristic()
9
>>> n = Node(1, 4, 3, 4, 2, None)
>>> n.calculate_heuristic()
2
>>> l = [k, n]
>>> n == l[0]
False
>>> l.sort()
>>> n == l[0]
True
"""
def__init__(self, pos_x, pos_y, goal_x, goal_y, g_cost, parent):
self.pos_x=pos_x
self.pos_y=pos_y
self.pos= (pos_y, pos_x)
self.goal_x=goal_x
self.goal_y=goal_y
self.g_cost=g_cost
self.parent=parent
self.f_cost=self.calculate_heuristic()
defcalculate_heuristic(self) ->float:
"""
The heuristic here is the Manhattan Distance
Could elaborate to offer more than one choice
"""
dy=abs(self.pos_x-self.goal_x)
dx=abs(self.pos_y-self.goal_y)
returndx+dy
def__lt__(self, other) ->bool:
returnself.f_cost<other.f_cost

so the below child_node not in self.open_nodes or child_node not in self.open_nodes did't work.

ifchild_nodenotinself.open_nodes:
self.open_nodes.append(child_node)
else:
# retrieve the best current path
better_node=self.open_nodes.pop(self.open_nodes.index(child_node))
ifchild_node.g_cost<better_node.g_cost:
self.open_nodes.append(child_node)
else:
self.open_nodes.append(better_node)

3. node in self.open_nodes is always better_node

How can a previous path cost more than the next one? A node that has already entered self.open_nodes is always less expensive than the next node.
So I checked that it works well even if I simply modify it as below.

ifchild_nodenotinself.open_nodes:
self.open_nodes.append(child_node)
# delete else

Additional information

I think sort() and pop(0) are unnecessary. I think it's rather to increase the complexity of time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting triageAwaiting triage from a maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions