Python网格深度优先搜索算法的实现
我正在尝试编写一个深度优先搜索算法,该算法将查找代理(黑立方体)到右侧路径底部出口的路径形式。但是,我写的算法作为找到的路径的一部分,在自身上循环。我如何实现一个不这样做的DFS算法 你知道我做错了什么吗 非常感谢您的帮助 谢谢 世界是什么样子:Python网格深度优先搜索算法的实现,python,algorithm,search,gridworld,Python,Algorithm,Search,Gridworld,我正在尝试编写一个深度优先搜索算法,该算法将查找代理(黑立方体)到右侧路径底部出口的路径形式。但是,我写的算法作为找到的路径的一部分,在自身上循环。我如何实现一个不这样做的DFS算法 你知道我做错了什么吗 非常感谢您的帮助 谢谢 世界是什么样子: class Agent(turtle.Turtle): def __init__(self, location, endPoint, world): turtle.Turtle.__init__(self) s
class Agent(turtle.Turtle):
def __init__(self, location, endPoint, world):
turtle.Turtle.__init__(self)
self.shape("square")
self.color("black")
self.penup()
self.speed(0)
# Variables
self._bump = 0
self._location = location
self._endPoint = endPoint
self._world = world
self._map = dict()
def dfs_paths(self, start, goal, path=None):
if path is None:
path = [start]
if start == goal:
yield path
for next in self._map[tuple(start)] - set(path):
yield from dfs_paths(next, goal, path + [next])
def _planPath(self, node, visited=None):
if visited is None:
visited = [node]
self._map[tuple(node)] = self._world.testDirections(node)
if node not in visited:
visited.append(tuple((node)))
print("Visited = " + str(visited))
for neighbour in self._map[tuple((node))]:
print("Neighbour = " + str(neighbour))
if neighbour == self._endPoint:
visited.append(neighbour)
print("Here 1...")
return [node, neighbour]
else:
path = self._planPath(neighbour,visited)
if path:
print("Here 2...")
return [node] + path
深度优先搜索路径规划的结果:
class Agent(turtle.Turtle):
def __init__(self, location, endPoint, world):
turtle.Turtle.__init__(self)
self.shape("square")
self.color("black")
self.penup()
self.speed(0)
# Variables
self._bump = 0
self._location = location
self._endPoint = endPoint
self._world = world
self._map = dict()
def dfs_paths(self, start, goal, path=None):
if path is None:
path = [start]
if start == goal:
yield path
for next in self._map[tuple(start)] - set(path):
yield from dfs_paths(next, goal, path + [next])
def _planPath(self, node, visited=None):
if visited is None:
visited = [node]
self._map[tuple(node)] = self._world.testDirections(node)
if node not in visited:
visited.append(tuple((node)))
print("Visited = " + str(visited))
for neighbour in self._map[tuple((node))]:
print("Neighbour = " + str(neighbour))
if neighbour == self._endPoint:
visited.append(neighbour)
print("Here 1...")
return [node, neighbour]
else:
path = self._planPath(neighbour,visited)
if path:
print("Here 2...")
return [node] + path
代理类的我的代码:
class Agent(turtle.Turtle):
def __init__(self, location, endPoint, world):
turtle.Turtle.__init__(self)
self.shape("square")
self.color("black")
self.penup()
self.speed(0)
# Variables
self._bump = 0
self._location = location
self._endPoint = endPoint
self._world = world
self._map = dict()
def dfs_paths(self, start, goal, path=None):
if path is None:
path = [start]
if start == goal:
yield path
for next in self._map[tuple(start)] - set(path):
yield from dfs_paths(next, goal, path + [next])
def _planPath(self, node, visited=None):
if visited is None:
visited = [node]
self._map[tuple(node)] = self._world.testDirections(node)
if node not in visited:
visited.append(tuple((node)))
print("Visited = " + str(visited))
for neighbour in self._map[tuple((node))]:
print("Neighbour = " + str(neighbour))
if neighbour == self._endPoint:
visited.append(neighbour)
print("Here 1...")
return [node, neighbour]
else:
path = self._planPath(neighbour,visited)
if path:
print("Here 2...")
return [node] + path
您正在根据访问的
信息构建路径。但这并不代表路径。它只表示您访问过的节点,其中包括您已经回溯的不成功路径
当您找到目标节点时,您应该创建一个(部分)路径,其中只包含结束节点,并将其返回给调用者。调用方可以从递归调用的返回值中检测到已找到目标,然后可以将其自己的节点前置到该部分路径,并将其返回给其自己的调用方
这样,回溯阶段(成功后)将用于构建路径
因此,要开始,请替换以下内容:
self._planPath(neighbour, visited)
self._world.showPath(visited)
与:
或者执行一个更有效的append
,但是必须在最末端反转路径
并将其替换为:
self._planPath(neighbour, visited)
self._world.showPath(visited)
与:
self.\u planPath
的主要调用方可能会执行以下操作:
path = self._planPath(startnode, [])
if path:
self._world.showPath(path)
请注意,对于已访问的
,您应该真正使用集合,而不是列表。现在是学习和逐步浏览代码并观察每行代码的功能的好时机。通过将这些中间结果与预期结果进行比较,确定您的计划与您的预期不同之处。从那里向后努力,缩小问题的原因。如果您仍然对代码的行为感到困惑,请提出一个特定的问题。转储您的代码并期望其他人为您调试它是不好的。我不是要求对它进行配音,我不知道如何实现深度优先搜索,这样它就不会自行循环,而是要求对它进行调试。请提供@MahmoudYassine如果您跟踪访问的节点,应该很容易防止cycle@AbhinavMathur我相信我正在用_planPath函数中的变量“visted”跟踪访问的节点。当你说将其返回给调用者时,你的意思是什么?请问您所指的调用方是什么?我指的是在self中时。\u planPath
它执行self.\u planPath(邻居,已访问)
。目前您没有返回值功能,但我想说的是让(部分)路径成为返回值(或者None
如果没有成功),请参阅addition to answer。我尝试了您的建议并编辑了上面的代码,以向您展示我所做的。还更改了调用者,但现在没有从代码中获得任何输出?请不要通过添加我的答案的元素来更改您的问题。这让我的答案看起来很愚蠢,因为它不再适用于你的问题。