A *算法最快时间

时间:2015-04-24 13:39:02

标签: java algorithm path shortest-path a-star

我已经实现了A *算法来提供最短的距离路线,但是我试图改变它以便计算最快的路线。 使用这个伪代码:

function A*(start,goal)
    closedset := the empty set    // The set of nodes already evaluated.
    openset := {start}    // The set of tentative nodes to be evaluated, initially containing the start node
    came_from := the empty map    // The map of navigated nodes.

    g_score[start] := 0    // Cost from start along best known path.
    // Estimated total cost from start to goal through y.
    f_score[start] := g_score[start] + heuristic_cost_estimate(start, goal)

    while openset is not empty
        current := the node in openset having the lowest f_score[] value
        if current = goal
            return reconstruct_path(came_from, goal)

        remove current from openset
        add current to closedset
        for each neighbor in neighbor_nodes(current)
            if neighbor in closedset
                continue
            tentative_g_score := g_score[current] + dist_between(current,neighbor)

            if neighbor not in openset or tentative_g_score < g_score[neighbor] 
                came_from[neighbor] := current
                g_score[neighbor] := tentative_g_score
                f_score[neighbor] := g_score[neighbor] + heuristic_cost_estimate(neighbor, goal)
                if neighbor not in openset
                    add neighbor to openset

    return failure

我认为计算最快路线的最简单方法是将当前和邻居之间的距离除以该道路的速度限制:tentative_g_score:= g_score [current] +(dist_between(current,neighbor)/ neighbor)。速度极限) 但是,这并没有在我的算法中给出正确的结果。

任何人都可以指出我如何有效地做到这一点的正确方向?

以下是我当前的代码:http://pastebin.com/QWi6AwF9

最快的路线,从开始位置到达目的地的时间最短。

我的启发式功能就是这个

private double heuristic(Vertex goal, Vertex next) 
    {
        return (Math.sqrt(Math.pow((goal.x - next.x), 2) + Math.pow((goal.y - next.y), 2)));
    }

干杯

1 个答案:

答案 0 :(得分:2)

启发式函数必须是可接受的(也就是说,它永远不应高估到目标的距离)。一旦开始将边长度除以speedlimit > 1,距目标的实际距离可能小于欧几里德距离(如果dist / MAX_SPEED_LIMIT)。它破坏了算法。怎么解决?例如,您可以使用SELECT TIMESTAMPDIFF( DAY, '2015-04-18 12:49:17', NOW() ) AS 'days', TIME_FORMAT( TIMEDIFF( NOW(), '2015-04-18 12:49:17' ), '%h' ) AS 'hours', TIME_FORMAT( TIMEDIFF( NOW(), '2015-04-18 12:49:17' ), '%i' ) AS 'minutes', TIME_FORMAT( TIMEDIFF( NOW(), '2015-04-18 12:49:17' ), '%s' ) AS 'seconds' 作为heursitic函数。