Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Python/Multi_Hueristic_Astar.py at master · ss18/Python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
ss18
/
Python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
Python
/
Multi_Hueristic_Astar.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
268 lines (230 loc) · 6.56 KB
master
Breadcrumbs
Python
/
Multi_Hueristic_Astar.py
Copy path
Top
File metadata and controls
Code
Blame
268 lines (230 loc) · 6.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from
__future__
import
print_function
import
heapq
import
numpy
as
np
import
math
import
copy
try
:
xrange
# Python 2
except
NameError
:
xrange
=
range
# Python 3
class
PriorityQueue
:
def
__init__
(
self
):
self
.
elements
=
[]
self
.
set
=
set
()
def
minkey
(
self
):
if
not
self
.
empty
():
return
self
.
elements
[
0
][
0
]
else
:
return
float
(
'inf'
)
def
empty
(
self
):
return
len
(
self
.
elements
)
==
0
def
put
(
self
,
item
,
priority
):
if
item
not
in
self
.
set
:
heapq
.
heappush
(
self
.
elements
, (
priority
,
item
))
self
.
set
.
add
(
item
)
else
:
# update
# print("update", item)
temp
=
[]
(
pri
,
x
)
=
heapq
.
heappop
(
self
.
elements
)
while
x
!=
item
:
temp
.
append
((
pri
,
x
))
(
pri
,
x
)
=
heapq
.
heappop
(
self
.
elements
)
temp
.
append
((
priority
,
item
))
for
(
pro
,
xxx
)
in
temp
:
heapq
.
heappush
(
self
.
elements
, (
pro
,
xxx
))
def
remove_element
(
self
,
item
):
if
item
in
self
.
set
:
self
.
set
.
remove
(
item
)
temp
=
[]
(
pro
,
x
)
=
heapq
.
heappop
(
self
.
elements
)
while
x
!=
item
:
temp
.
append
((
pro
,
x
))
(
pro
,
x
)
=
heapq
.
heappop
(
self
.
elements
)
for
(
prito
,
yyy
)
in
temp
:
heapq
.
heappush
(
self
.
elements
, (
prito
,
yyy
))
def
top_show
(
self
):
return
self
.
elements
[
0
][
1
]
def
get
(
self
):
(
priority
,
item
)
=
heapq
.
heappop
(
self
.
elements
)
self
.
set
.
remove
(
item
)
return
(
priority
,
item
)
def
consistent_hueristic
(
P
,
goal
):
# euclidean distance
a
=
np
.
array
(
P
)
b
=
np
.
array
(
goal
)
return
np
.
linalg
.
norm
(
a
-
b
)
def
hueristic_2
(
P
,
goal
):
# integer division by time variable
return
consistent_hueristic
(
P
,
goal
)
//
t
def
hueristic_1
(
P
,
goal
):
# manhattan distance
return
abs
(
P
[
0
]
-
goal
[
0
])
+
abs
(
P
[
1
]
-
goal
[
1
])
def
key
(
start
,
i
,
goal
,
g_function
):
ans
=
g_function
[
start
]
+
W1
*
hueristics
[
i
](
start
,
goal
)
return
ans
def
do_something
(
back_pointer
,
goal
,
start
):
grid
=
np
.
chararray
((
n
,
n
))
for
i
in
range
(
n
):
for
j
in
range
(
n
):
grid
[
i
][
j
]
=
'*'
for
i
in
range
(
n
):
for
j
in
range
(
n
):
if
(
j
, (
n
-
1
)
-
i
)
in
blocks
:
grid
[
i
][
j
]
=
"#"
grid
[
0
][(
n
-
1
)]
=
"-"
x
=
back_pointer
[
goal
]
while
x
!=
start
:
(
x_c
,
y_c
)
=
x
# print(x)
grid
[(
n
-
1
)
-
y_c
][
x_c
]
=
"-"
x
=
back_pointer
[
x
]
grid
[(
n
-
1
)][
0
]
=
"-"
for
i
in
xrange
(
n
):
for
j
in
range
(
n
):
if
(
i
,
j
)
==
(
0
,
n
-
1
):
print
(
grid
[
i
][
j
],
end
=
' '
)
print
(
"<-- End position"
,
end
=
' '
)
else
:
print
(
grid
[
i
][
j
],
end
=
' '
)
print
()
print
(
"^"
)
print
(
"Start position"
)
print
()
print
(
"# is an obstacle"
)
print
(
"- is the path taken by algorithm"
)
print
(
"PATH TAKEN BY THE ALGORITHM IS:-"
)
x
=
back_pointer
[
goal
]
while
x
!=
start
:
print
(
x
,
end
=
' '
)
x
=
back_pointer
[
x
]
print
(
x
)
quit
()
def
valid
(
p
):
if
p
[
0
]
<
0
or
p
[
0
]
>
n
-
1
:
return
False
if
p
[
1
]
<
0
or
p
[
1
]
>
n
-
1
:
return
False
return
True
def
expand_state
(
s
,
j
,
visited
,
g_function
,
close_list_anchor
,
close_list_inad
,
open_list
,
back_pointer
):
for
itera
in
range
(
n_hueristic
):
open_list
[
itera
].
remove_element
(
s
)
# print("s", s)
# print("j", j)
(
x
,
y
)
=
s
left
=
(
x
-
1
,
y
)
right
=
(
x
+
1
,
y
)
up
=
(
x
,
y
+
1
)
down
=
(
x
,
y
-
1
)
for
neighbours
in
[
left
,
right
,
up
,
down
]:
if
neighbours
not
in
blocks
:
if
valid
(
neighbours
)
and
neighbours
not
in
visited
:
# print("neighbour", neighbours)
visited
.
add
(
neighbours
)
back_pointer
[
neighbours
]
=
-
1
g_function
[
neighbours
]
=
float
(
'inf'
)
if
valid
(
neighbours
)
and
g_function
[
neighbours
]
>
g_function
[
s
]
+
1
:
g_function
[
neighbours
]
=
g_function
[
s
]
+
1
back_pointer
[
neighbours
]
=
s
if
neighbours
not
in
close_list_anchor
:
open_list
[
0
].
put
(
neighbours
,
key
(
neighbours
,
0
,
goal
,
g_function
))
if
neighbours
not
in
close_list_inad
:
for
var
in
range
(
1
,
n_hueristic
):
if
key
(
neighbours
,
var
,
goal
,
g_function
)
<=
W2
*
key
(
neighbours
,
0
,
goal
,
g_function
):
# print("why not plssssssssss")
open_list
[
j
].
put
(
neighbours
,
key
(
neighbours
,
var
,
goal
,
g_function
))
# print
def
make_common_ground
():
some_list
=
[]
# block 1
for
x
in
range
(
1
,
5
):
for
y
in
range
(
1
,
6
):
some_list
.
append
((
x
,
y
))
# line
for
x
in
range
(
15
,
20
):
some_list
.
append
((
x
,
17
))
# block 2 big
for
x
in
range
(
10
,
19
):
for
y
in
range
(
1
,
15
):
some_list
.
append
((
x
,
y
))
# L block
for
x
in
range
(
1
,
4
):
for
y
in
range
(
12
,
19
):
some_list
.
append
((
x
,
y
))
for
x
in
range
(
3
,
13
):
for
y
in
range
(
16
,
19
):
some_list
.
append
((
x
,
y
))
return
some_list
hueristics
=
{
0
:
consistent_hueristic
,
1
:
hueristic_1
,
2
:
hueristic_2
}
blocks_blk
=
[(
0
,
1
),(
1
,
1
),(
2
,
1
),(
3
,
1
),(
4
,
1
),(
5
,
1
),(
6
,
1
),(
7
,
1
),(
8
,
1
),(
9
,
1
),(
10
,
1
),(
11
,
1
),(
12
,
1
),(
13
,
1
),(
14
,
1
),(
15
,
1
),(
16
,
1
),(
17
,
1
),(
18
,
1
), (
19
,
1
)]
blocks_no
=
[]
blocks_all
=
make_common_ground
()
blocks
=
blocks_blk
# hyper parameters
W1
=
1
W2
=
1
n
=
20
n_hueristic
=
3
# one consistent and two other inconsistent
# start and end destination
start
=
(
0
,
0
)
goal
=
(
n
-
1
,
n
-
1
)
t
=
1
def
multi_a_star
(
start
,
goal
,
n_hueristic
):
g_function
=
{
start
:
0
,
goal
:
float
(
'inf'
)}
back_pointer
=
{
start
:
-
1
,
goal
:
-
1
}
open_list
=
[]
visited
=
set
()
for
i
in
range
(
n_hueristic
):
open_list
.
append
(
PriorityQueue
())
open_list
[
i
].
put
(
start
,
key
(
start
,
i
,
goal
,
g_function
))
close_list_anchor
=
[]
close_list_inad
=
[]
while
open_list
[
0
].
minkey
()
<
float
(
'inf'
):
for
i
in
range
(
1
,
n_hueristic
):
# print("i", i)
# print(open_list[0].minkey(), open_list[i].minkey())
if
open_list
[
i
].
minkey
()
<=
W2
*
open_list
[
0
].
minkey
():
global
t
t
+=
1
# print("less prio")
if
g_function
[
goal
]
<=
open_list
[
i
].
minkey
():
if
g_function
[
goal
]
<
float
(
'inf'
):
do_something
(
back_pointer
,
goal
,
start
)
else
:
_
,
get_s
=
open_list
[
i
].
top_show
()
visited
.
add
(
get_s
)
expand_state
(
get_s
,
i
,
visited
,
g_function
,
close_list_anchor
,
close_list_inad
,
open_list
,
back_pointer
)
close_list_inad
.
append
(
get_s
)
else
:
# print("more prio")
if
g_function
[
goal
]
<=
open_list
[
0
].
minkey
():
if
g_function
[
goal
]
<
float
(
'inf'
):
do_something
(
back_pointer
,
goal
,
start
)
else
:
# print("hoolla")
get_s
=
open_list
[
0
].
top_show
()
visited
.
add
(
get_s
)
expand_state
(
get_s
,
0
,
visited
,
g_function
,
close_list_anchor
,
close_list_inad
,
open_list
,
back_pointer
)
close_list_anchor
.
append
(
get_s
)
print
(
"No path found to goal"
)
print
()
for
i
in
range
(
n
-
1
,
-
1
,
-
1
):
for
j
in
range
(
n
):
if
(
j
,
i
)
in
blocks
:
print
(
'#'
,
end
=
' '
)
elif
(
j
,
i
)
in
back_pointer
:
if
(
j
,
i
)
==
(
n
-
1
,
n
-
1
):
print
(
'*'
,
end
=
' '
)
else
:
print
(
'-'
,
end
=
' '
)
else
:
print
(
'*'
,
end
=
' '
)
if
(
j
,
i
)
==
(
n
-
1
,
n
-
1
):
print
(
'<-- End position'
,
end
=
' '
)
print
()
print
(
"^"
)
print
(
"Start position"
)
print
()
print
(
"# is an obstacle"
)
print
(
"- is the path taken by algorithm"
)
multi_a_star
(
start
,
goal
,
n_hueristic
)
You can’t perform that action at this time.