Search in sources :

Example 1 with AStarShortestPath

use of org.jgrapht.alg.AStarShortestPath in project underlx by underlx.

the class Route method getShortestPath.

public static GraphPath getShortestPath(Network network, List<Stop> possibleSources, List<Stop> possibleTargets, @Nullable IEdgeWeighter weighter) {
    AStarShortestPath as = new AStarShortestPath(network);
    AStarAdmissibleHeuristic heuristic = new AStarAdmissibleHeuristic<Stop>() {

        @Override
        public double getCostEstimate(Stop sourceVertex, Stop targetVertex) {
            return 0;
        }
    };
    List<GraphPath> paths = new ArrayList<>();
    // lock on class so that if this method is called concurrently the annotations won't be messed up
    synchronized (Route.class) {
        IEdgeWeighter prevWeighter = network.getEdgeWeighter();
        if (weighter != null) {
            network.setEdgeWeighter(weighter);
        }
        for (Stop pSource : possibleSources) {
            for (Stop pTarget : possibleTargets) {
                // hackish "annotations" for the connection weighter
                pSource.putMeta("is_route_source", true);
                pTarget.putMeta("is_route_target", true);
                paths.add(as.getShortestPath(pSource, pTarget, heuristic));
                pSource.putMeta("is_route_source", null);
                pTarget.putMeta("is_route_target", null);
            }
        }
        network.setEdgeWeighter(prevWeighter);
    }
    GraphPath path = null;
    for (GraphPath p : paths) {
        if (path == null || p.getWeight() < path.getWeight()) {
            path = p;
        }
    }
    return path;
}
Also used : AStarShortestPath(org.jgrapht.alg.AStarShortestPath) Stop(im.tny.segvault.subway.Stop) GraphPath(org.jgrapht.GraphPath) ArrayList(java.util.ArrayList) AStarAdmissibleHeuristic(org.jgrapht.alg.interfaces.AStarAdmissibleHeuristic) IEdgeWeighter(im.tny.segvault.subway.IEdgeWeighter)

Aggregations

IEdgeWeighter (im.tny.segvault.subway.IEdgeWeighter)1 Stop (im.tny.segvault.subway.Stop)1 ArrayList (java.util.ArrayList)1 GraphPath (org.jgrapht.GraphPath)1 AStarShortestPath (org.jgrapht.alg.AStarShortestPath)1 AStarAdmissibleHeuristic (org.jgrapht.alg.interfaces.AStarAdmissibleHeuristic)1