MATSIM
ReconstructingUmlaufBuilder.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2010 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.pt;
21 
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.Map;
28 
29 import com.google.inject.Inject;
30 import org.apache.logging.log4j.LogManager;
31 import org.apache.logging.log4j.Logger;
32 import org.matsim.api.core.v01.Id;
33 import org.matsim.api.core.v01.Scenario;
34 import org.matsim.core.gbl.Gbl;
38 import org.matsim.vehicles.Vehicle;
40 
47 public final class ReconstructingUmlaufBuilder implements UmlaufBuilder {
48  private static final Logger log = LogManager.getLogger(ReconstructingUmlaufBuilder.class);
49 
50  private static final Comparator<UmlaufStueck> departureTimeComparator = new Comparator<UmlaufStueck>() {
51 
52  @Override
53  public int compare(UmlaufStueck o1, UmlaufStueck o2) {
54  return Double.compare(o1.getDeparture().getDepartureTime(), o2.getDeparture().getDepartureTime());
55  }
56 
57  };
58 
59  private final Collection<TransitLine> transitLines;
60  private final Vehicles vehicles;
61  private Map<Id<Umlauf>,Umlauf> umlaeufe = null;
62  private ArrayList<UmlaufStueck> umlaufStuecke;
64  private final Map<Id<Vehicle>, Id<Umlauf>> umlaufIdsByVehicleId;
65 
66  @Inject public ReconstructingUmlaufBuilder( Scenario scenario ) {
67  // (normal constructor used from TransitQSimEngine for testing :-(. kai, mar'20) yy change
68 
69  this.umlaufInterpolator = new UmlaufInterpolator(scenario.getNetwork(), scenario.getConfig().scoring());
70  this.transitLines = scenario.getTransitSchedule().getTransitLines().values();
71  this.vehicles = scenario.getTransitVehicles();
72  this.umlaufIdsByVehicleId = new HashMap<>();
73  }
74 
75  @Override
76  public Collection<Umlauf> build() {
77  umlaeufe = new HashMap<>();
81  return umlaeufe.values();
82  }
83 
88  private void createUmlaeufe(){
89  int cnt = 0;
90  for (UmlaufStueck umlaufStueck : umlaufStuecke) {
91  Id<Umlauf> umlaufId = this.getUmlaufIdForVehicleId(umlaufStueck.getDeparture().getVehicleId());
92  if (umlaufId == null) {
93  throw new RuntimeException("UmlaufId could not be found. veh=" + umlaufStueck.getDeparture().getVehicleId());
94  }
95  Umlauf umlauf = umlaeufe.get(umlaufId);
96  if (umlauf == null) {
97  throw new RuntimeException("Umlauf could not be found: " + umlaufId);
98  }
99  umlaufInterpolator.addUmlaufStueckToUmlauf(umlaufStueck, umlauf);
100  cnt++;
101  printStatus(cnt);
102  }
103  }
104 
106  return this.umlaufIdsByVehicleId.get(vehId);
107  }
108 
110  Id<Umlauf> id = Id.create(vehicle.getId().toString() + "_" + vehicle.getType().getId().toString(), Umlauf.class);
111  this.umlaufIdsByVehicleId.put(vehicle.getId(), id);
112  return id;
113  }
114 
118  private void createEmptyUmlaeufe() {
119  for (Vehicle vehicle : vehicles.getVehicles().values()) {
120  UmlaufImpl umlauf = new UmlaufImpl(this.createUmlaufIdFromVehicle(vehicle));
121  umlauf.setVehicleId(vehicle.getId());
122  umlaeufe.put(umlauf.getId(), umlauf);
123  }
124  }
125 
129  private void createUmlaufStuecke() {
130  this.umlaufStuecke = new ArrayList<>();
131  log.info("Generating UmlaufStuecke ...");
132  int cnt = 0;
133  for (TransitLine line : transitLines) {
134  for (TransitRoute route : line.getRoutes().values()) {
135  Gbl.assertNotNull(route.getRoute()); // will fail much later if this is null. kai, may'17
136  for (Departure departure : route.getDepartures().values()) {
137  UmlaufStueck umlaufStueck = new UmlaufStueck(line, route, departure);
138  umlaufStuecke.add(umlaufStueck);
139  cnt++;
140  printStatus(cnt);
141  }
142  }
143  }
144  log.info("... done generating UmlaufStuecke");
145  Collections.sort(this.umlaufStuecke, departureTimeComparator);
146  }
147 
148  private void printStatus(int cnt){
149  if ( cnt%100==0 ) {
150  System.out.print('.');
151  System.out.flush();
152  }
153  if ( cnt%10000==0 ) {
154  System.out.println();
155  System.out.flush();
156  }
157  }
158 
159 }
void addUmlaufStueckToUmlauf(UmlaufStueckI umlaufStueck, Umlauf umlauf)
final ScoringConfigGroup scoring()
Definition: Config.java:407
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
final Map< Id< Vehicle >, Id< Umlauf > > umlaufIdsByVehicleId
void setVehicleId(final Id< Vehicle > vehicleId)
Definition: UmlaufImpl.java:55
static void assertNotNull(Object obj)
Definition: Gbl.java:212
Id< Umlauf > getId()
Definition: UmlaufImpl.java:50
final Id< VehicleType > getId()
Map< Id< Vehicle >, Vehicle > getVehicles()
TransitSchedule getTransitSchedule()
Map< Id< TransitLine >, TransitLine > getTransitLines()
Id< Umlauf > getUmlaufIdForVehicleId(Id< Vehicle > vehId)
static final Comparator< UmlaufStueck > departureTimeComparator