Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更新A*以包含地形成本_Python_A Star - Fatal编程技术网

Python 更新A*以包含地形成本

Python 更新A*以包含地形成本,python,a-star,Python,A Star,尝试更新a*实现以包含不同地形的成本。我知道我必须更新我的child.g以包含地形的成本,而不是非地形成本实现的当前_node.g+1,但我不知道如何更新 # child.g = current_node.g + maze[child.g] 所有成本在我的gridmaze[]中以数字表示。尝试最后一行只会给我一个错误。这不再是一个迷宫,而是为了最初的几项任务 感谢大家在这方面的帮助 编辑:发布整个代码,它适用于非称重板 maze5 = [[50,50,50,50,50,10,10,10,10,

尝试更新a*实现以包含不同地形的成本。我知道我必须更新我的child.g以包含地形的成本,而不是非地形成本实现的当前_node.g+1,但我不知道如何更新

# child.g = current_node.g + maze[child.g]
所有成本在我的gridmaze[]中以数字表示。尝试最后一行只会给我一个错误。这不再是一个迷宫,而是为了最初的几项任务

感谢大家在这方面的帮助

编辑:发布整个代码,它适用于非称重板

maze5 = [[50,50,50,50,50,10,10,10,10,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,10,10,10,50,50,50,50,50],
    [50,50,50,10,10,10,10,10,10,10,10,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,10,10,10,10,10,50,50,50,50],
    [50,50,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,1,10,10,10,10,10,10,50,50,50,50],
    [50,50,10,10,10,10,10,10,10,10,10,10,10,10,10,100,100,100,100,100,10,10,10,10,10,10,10,10,10,1,10,10,10,10,10,10,10,50,50,50],
    [50,10,10,10,10,10,10,10,10,10,10,10,10,10,100,100,100,100,100,100,100,10,10,10,10,10,10,10,10,1,10,10,10,10,10,10,50,50,50,50],
    [50,50,10,10,10,10,10,10,10,10,10,10,10,10,100,100,100,100,100,100,100,10,10,1,1,1,1,1,1,1,1,1,1,1,1,1,50,50,50,50],
    [50,50,50,10,10,10,10,10,10,10,10,10,10,10,10,100,100,100,100,100,10,10,10,10,10,10,10,10,10,10,10,10,1,10,10,10,10,50,50,50],
    [50,50,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,1,10,10,10,10,10,50,50],
    [50,50,10,10,10,10,10,10,10,10,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,10,10,10,10,50,50],
    [50,50,50,10,10,10,10,5,5,5,5,5,5,5,5,5,5,0,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,10,10,50,50]]









class Node():
"""A node class for A* Pathfinding"""

    def __init__(self, parent=None, position=None):
        self.parent = parent
        self.position = position

        self.g = 0
        self.h = 0
        self.f = 0

    def __eq__(self, other):
        return self.position == other.position


def astar(maze, start, end):
"""Returns a list of tuples as a path from the given start to the given end in the given maze"""

# Create start and end node
    start_node = Node(None, start)
    start_node.g = start_node.h = start_node.f = 0
    end_node = Node(None, end)
    end_node.g = end_node.h = end_node.f = 0

# Initialize both open and closed list
    open_list = []
    closed_list = []

# Add the start node
    open_list.append(start_node)

# Loop until you find the end
    while len(open_list) > 0:

    # Get the current node
        current_node = open_list[0]
        current_index = 0
        for index, item in enumerate(open_list):
            if item.f < current_node.f:
                current_node = item
                current_index = index

    # Pop current off open list, add to closed list
        open_list.pop(current_index)
        closed_list.append(current_node)

    # Found the goal
        if current_node == end_node:
             path = []
            current = current_node
            while current is not None:
            path.append(current.position)
            current = current.parent
            return path[::-1]  # Return reversed path

    # Generate children
        children = []
        for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]:  # Adjacent squares

        # Get node position
            node_position = (current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])

        # Make sure within range
            if node_position[0] > (len(maze) - 1) or node_position[0] < 0 or node_position[1] > (len(maze[len(maze) - 1]) - 1) or node_position[1] < 0:
                continue

            if Node(current_node, node_position) in closed_list:
                continue

        # Make sure walkable terrain (Only for task 1)
            #if maze[node_position[0]][node_position[1]] != 0:
               # continue

        # Create new node
            new_node = Node(current_node, node_position)

        # Append
            children.append(new_node)

    # Loop through children
        for child in children:

        # Child is on the closed list
            for closed_child in closed_list:
                if child == closed_child:
                    continue

        # Create the f, g, and h values

            child.g = current_node.g + 1
            child.h = ((child.position[0] - end_node.position[0]) ** 2) + ((child.position[1] - end_node.position[1]) ** 2)
            child.f = child.g + child.h

        # Child is already in the open list
            for open_node in open_list:
                if child == open_node and child.g > open_node.g:
                    continue

        # Add the child to the open list
            open_list.append(child)


def main():

"""path = astar(maze1, start1, end1)
print("Path Maze 1: ")
print(path)
print("\n")

path2 = astar(maze2, start2, end2)
print("Path Maze 2: ")
print(path2)
print("\n")

path3 = astar(maze3, start3, end3)
print("Path Maze 3: ")
print(path3)
print("\n")

path4 = astar(maze4, start4, end4)
print("Path Maze 4: ")
print(path4)"""

    path5 = astar(maze5, start5, end5)
    print("Path Maze 5: ")
    print(path5)
变量child.g仅在for循环内有效。 由于您试图在循环结束后访问它,因此会导致其抛出错误


如果不是这样的话,请分享你得到的错误。

它给你带来了什么错误?@SuchARush,你能提供更多关于迷宫的信息吗。举一个迷宫的例子就可以了。现在发布了整个代码。算法不需要改变以支持权重,A*本身就支持加权图。好吧,你可能是对的,但如果你不详细说明的话,那对我一点帮助都没有。我想这是我的问题,你如何更新g-cost,使其适用于权重,然后我可以将迷宫中的1更改为一个大的数字,它适用于所有情况。所以,是的,child.g在退出for循环之前被添加到开放列表中