MATSIM
MatsimClassDijkstra.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * RunEmissionToolOffline.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 package tutorial.programming.example21tutorialTUBclass.leastCostPath;
21 
22 import java.util.ArrayList;
23 import java.util.Comparator;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.PriorityQueue;
28 
29 import org.matsim.api.core.v01.Id;
38 import org.matsim.vehicles.Vehicle;
39 
46 public final class MatsimClassDijkstra implements LeastCostPathCalculator {
47 
48  private final Network network;
49  private Map<Id<Node>,Double> costToNode = new HashMap<Id<Node>, Double>();
50  private Map<Id<Node>,Id<Node>> previousNodes = new HashMap<Id<Node>, Id<Node>>();
51  PriorityQueue<Id<Node>> queue = new PriorityQueue<Id<Node>>(11, new Comparator<Id<Node>>() {
52 
53  @Override
54  public int compare(Id<Node> o1, Id<Node> o2) {
55  return costToNode.get(o1).compareTo(costToNode.get(o2));
56  }
57 
58  });
59  MatsimClassDijkstra(Network network, TravelDisutility travelCosts,
60  TravelTime travelTimes) {
61  this.network = network;
62 
63  }
64 
65  @Override
66  public Path calcLeastCostPath(Node fromNode, Node toNode, double starttime,
67  Person person, Vehicle vehicle) {
68 
69  initializeNetwork(fromNode.getId());
70 
71  while (!queue.isEmpty()) {
72  Id<Node> currentId = queue.poll();
73  if (currentId == toNode.getId()) return createPath(toNode.getId(),fromNode.getId());
74  Node currentNode = network.getNodes().get(currentId);
75  for (Link link: currentNode.getOutLinks().values()){
76  Node currentToNode = link.getToNode();
77  double distance = link.getLength() + this.costToNode.get(currentId);
78  if (distance < this.costToNode.get(currentToNode.getId())){
79  this.costToNode.put(currentToNode.getId(), distance);
80  update(currentToNode.getId());
81  this.previousNodes.put(currentToNode.getId(), currentId);
82  }
83  }
84  }
85 
86  return null;
87  }
88 
89  private Path createPath(Id<Node> toNodeId, Id<Node> fromNodeId) {
90  List<Node> nodes = new ArrayList<Node>();
91  List<Link> links = new ArrayList<Link>();
92  Node lastNode = network.getNodes().get(toNodeId);
93  while (!lastNode.getId().equals(fromNodeId)){
94  if (!lastNode.getId().equals(toNodeId))
95  nodes.add(0, lastNode);
96  Node newLastNode = network.getNodes().get(this.previousNodes.get(lastNode.getId()));
97  Link l = NetworkUtils.getConnectingLink(newLastNode,lastNode);
98  links.add(0, l);
99  lastNode = newLastNode;
100  }
101 
102 
103  return new Path(nodes,links,0.0,0.0);
104  }
105 
106  private void initializeNetwork(Id<Node> startNode) {
107  for (Node node : network.getNodes().values()){
108  this.costToNode.put(node.getId(), Double.POSITIVE_INFINITY);
109  this.previousNodes.put(node.getId(), null);
110  }
111  this.costToNode.put(startNode, 0.0);
112  this.queue.add(startNode);
113 
114  }
115  private void update(Id<Node> nodeToUpdate){
116  this.queue.remove(nodeToUpdate);
117  this.queue.add(nodeToUpdate);
118  }
119 
120 }
static Link getConnectingLink(final Node fromNode, final Node toNode)
Map< Id< Node >,?extends Node > getNodes()
Path calcLeastCostPath(Node fromNode, Node toNode, double starttime, Person person, Vehicle vehicle)
Map< Id< Link >,?extends Link > getOutLinks()