【问题标题】:AStar Search - How to change directions and costs?AStar Search - 如何更改路线和费用?
【发布时间】:2018-05-20 17:27:50
【问题描述】:

因此,在我正在研究的 A* 搜索实现中,我可以使用向上、向左、向右、向下的寻路方向:

#define the moving direction: up /left /right /down
xs = (0, -1, 1, 0)
ys = (-1, 0, 0, 1)

如何将移动方向更改为:上、左、右、下、左上、左下、右上和右下?

至于成本,上,左,右,下我有1.0,但如果我必须将成本作为2.0的方向:左上,左下,右上和右下,我该如何实现那个?

def get_cost(self, x1, y1, x2, y2):
    if x1 == x2 or y1 == y2:
      return 1.0

【问题讨论】:

    标签: python-3.x artificial-intelligence a-star


    【解决方案1】:

    好吧,您可以简单地检查一下您是否在每个轴上的相同位置。

    def get_cost(self, x1, y1, x2, y2):
        if (x1 == x2 and y1 != y2) or (x1 != x2 and y1 == y2):
            # you have just moved in one axis
            return 1.0
        else:
            return 2.0
    

    【讨论】:

    • 如何更改路线?如何为此添加更多路线?
    • 计算 x_dir = x2-x1 和 y_dir = y2-y1。根据结果​​,您可以找到方向。例如,如果 x_dir = 1 且 y_dir = 0,则它已向右移动
    • 我需要手动定义方向。 #define the moving direction: up /left /right /down xs = (0, -1, 1, 0) ys = (-1, 0, 0, 1) 定义为上/左/右/下。
    • 您的新方向将如下所示:上/左/右/下/右上/左上/右下/左下 xs = (0, -1, 1, 0, 1, -1, 1, -1) ys = (-1, 0, 0, 1, -1, -1, 1, 1)
    猜你喜欢
    • 1970-01-01
    • 2012-10-26
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多