Skip to content

Commit

Permalink
optimize search for applicable transfer rules, replacing streams with…
Browse files Browse the repository at this point in the history
… declarative loops. fixes #2078.
  • Loading branch information
mattwigway committed Aug 3, 2015
1 parent 5a60068 commit aa66dcd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
7 changes: 3 additions & 4 deletions src/main/java/org/opentripplanner/profile/RaptorWorker.java
Expand Up @@ -44,7 +44,7 @@ public class RaptorWorker {
* (i.e. many minutes look like each other), so several minutes' monte carlo draws are effectively
* pooled.
*/
public static final int MONTE_CARLO_COUNT_PER_MINUTE = 4;
public static final int MONTE_CARLO_COUNT_PER_MINUTE = 1;

/** If there are no schedules, the number of Monte Carlo draws to take */
public static final int TOTAL_MONTE_CARLO_COUNT = 99;
Expand Down Expand Up @@ -302,7 +302,6 @@ public boolean doOneRound (int[] bestTimes, int[] bestNonTransferTimes, int[] pr
//LOG.info("pattern {} {}", p, data.patternNames.get(p));
int onTrip = -1;
RaptorWorkerTimetable timetable = data.timetablesForPattern.get(p);
int[] stops = data.stopsForPattern.get(p);
int stopPositionInPattern = -1; // first increment will land this at zero

int bestFreqBoardTime = Integer.MAX_VALUE;
Expand All @@ -311,7 +310,7 @@ public boolean doOneRound (int[] bestTimes, int[] bestNonTransferTimes, int[] pr

// first look for a frequency entry
if (useFrequencies) {
for (int stopIndex : stops) {
for (int stopIndex : timetable.stopIndices) {
stopPositionInPattern += 1;

// the time at this stop if we remain on board a vehicle we had already boarded
Expand Down Expand Up @@ -375,7 +374,7 @@ public boolean doOneRound (int[] bestTimes, int[] bestNonTransferTimes, int[] pr
// perform scheduled search
stopPositionInPattern = -1;

for (int stopIndex : stops) {
for (int stopIndex : timetable.stopIndices) {
stopPositionInPattern += 1;
if (onTrip == -1) {
// We haven't boarded yet
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/org/opentripplanner/profile/RaptorWorkerData.java
Expand Up @@ -64,9 +64,6 @@ public class RaptorWorkerData implements Serializable {
/** A list of pattern indexes passing through each stop, again using Raptor indices. */
public final List<int[]> patternsForStop = new ArrayList<>();

/** An ordered list of stops indices (in the Raptor data) visited by each pattern. */
public final List<int[]> stopsForPattern = new ArrayList<>();

/** For each pattern, a 2D array of stoptimes for each trip on the pattern. */
public List<RaptorWorkerTimetable> timetablesForPattern = new ArrayList<>();

Expand Down Expand Up @@ -153,7 +150,7 @@ public RaptorWorkerData (Graph graph, TimeWindow window, ProfileRequest req, Sam

List<FrequencyEntry> frequencyEntries = graphPatterns.stream()
.filter(p -> p.getSingleFrequencyEntry() != null)
.map(p -> p.getSingleFrequencyEntry())
.flatMap(p -> p.scheduledTimetable.frequencyEntries.stream())
.collect(Collectors.toList());

for (ConvertToFrequency c : frequencies) {
Expand Down Expand Up @@ -247,7 +244,8 @@ public RaptorWorkerData (Graph graph, TimeWindow window, ProfileRequest req, Sam
}
stopIndexesForPattern.add(stopIndex);
}
stopsForPattern.add(stopIndexesForPattern.toArray());

timetable.stopIndices = stopIndexesForPattern.toArray();
}
}

Expand All @@ -259,7 +257,6 @@ public RaptorWorkerData (Graph graph, TimeWindow window, ProfileRequest req, Sam
if (timetable == null)
continue;

timetable.dataIndex = timetablesForPattern.size();
timetablesForPattern.add(timetable);

// TODO: patternForIndex, indexForPattern
Expand All @@ -274,10 +271,10 @@ public RaptorWorkerData (Graph graph, TimeWindow window, ProfileRequest req, Sam
indexForStop.put(t.index, stopIndex);
stopForIndex.add(t.index);
}
stopsForPattern.add(Arrays.asList(atp.temporaryStops).stream()

timetable.stopIndices = Arrays.asList(atp.temporaryStops).stream()
.mapToInt(t -> indexForStop.get(t.index))
.toArray());
.toArray();
}
}

Expand Down Expand Up @@ -380,8 +377,8 @@ public RaptorWorkerData (Graph graph, TimeWindow window, ProfileRequest req, Sam

// create the mapping from stops to patterns
TIntObjectMap<TIntList> patternsForStopList = new TIntObjectHashMap<>();
for (int pattern = 0; pattern < stopsForPattern.size(); pattern++) {
for (int stop : stopsForPattern.get(pattern)) {
for (int pattern = 0; pattern < timetablesForPattern.size(); pattern++) {
for (int stop : timetablesForPattern.get(pattern).stopIndices) {
if (!patternsForStopList.containsKey(stop))
patternsForStopList.put(stop, new TIntArrayList());

Expand Down
Expand Up @@ -64,6 +64,9 @@ public class RaptorWorkerTimetable implements Serializable {
/** End times for frequency trips */
private int[] endTimes;

/** Indices of stops in parent data */
public int[] stopIndices;

/** parent raptorworkerdata of this timetable */
public RaptorWorkerData raptorData;

Expand Down Expand Up @@ -117,20 +120,29 @@ public int getFrequencyDeparture (int trip, int stop, int time, int previousPatt
// this is a transfer

// stop index in Raptor data
int stopIndex = raptorData.stopsForPattern.get(dataIndex)[stop];
int stopIndex = stopIndices[stop];

// first check for specific rules
if (raptorData.transferRules.containsKey(stopIndex)) {
transferRule = raptorData.transferRules.get(stopIndex).stream().filter(
tr -> tr.matches(raptorData.timetablesForPattern.get(previousPattern),
this)).findFirst().orElse(null);
// calling containsKey can be expensive so first check if list is empty
if (!raptorData.transferRules.isEmpty() && raptorData.transferRules.containsKey(stopIndex)) {
for (TransferRule tr : raptorData.transferRules.get(stopIndex)) {
if (tr.matches(raptorData.timetablesForPattern.get(previousPattern), this)) {
transferRule = tr;
break;
}
}
}

if (transferRule == null) {
if (transferRule == null && !raptorData.baseTransferRules.isEmpty()) {
// look for global rules
transferRule = raptorData.baseTransferRules.stream().filter(
tr -> tr.matches(raptorData.timetablesForPattern.get(previousPattern),
this)).findFirst().orElse(null);
// using declarative for loop because constructing a stream and doing a filter is
// slow.
for (TransferRule tr : raptorData.baseTransferRules) {
if (tr.matches(raptorData.timetablesForPattern.get(previousPattern), this)) {
transferRule = tr;
break;
}
}
}
}

Expand Down

0 comments on commit aa66dcd

Please sign in to comment.