@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); }
/** * Logic that was previously located in the relaxNode(...) method. * By doing so, the FastDijkstra can overwrite relaxNode without copying the logic. */ /*package*/ void relaxNodeLogic(final Link l, final RouterPriorityQueue<Node> pendingNodes, final double currTime, final double currCost) { addToPendingNodes(l, l.getToNode(), pendingNodes, currTime, currCost); }
public TransitLeastCostPathTree(final Network network, final TransitTravelDisutility costFunction, final TravelTime timeFunction, final Map<Node, InitialNode> fromNodes, final Map<Node, InitialNode> toNodes, final Person person) { this.network = network; this.costFunction = costFunction; this.timeFunction = timeFunction; this.nodeData = new HashMap<>((int)(network.getNodes().size() * 1.1), 0.95f); //create tree this.resetNetworkVisited(); this.person = person; this.customDataManager.reset(); this.fromNodes = fromNodes; pendingNodes = (RouterPriorityQueue<Node>) createRouterPriorityQueue(); for (Map.Entry<Node, InitialNode> entry : fromNodes.entrySet()) { DijkstraNodeData data = getData(entry.getKey()); visitNode(entry.getKey(), data, pendingNodes, entry.getValue().initialTime, entry.getValue().initialCost, null); } expandNodeData(toNodes); }
public TransitLeastCostPathTree(final Network network, final TransitTravelDisutility costFunction, final TravelTime timeFunction, final Map<Node, InitialNode> fromNodes, final Person person) { this.network = network; this.costFunction = costFunction; this.timeFunction = timeFunction; this.nodeData = new HashMap<>((int)(network.getNodes().size() * 1.1), 0.95f); //create tree this.resetNetworkVisited(); this.person = person; this.customDataManager.reset(); this.fromNodes = fromNodes; pendingNodes = (RouterPriorityQueue<Node>) createRouterPriorityQueue(); for (Map.Entry<Node, InitialNode> entry : fromNodes.entrySet()) { DijkstraNodeData data = getData(entry.getKey()); visitNode(entry.getKey(), data, pendingNodes, entry.getValue().initialTime, entry.getValue().initialCost, null); } // do the real work while (pendingNodes.size() > 0) { Node outNode = pendingNodes.poll(); relaxNode(outNode, pendingNodes); } }
double travelTime = this.timeFunction.getLinkTravelTime(l, currTime, this.person, this.vehicle); double travelCost = this.costFunction.getLinkTravelDisutility(l, currTime, this.person, this.vehicle, this.customDataManager); DijkstraNodeData data = getData(n); double nCost = data.getCost(); if (!data.isVisited(getIterationId())) { visitNode(n, data, pendingNodes, currTime + travelTime, currCost + travelCost, l); this.customDataManager.storeTmpData(); return true; revisitNode(n, data, pendingNodes, currTime + travelTime, totalCost, l); this.customDataManager.storeTmpData(); return true;
tree = new TransitLeastCostPathTree(getTransitRouterNetwork(), getTravelDisutility(), getTravelTime(), tree = new TransitLeastCostPathTree(getTransitRouterNetwork(), getTravelDisutility(), getTravelTime(), transitPassengerRoute = tree.getTransitPassengerRoute(wrappedToNodes);
/** * Inserts the given Node n into the pendingNodes queue and updates its time * and cost information. * * @param n * The Node that is revisited. * @param data * The data for n. * @param pendingNodes * The nodes visited and not processed yet. * @param time * The time of the visit of n. * @param cost * The accumulated cost at the time of the visit of n. * @param outLink * The node from which we came visiting n. */ protected void visitNode(final Node n, final DijkstraNodeData data, final RouterPriorityQueue<Node> pendingNodes, final double time, final double cost, final Link outLink) { data.visit(outLink, cost, time, getIterationId()); pendingNodes.add(n, getPriority(data)); }
DijkstraNodeData r = this.nodeData.get(currentNode.getId()); if (r == null) { expandNodeData(toNodes); DijkstraNodeData data = getData(currentNode); InitialNode initData = e.getValue(); double cost = data.getCost() + initData.initialCost; Link tmpLink = getData(minCostNode).getPrevLink(); while (tmpLink != null) { links.add(0, tmpLink); nodes.add(0, tmpLink.getFromNode()); tmpLink = getData(tmpLink.getFromNode()).getPrevLink(); DijkstraNodeData startNodeData = getData(nodes.get(0)); DijkstraNodeData toNodeData = getData(minCostNode);
/** * Expands the given Node in the routing algorithm; may be overridden in * sub-classes. * * @param outNode * The Node to be expanded. * @param pendingNodes * The set of pending nodes so far. */ protected void relaxNode(final Node outNode, final RouterPriorityQueue<Node> pendingNodes) { DijkstraNodeData outData = getData(outNode); double currTime = outData.getTime(); double currCost = outData.getCost(); for (Link l : outNode.getOutLinks().values()) { relaxNodeLogic(l, pendingNodes, currTime, currCost); } }
private void expandNodeData(final Map<Node, InitialNode> toNodes) { Set<Node> endNodes = new HashSet<>(toNodes.keySet()); double minCost = Double.POSITIVE_INFINITY; // do the real work while (endNodes.size() > 0) { Node outNode = pendingNodes.poll(); if (outNode == null) { // seems we have no more nodes left, but not yet reached all endNodes... endNodes.clear(); } else { DijkstraNodeData data = getData(outNode); boolean isEndNode = endNodes.remove(outNode); if (isEndNode) { InitialNode initData = toNodes.get(outNode); double cost = data.getCost() + initData.initialCost; if (cost < minCost) { minCost = cost; } } if (data.getCost() > minCost) { endNodes.clear(); // we can't get any better now } else { relaxNode(outNode, pendingNodes); } } } }
new TransitLeastCostPathTree(f.network, tc, tc, fromNodes, null); long endTime = System.currentTimeMillis(); long elapsedTimeWithoutStopCreterion = (endTime - startTime); startTime = System.currentTimeMillis(); new TransitLeastCostPathTree(f.network, tc, tc, fromNodes, toNodes, null); endTime = System.currentTimeMillis(); long elapsedTimeWithStopCreterion = (endTime - startTime);
/** * Resets all nodes in the network as if they have not been visited yet. */ private void resetNetworkVisited() { for (Node node : this.network.getNodes().values()) { DijkstraNodeData data = getData(node); data.resetVisited(); } }
/** * Changes the position of the given Node n in the pendingNodes queue and * updates its time and cost information. * * @param n * The Node that is revisited. * @param data * The data for n. * @param pendingNodes * The nodes visited and not processed yet. * @param time * The time of the visit of n. * @param cost * The accumulated cost at the time of the visit of n. * @param outLink * The link from which we came visiting n. */ void revisitNode(final Node n, final DijkstraNodeData data, final RouterPriorityQueue<Node> pendingNodes, final double time, final double cost, final Link outLink) { pendingNodes.remove(n); data.visit(outLink, cost, time, getIterationId()); pendingNodes.add(n, getPriority(data)); }
DijkstraNodeData r = this.nodeData.get(currentNode.getId()); if (r == null) { expandNodeData(toNodes); DijkstraNodeData data = getData(currentNode); InitialNode initData = e.getValue(); double cost = data.getCost() + initData.initialCost; TransitRouterNetworkLink link = (TransitRouterNetworkLink) getData(minCostNode).getPrevLink(); TransitRouterNetworkLink downstreamLink = null; Node previousFromNode = minCostNode; TransitRouterNetworkNode toNode = link.toNode; double travelTime = getData(toNode).getTime() - getData(fromNode).getTime(); Id<TransitLine> transitLineId = null; Id<TransitRoute> routeId = null; link = (TransitRouterNetworkLink) getData(fromNode).getPrevLink(); DijkstraNodeData startNodeData = getData(previousFromNode); DijkstraNodeData toNodeData = getData(minCostNode);
/** * Try to route a connection with interchange. */ @Test public void TestValidRoutingWithInterchange() { instantiateNetworkAndTravelDisutility(); Coord fromCoord = new Coord(1050d, 1050d); Map<Node, InitialNode> wrappedFromNodes = this.locateWrappedNearestTransitNode(null, fromCoord, 28800); TransitLeastCostPathTree tree = new TransitLeastCostPathTree(network, travelDisutility, travelDisutility, wrappedFromNodes, null); Coord toCoord = new Coord(2050d, 2960d); Map<Node, InitialNode> wrappedToNodes = this.locateWrappedNearestTransitNode(null, toCoord, 28800); Path path = tree.getPath(wrappedToNodes); Assert.assertNotNull(path); double pathCost = path.travelCost; Assert.assertEquals(1.7706666666666668d, pathCost, MatsimTestUtils.EPSILON); double pathTime = path.travelTime; Assert.assertEquals(231.20000000000073d, pathTime, MatsimTestUtils.EPSILON); }
/** * Try to route a standard connection. */ @Test public void TestValidRouting() { instantiateNetworkAndTravelDisutility(); Coord fromCoord = new Coord(1050d, 1050d); Map<Node, InitialNode> wrappedFromNodes = this.locateWrappedNearestTransitNode(null, fromCoord, 28800); TransitLeastCostPathTree tree = new TransitLeastCostPathTree(network, travelDisutility, travelDisutility, wrappedFromNodes, null); Coord toCoord = new Coord(3950d, 1050d); Map<Node, InitialNode> wrappedToNodes = this.locateWrappedNearestTransitNode(null, toCoord, 28800); Path path = tree.getPath(wrappedToNodes); Assert.assertNotNull(path); double pathCost = path.travelCost; Assert.assertEquals(1.8d, pathCost, MatsimTestUtils.EPSILON); double pathTime = path.travelTime; Assert.assertEquals(540d, pathTime, MatsimTestUtils.EPSILON); }