Skip to content

Commit 2c20bf0

Browse files
committed
Added multiple path plotting capabilities
1 parent 84da2dd commit 2c20bf0

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

main.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ def is_valid_pos(cur_pos):
2222
]
2323
cost_func = linear_cost()
2424
path_finder = PathFinder(linear_cost(), randomized_cost(1, 0.3, cost_func), is_valid_pos)
25-
path = path_finder.find_path(moves, start, end)
26-
map_generator.view_path(map_data, path)
27-
25+
path1 = path_finder.find_path(moves, start, end)
26+
path2 = path_finder.find_path(moves, start, end)
27+
map_generator.view_path(map_data, path1, path2)

map_generator.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import matplotlib.pyplot as plt
22
import numpy as np
3-
43
import perlin
54

65

@@ -22,7 +21,7 @@ def view_map(map_data, grid=True):
2221
return
2322

2423

25-
def view_path(map_data, path, grid=True, markers=True):
24+
def view_path(map_data, *args, grid=True, markers=False):
2625
"""View path
2726
a) map_data: Boolean numpy array. True for passable, False for impassable.
2827
b) path: List[(int, int)]. List of points (x,y) in path from 0(start) to end(stop).
@@ -31,23 +30,32 @@ def view_path(map_data, path, grid=True, markers=True):
3130
e) color code: As in view_map; plus green cross for start and red cross for destination.
3231
"""
3332

34-
start = path[0]
35-
end = path[-1]
36-
for point in path:
37-
plt.plot(point[0], point[1])
38-
plt.scatter(point[0], point[1])
39-
plt.plot(start[0], start[1], 'gx')
40-
plt.plot(end[0], end[1], 'rx')
33+
for path in args:
34+
if not path:
35+
continue
36+
start = path[0]
37+
end = path[-1]
38+
x = []
39+
y = []
40+
for point in path:
41+
x.append(point[0])
42+
y.append(point[1])
43+
plt.plot(x, y)
44+
if markers:
45+
plt.scatter(x, y)
46+
plt.plot(start[0], start[1], 'gx')
47+
plt.plot(end[0], end[1], 'rx')
4148
view_map(map_data, grid)
4249
return
4350

4451

52+
# noinspection PyTypeChecker
4553
def generate_map(rows, cols, obstacle_density=0.35, var_index=0.1, seed=0):
4654
"""Generate 2D map
4755
a) rows, cols: No. of rows and columns.
4856
b) obstacle_density: Percentage of map filled by obstacles. Defaults to 0.35.
4957
c) var_index: Variability index. Controls "noisiness" of obstacles. Defaults to 0.1.
50-
d) seed: Seed for rng.
58+
d) seed: Seed for rng. Use 0 for random seed. Defaults to 0.
5159
e) return value: Boolean numpy array. True for passable, False for impassable.
5260
"""
5361

movement_cost.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import random
2-
32
import math
43

54

@@ -35,7 +34,7 @@ def euclidean_cost(scale=1):
3534
scale (int): scale of one step
3635
3736
Returns:
38-
Returns euclidean cost fuction between start and end point
37+
Returns euclidean cost function between start and end point
3938
"""
4039

4140
def cost(start, end):

path_finding.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44

55
class PathFinder:
6-
76
def __init__(self, movement_cost, heuristic_cost, is_valid_move):
87
"""
98
Initializes the path finder with relevant functions. Helps reduce the number of
@@ -111,11 +110,12 @@ def generic_a_star(self, moves, start, end):
111110
store: contains the all the states encountered with links to parent states
112111
it can be used to generate the path and the next step
113112
"""
113+
114114
def total_cost(cur_pos, next_pos, end):
115115
return self.movement_cost(cur_pos, next_pos) + self.heuristic_cost(next_pos, end)
116116

117117
queue = priority_queue.PriorityQueue()
118-
queue.push((self.heuristic_cost(start, end), start))
118+
queue.push((self.heuristic_cost(start, end), start))
119119
store = {start: (0, start)}
120120

121121
while not queue.is_empty():

priority_queue.py

-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
class PriorityQueue:
7-
87
def __init__(self):
98
self.elements = []
109

@@ -22,7 +21,6 @@ def is_empty(self):
2221

2322

2423
class SimpleQueue:
25-
2624
def __init(self):
2725
self.elements = deque()
2826

0 commit comments

Comments
 (0)