解決策を継続的に検索するために私の迷路を更新しようとしています(これは迷路の単純な統一コスト検索です)。私はパイロットを使用してion()
各ノードの訪問後に私の数字を更新しています。私の問題は、数値が約20回の反復で非常にゆっくりと更新されることです。私はpause()
の値を減らそうとしましたが、効果がないようです。 私はそれが私のPCではないと確信しています。ここPython - matplotlib.pyplot ion()slow
import matplotlib.pyplot as plt
from matplotlib import colors as c
import math
import numpy as np
class Queue:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def enqueue(self, item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
def euclideanDistance(pos1, pos2):
return math.sqrt(math.pow((pos2[0]-pos1[0]),2) + math.pow((pos2[1]-pos1[1]),2))
def getChildren(node, maze):
children = []
y = node[0]
x = node[1]
i = 0
if y-1 != -1 and maze[y-1][x] != 1 and maze[y-1][x] != 2:
children.append([])
children[i].append(y-1)
children[i].append(x)
i += 1
if y-1 != -1 and x+1 != 12 and maze[y-1][x+1] != 1 and maze[y-1][x+1] != 2:
children.append([])
children[i].append(y-1)
children[i].append(x+1)
i += 1
if x+1 != 12 and maze[y][x+1] != 1 and maze[y][x+1] != '.':
children.append([])
children[i].append(y)
children[i].append(x+1)
i += 1
if y+1 != 12 and x-1 != -1 and maze[y+1][x-1] != 1 and maze[y+1][x-1] != 2:
children.append([])
children[i].append(y+1)
children[i].append(x-1)
i += 1
if y+1 != 12 and maze[y+1][x] != 1 and maze[y+1][x] != '.':
children.append([])
children[i].append(y+1)
children[i].append(x)
i += 1
if y+1 != 12 and x+1 != 12 and maze[y+1][x+1] != 1 and maze[y+1][x+1] != 2:
children.append([])
children[i].append(y+1)
children[i].append(x+1)
i += 1
if x-1 != -1 and maze[y][x-1] != 1 and maze[y][x-1] != 2:
children.append([])
children[i].append(y)
children[i].append(x-1)
i += 1
if y-1 != -1 and x-1 != -1 and maze[y-1][x-1] != 1 and maze[y-1][x-1] != 2:
children.append([])
children[i].append(y-1)
children[i].append(x-1)
i += 1
return children
def uniformCostSearch(root, goal, maze):
q = Queue()
path = maze
root.append(0)
q.enqueue(root)
while not q.isEmpty():
temp = q.dequeue()
printMaze(path)
path[temp[0]][temp[1]] = 2
if temp[0] == goal[0] and temp[1] == goal[1]:
return path
else:
children = getChildren(temp, path)
cArray = []
if len(children) != 0:
for child in children:
child.append(temp[2]+euclideanDistance(temp, child))
cArray.append(child)
cArray.sort(key=lambda x:x[2])
for child in cArray:
q.enqueue(child)
def printMaze(maze):
y = [12,11,10,9,8,7,6,5,4,3,2,1,0]
x = [0,1,2,3,4,5,6,7,8,9,10,11,12]
x, y = np.meshgrid(x, y)
maze = np.array(maze)
plt.ion()
cMap = c.ListedColormap(['w','grey','green','red'])
plt.xticks([0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5,11.5], [0,1,2,3,4,5,6,7,8,9,10,11])
plt.yticks([0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5,11.5], [0,1,2,3,4,5,6,7,8,9,10,11])
plt.pcolormesh(x, y, maze, edgecolor='k',cmap=cMap)
plt.pause(0.000000001)
plt.show()
maze = [[0,0,0,0,0,0,0,0,0,0,0,0],
[0,1,1,1,1,1,1,1,1,1,1,0],
[0,1,1,1,1,1,1,1,0,3,1,0],
[0,0,0,0,0,0,1,1,0,0,1,0],
[0,1,1,0,0,0,1,1,0,0,1,0],
[0,1,1,0,0,0,1,1,0,0,1,0],
[0,1,0,0,0,0,1,1,0,0,1,0],
[0,0,0,0,0,1,1,1,0,0,1,0],
[0,0,0,0,1,1,1,1,0,0,1,0],
[0,0,0,1,1,1,1,1,0,0,1,0],
[0,0,1,1,1,1,1,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0]]
root = []
root.append(11)
root.append(0)
goal = []
goal.append(2)
goal.append(9)
printMaze(maze)
uniformCostSearch(root, goal, maze)