import math
import random
import copy
import time
import sys
import tkinter
import threading
tm = [
'############################################################',
'#S............................#............#.....#.........#',
'#..........#..................#......#.....#.....#.........#',
'#..........#..................#......#.....#.....#.........#',
'#..........#..................#......#.....#.....#.........#',
'#..........#.........................#.....#.....#.........#',
'#..........#..................#......#.....#...............#',
'#..#########..................#......#.....#.....#.........#',
'#..#..........................#......#.....#.....#.........#',
'#..#..........................#......#.....#.....#.........#',
'#..############################......#.....#.....#.........#',
'#.............................#......#.....#.....#.........#',
'#.............................#......#...........#.........#',
'#######.##################################################.#',
'#....#........#.................#.............#............#',
'#....#........#........#........#.............#............#',
'#....####.#####........#........#.............#............#',
'#.........#............#........#.............#............#',
'#.........#............#........#.............#............#',
'#.........#............#........#.............#............#',
'#.........#............#........#.............#............#',
'#.........#............#........#.............#............#',
'#.........#............#........####.#######.##............#',
'#.........#............#........#....#.......#.............#',
'#.........#............#........#....#.......#.............#',
'#......................#........#....#.......#.............#',
'#.........#............#........##.########..#.............#',
'#.........#............#..................#..########.######',
'#.........#............#..................#...............E#',
'############################################################']
test_map = []
class Node_Elem:
def __init__(self, parent, x, y, dist):
self.parent = parent
self.x = x
self.y = y
self.dist = dist
class A_Star:
def __init__(self, root, s_x, s_y, e_x, e_y, w=60, h=30):
self.s_x = s_x
self.s_y = s_y
self.e_x = e_x
self.e_y = e_y
self.open = []
self.close = []
self.path = []
self.root = root
self.width = w
self.height = h
self.__r = 3
self.canvas = tkinter.Canvas(
root,
width=self.width * 10 + 100,
height=self.height * 10 + 100,
bg="#EBEBEB",
xscrollincrement=1,
yscrollincrement=1
)
self.canvas.pack(expand=tkinter.YES, fill=tkinter.BOTH)
self.title("A*迷宫算法(e:开始搜索或退出)")
self.__bindEvents()
self.new()
def __bindEvents(self):
self.root.bind("e", self.quite)
def quite(self, evt):
self.root.destroy()
def title(self, s):
self.root.title(s)
def new(self):
node = self.canvas.create_oval(100 - self.__r,
20 - self.__r, 100 + self.__r, 20 + self.__r,
fill="#ff0000",
outline="#ffffff",
tags="node",
)
self.canvas.create_text(130, 20,
text=u'Wall',
fill='black'
)
node = self.canvas.create_oval(200 - self.__r,
20 - self.__r, 200 + self.__r, 20 + self.__r,
fill="#00ff00",
outline="#ffffff",
tags="node",
)
self.canvas.create_text(230, 20,
text=u'Path',
fill='black'
)
node = self.canvas.create_oval(300 - self.__r,
20 - self.__r, 300 + self.__r, 20 + self.__r,
fill="#AAAAAA",
outline="#ffffff",
tags="node",
)
self.canvas.create_text(330, 20,
text=u'Searched',
fill='black'
)
for i in range(self.width):
for j in range(self.height):
if test_map[j][i] == '#':
node = self.canvas.create_oval(i * 10 + 50 - self.__r,
j * 10 + 50 - self.__r, i * 10 + 50 + self.__r, j * 10 + 50 + self.__r,
fill="#ff0000",
outline="#ffffff",
tags="node",
)
if test_map[j][i] == 'S':
node = self.canvas.create_oval(i * 10 + 50 - self.__r,
j * 10 + 50 - self.__r, i * 10 + 50 + self.__r, j * 10 + 50 + self.__r,
fill="#00ff00",
outline="#ffffff",
tags="node",
)
self.canvas.create_text(i * 10 + 50, j * 10 + 50 - 20,
text=u'Start',
fill='black'
)
if test_map[j][i] == 'E':
node = self.canvas.create_oval(i * 10 + 50 - self.__r,
j * 10 + 50 - self.__r, i * 10 + 50 + self.__r, j * 10 + 50 + self.__r,
fill="#00ff00",
outline="#ffffff",
tags="node",
)
self.canvas.create_text(i * 10 + 50, j * 10 + 50 + 20,
text=u'End',
fill='black'
)
if test_map[j][i] == '*':
node = self.canvas.create_oval(i * 10 + 50 - self.__r,
j * 10 + 50 - self.__r, i * 10 + 50 + self.__r, j * 10 + 50 + self.__r,
fill="#0000ff",
outline="#ffffff",
tags="node",
)
if test_map[j][i] == ' ':
node = self.canvas.create_oval(i * 10 + 50 - self.__r,
j * 10 + 50 - self.__r, i * 10 + 50 + self.__r, j * 10 + 50 + self.__r,
fill="#AAAAAA",
outline="#ffffff",
tags="node",
)
def find_path(self):
p = Node_Elem(None, self.s_x, self.s_y, 0.0)
while True:
self.extend_round(p)
if not self.open:
return
idx, p = self.get_best()
if self.is_target(p):
self.make_path(p)
return
self.close.append(p)
del self.open[idx]
def make_path(self, p):
while p:
self.path.append((p.x, p.y))
p = p.parent
def is_target(self, i):
return i.x == self.e_x and i.y == self.e_y
def get_best(self):
best = None
bv = 10000000
bi = -1
for idx, i in enumerate(self.open):
value = self.get_dist(i)
if value < bv:
best = i
bv = value
bi = idx
return bi, best
def get_dist(self, i):
return i.dist + math.sqrt((self.e_x - i.x) * (self.e_x - i.x)) + math.sqrt((self.e_y - i.y) * (self.e_y - i.y))
def extend_round(self, p):
xs = (-1, 0, 1, -1, 1, -1, 0, 1)
ys = (-1, -1, -1, 0, 0, 1, 1, 1)
xs = (0, -1, 1, 0)
ys = (-1, 0, 0, 1)
for x, y in zip(xs, ys):
new_x, new_y = x + p.x, y + p.y
if not self.is_valid_coord(new_x, new_y):
continue
node = Node_Elem(p, new_x, new_y, p.dist + self.get_cost(
p.x, p.y, new_x, new_y))
if self.node_in_close(node):
continue
i = self.node_in_open(node)
if i != -1:
if self.open[i].dist > node.dist:
self.open[i].parent = p
self.open[i].dist = node.dist
continue
self.open.append(node)
def get_cost(self, x1, y1, x2, y2):
if x1 == x2 or y1 == y2:
return 1.0
return 1.4
def node_in_close(self, node):
for i in self.close:
if node.x == i.x and node.y == i.y:
return True
return False
def node_in_open(self, node):
for i, n in enumerate(self.open):
if node.x == n.x and node.y == n.y:
return i
return -1
def is_valid_coord(self, x, y):
if x < 0 or x >= self.width or y < 0 or y >= self.height:
return False
return test_map[y][x] != '#'
def get_searched(self):
l = []
for i in self.open:
l.append((i.x, i.y))
for i in self.close:
l.append((i.x, i.y))
return l
def get_start_XY():
return get_symbol_XY('S')
def get_end_XY():
return get_symbol_XY('E')
def get_symbol_XY(s):
for y, line in enumerate(test_map):
try:
x = line.index(s)
except:
continue
else:
break
return x, y
def mark_path(l):
mark_symbol(l, '*')
def mark_searched(l):
mark_symbol(l, ' ')
def mark_symbol(l, s):
for x, y in l:
test_map[y][x] = s
def mark_start_end(s_x, s_y, e_x, e_y):
test_map[s_y][s_x] = 'S'
test_map[e_y][e_x] = 'E'
def tm_to_test_map():
for line in tm:
test_map.append(list(line))
def find_path():
s_x, s_y = get_start_XY()
e_x, e_y = get_end_XY()
a_star = A_Star(tkinter.Tk(), s_x, s_y, e_x, e_y)
a_star.root.mainloop()
a_star.find_path()
searched = a_star.get_searched()
path = a_star.path
mark_searched(searched)
mark_path(path)
mark_start_end(s_x, s_y, e_x, e_y)
print(u"路径长度:%d" % (len(path)))
print(u"搜索过的区域:%d" % (len(searched)))
a_star = A_Star(tkinter.Tk(), s_x, s_y, e_x, e_y)
a_star.root.mainloop()
if __name__ == '__main__':
print (u"""
--------------------------------------------------------
程序:A*迷宫问题程序
作者:Gm
日期:2019-7-08
语言:Python 3.7
--------------------------------------------------------
""")
tm_to_test_map()
find_path()