public TransitRouterVariableImpl(final TransitRouterConfig config, final TransitRouterNetworkTravelTimeAndDisutility ttCalculator, final TransitRouterNetworkWW routerNetwork) { this.config = config; this.transitNetwork = routerNetwork; this.ttCalculator = ttCalculator; this.dijkstra = new MultiNodeDijkstra(this.transitNetwork, this.ttCalculator, this.ttCalculator); PreProcessDijkstra preProcessDijkstra = new PreProcessDijkstra(); preProcessDijkstra.run(routerNetwork); mDijkstra = new MultiDestinationDijkstra(routerNetwork, this.ttCalculator, this.ttCalculator, preProcessDijkstra); }
public Path calcPathRoute(final Coord fromCoord, final Coord toCoord, final double departureTime, final Person person) { // find possible start stops // find possible start stops Map<Node, InitialNode> wrappedFromNodes = this.locateWrappedNearestTransitNodes(person, fromCoord, departureTime); // find possible end stops Map<Node, InitialNode> wrappedToNodes = this.locateWrappedNearestTransitNodes(person, toCoord, departureTime); // find routes between start and end stops Path path = this.dijkstra.calcLeastCostPath(wrappedFromNodes, wrappedToNodes, person); if (path == null) { return null; } double directWalkTime = CoordUtils.calcEuclideanDistance(fromCoord, toCoord) / this.config.getBeelineWalkSpeed(); double directWalkCost = directWalkTime * ( 0 - this.config.getMarginalUtilityOfTravelTimeWalk_utl_s()); double pathCost = path.travelCost + wrappedFromNodes.get(path.nodes.get(0)).initialCost + wrappedToNodes.get(path.nodes.get(path.nodes.size() - 1)).initialCost; if (directWalkCost < pathCost) { return new Path(new ArrayList<Node>(), new ArrayList<Link>(), directWalkTime, directWalkCost); } double pathTime = path.travelTime + wrappedFromNodes.get(path.nodes.get(0)).initialTime + wrappedToNodes.get(path.nodes.get(path.nodes.size() - 1)).initialTime - 2*departureTime; return new Path(path.nodes, path.links, pathTime, pathCost); }
@SuppressWarnings("unchecked") public Path calcLeastCostPath(final Map<Node, InitialNode> fromNodes, final Map<Node, InitialNode> toNodes, final Person person) { Map<Node, InitialNode> swapedToNodes = swapNodes(toNodes); TransitLeastCostPathTree tree = new TransitLeastCostPathTree(network, costFunction, timeFunction, swapNodes(fromNodes), swapedToNodes, person); return tree.getPath(swapedToNodes); }
/** * Tests that a route is found even if not all given start nodes lead to an end node */ public void testSomeStartNodesNotUseable() { Fixture f = new Fixture(); TestTimeCost tc = new TestTimeCost(); tc.setData(Id.create(1, Link.class), 2.0, 2.0); tc.setData(Id.create(2, Link.class), 1.0, 1.0); tc.setData(Id.create(3, Link.class), 3.0, 3.0); tc.setData(Id.create(4, Link.class), 2.0, 2.0); tc.setData(Id.create(5, Link.class), 1.0, 1.0); tc.setData(Id.create(6, Link.class), 3.0, 3.0); tc.setData(Id.create(7, Link.class), 4.0, 4.0); MultiNodeDijkstra dijkstra = new MultiNodeDijkstra(f.network, tc, tc); Map<Node, InitialNode> fromNodes = new HashMap<Node, InitialNode>(); fromNodes.put(f.network.getNodes().get(Id.create(2, Node.class)), new InitialNode(2.0, 2.0)); fromNodes.put(f.network.getNodes().get(Id.create(4, Node.class)), new InitialNode(3.0, 3.0)); // cannot lead to 5 or 6 Map<Node, InitialNode> toNodes = new HashMap<Node, InitialNode>(); toNodes.put(f.network.getNodes().get(Id.create(5, Node.class)), new InitialNode(1.0, 1.0)); toNodes.put(f.network.getNodes().get(Id.create(6, Node.class)), new InitialNode(3.0, 3.0)); Path p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size()); assertEquals("2", p.links.get(0).getId().toString()); assertEquals("7", p.links.get(1).getId().toString()); assertEquals("5", p.links.get(2).getId().toString()); }
public void testEndViaFaster() { Fixture f = new Fixture(); TestTimeCost tc = new TestTimeCost(); tc.setData(Id.create(1, Link.class), 2.0, 2.0); tc.setData(Id.create(2, Link.class), 1.0, 1.0); tc.setData(Id.create(3, Link.class), 3.0, 3.0); tc.setData(Id.create(4, Link.class), 2.0, 2.0); tc.setData(Id.create(5, Link.class), 1.0, 1.0); tc.setData(Id.create(6, Link.class), 3.0, 3.0); tc.setData(Id.create(7, Link.class), 4.0, 4.0); MultiNodeDijkstra dijkstra = new MultiNodeDijkstra(f.network, tc, tc); Map<Node, InitialNode> fromNodes = new HashMap<Node, InitialNode>(); fromNodes.put(f.network.getNodes().get(Id.create(2, Node.class)), new InitialNode(1.0, 1.0)); Map<Node, InitialNode> toNodes = new HashMap<Node, InitialNode>(); toNodes.put(f.network.getNodes().get(Id.create(8, Node.class)), new InitialNode(3.0, 3.0)); toNodes.put(f.network.getNodes().get(Id.create(5, Node.class)), new InitialNode(1.0, 1.0)); Path p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size()); assertEquals("2", p.links.get(0).getId().toString()); assertEquals("7", p.links.get(1).getId().toString()); assertEquals("5", p.links.get(2).getId().toString()); }
/** * Tests that a path is found if some links are in the set of start * as well as in the set of end nodes and the path only containing * of this node is the cheapest. */ public void testSameNodeInFromToSetCheapest() { Fixture f = new Fixture(); TestTimeCost tc = new TestTimeCost(); tc.setData(Id.create(1, Link.class), 2.0, 2.0); tc.setData(Id.create(2, Link.class), 1.0, 1.0); tc.setData(Id.create(3, Link.class), 3.0, 3.0); tc.setData(Id.create(4, Link.class), 2.0, 2.0); tc.setData(Id.create(5, Link.class), 1.0, 1.0); tc.setData(Id.create(6, Link.class), 3.0, 3.0); tc.setData(Id.create(7, Link.class), 4.0, 4.0); MultiNodeDijkstra dijkstra = new MultiNodeDijkstra(f.network, tc, tc); Map<Node, InitialNode> fromNodes = new HashMap<Node, InitialNode>(); fromNodes.put(f.network.getNodes().get(Id.create(2, Node.class)), new InitialNode(2.0, 2.0)); fromNodes.put(f.network.getNodes().get(Id.create(4, Node.class)), new InitialNode(1.0, 1.0)); Map<Node, InitialNode> toNodes = new HashMap<Node, InitialNode>(); toNodes.put(f.network.getNodes().get(Id.create(4, Node.class)), new InitialNode(1.0, 1.0)); toNodes.put(f.network.getNodes().get(Id.create(6, Node.class)), new InitialNode(3.0, 3.0)); Path p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(0, p.links.size()); assertEquals(1, p.nodes.size()); assertEquals("4", p.nodes.get(0).getId().toString()); }
@Override public List<Leg> calcRoute(final Facility fromFacility, final Facility toFacility, final double departureTime, final Person person) { // find possible start stops Map<Node, InitialNode> wrappedFromNodes = this.locateWrappedNearestTransitNodes(person, fromFacility.getCoord(), departureTime); // find possible end stops Map<Node, InitialNode> wrappedToNodes = this.locateWrappedNearestTransitNodes(person, toFacility.getCoord(), departureTime); // find routes between start and end stops Path p = this.dijkstra.calcLeastCostPath(wrappedFromNodes, wrappedToNodes, person); if (p == null) { return null; } double directWalkCost = CoordUtils.calcEuclideanDistance(fromFacility.getCoord(), toFacility.getCoord()) / this.config.getBeelineWalkSpeed() * ( 0 - this.config.getMarginalUtilityOfTravelTimeWalk_utl_s()); double pathCost = p.travelCost + wrappedFromNodes.get(p.nodes.get(0)).initialCost + wrappedToNodes.get(p.nodes.get(p.nodes.size() - 1)).initialCost; if (directWalkCost < pathCost) { List<Leg> legs = new ArrayList<Leg>(); Leg leg = PopulationUtils.createLeg(TransportMode.transit_walk); double walkDistance = CoordUtils.calcEuclideanDistance(fromFacility.getCoord(), toFacility.getCoord()); Route walkRoute = RouteUtils.createGenericRouteImpl(null, null); walkRoute.setDistance(walkDistance); leg.setRoute(walkRoute); leg.setTravelTime(walkDistance/this.config.getBeelineWalkSpeed()); legs.add(leg); return legs; } return convertPathToLegList( departureTime, p, fromFacility.getCoord(), toFacility.getCoord(), person ) ; }
/** * Both nodes 1 and 4 are part of the start set. Even if the path from 1 to the * target leads over node 4, it may be faster, due to the intial cost values. * Test that the route does not cut at node 4 as the first node backwards from * the start set. */ public void testStartViaFaster() { Fixture f = new Fixture(); TestTimeCost tc = new TestTimeCost(); tc.setData(Id.create(1, Link.class), 2.0, 2.0); tc.setData(Id.create(2, Link.class), 1.0, 1.0); tc.setData(Id.create(3, Link.class), 3.0, 3.0); tc.setData(Id.create(4, Link.class), 2.0, 2.0); tc.setData(Id.create(5, Link.class), 1.0, 1.0); tc.setData(Id.create(6, Link.class), 3.0, 3.0); tc.setData(Id.create(7, Link.class), 4.0, 4.0); MultiNodeDijkstra dijkstra = new MultiNodeDijkstra(f.network, tc, tc); Map<Node, InitialNode> fromNodes = new HashMap<Node, InitialNode>(); fromNodes.put(f.network.getNodes().get(Id.create(1, Node.class)), new InitialNode(1.0, 1.0)); fromNodes.put(f.network.getNodes().get(Id.create(4, Node.class)), new InitialNode(4.0, 4.0)); Map<Node, InitialNode> toNodes = new HashMap<Node, InitialNode>(); toNodes.put(f.network.getNodes().get(Id.create(5, Node.class)), new InitialNode(0.0, 0.0)); Path p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size()); assertEquals("1", p.links.get(0).getId().toString()); assertEquals("7", p.links.get(1).getId().toString()); assertEquals("5", p.links.get(2).getId().toString()); }
public void testOnlyFromToSameNode() { Fixture f = new Fixture(); TestTimeCost tc = new TestTimeCost(); tc.setData(Id.create(1, Link.class), 2.0, 2.0); tc.setData(Id.create(2, Link.class), 1.0, 1.0); tc.setData(Id.create(3, Link.class), 3.0, 3.0); tc.setData(Id.create(4, Link.class), 2.0, 2.0); tc.setData(Id.create(5, Link.class), 1.0, 1.0); tc.setData(Id.create(6, Link.class), 3.0, 3.0); tc.setData(Id.create(7, Link.class), 4.0, 4.0); MultiNodeDijkstra dijkstra = new MultiNodeDijkstra(f.network, tc, tc); Map<Node, InitialNode> fromNodes = new HashMap<Node, InitialNode>(); fromNodes.put(f.network.getNodes().get(Id.create(2, Node.class)), new InitialNode(1.0, 1.0)); Map<Node, InitialNode> toNodes = new HashMap<Node, InitialNode>(); toNodes.put(f.network.getNodes().get(Id.create(2, Node.class)), new InitialNode(3.0, 3.0)); Path p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(0, p.links.size()); assertEquals(1, p.nodes.size()); assertEquals("2", p.nodes.get(0).getId().toString()); }
/** * Tests that a route is found even if not all given end nodes are reachable */ public void testSomeEndNodesNotReachable() { Fixture f = new Fixture(); TestTimeCost tc = new TestTimeCost(); tc.setData(Id.create(1, Link.class), 2.0, 2.0); tc.setData(Id.create(2, Link.class), 1.0, 1.0); tc.setData(Id.create(3, Link.class), 3.0, 3.0); tc.setData(Id.create(4, Link.class), 2.0, 2.0); tc.setData(Id.create(5, Link.class), 1.0, 1.0); tc.setData(Id.create(6, Link.class), 3.0, 3.0); tc.setData(Id.create(7, Link.class), 4.0, 4.0); MultiNodeDijkstra dijkstra = new MultiNodeDijkstra(f.network, tc, tc); Map<Node, InitialNode> fromNodes = new HashMap<Node, InitialNode>(); fromNodes.put(f.network.getNodes().get(Id.create(2, Node.class)), new InitialNode(2.0, 2.0)); fromNodes.put(f.network.getNodes().get(Id.create(1, Node.class)), new InitialNode(3.0, 3.0)); Map<Node, InitialNode> toNodes = new HashMap<Node, InitialNode>(); toNodes.put(f.network.getNodes().get(Id.create(5, Node.class)), new InitialNode(1.0, 1.0)); toNodes.put(f.network.getNodes().get(Id.create(3, Node.class)), new InitialNode(3.0, 3.0)); // cannot be reached! Path p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size()); assertEquals("2", p.links.get(0).getId().toString()); assertEquals("7", p.links.get(1).getId().toString()); assertEquals("5", p.links.get(2).getId().toString()); }
tc.setData(Id.create(6, Link.class), 7.0, 7.0); tc.setData(Id.create(7, Link.class), 4.0, 4.0); MultiNodeDijkstra dijkstra = new MultiNodeDijkstra(f.network, tc, tc); Map<Node, InitialNode> fromNodes = new HashMap<Node, InitialNode>(); fromNodes.put(f.network.getNodes().get(Id.create(2, Node.class)), new InitialNode(0.0, 0.0)); toNodes.put(f.network.getNodes().get(Id.create(6, Node.class)), new InitialNode(1.0, 1.0)); Path p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size()); p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size()); p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size());
tc.setData(Id.create(6, Link.class), 3.0, 3.0); tc.setData(Id.create(7, Link.class), 4.0, 4.0); MultiNodeDijkstra dijkstra = new MultiNodeDijkstra(f.network, tc, tc); Map<Node, InitialNode> fromNodes = new HashMap<Node, InitialNode>(); fromNodes.put(f.network.getNodes().get(Id.create(1, Node.class)), new InitialNode(1.0, 1.0)); toNodes.put(f.network.getNodes().get(Id.create(5, Node.class)), new InitialNode(0.0, 0.0)); Path p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size()); p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size()); p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size());
public void testImpossibleRoute() { Fixture f = new Fixture(); TestTimeCost tc = new TestTimeCost(); tc.setData(Id.create(1, Link.class), 2.0, 2.0); tc.setData(Id.create(2, Link.class), 1.0, 1.0); tc.setData(Id.create(3, Link.class), 3.0, 3.0); tc.setData(Id.create(4, Link.class), 2.0, 2.0); tc.setData(Id.create(5, Link.class), 1.0, 1.0); tc.setData(Id.create(6, Link.class), 3.0, 3.0); tc.setData(Id.create(7, Link.class), 4.0, 4.0); MultiNodeDijkstra dijkstra = new MultiNodeDijkstra(f.network, tc, tc); Map<Node, InitialNode> fromNodes = new HashMap<Node, InitialNode>(); fromNodes.put(f.network.getNodes().get(Id.create(1, Node.class)), new InitialNode(1.0, 1.0)); Map<Node, InitialNode> toNodes = new HashMap<Node, InitialNode>(); toNodes.put(f.network.getNodes().get(Id.create(2, Node.class)), new InitialNode(3.0, 3.0)); Path p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNull("wow, impossible path found!", p); }
/** * Tests that a path is found if some links are in the set of start * as well as in the set of end nodes, but the path only containing * of this node is the not the cheapest. */ public void testSameNodeInFromToSetNotCheapest() { Fixture f = new Fixture(); TestTimeCost tc = new TestTimeCost(); tc.setData(Id.create(1, Link.class), 2.0, 2.0); tc.setData(Id.create(2, Link.class), 1.0, 1.0); tc.setData(Id.create(3, Link.class), 3.0, 3.0); tc.setData(Id.create(4, Link.class), 2.0, 2.0); tc.setData(Id.create(5, Link.class), 1.0, 1.0); tc.setData(Id.create(6, Link.class), 3.0, 3.0); tc.setData(Id.create(7, Link.class), 4.0, 4.0); MultiNodeDijkstra dijkstra = new MultiNodeDijkstra(f.network, tc, tc); Map<Node, InitialNode> fromNodes = new HashMap<Node, InitialNode>(); fromNodes.put(f.network.getNodes().get(Id.create(2, Node.class)), new InitialNode(2.0, 2.0)); fromNodes.put(f.network.getNodes().get(Id.create(4, Node.class)), new InitialNode(10.0, 10.0)); Map<Node, InitialNode> toNodes = new HashMap<Node, InitialNode>(); toNodes.put(f.network.getNodes().get(Id.create(4, Node.class)), new InitialNode(8.0, 8.0)); toNodes.put(f.network.getNodes().get(Id.create(6, Node.class)), new InitialNode(3.0, 3.0)); Path p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size()); assertEquals("2", p.links.get(0).getId().toString()); assertEquals("7", p.links.get(1).getId().toString()); assertEquals("6", p.links.get(2).getId().toString()); }
tc.setData(Id.create(6, Link.class), 7.0, 7.0); tc.setData(Id.create(7, Link.class), 4.0, 4.0); MultiNodeDijkstra dijkstra = new MultiNodeDijkstra(f.network, tc, tc); Map<Node, InitialNode> fromNodes = new HashMap<Node, InitialNode>(); fromNodes.put(f.network.getNodes().get(Id.create(2, Node.class)), new InitialNode(4.0, 4.0)); toNodes.put(f.network.getNodes().get(Id.create(6, Node.class)), new InitialNode(1.0, 1.0)); Path p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size()); tc.setData(Id.create(4, Link.class), 3.0, 1.0); p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size()); tc.setData(Id.create(6, Link.class), 7.0, 3.0); p = dijkstra.calcLeastCostPath(fromNodes, toNodes, null); assertNotNull("no path found!", p); assertEquals(3, p.links.size());