Skip to content

Commit 9bc80ea

Browse files
Added Dequeue in Python
0 parents  commit 9bc80ea

File tree

105 files changed

+295341
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+295341
-0
lines changed

.gitignore

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*,cover
46+
.hypothesis/
47+
48+
# Translations
49+
*.mo
50+
*.pot
51+
52+
# Django stuff:
53+
*.log
54+
local_settings.py
55+
56+
# Flask stuff:
57+
instance/
58+
.webassets-cache
59+
60+
# Scrapy stuff:
61+
.scrapy
62+
63+
# Sphinx documentation
64+
docs/_build/
65+
66+
# PyBuilder
67+
target/
68+
69+
# IPython Notebook
70+
.ipynb_checkpoints
71+
72+
# pyenv
73+
.python-version
74+
75+
# celery beat schedule file
76+
celerybeat-schedule
77+
78+
# dotenv
79+
.env
80+
81+
# virtualenv
82+
venv/
83+
ENV/
84+
85+
# Spyder project settings
86+
.spyderproject
87+
88+
# Rope project settings
89+
.ropeproject
90+
.idea
91+
.DS_Store

Graphs/a_star.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
grid = [[0, 1, 0, 0, 0, 0],
3+
[0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles
4+
[0, 1, 0, 0, 0, 0],
5+
[0, 1, 0, 0, 1, 0],
6+
[0, 0, 0, 0, 1, 0]]
7+
8+
'''
9+
heuristic = [[9, 8, 7, 6, 5, 4],
10+
[8, 7, 6, 5, 4, 3],
11+
[7, 6, 5, 4, 3, 2],
12+
[6, 5, 4, 3, 2, 1],
13+
[5, 4, 3, 2, 1, 0]]'''
14+
15+
init = [0, 0]
16+
goal = [len(grid)-1, len(grid[0])-1] #all coordinates are given in format [y,x]
17+
cost = 1
18+
19+
#the cost map which pushes the path closer to the goal
20+
heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
21+
for i in range(len(grid)):
22+
for j in range(len(grid[0])):
23+
heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1])
24+
if grid[i][j] == 1:
25+
heuristic[i][j] = 99 #added extra penalty in the heuristic map
26+
27+
28+
#the actions we can take
29+
delta = [[-1, 0 ], # go up
30+
[ 0, -1], # go left
31+
[ 1, 0 ], # go down
32+
[ 0, 1 ]] # go right
33+
34+
35+
#function to search the path
36+
def search(grid,init,goal,cost,heuristic):
37+
38+
closed = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]# the referrence grid
39+
closed[init[0]][init[1]] = 1
40+
action = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]#the action grid
41+
42+
x = init[0]
43+
y = init[1]
44+
g = 0
45+
f = g + heuristic[init[0]][init[0]]
46+
cell = [[f, g, x, y]]
47+
48+
found = False # flag that is set when search is complete
49+
resign = False # flag set if we can't find expand
50+
51+
while not found and not resign:
52+
if len(cell) == 0:
53+
resign = True
54+
return "FAIL"
55+
else:
56+
cell.sort()#to choose the least costliest action so as to move closer to the goal
57+
cell.reverse()
58+
next = cell.pop()
59+
x = next[2]
60+
y = next[3]
61+
g = next[1]
62+
f = next[0]
63+
64+
65+
if x == goal[0] and y == goal[1]:
66+
found = True
67+
else:
68+
for i in range(len(delta)):#to try out different valid actions
69+
x2 = x + delta[i][0]
70+
y2 = y + delta[i][1]
71+
if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
72+
if closed[x2][y2] == 0 and grid[x2][y2] == 0:
73+
g2 = g + cost
74+
f2 = g2 + heuristic[x2][y2]
75+
cell.append([f2, g2, x2, y2])
76+
closed[x2][y2] = 1
77+
action[x2][y2] = i
78+
invpath = []
79+
x = goal[0]
80+
y = goal[1]
81+
invpath.append([x, y])#we get the reverse path from here
82+
while x != init[0] or y != init[1]:
83+
x2 = x - delta[action[x][y]][0]
84+
y2 = y - delta[action[x][y]][1]
85+
x = x2
86+
y = y2
87+
invpath.append([x, y])
88+
89+
path = []
90+
for i in range(len(invpath)):
91+
path.append(invpath[len(invpath) - 1 - i])
92+
print "ACTION MAP"
93+
for i in range(len(action)):
94+
print action[i]
95+
96+
return path
97+
98+
a = search(grid,init,goal,cost,heuristic)
99+
for i in range(len(a)):
100+
print a[i]
101+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy