Skip to content

Fix mypy errors at greedy best first algo #4575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions graphs/greedy_best_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

from __future__ import annotations

from typing import Optional

Path = list[tuple[int, int]]

grid = [
[0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles
Expand Down Expand Up @@ -33,7 +37,15 @@ class Node:
True
"""

def __init__(self, pos_x, pos_y, goal_x, goal_y, g_cost, parent):
def __init__(
self,
pos_x: int,
pos_y: int,
goal_x: int,
goal_y: int,
g_cost: float,
parent: Optional[Node],
):
self.pos_x = pos_x
self.pos_y = pos_y
self.pos = (pos_y, pos_x)
Expand Down Expand Up @@ -72,16 +84,16 @@ class GreedyBestFirst:
(6, 2), (6, 3), (5, 3), (5, 4), (5, 5), (6, 5), (6, 6)]
"""

def __init__(self, start, goal):
def __init__(self, start: tuple[int, int], goal: tuple[int, int]):
self.start = Node(start[1], start[0], goal[1], goal[0], 0, None)
self.target = Node(goal[1], goal[0], goal[1], goal[0], 99999, None)

self.open_nodes = [self.start]
self.closed_nodes = []
self.closed_nodes: list[Node] = []

self.reached = False

def search(self) -> list[tuple[int]]:
def search(self) -> Optional[Path]:
"""
Search for the path,
if a path is not found, only the starting position is returned
Expand Down Expand Up @@ -113,8 +125,9 @@ def search(self) -> list[tuple[int]]:
else:
self.open_nodes.append(better_node)

if not (self.reached):
if not self.reached:
return [self.start.pos]
return None

def get_successors(self, parent: Node) -> list[Node]:
"""
Expand Down Expand Up @@ -143,7 +156,7 @@ def get_successors(self, parent: Node) -> list[Node]:
)
return successors

def retrace_path(self, node: Node) -> list[tuple[int]]:
def retrace_path(self, node: Optional[Node]) -> Path:
"""
Retrace the path from parents to parents until start node
"""
Expand All @@ -166,9 +179,9 @@ def retrace_path(self, node: Node) -> list[tuple[int]]:

greedy_bf = GreedyBestFirst(init, goal)
path = greedy_bf.search()
if path:
for pos_x, pos_y in path:
grid[pos_x][pos_y] = 2

for elem in path:
grid[elem[0]][elem[1]] = 2

for elem in grid:
print(elem)
for elem in grid:
print(elem)