Skip to content

Commit

Permalink
Update breadth_first_search.py (AtsushiSakai#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashvarshney003 authored Aug 15, 2020
1 parent 6d29bcd commit 750e8a1
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 17 deletions.
8 changes: 4 additions & 4 deletions PathPlanning/BreadthFirstSearch/breadth_first_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ def __init__(self, ox, oy, reso, rr):
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):
"""
Expand Down Expand Up @@ -92,7 +92,7 @@ def planning(self, sx, sy, gx, gy):

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

Expand Down
8 changes: 4 additions & 4 deletions PathPlanning/DepthFirstSearch/depth_first_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ def __init__(self, ox, oy, reso, rr):
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):
"""
Expand Down Expand Up @@ -88,7 +88,7 @@ def planning(self, sx, sy, gx, gy):

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

Expand Down
16 changes: 8 additions & 8 deletions PathPlanning/Dijkstra/dijkstra.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ def __init__(self, ox, oy, resolution, robot_radius):
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):
"""
Expand Down Expand Up @@ -88,7 +88,7 @@ def planning(self, sx, sy, gx, gy):

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

Expand Down Expand Up @@ -126,12 +126,12 @@ def calc_final_path(self, goal_node, closed_set):
# 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

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions tests/test_breadth_first_search.py
Original file line number Diff line number Diff line change
@@ -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()
26 changes: 26 additions & 0 deletions tests/test_depth_first_search.py
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 15 additions & 1 deletion tests/test_dijkstra.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand All @@ -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()

0 comments on commit 750e8a1

Please sign in to comment.