1
1
"""
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
12
14
"""
13
15
import numpy as np
14
16
15
17
16
18
class Cell :
17
19
"""
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.
26
24
"""
27
25
28
26
def __init__ (self ):
29
27
self .position = (0 , 0 )
30
28
self .parent = None
31
-
32
29
self .g = 0
33
30
self .h = 0
34
31
self .f = 0
35
32
36
33
"""
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.
39
36
"""
40
37
41
38
def __eq__ (self , cell ):
@@ -48,8 +45,8 @@ def showcell(self):
48
45
class Gridworld :
49
46
"""
50
47
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.
53
50
"""
54
51
55
52
def __init__ (self , world_size = (5 , 5 )):
@@ -90,10 +87,10 @@ def get_neigbours(self, cell):
90
87
91
88
def astar (world , start , goal ):
92
89
"""
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.
97
94
98
95
>>> p = Gridworld()
99
96
>>> start = Cell()
@@ -137,14 +134,14 @@ def astar(world, start, goal):
137
134
138
135
if __name__ == "__main__" :
139
136
world = Gridworld ()
140
- # stat position and Goal
137
+ # Start position and goal
141
138
start = Cell ()
142
139
start .position = (0 , 0 )
143
140
goal = Cell ()
144
141
goal .position = (4 , 4 )
145
142
print (f"path from { start .position } to { goal .position } " )
146
143
s = astar (world , start , goal )
147
- # Just for visual reasons
144
+ # Just for visual reasons.
148
145
for i in s :
149
146
world .w [i ] = 1
150
147
print (world .w )
0 commit comments