1
- import numpy as np
2
-
3
1
'''
4
2
The A* algorithm combines features of uniform-cost search and pure
5
3
heuristic search to efficiently compute optimal solutions.
12
10
essentially planning ahead at each step so a more optimal decision
13
11
is made.A* also known as the algorithm with brains
14
12
'''
13
+ import numpy as np
15
14
16
15
17
16
class Cell (object ):
@@ -32,6 +31,7 @@ def __init__(self):
32
31
self .g = 0
33
32
self .h = 0
34
33
self .f = 0
34
+
35
35
'''
36
36
overrides equals method because otherwise cell assign will give
37
37
wrong results
@@ -44,11 +44,10 @@ def showcell(self):
44
44
45
45
46
46
class Gridworld (object ):
47
-
48
47
'''
49
48
Gridworld class represents the external world here a grid M*M
50
49
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
52
51
'''
53
52
54
53
def __init__ (self , world_size = (5 , 5 )):
@@ -59,12 +58,10 @@ def __init__(self, world_size=(5, 5)):
59
58
def show (self ):
60
59
print (self .w )
61
60
62
- '''
63
- get_neighbours
64
- As the name suggests this function will return the neighbours of
65
- the a particular cell
66
- '''
67
61
def get_neigbours (self , cell ):
62
+ '''
63
+ Return the neighbours of cell
64
+ '''
68
65
neughbour_cord = [
69
66
(- 1 , - 1 ), (- 1 , 0 ), (- 1 , 1 ), (0 , - 1 ),
70
67
(0 , 1 ), (1 , - 1 ), (1 , 0 ), (1 , 1 )]
@@ -74,25 +71,21 @@ def get_neigbours(self, cell):
74
71
for n in neughbour_cord :
75
72
x = current_x + n [0 ]
76
73
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 :
80
75
c = Cell ()
81
76
c .position = (x , y )
82
77
c .parent = cell
83
78
neighbours .append (c )
84
79
return neighbours
85
80
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
-
93
81
94
82
def astar (world , start , goal ):
95
83
'''
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
+
96
89
>>> p = Gridworld()
97
90
>>> start = Cell()
98
91
>>> start.position = (0,0)
@@ -130,23 +123,19 @@ def astar(world, start, goal):
130
123
path .append (current .position )
131
124
current = current .parent
132
125
path .append (current .position )
133
- path = path [::- 1 ]
134
- return path
126
+ return path [::- 1 ]
127
+
135
128
136
129
if __name__ == '__main__' :
137
- '''
138
- sample run
139
- '''
140
- # object for the world
141
- p = Gridworld ()
130
+ world = Gridworld ()
142
131
# stat position and Goal
143
132
start = Cell ()
144
133
start .position = (0 , 0 )
145
134
goal = Cell ()
146
135
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
150
139
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