【问题标题】:Bloxorz a-Star SearchBloxorz a-Star 搜索
【发布时间】:2022-01-15 23:03:50
【问题描述】:

我正在努力在 Bloxorz 游戏上实现 a-Star 算法。目标是使用 1 x 1 x 2 块到达终点。我实现了算法,但它不一致。有时它不会给出最短的解决方案。例如:

maze = ['00011111110000',
        '00011111110000',
        '11110000011100',
        '11100000001100',
        '11100000001100',
        '1S100111111111',
        '11100111111111',
        '000001E1001111',
        '00000111001111']

对于这个迷宫,我的实现给出了这样的结果:

U,L,U,R,R,U,R,R,R,R,R,R,D,R,D,D,D,L,L,L,D,R,D,L ,U,R,U,L,D

有 29 步。但是有一个较短的解决方案,它有 28 步:

U,L,U,R,R,U,R,R,R,R,R,R,D,R,D,D,D,D,D,R,U,L,L,L ,L,L,L,D

这是我的实现,完整代码是here,我能做些什么呢?

class Node:
    def __init__(self,parent:'Node', node_type:str, x1:int, y1:int, x2:int, y2:int, direction:str=''):
        self.parent = parent
        self.node_type = node_type
        self.g = 0
        self.h = 0
        self.f = 0
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.visited = False
        self.direction = direction
    def get_positions(self) -> tuple:
        return (self.x1, self.y1, self.x2, self.y2)
    def __eq__(self, other):
        if type(other) is Node:
            return self.x1 == other.x1 and self.y1 == other.y1 and self.x2 == other.x2 and self.y2 == other.y2
        elif type(other) is tuple:
            return self.x1 == other[0] and self.y1 == other[1] and self.x2 == other[2] and self.y2 == other[3]
        else:
            return False
    def __lt__(self, other:'Node'):
        return self.f < other.f

def aStar(start:Node, end:Node, grid:List[List[str]]) -> List[tuple]:
    open_list = []
    closed_list = []
    heapq.heappush(open_list, start)
    while open_list:
        current:Node = heapq.heappop(open_list)
        if current == end:
            return reconstruct_path(current)
        closed_list.append(current)
        for neighbor in get_neighbors(current, grid):
            if neighbor not in closed_list:
                neighbor.g = current.g + 1
                neighbor.h = get_heuristic(neighbor, end)
                neighbor.f = neighbor.g + neighbor.h
                if neighbor not in open_list:
                    heapq.heappush(open_list, neighbor)
    return []

def reconstruct_path(current:Node) -> List[tuple]:
    path = []
    while current.parent is not None:
        path.append(current.direction)
        current = current.parent
    return ''.join(path[::-1])

def get_heuristic(current:Node, end:Node) -> int:
    return max(abs(current.x2 - end.x1), abs(current.y2 - end.y1))

【问题讨论】:

    标签: python python-3.x algorithm shortest-path a-star


    【解决方案1】:

    假设您的实现中的其他所有内容都是正确的,那只是因为您的启发式方法不可接受。

    考虑迷宫:

    B 1 1 X
    

    您可以在 2 步内达到目标:

    1 B B X : move1
    1 1 1 B : move2
    

    但您的启发式建议至少需要 3 步

    max(abs(current.x2 - end.x1), abs(current.y2 - end.y1))
    = max(abs(0-3), abs(0-0)) = max(3, 0) = 3
    

    启发式函数不能高估 A* 达到目标所需的移动次数以始终给出最优路径,因为这样做可能会在达到目标时留下未探索的潜在最优路径(最优路径可能永远不会被扩展,因为它的成本被 h(n)) 高估了。

    您需要一个启发式算法,它考虑到给定坐标在任何给定移动中最多可以更改 2 的事实(当一个块从站立变为躺下时,反之亦然)。为此,您可以将当前启发式函数的结果除以 2。

    def get_heuristic(current:Node, end:Node) -> int:
        return 1/2 * max(abs(current.x2 - end.x1), abs(current.y2 - end.y1))
    

    这给出了长度为 28 的路径 ULURRURRRRRRDRDDDDDRULLLLLLD

    【讨论】:

    • 感谢您的回复。在你的帮助下,我做到了。我把如何回答。再次感谢。
    【解决方案2】:

    正如 inordirection 所说,问题在于启发式函数。 inordirection 的答案不能直接起作用,但给了我一些想法,我想出了这个可行的方法。

    return 1/4 * max(max(abs(current.x1 - end.x1), abs(current.y1 - end.y1)), max(abs(current.x2 - end.x2), abs(current.y2 - end.y2)))
    

    因为可能有两个点定位块,所以我应该从末端选择 x1 和 x2 的最大差异,从末端选择 y1 和 y2,而不是选择它们中的最大值并乘以 1/4,因为它是从 4 个点中选择的。

    【讨论】:

    • 啊,我想包括所有4点,但我认为没有必要。也许我只是想不出正确的反例。但是你确定你需要乘以 1/4 吗?有 2 或 4 个可能的点需要考虑的事实与乘数没有直接关系——它只是估计达到目标可能需要的最小移动次数(我使用了 1/2,因为移动可以让你更近 2 步)。
    • 一般来说,你想让你的启发式值尽可能大而不高估,因为它越大你可以越有效地修剪搜索空间,所以A*可以运行得更快。例如,如果您的启发式为零,则 A* 会演变为 BFS。
    • 我测试了 1/2 超过 150 个案例,它通过了 145。正因为如此,我想出了这个。谢谢你的回答。
    • 以下是失败一例:[ '01100000000000000000', '11110000011001111111', '11111111111001111111', '1B111111111001110011', '11110000011111110011', '11110000011111110011', '00000000000000000011', '11111111101110011011',“11X11011101111111111 ', '11110011111110011000'] 灵魂有 40 个步骤。最短的有 39 个。使用 1/4 可以避免这种情况。
    • 奇怪。我得到了它:def get_heuristic(current:Node, end:Node) -&gt; int: maxXdist = max(abs(current.x1 - end.x1), abs(current.x2 - end.x1)) maxYdist = max(abs(current.y1 - end.y1), abs(current.y2 - end.y1)) return 0.4999 * max(maxXdist, maxYdist),但不适用于0.5。一定是在看什么。无论如何,谢谢。
    猜你喜欢
    • 2012-02-06
    • 2013-01-16
    • 1970-01-01
    • 2023-03-25
    • 2015-05-17
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    相关资源
    最近更新 更多