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