-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 Type hinted the heck out of the project
Some things are still left to type hint.
- Loading branch information
1 parent
c337a11
commit 44e317b
Showing
9 changed files
with
183 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import numpy as np | ||
|
||
|
||
# Seperated from the Graph.py file because of circular imports in _typing.py | ||
class Node: | ||
def __init__(self, id: np.uint16): | ||
""" | ||
id: the id of the node, np.uint16 for maximum | ||
of 2^16 (= 65535 + 1) nodes on the screen | ||
""" | ||
self.id = id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,45 @@ | ||
#!/usr/bin/env python | ||
|
||
from .search_utils import ReconstructPath, MakePairs, RemoveSeen | ||
from .Graph import Graph | ||
|
||
|
||
def depthfirstsearch(self, start_state): | ||
open = [(start_state, None)] | ||
closed = [] | ||
|
||
while len(open) > 0 and len(closed) < 50: | ||
print(f"open: {len(open)}") | ||
print(f"closed: {len(closed)}") | ||
nodePair = open[0] | ||
node = nodePair[0] | ||
# print(node) | ||
# print(f"closed {closed}") | ||
# print() | ||
if self.GoalTest(node) is True: | ||
print("Found goal!") | ||
return ReconstructPath(nodePair, closed) | ||
else: | ||
closed = [nodePair] + closed | ||
children = self.MoveGen(node) | ||
noLoops = RemoveSeen(children, open, closed) | ||
new = MakePairs(noLoops, node) | ||
open = new + open[1:] # The only change from DFS | ||
yield open | ||
|
||
print("No path found") | ||
return -1 | ||
class Search: | ||
def __init__(self, name, graph: Graph): | ||
self.name = name | ||
# self.search = types.MethodType(search, Search) | ||
self.graph = graph | ||
|
||
def search(self): | ||
raise NotImplementedError | ||
|
||
|
||
class depthfirstsearch(Search): | ||
def __init__(self): | ||
self.name = "DFS" | ||
self.graph: Graph | ||
|
||
self.i = 0 | ||
|
||
def search(self): | ||
open = [(self.graph.start_node, None)] | ||
print(open[0][0].id) | ||
closed = [] | ||
|
||
while len(open) > 0: | ||
nodePair = open[0] | ||
node = nodePair[0] | ||
if self.graph.GoalTest(node) is True: | ||
# print("Found goal!") | ||
# print(path = ReconstructPath(nodePair, closed)]) | ||
return | ||
else: | ||
closed = [nodePair] + closed | ||
children = self.graph.MoveGen(node) | ||
noLoops = RemoveSeen(children, open, closed) | ||
new = MakePairs(noLoops, node) | ||
open = new + open[1:] # The only change from DFS | ||
yield [nodepair[0].id for nodepair in open] | ||
|
||
# print("No path found") | ||
return -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from .Node import Node | ||
|
||
import numpy as np | ||
|
||
import typing | ||
from numpy.typing import NDArray | ||
|
||
# TODO: put Node.py Node class inside Graph file? Circular imports? | ||
# from __future__ import annotations | ||
# from typing import TYPE_CHECKING | ||
# if TYPE_CHECKING: | ||
# pass | ||
|
||
|
||
NodeType = Node | ||
NodeLocs = NDArray[np.float64] | ||
NodeList = typing.List[Node] | ||
NodeCount = np.uint16 | ||
SearchMethod = typing.Callable[[], typing.List[Node]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.