1- '''
1+ """
22The A* algorithm combines features of uniform-cost search and pure
33heuristic search to efficiently compute optimal solutions.
44A* algorithm is a best-first search algorithm in which the cost
99regular graph-searching algorithm,
1010essentially planning ahead at each step so a more optimal decision
1111is made.A* also known as the algorithm with brains
12- '''
12+ """
1313import numpy as np
1414
1515
1616class Cell (object ):
17- '''
17+ """
1818 Class cell represents a cell in the world which have the property
1919 position : The position of the represented by tupleof x and y
2020 co-ordinates initially set to (0,0)
@@ -23,7 +23,8 @@ class Cell(object):
2323 g,h,f : The parameters for constructing the heuristic function
2424 which can be any function. for simplicity used line
2525 distance
26- '''
26+ """
27+
2728 def __init__ (self ):
2829 self .position = (0 , 0 )
2930 self .parent = None
@@ -32,10 +33,11 @@ def __init__(self):
3233 self .h = 0
3334 self .f = 0
3435
35- '''
36+ """
3637 overrides equals method because otherwise cell assign will give
3738 wrong results
38- '''
39+ """
40+
3941 def __eq__ (self , cell ):
4042 return self .position == cell .position
4143
@@ -44,11 +46,11 @@ def showcell(self):
4446
4547
4648class Gridworld (object ):
47- '''
49+ """
4850 Gridworld class represents the external world here a grid M*M
4951 matrix
5052 world_size: create a numpy array with the given world_size default is 5
51- '''
53+ """
5254
5355 def __init__ (self , world_size = (5 , 5 )):
5456 self .w = np .zeros (world_size )
@@ -59,12 +61,19 @@ def show(self):
5961 print (self .w )
6062
6163 def get_neigbours (self , cell ):
62- '''
64+ """
6365 Return the neighbours of cell
64- '''
66+ """
6567 neughbour_cord = [
66- (- 1 , - 1 ), (- 1 , 0 ), (- 1 , 1 ), (0 , - 1 ),
67- (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+ ]
6877 current_x = cell .position [0 ]
6978 current_y = cell .position [1 ]
7079 neighbours = []
@@ -80,7 +89,7 @@ def get_neigbours(self, cell):
8089
8190
8291def astar (world , start , goal ):
83- '''
92+ """
8493 Implementation of a start algorithm
8594 world : Object of the world object
8695 start : Object of the cell as start position
@@ -93,7 +102,7 @@ def astar(world, start, goal):
93102 >>> goal.position = (4,4)
94103 >>> astar(p, start, goal)
95104 [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
96- '''
105+ """
97106 _open = []
98107 _closed = []
99108 _open .append (start )
@@ -111,7 +120,7 @@ def astar(world, start, goal):
111120 n .g = current .g + 1
112121 x1 , y1 = n .position
113122 x2 , y2 = goal .position
114- n .h = (y2 - y1 )** 2 + (x2 - x1 )** 2
123+ n .h = (y2 - y1 ) ** 2 + (x2 - x1 ) ** 2
115124 n .f = n .h + n .g
116125
117126 for c in _open :
@@ -126,16 +135,16 @@ def astar(world, start, goal):
126135 return path [::- 1 ]
127136
128137
129- if __name__ == ' __main__' :
138+ if __name__ == " __main__" :
130139 world = Gridworld ()
131- # stat position and Goal
140+ # stat position and Goal
132141 start = Cell ()
133142 start .position = (0 , 0 )
134143 goal = Cell ()
135144 goal .position = (4 , 4 )
136145 print (f"path from { start .position } to { goal .position } " )
137146 s = astar (world , start , goal )
138- # Just for visual reasons
147+ # Just for visual reasons
139148 for i in s :
140149 world .w [i ] = 1
141150 print (world .w )
0 commit comments