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.
|
class Node: |
|
""" |
|
>>> 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() |
|
|
|
def calculate_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) |
|
return dx + dy |
|
|
|
def __lt__(self, other) -> bool: |
|
return self.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.
|
if child_node not in self.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)) |
|
|
|
if child_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.
if child_node not in self.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.
What would you like to share?
1. Typo
self.target.pos_yandself.target.pos_xare reversed.Python/graphs/greedy_best_first.py
Lines 145 to 154 in a17791d
parameter of Node is below
Python/graphs/greedy_best_first.py
Lines 38 to 46 in a17791d
The reason why it worked well is that
self.target.pos_y(6) andself.target.pos_x(6) are the same.2. Class equality
__eq__is not implement inNodeclass.Python/graphs/greedy_best_first.py
Lines 20 to 56 in 6f21f76
so the below
child_node not in self.open_nodesorchild_node not in self.open_nodesdid't work.Python/graphs/greedy_best_first.py
Lines 105 to 114 in 6f21f76
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.
Additional information
I think
sort()andpop(0)are unnecessary. I think it's rather to increase the complexity of time.