Python实现中的双向BFS

Python实现中的双向BFS,python,breadth-first-search,maze,bidirectional-search,Python,Breadth First Search,Maze,Bidirectional Search,我正在Python3.7.3中为2d迷宫问题实现一个双向BFS 首先,以下是我如何创建迷宫: for row in range(dim): # Add an empty array that will hold each cell # in this row grid.append([]) for column in range(dim): grid[row].append(np.random.binomial(1, 0.2, 1)) # App

我正在Python3.7.3中为2d迷宫问题实现一个双向BFS

首先,以下是我如何创建迷宫:

for row in range(dim):
    # Add an empty array that will hold each cell
    # in this row
    grid.append([])
    for column in range(dim):
        grid[row].append(np.random.binomial(1, 0.2, 1))  # Append a cell
        if (row == column == dim - 1):
            grid[row][column] = 0
    grid[0][0] = 0
现在,为了促进双向BFS,我定义了一个junct类,它本质上是一个节点,存储起始方向的父junct和目标方向的父junct

class junct():
    def __init__(self, row, column, parent_S, parent_G):
        self.row = row
        self.column = column
        self.parent_S = parent_S
        self.parent_G = parent_G

    def __eq__(self, other):
        return (self.row == other.row and self.column == other.column)
这是我的双向bfs函数。这是一个代理类的一部分,到目前为止还没有给我任何问题。我希望此函数返回从开始到目标的路径中所有节点的元组列表(行、列)

def bidirectional_bfs(self):
        def get_value(maze, a, b):
            return maze[a][b]
        def is_out_of_bounds(a, b, d):
            return (a < 0 or a >= dim) or (b < 0 or b >= d)

        grid = self.grid
        dim = len(grid)
        Q_start = []
        Q_goal = []
        visited = []
        start = junct(0, 0, None, None)
        goal = junct(dim-1, dim-1, None, None)
        Q_start.append(start)
        Q_goal.append(goal)
        visited.append(start)
        visited.append(goal)


        #beginning loop
        while (len(Q_start) > 0) and (len(Q_goal) > 0):
            #initializations
            current_S = Q_start[0]
            current_G = Q_goal[0]

            row_S = current_S.row
            column_S = current_S.column

            row_G = current_G.row
            column_G = current_G.column

            #some mechanics
            if len(Q_start) > 0:
                Q_start.pop(0)
            if len(Q_goal) > 0:
                Q_goal.pop(0)

            #in case the current node from starting is in the goal Queue
            if (current_S in Q_goal):
                #forming the path back to G
                current = current_S
                path_S = [current]
                while current.parent_S is not None:
                    path_S.append(current.parent_S)
                    current = current.parent_S
                path_S = [(item.row, item.column) for item in path_S]
                print(path_S)

             #in case the current node from goal is in the start Queue
            if (current_G in Q_start):
                #forming the path back to S
                current = current_G
                path_G = [currrent]
                while current.parent_S is not None:
                    path_G.append(current.parent_G)
                    current = current.parent_G
                path_G = [(item.row, item.column) for item in path_G]
                print(path_G)

            if (current_S in Q_goal) and (current_G in Q_start):
                path = [item for item in path_G for item in path_S]
                print(path)
                return path

            #enqueueing children from the start direction
            children_S = [junct(row_S+1, column_S, current_S, None), junct(row_S-1, column_S, current_S, None), junct(row_S, column_S+1, current_S, None), junct(row_S, column_S-1, current_S, None)]
            for child in children_S:
                if not is_out_of_bounds(child.row, child.column, dim):
                    if child not in visited:
                        if get_value(grid, child.row, child.column) == 0:
                            Q_start.append(child)
                            visited.append(child)

            #enqueueing childen from the goal direction
            #enqueueing children from the start direction
            children_G = [junct(row_G+1, column_G, None, current_G), junct(row_G-1, column_G, None, current_G), junct(row_G, column_G+1, None, current_G), junct(row_G, column_G-1, None, current_G)]
                for child in children_S:
                    if not is_out_of_bounds(child.row, child.column, dim):
                        if child not in visited:
                            if get_value(grid, child.row, child.column) == 0:
                                Q_goal.append(child)
                                visited.append(child)

        return []
        print("No path")
def双向_bfs(自):
def get_值(迷宫、a、b):
返回迷宫[a][b]
def超出范围(a、b、d):
返回(a<0或a>=dim)或(b<0或b>=d)
grid=self.grid
尺寸=透镜(网格)
Q_start=[]
Q_目标=[]
已访问=[]
start=junct(0,0,无,无)
目标=容克(dim-1,dim-1,无,无)
Q_start.append(开始)
Q_goal.append(目标)
已访问。追加(开始)
已访问。追加(目标)
#起始循环
而(len(Q_开始)>0)和(len(Q_目标)>0):
#初始化
当前\u S=Q\u开始[0]
当前目标=Q目标[0]
行\美国=当前\美国行
列\ S=当前\ S列
行=当前行
列=当前列
#一些机械师
如果len(Q_start)>0:
Q_start.pop(0)
如果len(Q_目标)>0:
Q_goal.pop(0)
#如果起始的当前节点位于目标队列中
如果(Q_目标中的当前_):
#形成返回G的路径
电流=电流
路径_S=[当前]
当current.parent_不是None时:
路径附加(当前的父路径)
current=current.parent\S
路径_S=[(item.row,item.column)用于路径_S中的项]
打印(路径)
#如果目标的当前节点在开始队列中
如果(Q_启动中的当前_G):
#形成返回S的路径
电流=电流
路径_G=[当前值]
当current.parent_不是None时:
路径追加(当前的父路径)
current=current.parent\u G
path_G=[(item.row,item.column)用于路径_G中的项]
打印(路径)
if(当前Q_目标)和(当前Q_开始):
path=[路径中的项目对应于路径中的项目]
打印(路径)
返回路径
#从开始方向将子项排队
children_S=[junct(行_S+1,列_S,当前_S,无),junct(行_S-1,列_S,当前_S,无),junct(行_S,列_S+1,当前_S,无)]
对于儿童中的儿童:
如果不是,则超出范围(child.row、child.column、dim):
如果未探访儿童:
如果get_值(网格、child.row、child.column)=0:
Q_start.append(子项)
已访问。追加(子项)
#将儿童从目标方向排队
#从开始方向将子项排队
children\u G=[junct(行+列,无,当前),junct(行,列,无,当前),junct(行,列+列,无,当前),junct(行,列,当前),junct(行,列,无,当前)]
对于儿童中的儿童:
如果不是,则超出范围(child.row、child.column、dim):
如果未探访儿童:
如果get_值(网格、child.row、child.column)=0:
Q_目标追加(子项)
已访问。追加(子项)
返回[]
打印(“无路径”)

我的代码哪里出错了?返回路径需要更改哪些内容?

请指定遇到的问题以及重现该问题的示例输入。