Skip to content

Commit

Permalink
Add an invariants check
Browse files Browse the repository at this point in the history
As requested in #935 (comment)
  • Loading branch information
trevorgerhardt committed Feb 17, 2025
1 parent f466d00 commit eacbe29
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/com/conveyal/r5/analyst/TemporalDensityResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,37 @@ public void recordOneTarget (int target, int[] travelTimePercentilesSeconds) {
}
}

/**
* As travel time cutoff increases, accessibility should increase.
* As percentile increases, travel time should decrease, and accessibility should decrease.
* 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]) {
throw new AssertionError("Increasing travel time decreased accessibility.");
}
if (p > 0 && opportunitiesPerMinute[d][p][m] > opportunitiesPerMinute[d][p - 1][m]) {
throw new AssertionError("Increasing percentile increased accessibility.");
}
}
}
}
}

/**
* Writes dual accessibility values (in minutes) to our standard access grid format. The value returned (for
* an origin) is the number of minutes required to reach a threshold number of opportunities (specified by
* task.dualAccessibilityThresholds) in the specified destination layer at a given percentile of
* travel time. If the threshold cannot be reached in less than 120 minutes, returns 0.
*/
public int[][][] calculateDualAccessibilityGrid() {
checkInvariants();

int nPointSets = opportunitiesPerMinute.length;
int nPercentiles = opportunitiesPerMinute.length > 0 ? opportunitiesPerMinute[0].length : 0;
int nThresholds = dualAccessibilityThresholds.length;
Expand All @@ -88,6 +112,7 @@ public int[][][] calculateDualAccessibilityGrid() {
}
}
}
// TODO check invariants
return dualAccessibilityGrid;
}
}

0 comments on commit eacbe29

Please sign in to comment.