简体   繁体   English

当不是所有节点都使用A *算法连接时,如何找到最短路径?

[英]How to find the shortest path when not all the nodes are connected using A* algorithm?

Im trying to find the shortest path/best path from Bus Station A to Bus Station B using the A* algorithm where the bus route containing Station A and the bus route containing Station B might not have a common bus station. 我试图使用A *算法查找从公交车站A到公交车站B的最短路径/最佳路径,其中包含车站A的公交路线和包含车站B的公交路线可能没有公共巴士站。

(I would also like to change the cost of a move in such way so that i use as little buses as possible to reach my destination) (我也想以此方式改变出行费用,以使我尽量少乘坐公交车到达目的地)

My BusRoute Class contains a linked list of bus stations,and each station has a name,latitude and longitude 我的BusRoute类包含一个公交车站的链接列表,每个车站都有一个名称,纬度和经度

The NODE Class i use to represent the states in A*: 我使用NODE类来表示A *中的状态:

public class Node
{
    double gCost;
    double hHeuristic;
    double f;//f=g+h
    Node parent;
    BusStation station;//contains lat,lng name
    String reached;//is either "Bus" or "Walking" , how i reached this node
    int busRouteId;//via which route i reached this station
}

gCost is the Havesine Distance between 2 stations gCost是2个站点之间的Havesine距离

hHeuristic is the Havesine Distance between the current station and the goal hHeuristic是当前测站与目标之间的Havesine距离

I keep expanding the node with the lowest f value from the priority queue util it has the goal station 我继续从具有目标站的优先级队列开始扩展具有最低f值的节点

This is the part where i generate all the possible transitions from the current node: 这是我从当前节点生成所有可能过渡的部分:

ArrayList<Node> posibleMoves=getPosibleMoves(current);
    for(Node p:posibleMoves){
        if(!visitedNodes.contains(p) ) {
            double predictedDistance = calculateHeuristic(p, goal);
            double cost;
            if (p.getReached().equals("Walking")) {
                cost=(10* havesineDistance(current.getLat(), current.getLng(), p.getLat(), p.getLng())) + current.getgCost();
            } else { //Reached using a bus
                cost=havesineDistance(current.getLat(), current.getLng(), p.getLat(), p.getLng()) + current.getgCost();
            }
            p.setgCost(cost);
            p.sethHeuristic(predictedDistance);
            p.setF(cost + predictedDistance);
            p.setParent(current);
            priorityQueue.add(p);
        }
    }
}

getPossibleMoves(current) finds all the bus routes containing this station and adds the next station from each found route to an arraylist. getPossibleMoves(current)查找包含该站的所有公交路线,并将每个找到的路线中的下一个站添加到数组列表中。

After that it adds "Walking" Nodes to every other bus route that does not contain this station, trying to connect the routes togheter this way. 之后,它将“正在行走”节点添加到不包含该站点的所有其他总线路线中,尝试以这种方式将路线连接至gheter。 (it finds the closest station from each route to the current station) (它找到从每个路线到当前站点的最近站点)

I set the cost of a Node reached by Walking to be 10 times the cost because a human is about 10 times slower than a bus. 我将步行到达的节点的成本设置为成本的10倍,因为人的速度比公共汽车慢10倍左右。

After running the program i am not geting desired results, it either changes too many buses or it seems to walk for no reason. 运行该程序后,我没有得到期望的结果,它要么更换了太多的公共汽车,要么似乎无缘无故走路。 Also after checking the priority queue sometimes it doesnt poll the node with the lowest f value. 同样,在检查优先级队列之后,有时它不会轮询具有最低f值的节点。

Is this a good way to aproach this problem? 这是解决这个问题的好方法吗? Am i missing something? 我想念什么吗?

Most beginners in programming underestimate the difficulty of implementing pathplanning algorithm. 大多数编程初学者都低估了实现路径规划算法的难度。 A* is not a easy-going task, especially if is used with additional constraints. A *不是一项容易完成的任务,尤其是在与其他约束一起使用时。 It must defined as a middle-size software project. 它必须定义为中型软件项目。 I would guess that a vanilla A* algorithm needs 500 lines of code, while the extended variant of the OP with priority queue and heuristics needs 1000 lines of code. 我猜想香草A *算法需要500行代码,而具有优先级队列和启发式的OP的扩展变体需要1000行代码。 It is a good practice in software engineering to use UML charts for describing the interactions of the different classes. 在软件工程中,最好使用UML图表来描述不同类的交互。 So my advice is, to download the dia software from the internet, and drawing around 10 classes in the chart for solving the A* algorithm. 因此,我的建议是从互联网上下载dia软件,并在图表中绘制约10个类来解决A *算法。 Programming a pathplanner without any UML chart and in the hope for doing it in one small method is some kind of blind flying. 对没有任何UML图表的路径规划器进行编程,并希望以一种较小的方法来完成它是一种盲目的尝试。

Two exmple UML chart for pathplanning applications are here: [1] [2] They can't be used out-of-the box, but they are showing in which direction the project should go. 这里有两个用于路径规划应用的示例性UML图表: [1] [2]不能开箱即用地使用它们,但是它们显示了项目应该朝哪个方向发展。 They have around 10-15 boxes connected with edges which are describing a complex software project with lots of classes and methods. 它们有大约10-15个与边连接的盒子,这些盒子描述了具有许多类和方法的复杂软件项目。 Otherwise, the amount of complexity can't be handled. 否则,复杂性将无法处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dijkstra算法在大图中找到两个节点之间的最短路径? - Dijkstra algorithm to find shortest path between two nodes in big graph? 无法使用Dijkstra算法找到最短路径? - Not able to find shortest path using Dijkstra algorithm? Dijkstra算法是否跟踪访问的节点以找到最短路径? - Does Dijkstra algorithm keep track of visited nodes to find the shortest path? 找到最短路径的算法,有障碍物 - Algorithm to find the shortest path, with obstacles 使用我访问过的节点查找到原点的最短路径的图表 - graph to find shortest path to origin using nodes I visited 如何使用 Dijkstra 算法在地铁中找到具有换乘时间的最短路径 - How to find Shortest Path in subway with transfer time using Dijkstra's Algorithm 使用 A* 查找最短路径 - Find shortest path using A* 如何实现Dijkstra算法以找到Java中的最短路径 - How to implement Dijkstra's Algorithm to find the shortest path in Java 找到所有路径(和最短路径)的迷宫算法 - maze algorithm to finding all path (and shortest path) 贪婪递归算法,用于为具有节点和边权重的完全二叉树中的所有节点寻找最短路径 - greedy and recursive algorithm for finding shortest path for all nodes in a complete binary tree with node and edge weights
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM