11"""
2- The A* algorithm combines features of uniform-cost search and pure
3- heuristic search to efficiently compute optimal solutions.
4- A* algorithm is a best-first search algorithm in which the cost
5- associated with a node is f(n) = g(n) + h(n),
6- where g(n) is the cost of the path from the initial state to node n and
7- h(n) is the heuristic estimate or the cost or a path
8- from node n to a goal.A* algorithm introduces a heuristic into a
9- regular graph-searching algorithm,
10- essentially planning ahead at each step so a more optimal decision
11- is made.A* also known as the algorithm with brains
2+ The A* algorithm combines features of uniform-cost search and pure heuristic search to
3+ efficiently compute optimal solutions.
4+
5+ The A* algorithm is a best-first search algorithm in which the cost associated with a
6+ node is f(n) = g(n) + h(n), where g(n) is the cost of the path from the initial state to
7+ node n and h(n) is the heuristic estimate or the cost or a path from node n to a goal.
8+
9+ The A* algorithm introduces a heuristic into a regular graph-searching algorithm,
10+ essentially planning ahead at each step so a more optimal decision is made. For this
11+ reason, A* is known as an algorithm with brains.
12+
13+ https://en.wikipedia.org/wiki/A*_search_algorithm
1214"""
1315import numpy as np
1416
1517
1618class Cell :
1719 """
18- Class cell represents a cell in the world which have the property
19- position : The position of the represented by tupleof x and y
20- coordinates initially set to (0,0)
21- parent : This contains the parent cell object which we visited
22- before arrinving this cell
23- g,h,f : The parameters for constructing the heuristic function
24- which can be any function. for simplicity used line
25- distance
20+ Class cell represents a cell in the world which have the properties:
21+ position: represented by tuple of x and y coordinates initially set to (0,0).
22+ parent: Contains the parent cell object visited before we arrived at this cell.
23+ g, h, f: Parameters used when calling our heuristic function.
2624 """
2725
2826 def __init__ (self ):
2927 self .position = (0 , 0 )
3028 self .parent = None
31-
3229 self .g = 0
3330 self .h = 0
3431 self .f = 0
3532
3633 """
37- overrides equals method because otherwise cell assign will give
38- wrong results
34+ Overrides equals method because otherwise cell assign will give
35+ wrong results.
3936 """
4037
4138 def __eq__ (self , cell ):
@@ -48,8 +45,8 @@ def showcell(self):
4845class Gridworld :
4946 """
5047 Gridworld class represents the external world here a grid M*M
51- matrix
52- world_size: create a numpy array with the given world_size default is 5
48+ matrix.
49+ world_size: create a numpy array with the given world_size default is 5.
5350 """
5451
5552 def __init__ (self , world_size = (5 , 5 )):
@@ -90,10 +87,10 @@ def get_neigbours(self, cell):
9087
9188def astar (world , start , goal ):
9289 """
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
90+ Implementation of a start algorithm.
91+ world : Object of the world object.
92+ start : Object of the cell as start position.
93+ stop : Object of the cell as goal position.
9794
9895 >>> p = Gridworld()
9996 >>> start = Cell()
@@ -137,14 +134,14 @@ def astar(world, start, goal):
137134
138135if __name__ == "__main__" :
139136 world = Gridworld ()
140- # stat position and Goal
137+ # Start position and goal
141138 start = Cell ()
142139 start .position = (0 , 0 )
143140 goal = Cell ()
144141 goal .position = (4 , 4 )
145142 print (f"path from { start .position } to { goal .position } " )
146143 s = astar (world , start , goal )
147- # Just for visual reasons
144+ # Just for visual reasons.
148145 for i in s :
149146 world .w [i ] = 1
150147 print (world .w )
0 commit comments