MATSIM
LeastCostPathTree.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * SpanningTree.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2008 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 
21 package org.matsim.utils.leastcostpathtree;
22 
23 import java.util.Comparator;
24 import java.util.Map;
25 import java.util.PriorityQueue;
26 
27 import org.matsim.api.core.v01.Id;
28 import org.matsim.api.core.v01.IdMap;
43 import org.matsim.vehicles.Vehicle;
45 
53 public class LeastCostPathTree {
54 
55  // ////////////////////////////////////////////////////////////////////
56  // member variables
57  // ////////////////////////////////////////////////////////////////////
58 
59  private Node origin1 = null;
60  private double dTime = Double.NaN;
61 
62  private final TravelTime ttFunction;
65 
67  private final Person PERSON = PopulationUtils.getFactory().createPerson(Id.create("thePerson", Person.class));
68 
69  // ////////////////////////////////////////////////////////////////////
70  // constructors
71  // ////////////////////////////////////////////////////////////////////
72 
74  this.ttFunction = tt;
75  this.tcFunction = tc;
76  }
77 
78  public void calculate(final Network network, final Node origin, final double time) {
79  this.origin1 = origin;
80  this.dTime = time;
81 
82  this.nodeData = new IdMap<>(Node.class);
83  NodeData d = new NodeData();
84  d.time = time;
85  d.cost = 0;
86  this.nodeData.put(origin.getId(), d);
87 
88  ComparatorCost comparator = new ComparatorCost(this.nodeData);
89  PriorityQueue<Node> pendingNodes = new PriorityQueue<>(500, comparator);
90  relaxNode(origin, pendingNodes);
91  while (!pendingNodes.isEmpty()) {
92  Node n = pendingNodes.poll();
93  relaxNode(n, pendingNodes);
94  }
95  }
96 
97  // ////////////////////////////////////////////////////////////////////
98  // inner classes
99  // ////////////////////////////////////////////////////////////////////
100 
101  public static class NodeData {
102  private Id<Node> prevId = null;
103  private double cost = Double.MAX_VALUE;
104  private double time = 0;
105 
106  /*package*/ void visit(final Id<Node> comingFromNodeId, final double cost1, final double time1) {
107  this.prevId = comingFromNodeId;
108  this.cost = cost1;
109  this.time = time1;
110  }
111 
112  public double getCost() {
113  return this.cost;
114  }
115 
116  public double getTime() {
117  return this.time;
118  }
119 
121  return this.prevId;
122  }
123  }
124 
125  /*package*/ static class ComparatorCost implements Comparator<Node> {
126  protected Map<Id<Node>, ? extends NodeData> nodeData;
127 
128  ComparatorCost(final IdMap<Node, ? extends NodeData> nodeData) {
129  this.nodeData = nodeData;
130  }
131 
132  @Override
133  public int compare(final Node n1, final Node n2) {
134  double c1 = getCost(n1);
135  double c2 = getCost(n2);
136  if (c1 < c2)
137  return -1;
138  if (c1 > c2)
139  return +1;
140  return n1.getId().compareTo(n2.getId());
141  }
142 
143  protected double getCost(final Node node) {
144  return this.nodeData.get(node.getId()).getCost();
145  }
146  }
147 
148  // ////////////////////////////////////////////////////////////////////
149  // get methods
150  // ////////////////////////////////////////////////////////////////////
151 
153  return this.nodeData;
154  }
155 
159  public final Node getOrigin() {
160  return this.origin1;
161  }
162 
163  public final double getDepartureTime() {
164  return this.dTime;
165  }
166 
167  // ////////////////////////////////////////////////////////////////////
168  // private methods
169  // ////////////////////////////////////////////////////////////////////
170 
171  private void relaxNode(final Node n, PriorityQueue<Node> pendingNodes) {
172  NodeData nData = nodeData.get(n.getId());
173  double currTime = nData.getTime();
174  double currCost = nData.getCost();
175  for (Link l : n.getOutLinks().values()) {
176  Node nn = l.getToNode();
177  NodeData nnData = nodeData.get(nn.getId());
178  if (nnData == null) {
179  nnData = new NodeData();
180  this.nodeData.put(nn.getId(), nnData);
181  }
182  double visitCost = currCost + tcFunction.getLinkTravelDisutility(l, currTime, PERSON, VEHICLE);
183  double visitTime = currTime + ttFunction.getLinkTravelTime(l, currTime, PERSON, VEHICLE);
184 
185 
186  if (visitCost < nnData.getCost()) {
187  pendingNodes.remove(nn);
188  nnData.visit(n.getId(), visitCost, visitTime);
189  additionalComputationsHook( l, currTime ) ;
190  pendingNodes.add(nn);
191  }
192  }
193  }
194 
195  protected void additionalComputationsHook( Link link, double currTime ) {
196  // left empty for inheritance
197  }
198 
199  // ////////////////////////////////////////////////////////////////////
200  // main method
201  // ////////////////////////////////////////////////////////////////////
202 
203  public static void main(String[] args) {
205  Network network = scenario.getNetwork();
206  new MatsimNetworkReader(scenario.getNetwork()).readFile("../../input/network.xml");
207 
208  TravelTimeCalculator ttc = new TravelTimeCalculator(network, 60, 30 * 3600, scenario.getConfig().travelTimeCalculator());
210  Node origin = network.getNodes().get(Id.create(1, Node.class));
211  st.calculate(network, origin, 8*3600);
212  IdMap<Node, NodeData> tree = st.getTree();
213  for (Map.Entry<Id<Node>, NodeData> e : tree.entrySet()) {
214  Id<Node> id = e.getKey();
215  NodeData d = e.getValue();
216  if (d.getPrevNodeId() != null) {
217  System.out.println(id + "\t" + d.getTime() + "\t" + d.getCost() + "\t" + d.getPrevNodeId());
218  } else {
219  System.out.println(id + "\t" + d.getTime() + "\t" + d.getCost() + "\t" + "0");
220  }
221  }
222  }
223 }
Map< Id< Node >, ? extends Node > getNodes()
Set< Map.Entry< Id< T >, V > > entrySet()
Definition: IdMap.java:209
double getLinkTravelTime(Link link, double time, Person person, Vehicle vehicle)
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
TravelTimeCalculatorConfigGroup travelTimeCalculator()
Definition: Config.java:431
double getLinkTravelDisutility(final Link link, final double time, final Person person, final Vehicle vehicle)
LeastCostPathTree(TravelTime tt, TravelDisutility tc)
static VehicleType createDefaultVehicleType()
void relaxNode(final Node n, PriorityQueue< Node > pendingNodes)
Vehicle createVehicle(Id< Vehicle > id, VehicleType type)
void additionalComputationsHook(Link link, double currTime)
V put(Id< T > key, V value)
Definition: IdMap.java:141
Map< Id< Link >, ? extends Link > getOutLinks()
static Scenario createScenario(final Config config)
void calculate(final Network network, final Node origin, final double time)
static VehiclesFactory getFactory()
static Config createConfig(final String context)