问题描述
这是我的代码,使用 4 点连接在矩阵上的 2 个点之间找到最短路径。如果我想使用8点连接波前算法应该如何改变它?非常感谢你! 类waveFrontPlanner:
def __init__(self,map,slow=False):
self.__slow = slow
self.__map = map
if str(type(map)).find("numpy") != -1:
# If its a numpy array
self.__height,self.__width = self.__map.shape
else:
self.__height,self.__width = len(self.__map),len(self.__map[0])
self.__nothing = 0
self.__wall = 999
self.__goal = 1
self.__path = "PATH"
self.__finalPath = []
# Robot value
self.__robot = 254
#Robot default Location
self.__robot_x = 0
self.__robot_y = 0
# default goal location
self.__goal_x = 5
self.__goal_y = 11
# temp variables
self.__temp_A = 0
self.__temp_B = 0
self.__counter = 0
self.__steps = 0 # determine how processor intensive the algorithm was
# when searching for a node with a lower value
self.__minimum_node = 250
self.__min_node_location = 250
self.__new_state = 1
self.__old_state = 1
self.__reset_min = 250 # above this number is a special (wall or robot)
###########################################################################
def setRobotPosition(self,x,y):
"""Sets the robot's current position"""
self.__robot_x = x
self.__robot_y = y
###########################################################################
def setGoalPosition(self,y):
"""Sets the goal position."""
self.__goal_x = x
self.__goal_y = y
###########################################################################
def robotPosition(self):
return (self.__robot_x,self.__robot_y)
###########################################################################
def goalPosition(self):
return (self.__goal_x,self.__goal_y)
###########################################################################
def run(self,prnt=False):
"""The entry point for the robot algorithm to use wavefront propagation."""
path = []
while self.__map[self.__robot_x][self.__robot_y] != self.__goal:
if self.__steps > 20000:
print("Cannot find a path.")
return
# find new location to go to
self.__new_state = self.propagateWavefront()
# update location of robot
if self.__new_state == 1:
self.__robot_x -= 1
if prnt:
print("Move to x=%d y=%d\n\n" % \
(self.__robot_x,self.__robot_y))
path.append((self.__robot_x,self.__robot_y))
if self.__new_state == 2:
self.__robot_y += 1
if prnt:
print("Move to x=%d y=%d\n\n" % \
(self.__robot_x,self.__robot_y))
if self.__new_state == 3:
self.__robot_x += 1
if prnt:
print("Move to x=%d y=%d\n\n" % \
(self.__robot_x,self.__robot_y))
if self.__new_state == 4:
self.__robot_y -= 1
if prnt:
print("Move to x=%d y=%d\n\n" % \
(self.__robot_x,self.__robot_y))
self.__old_state = self.__new_state
msg = "Found the goal in %i steps:\n" % self.__steps
msg += "Map size= %i %i\n\n" % (self.__height,self.__width)
if prnt:
print(msg)
self.printMap()
return path
###########################################################################
def propagateWavefront(self,prnt=False):
""""""
self.unpropagate()
# Old robot location was deleted,store new robot location in map
self.__map[self.__robot_x][self.__robot_y] = self.__robot
self.__path = self.__robot
# start location to begin scan at goal location
self.__map[self.__goal_x][self.__goal_y] = self.__goal
counter = 0
while counter < 200: # allows for recycling until robot is found
x = 0
y = 0
# while the map hasnt been fully scanned
while x < self.__height and y < self.__width:
#if this location is a wall or the goal,just ignore it
if self.__map[x][y] != self.__wall and \
self.__map[x][y] != self.__goal:
# a full trail to the robot has been located,finished!
minLoc = self.minSurroundingNodeValue(x,y)
if minLoc < self.__reset_min and \
self.__map[x][y] == self.__robot:
if prnt:
print("Finished Wavefront:\n")
self.printMap()
# Tell the robot to move after this return.
return self.__min_node_location
# record a value in to this node
elif self.__minimum_node != self.__reset_min:
# if this isnt here,'nothing' will go in the location
self.__map[x][y] = self.__minimum_node + 1
# go to next node and/or row
y += 1
if y == self.__width and x != self.__height:
x += 1
y = 0
# print self.__robot_x,self.__robot_y
self.__steps += 1
counter += 1
return 0
###########################################################################
def unpropagate(self):
"""clears old path to determine new path stay within boundary"""
for x in range(0,self.__height):
for y in range(0,self.__width):
if self.__map[x][y] != self.__wall and \
self.__map[x][y] != self.__goal and \
self.__map[x][y] != self.__path:
# if this location is a wall or goal,just ignore it
self.__map[x][y] = self.__nothing # clear that space
###########################################################################
def minSurroundingNodeValue(self,y):
"""this method looks at a node and returns the lowest value around that node."""
# reset minimum
self.__minimum_node = self.__reset_min
# down
if x < self.__height -1:
if self.__map[x + 1][y] < self.__minimum_node and \
self.__map[x + 1][y] != self.__nothing:
#find the lowest number node,and exclude empty nodes (0's)
self.__minimum_node = self.__map[x + 1][y]
self.__min_node_location = 3
# up
if x > 0:
if self.__map[x-1][y] < self.__minimum_node and \
self.__map[x-1][y] != self.__nothing:
self.__minimum_node = self.__map[x-1][y]
self.__min_node_location = 1
# right
if y < self.__width -1:
if self.__map[x][y + 1] < self.__minimum_node and \
self.__map[x][y + 1] != self.__nothing:
self.__minimum_node = self.__map[x][y + 1]
self.__min_node_location = 2
# left
if y > 0:
if self.__map[x][y - 1] < self.__minimum_node and \
self.__map[x][y - 1] != self.__nothing:
self.__minimum_node = self.__map[x][y-1]
self.__min_node_location = 4
return self.__minimum_node
###########################################################################
def printMap(self):
"""Prints out the map of this instance of the class."""
msg = ''
for temp_B in range(0,self.__height):
for temp_A in range(0,self.__width):
if self.__map[temp_B][temp_A] == self.__wall:
msg += "%04s" % "[1]"
elif self.__map[temp_B][temp_A] == self.__robot:
msg += "%04s" % "S"
elif self.__map[temp_B][temp_A] == self.__goal:
msg += "%04s" % "G"
else:
msg += "%04s" % str(self.__map[temp_B][temp_A])
msg += "\n\n"
msg += "\n\n"
print(msg)
这是我的 4 点连接波前的波扩展: enter image description here
以下是 4 点连接和 8 点连接的区别: enter image description here
8 点连接的结果应该是这样的: enter image description here
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)