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.
6
4
A* algorithm is a best-first search algorithm in which the cost
11
9
regular graph-searching algorithm,
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 ):
18
- '''
17
+ """
19
18
Class cell represents a cell in the world which have the property
20
19
position : The position of the represented by tupleof x and y
21
20
co-ordinates initially set to (0,0)
@@ -24,18 +23,21 @@ class Cell(object):
24
23
g,h,f : The parameters for constructing the heuristic function
25
24
which can be any function. for simplicity used line
26
25
distance
27
- '''
26
+ """
27
+
28
28
def __init__ (self ):
29
29
self .position = (0 , 0 )
30
30
self .parent = None
31
31
32
32
self .g = 0
33
33
self .h = 0
34
34
self .f = 0
35
- '''
35
+
36
+ """
36
37
overrides equals method because otherwise cell assign will give
37
38
wrong results
38
- '''
39
+ """
40
+
39
41
def __eq__ (self , cell ):
40
42
return self .position == cell .position
41
43
@@ -44,12 +46,11 @@ def showcell(self):
44
46
45
47
46
48
class Gridworld (object ):
47
-
48
- '''
49
+ """
49
50
Gridworld class represents the external world here a grid M*M
50
51
matrix
51
- w : create a numpy array with the given world_size default is 5
52
- '''
52
+ world_size : create a numpy array with the given world_size default is 5
53
+ """
53
54
54
55
def __init__ (self , world_size = (5 , 5 )):
55
56
self .w = np .zeros (world_size )
@@ -59,48 +60,49 @@ def __init__(self, world_size=(5, 5)):
59
60
def show (self ):
60
61
print (self .w )
61
62
62
- '''
63
- get_neighbours
64
- As the name suggests this function will return the neighbours of
65
- the a particular cell
66
- '''
67
63
def get_neigbours (self , cell ):
64
+ """
65
+ Return the neighbours of cell
66
+ """
68
67
neughbour_cord = [
69
- (- 1 , - 1 ), (- 1 , 0 ), (- 1 , 1 ), (0 , - 1 ),
70
- (0 , 1 ), (1 , - 1 ), (1 , 0 ), (1 , 1 )]
68
+ (- 1 , - 1 ),
69
+ (- 1 , 0 ),
70
+ (- 1 , 1 ),
71
+ (0 , - 1 ),
72
+ (0 , 1 ),
73
+ (1 , - 1 ),
74
+ (1 , 0 ),
75
+ (1 , 1 ),
76
+ ]
71
77
current_x = cell .position [0 ]
72
78
current_y = cell .position [1 ]
73
79
neighbours = []
74
80
for n in neughbour_cord :
75
81
x = current_x + n [0 ]
76
82
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 )):
83
+ if 0 <= x < self .world_x_limit and 0 <= y < self .world_y_limit :
80
84
c = Cell ()
81
85
c .position = (x , y )
82
86
c .parent = cell
83
87
neighbours .append (c )
84
88
return neighbours
85
89
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
90
94
91
def astar (world , start , goal ):
95
- '''
92
+ """
93
+ Implementation of a start algorithm
94
+ world : Object of the world object
95
+ start : Object of the cell as start position
96
+ stop : Object of the cell as goal position
97
+
96
98
>>> p = Gridworld()
97
99
>>> start = Cell()
98
100
>>> start.position = (0,0)
99
101
>>> goal = Cell()
100
102
>>> goal.position = (4,4)
101
103
>>> astar(p, start, goal)
102
104
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
103
- '''
105
+ """
104
106
_open = []
105
107
_closed = []
106
108
_open .append (start )
@@ -118,7 +120,7 @@ def astar(world, start, goal):
118
120
n .g = current .g + 1
119
121
x1 , y1 = n .position
120
122
x2 , y2 = goal .position
121
- n .h = (y2 - y1 )** 2 + (x2 - x1 )** 2
123
+ n .h = (y2 - y1 ) ** 2 + (x2 - x1 ) ** 2
122
124
n .f = n .h + n .g
123
125
124
126
for c in _open :
@@ -130,23 +132,19 @@ def astar(world, start, goal):
130
132
path .append (current .position )
131
133
current = current .parent
132
134
path .append (current .position )
133
- path = path [::- 1 ]
134
- return path
135
-
136
- if __name__ == '__main__' :
137
- '''
138
- sample run
139
- '''
140
- # object for the world
141
- p = Gridworld ()
142
- # stat position and Goal
135
+ return path [::- 1 ]
136
+
137
+
138
+ if __name__ == "__main__" :
139
+ world = Gridworld ()
140
+ # stat position and Goal
143
141
start = Cell ()
144
142
start .position = (0 , 0 )
145
143
goal = Cell ()
146
144
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
145
+ print (f "path from { start . position } to { goal .position } " )
146
+ s = astar (world , start , goal )
147
+ # Just for visual reasons
150
148
for i in s :
151
- p .w [i ] = 1
152
- print (p .w )
149
+ world .w [i ] = 1
150
+ print (world .w )
0 commit comments