Skip to content

Commit

Permalink
Store dimension sizes for better clarity across methods
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorgerhardt committed Feb 25, 2025
1 parent 885744f commit 8f5ab96
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions src/main/java/com/conveyal/r5/analyst/TemporalDensityResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
*/
public class TemporalDensityResult {

private static final int TIME_LIMIT = 120;

// Internal state fields

private final int nPointSets;
private final int nPercentiles;
private final int nThresholds;
private final PointSet[] destinationPointSets;
private final int[] dualAccessibilityThresholds;

Expand All @@ -44,20 +49,23 @@ public TemporalDensityResult(AnalysisWorkerTask task) {
);
this.destinationPointSets = task.destinationPointSets;
this.dualAccessibilityThresholds = task.dualAccessibilityThresholds;
this.opportunitiesPerMinute = new double[destinationPointSets.length][task.percentiles.length][120];
this.nPercentiles = task.percentiles.length;
this.nPointSets = this.destinationPointSets.length;
this.nThresholds = this.dualAccessibilityThresholds.length;
opportunitiesPerMinute = new double[this.nPointSets][this.nPercentiles][TIME_LIMIT];
}

public void recordOneTarget (int target, int[] travelTimePercentilesSeconds) {
// Increment histogram bin for the number of minutes of travel by the number of opportunities at the target.
for (int d = 0; d < destinationPointSets.length; d++) {
PointSet dps = destinationPointSets[d];
for (int p = 0; p < opportunitiesPerMinute.length; p++) {
if (travelTimePercentilesSeconds[p] == UNREACHED) {
for (int i = 0; i < nPointSets; i++) {
PointSet dps = destinationPointSets[i];
for (int j = 0; j < nPercentiles; j++) {
if (travelTimePercentilesSeconds[j] == UNREACHED) {
break; // If any percentile is unreached, all higher ones are also unreached.
}
int m = travelTimePercentilesSeconds[p] / 60;
if (m < 120) {
opportunitiesPerMinute[d][p][m] += dps.getOpportunityCount(target);
int minutes = travelTimePercentilesSeconds[j] / 60;
if (minutes < TIME_LIMIT) {
opportunitiesPerMinute[i][j][minutes] += dps.getOpportunityCount(target);
}
}
}
Expand All @@ -69,15 +77,13 @@ public void recordOneTarget (int target, int[] travelTimePercentilesSeconds) {
* If one of these invariants does not hold, there is something wrong with the calculations.
*/
private void checkInvariants() {
int nPointSets = opportunitiesPerMinute.length;
int nPercentiles = opportunitiesPerMinute.length > 0 ? opportunitiesPerMinute[0].length : 0;
for (int d = 0; d < nPointSets; d++) {
for (int p = 0; p < nPercentiles; p++) {
for (int m = 0; m < 120; m++) {
if (m > 0 && opportunitiesPerMinute[d][p][m] < opportunitiesPerMinute[d][p][m - 1]) {
for (int i = 0; i < nPointSets; i++) {
for (int j = 0; j < nPercentiles; j++) {
for (int m = 0; m < TIME_LIMIT; m++) {
if (m > 0 && opportunitiesPerMinute[i][j][m] < opportunitiesPerMinute[i][j][m - 1]) {
throw new AssertionError("Increasing travel time decreased accessibility.");
}
if (p > 0 && opportunitiesPerMinute[d][p][m] > opportunitiesPerMinute[d][p - 1][m]) {
if (j > 0 && opportunitiesPerMinute[i][j][m] > opportunitiesPerMinute[i][j - 1][m]) {
throw new AssertionError("Increasing percentile increased accessibility.");
}
}
Expand All @@ -94,25 +100,21 @@ private void checkInvariants() {
public int[][][] calculateDualAccessibilityGrid() {
checkInvariants();

int nPointSets = opportunitiesPerMinute.length;
int nPercentiles = opportunitiesPerMinute.length > 0 ? opportunitiesPerMinute[0].length : 0;
int nThresholds = dualAccessibilityThresholds.length;
int[][][] dualAccessibilityGrid = new int[nPointSets][nPercentiles][nThresholds];
for (int i = 0; i < nPointSets; i++) {
for (int j = 0; j < nPercentiles; j++) {
for (int k = 0; k < nThresholds; k++) {
int threshold = dualAccessibilityThresholds[k];
int minute = 0;
int minutes = 0;
double sum = 0;
while (sum < threshold && minute < 120) {
sum += opportunitiesPerMinute[i][j][minute];
minute += 1;
while (sum < threshold && minutes < TIME_LIMIT) {
sum += opportunitiesPerMinute[i][j][minutes];
minutes += 1;
}
dualAccessibilityGrid[i][j][k] = minute;
dualAccessibilityGrid[i][j][k] = minutes;
}
}
}
// TODO check invariants
return dualAccessibilityGrid;
}
}

0 comments on commit 8f5ab96

Please sign in to comment.