@Override public List<Path> calcPaths(int from, int to) { List<AlternativeInfo> alts = calcAlternatives(from, to); List<Path> paths = new ArrayList<>(alts.size()); for (AlternativeInfo a : alts) { paths.add(a.getPath()); } return paths; }
void checkAlternatives(List<AlternativeRoute.AlternativeInfo> alternativeInfos) { assertFalse("alternativeInfos should contain alternatives", alternativeInfos.isEmpty()); AlternativeRoute.AlternativeInfo bestInfo = alternativeInfos.get(0); for (int i = 1; i < alternativeInfos.size(); i++) { AlternativeRoute.AlternativeInfo a = alternativeInfos.get(i); if (a.getPath().getWeight() < bestInfo.getPath().getWeight()) assertTrue("alternative is not longer -> " + a + " vs " + bestInfo, false); if (a.getShareWeight() > bestInfo.getPath().getWeight() || a.getShareWeight() > a.getPath().getWeight()) assertTrue("share or sortby incorrect -> " + a + " vs " + bestInfo, false); } }
@Test public void testCalcAlternatives2() throws Exception { Weighting weighting = new FastestWeighting(carFE); Graph g = createTestGraph(true, em); AlternativeRoute altDijkstra = new AlternativeRoute(g, weighting, traversalMode); altDijkstra.setMaxPaths(3); altDijkstra.setMaxShareFactor(0.7); altDijkstra.setMinPlateauFactor(0.15); altDijkstra.setMaxWeightFactor(2); // edge based traversal requires a bit more exploration than the default of 1 altDijkstra.setMaxExplorationFactor(1.2); List<AlternativeRoute.AlternativeInfo> pathInfos = altDijkstra.calcAlternatives(5, 4); checkAlternatives(pathInfos); assertEquals(3, pathInfos.size()); // result is sorted based on the plateau to full weight ratio assertEquals(IntArrayList.from(new int[]{5, 6, 3, 4}), pathInfos.get(0).getPath().calcNodes()); assertEquals(IntArrayList.from(new int[]{5, 6, 7, 8, 4}), pathInfos.get(1).getPath().calcNodes()); assertEquals(IntArrayList.from(new int[]{5, 1, 9, 2, 3, 4}), pathInfos.get(2).getPath().calcNodes()); assertEquals(2416.0, pathInfos.get(2).getPath().getWeight(), .1); }
@Override public List<Path> calcPaths(int from, int to) { List<AlternativeInfo> alts = calcAlternatives(from, to); List<Path> paths = new ArrayList<Path>(alts.size()); for (AlternativeInfo a : alts) { paths.add(a.getPath()); } return paths; }
@Override public List<Path> calcPaths( int from, int to ) { List<AlternativeInfo> alts = calcAlternatives(from, to); List<Path> paths = new ArrayList<Path>(alts.size()); for (AlternativeInfo a : alts) { paths.add(a.getPath()); } return paths; }
@Override public List<Path> calcPaths(int from, int to) { List<AlternativeInfo> alts = calcAlternatives(from, to); List<Path> paths = new ArrayList<>(alts.size()); for (AlternativeInfo a : alts) { paths.add(a.getPath()); } return paths; }
@Test public void testCalcAlternatives() throws Exception { Weighting weighting = new FastestWeighting(carFE); GraphHopperStorage g = createTestGraph(true, em); AlternativeRoute altDijkstra = new AlternativeRoute(g, weighting, traversalMode); altDijkstra.setMaxShareFactor(0.5); altDijkstra.setMaxWeightFactor(2); List<AlternativeRoute.AlternativeInfo> pathInfos = altDijkstra.calcAlternatives(5, 4); checkAlternatives(pathInfos); assertEquals(2, pathInfos.size()); DijkstraBidirectionRef dijkstra = new DijkstraBidirectionRef(g, weighting, traversalMode); Path bestPath = dijkstra.calcPath(5, 4); Path bestAlt = pathInfos.get(0).getPath(); Path secondAlt = pathInfos.get(1).getPath(); assertEquals(bestPath.calcNodes(), bestAlt.calcNodes()); assertEquals(bestPath.getWeight(), bestAlt.getWeight(), 1e-3); assertEquals(IntArrayList.from(new int[]{5, 6, 3, 4}), bestAlt.calcNodes()); // Note: here plateau is longer, even longer than optimum, but path is longer // so which alternative is better? longer plateau.weight with bigger path.weight or smaller path.weight with smaller plateau.weight // assertEquals(Helper.createTList(5, 1, 9, 2, 3, 4), secondAlt.calcNodes()); assertEquals(IntArrayList.from(new int[]{5, 6, 7, 8, 4}), secondAlt.calcNodes()); assertEquals(1667.9, secondAlt.getWeight(), .1); }