20 package tutorial.programming.example21tutorialTUBclass.leastCostPath;
22 import java.util.ArrayList;
23 import java.util.Comparator;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.PriorityQueue;
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>>() {
54 public int compare(Id<Node> o1, Id<Node> o2) {
55 return costToNode.get(o1).compareTo(costToNode.get(o2));
71 while (!queue.isEmpty()) {
72 Id<Node> currentId = queue.poll();
73 if (currentId == toNode.getId())
return createPath(toNode.getId(),fromNode.getId());
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);
90 List<Node> nodes =
new ArrayList<Node>();
91 List<Link> links =
new ArrayList<Link>();
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()));
99 lastNode = newLastNode;
103 return new Path(nodes,links,0.0,0.0);
108 this.costToNode.put(node.getId(), Double.POSITIVE_INFINITY);
109 this.previousNodes.put(node.getId(), null);
111 this.costToNode.put(startNode, 0.0);
112 this.queue.add(startNode);
115 private void update(Id<Node> nodeToUpdate){
116 this.queue.remove(nodeToUpdate);
117 this.queue.add(nodeToUpdate);
static Link getConnectingLink(final Node fromNode, final Node toNode)
Map< Id< Node >,?extends Node > getNodes()
Map< Id< Node >, Id< Node > > previousNodes
Path calcLeastCostPath(Node fromNode, Node toNode, double starttime, Person person, Vehicle vehicle)
Path createPath(Id< Node > toNodeId, Id< Node > fromNodeId)
void update(Id< Node > nodeToUpdate)
void initializeNetwork(Id< Node > startNode)
Map< Id< Link >,?extends Link > getOutLinks()
Map< Id< Node >, Double > costToNode