Skip to content

Commit 468e54d

Browse files
committed
a* algo
1 parent b6d6a52 commit 468e54d

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

Machine_Learning/a_star.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import numpy as np
2+
3+
'''
4+
The A* algorithm combines features of uniform-cost search and pure heuristic search to efficiently compute optimal solutions.
5+
A* algorithm is a best-first search algorithm in which the cost 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 h(n) is the heuristic estimate or the cost or a path
7+
from node n to a goal.A* algorithm introduces a heuristic into a regular graph-searching algorithm,
8+
essentially planning ahead at each step so a more optimal decision is made.A* also known as the algorithm with brains
9+
'''
10+
11+
class Cell(object):
12+
'''
13+
Class cell represents a cell in the world which have the property
14+
position : The position of the represented by tupleof x and y
15+
co-ordinates initially set to (0,0)
16+
parent : This contains the parent cell object which we visted
17+
before arrinving this cell
18+
g,h,f : The parameters for constructing the huristic function
19+
which can be any function. for simplicity used line distance
20+
'''
21+
def __init__(self):
22+
self.position=(0,0)
23+
self.parent=None
24+
25+
self.g = 0
26+
self.h = 0
27+
self.f = 0
28+
'''
29+
overides equals method because otherwise cell assing will give wrong results
30+
'''
31+
def __eq__(self, cell):
32+
return self.position == cell.position
33+
34+
def showcell(self):
35+
print(self.position)
36+
37+
38+
class Gridworld(object):
39+
40+
'''
41+
Gridworld class represents the external world here a grid M*M matrix
42+
w : create a numpy array with the given world_size default is 5
43+
'''
44+
45+
def __init__(self, world_size=(5, 5)):
46+
self.w = np.zeros(world_size)
47+
self.world_x_limit = world_size[0]
48+
self.world_y_limit = world_size[1]
49+
50+
def show(self):
51+
print(self.w)
52+
53+
'''
54+
get_neighbours
55+
56+
as the name suggests this function will return the neighbours of the a particular cell
57+
58+
'''
59+
def get_neigbours(self, cell):
60+
neughbour_cord = [(-1, -1), (-1, 0), (-1, 1),
61+
(0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
62+
current_x = cell.position[0]
63+
current_y = cell.position[1]
64+
neighbours = []
65+
for n in neughbour_cord:
66+
x = current_x + n[0]
67+
y = current_y + n[1]
68+
if x >=0 and x < self.world_x_limit and y >=0 and y < self.world_y_limit:
69+
c = Cell()
70+
c.position = (x,y)
71+
c.parent = cell
72+
neighbours.append(c)
73+
return neighbours
74+
75+
'''
76+
Implementation of a start algotithm
77+
world : Object of the world object
78+
start : Object of the cell as start position
79+
stop : Object of the cell as goal position
80+
'''
81+
def astar(world, start, goal):
82+
_open = []
83+
_closed = []
84+
_open.append(start)
85+
86+
while _open:
87+
min_f = np.argmin([n.f for n in _open])
88+
current = _open[min_f]
89+
_closed.append(_open.pop(min_f))
90+
91+
92+
if current == goal:
93+
break
94+
95+
for n in world.get_neigbours(current):
96+
for c in _closed:
97+
if c == n:
98+
continue
99+
n.g = current.g + 1
100+
x1, y1 = n.position
101+
x2, y2 = goal.position
102+
n.h = (y2 - y1)**2 + (x2 - x1)**2
103+
n.f = n.h + n.g
104+
105+
for c in _open:
106+
if c == n and c.f < n.f:
107+
continue
108+
_open.append(n)
109+
path = []
110+
while current.parent is not None:
111+
path.append(current.position)
112+
current = current.parent
113+
path.append(current.position)
114+
path = path[::-1]
115+
print(path)
116+
return path
117+
118+
119+
'''
120+
121+
sample run
122+
123+
'''
124+
# object for the world
125+
p = Gridworld()
126+
#stat position and Goal
127+
start = Cell()
128+
start.position = (0,0)
129+
goal = Cell()
130+
goal.position = (4,4)
131+
print("path from {} to {} ".format(start.position, goal.position))
132+
s=astar(p, start, goal)
133+
for i in s:
134+
p.w[i] = 1
135+
print(p.w)
136+
# nei = p.get_neigbours(k)
137+
# [print(i.position) for i in nei]
138+
# p.show()

0 commit comments

Comments
 (0)