Skip to content

Commit 1356ded

Browse files
authored
Fix astar
Single character variable names are old school.
1 parent 3d9bb05 commit 1356ded

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

machine_learning/astar.py

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import numpy as np
2-
31
'''
42
The A* algorithm combines features of uniform-cost search and pure
53
heuristic search to efficiently compute optimal solutions.
@@ -12,6 +10,7 @@
1210
essentially planning ahead at each step so a more optimal decision
1311
is made.A* also known as the algorithm with brains
1412
'''
13+
import numpy as np
1514

1615

1716
class Cell(object):
@@ -32,6 +31,7 @@ def __init__(self):
3231
self.g = 0
3332
self.h = 0
3433
self.f = 0
34+
3535
'''
3636
overrides equals method because otherwise cell assign will give
3737
wrong results
@@ -44,11 +44,10 @@ def showcell(self):
4444

4545

4646
class Gridworld(object):
47-
4847
'''
4948
Gridworld class represents the external world here a grid M*M
5049
matrix
51-
w : create a numpy array with the given world_size default is 5
50+
world_size: create a numpy array with the given world_size default is 5
5251
'''
5352

5453
def __init__(self, world_size=(5, 5)):
@@ -59,12 +58,10 @@ def __init__(self, world_size=(5, 5)):
5958
def show(self):
6059
print(self.w)
6160

62-
'''
63-
get_neighbours
64-
As the name suggests this function will return the neighbours of
65-
the a particular cell
66-
'''
6761
def get_neigbours(self, cell):
62+
'''
63+
Return the neighbours of cell
64+
'''
6865
neughbour_cord = [
6966
(-1, -1), (-1, 0), (-1, 1), (0, -1),
7067
(0, 1), (1, -1), (1, 0), (1, 1)]
@@ -74,25 +71,21 @@ def get_neigbours(self, cell):
7471
for n in neughbour_cord:
7572
x = current_x + n[0]
7673
y = current_y + n[1]
77-
if (
78-
(x >= 0 and x < self.world_x_limit) and
79-
(y >= 0 and y < self.world_y_limit)):
74+
if 0 <= x < self.world_x_limit and 0 <= y < self.world_y_limit:
8075
c = Cell()
8176
c.position = (x, y)
8277
c.parent = cell
8378
neighbours.append(c)
8479
return neighbours
8580

86-
'''
87-
Implementation of a start algorithm
88-
world : Object of the world object
89-
start : Object of the cell as start position
90-
stop : Object of the cell as goal position
91-
'''
92-
9381

9482
def astar(world, start, goal):
9583
'''
84+
Implementation of a start algorithm
85+
world : Object of the world object
86+
start : Object of the cell as start position
87+
stop : Object of the cell as goal position
88+
9689
>>> p = Gridworld()
9790
>>> start = Cell()
9891
>>> start.position = (0,0)
@@ -130,23 +123,19 @@ def astar(world, start, goal):
130123
path.append(current.position)
131124
current = current.parent
132125
path.append(current.position)
133-
path = path[::-1]
134-
return path
126+
return path[::-1]
127+
135128

136129
if __name__ == '__main__':
137-
'''
138-
sample run
139-
'''
140-
# object for the world
141-
p = Gridworld()
130+
world = Gridworld()
142131
# stat position and Goal
143132
start = Cell()
144133
start.position = (0, 0)
145134
goal = Cell()
146135
goal.position = (4, 4)
147-
print("path from {} to {} ".format(start.position, goal.position))
148-
s = astar(p, start, goal)
149-
# Just for visual Purpose
136+
print(f"path from {start.position} to {goal.position}")
137+
s = astar(world, start, goal)
138+
# Just for visual reasons
150139
for i in s:
151-
p.w[i] = 1
152-
print(p.w)
140+
world.w[i] = 1
141+
print(world.w)

0 commit comments

Comments
 (0)