From 750e8a185e20b7f28424f258f503e9ee4870515e Mon Sep 17 00:00:00 2001 From: yashvarshney003 <59112785+yashvarshney003@users.noreply.github.com> Date: Sat, 15 Aug 2020 08:24:36 +0530 Subject: [PATCH] Update breadth_first_search.py (#374) --- .../breadth_first_search.py | 8 +++--- .../DepthFirstSearch/depth_first_search.py | 8 +++--- PathPlanning/Dijkstra/dijkstra.py | 16 ++++++------ README.md | 2 ++ tests/test_breadth_first_search.py | 26 +++++++++++++++++++ tests/test_depth_first_search.py | 26 +++++++++++++++++++ tests/test_dijkstra.py | 16 +++++++++++- 7 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 tests/test_breadth_first_search.py create mode 100644 tests/test_depth_first_search.py diff --git a/PathPlanning/BreadthFirstSearch/breadth_first_search.py b/PathPlanning/BreadthFirstSearch/breadth_first_search.py index 01f1a31d..406e5eb5 100644 --- a/PathPlanning/BreadthFirstSearch/breadth_first_search.py +++ b/PathPlanning/BreadthFirstSearch/breadth_first_search.py @@ -33,16 +33,16 @@ class BreadthFirstSearchPlanner: self.motion = self.get_motion_model() class Node: - def __init__(self, x, y, cost, pind, parent): + def __init__(self, x, y, cost, parent_index, parent): self.x = x # index of grid self.y = y # index of grid self.cost = cost - self.pind = pind + self.parent_index = parent_index self.parent = parent def __str__(self): return str(self.x) + "," + str(self.y) + "," + str( - self.cost) + "," + str(self.pind) + self.cost) + "," + str(self.parent_index) def planning(self, sx, sy, gx, gy): """ @@ -92,7 +92,7 @@ class BreadthFirstSearchPlanner: if current.x == ngoal.x and current.y == ngoal.y: print("Find goal") - ngoal.pind = current.pind + ngoal.parent_index = current.parent_index ngoal.cost = current.cost break diff --git a/PathPlanning/DepthFirstSearch/depth_first_search.py b/PathPlanning/DepthFirstSearch/depth_first_search.py index c8d2d5ea..523225ee 100644 --- a/PathPlanning/DepthFirstSearch/depth_first_search.py +++ b/PathPlanning/DepthFirstSearch/depth_first_search.py @@ -33,16 +33,16 @@ class DepthFirstSearchPlanner: self.motion = self.get_motion_model() class Node: - def __init__(self, x, y, cost, pind, parent): + def __init__(self, x, y, cost, parent_index, parent): self.x = x # index of grid self.y = y # index of grid self.cost = cost - self.pind = pind + self.parent_index = parent_index self.parent = parent def __str__(self): return str(self.x) + "," + str(self.y) + "," + str( - self.cost) + "," + str(self.pind) + self.cost) + "," + str(self.parent_index) def planning(self, sx, sy, gx, gy): """ @@ -88,7 +88,7 @@ class DepthFirstSearchPlanner: if current.x == ngoal.x and current.y == ngoal.y: print("Find goal") - ngoal.pind = current.pind + ngoal.parent_index = current.parent_index ngoal.cost = current.cost break diff --git a/PathPlanning/Dijkstra/dijkstra.py b/PathPlanning/Dijkstra/dijkstra.py index b744bc6f..9956dff3 100644 --- a/PathPlanning/Dijkstra/dijkstra.py +++ b/PathPlanning/Dijkstra/dijkstra.py @@ -38,15 +38,15 @@ class Dijkstra: self.motion = self.get_motion_model() class Node: - def __init__(self, x, y, cost, parent): + def __init__(self, x, y, cost, parent_index): self.x = x # index of grid self.y = y # index of grid self.cost = cost - self.parent = parent # index of previous Node + self.parent_index = parent_index # index of previous Node def __str__(self): return str(self.x) + "," + str(self.y) + "," + str( - self.cost) + "," + str(self.parent) + self.cost) + "," + str(self.parent_index) def planning(self, sx, sy, gx, gy): """ @@ -88,7 +88,7 @@ class Dijkstra: if current.x == goal_node.x and current.y == goal_node.y: print("Find goal") - goal_node.parent = current.parent + goal_node.parent_index = current.parent_index goal_node.cost = current.cost break @@ -126,12 +126,12 @@ class Dijkstra: # generate final course rx, ry = [self.calc_position(goal_node.x, self.min_x)], [ self.calc_position(goal_node.y, self.min_y)] - parent = goal_node.parent - while parent != -1: - n = closed_set[parent] + parent_index = goal_node.parent_index + while parent_index != -1: + n = closed_set[parent_index] rx.append(self.calc_position(n.x, self.min_x)) ry.append(self.calc_position(n.y, self.min_y)) - parent = n.parent + parent_index = n.parent_index return rx, ry diff --git a/README.md b/README.md index 49a99959..07dbf7ae 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,8 @@ All animation gifs are stored here: [AtsushiSakai/PythonRoboticsGifs: Animation 2. Install the required libraries. You can use environment.yml with conda command. > conda env create -f environment.yml +> using pip :- + pip install -r requirements.txt 3. Execute python script in each directory. diff --git a/tests/test_breadth_first_search.py b/tests/test_breadth_first_search.py new file mode 100644 index 00000000..7dc36b28 --- /dev/null +++ b/tests/test_breadth_first_search.py @@ -0,0 +1,26 @@ +from unittest import TestCase +import sys +import os +sys.path.append(os.path.dirname(os.path.abspath(__file__)) + + "/../PathPlanning/BreadthFirstSearch/") + + +try: + import breadth_first_search as m +except ImportError: + raise + + +print(__file__) + + +class Test(TestCase): + + def test1(self): + m.show_animation = False + m.main() + + +if __name__ == '__main__': # pragma: no cover + test = Test() + test.test1() diff --git a/tests/test_depth_first_search.py b/tests/test_depth_first_search.py new file mode 100644 index 00000000..15aa7a0a --- /dev/null +++ b/tests/test_depth_first_search.py @@ -0,0 +1,26 @@ +from unittest import TestCase +import sys +import os +sys.path.append(os.path.dirname(os.path.abspath(__file__)) + + "/../PathPlanning/DepthFirstSearch/") + + +try: + import depth_first_search as m +except ImportError: + raise + + +print(__file__) + + +class Test(TestCase): + + def test1(self): + m.show_animation = False + m.main() + + +if __name__ == '__main__': # pragma: no cover + test = Test() + test.test1() diff --git a/tests/test_dijkstra.py b/tests/test_dijkstra.py index b86f2e6c..7650eee8 100644 --- a/tests/test_dijkstra.py +++ b/tests/test_dijkstra.py @@ -1,6 +1,15 @@ from unittest import TestCase +import sys +import os +sys.path.append(os.path.dirname(os.path.abspath(__file__)) + + "/../PathPlanning/Dijkstra/") + + +try: + import dijkstra as m +except ImportError: + raise -from PathPlanning.Dijkstra import dijkstra as m print(__file__) @@ -10,3 +19,8 @@ class Test(TestCase): def test1(self): m.show_animation = False m.main() + + +if __name__ == '__main__': # pragma: no cover + test = Test() + test.test1()