Skip to content

Commit

Permalink
add scaling for congestion KPi output
Browse files Browse the repository at this point in the history
  • Loading branch information
KasiaKoz committed Mar 25, 2024
1 parent 40f8663 commit 4de1e03
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/arup/cml/abm/kpi/KpiCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface KpiCalculator {

Map<String, Double> writeAccessToMobilityServicesKpi(Path outputDirectory, ScalingFactor scalingFactor);

Table writeCongestionKpi(Path directory);
Table writeCongestionKpi(Path directory, ScalingFactor scalingFactor);

double writeMobilitySpaceUsageKpi(Path outputDirectory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void run() {
kpiCalculator.writeSpeedKpi(outputDir);
kpiCalculator.writeGHGKpi(outputDir, new LinearScale(0, 10, 8.87, 0.0));
kpiCalculator.writeAccessToMobilityServicesKpi(outputDir, new LinearScale(0, 10, 0.0, 100.0));
kpiCalculator.writeCongestionKpi(outputDir);
kpiCalculator.writeCongestionKpi(outputDir, new LinearScale(0, 10, 3.0, 1.25));
kpiCalculator.writeTravelTimeKpi(outputDir, new LinearScale(0, 10, 90.0, 10.0));
kpiCalculator.writeMobilitySpaceUsageKpi(outputDir);
MemoryObserver.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ public Table addPTAccessColumnWithinDistance(Table table, Table stops, double di
}

@Override
public Table writeCongestionKpi(Path outputDirectory) {
public Table writeCongestionKpi(Path outputDirectory, ScalingFactor scalingFactor) {
LOGGER.info("Writing Congestion KPIs to {}", outputDirectory);

// compute travel time on links
Expand Down Expand Up @@ -715,7 +715,17 @@ public Table writeCongestionKpi(Path outputDirectory) {
.summarize("delayRatio", mean)
.by("mode")
.setName("Congestion KPI");

// add scaled output column
DoubleColumn normalisedDelayedRatio = DoubleColumn.create("Normalised [Mean [delayRatio]]");
kpi.doubleColumn("Mean [delayRatio]")
.forEach(meanDelayRatio -> normalisedDelayedRatio.append(
scalingFactor.scale(meanDelayRatio)
));
kpi.addColumns(normalisedDelayedRatio);

kpi.replaceColumn(round(kpi.doubleColumn("Mean [delayRatio]"), 2));
kpi.replaceColumn(round(kpi.doubleColumn("Normalised [Mean [delayRatio]]"), 2));
this.writeTableCompressed(kpi, String.format("%s/kpi-congestion.csv", outputDirectory), compressionType);
return kpi;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.arup.cml.abm.kpi.tablesaw;

import com.arup.cml.abm.kpi.LinearScale;
import com.arup.cml.abm.kpi.ScalingFactor;
import com.arup.cml.abm.kpi.builders.KpiCalculatorBuilder;
import com.arup.cml.abm.kpi.builders.LinkLogBuilder;
import com.arup.cml.abm.kpi.builders.NetworkBuilder;
Expand All @@ -15,6 +17,10 @@
import java.nio.file.Path;

public class TestTablesawCongestionKpi {
// this scale is not the proposed KPI scale. The Value bounds where chosen so that we have a multiplicative
// `equivalentScalingFactor` to multiply the expected KPI output by
ScalingFactor linearScalingFactor = new LinearScale(0, 1, 0, 50);
double equivalentScalingFactor = 1.0 / 50.0;

@Rule
public TemporaryFolder tmpDir = new TemporaryFolder();
Expand All @@ -39,13 +45,18 @@ public void reportsAverageDelayRatio() {
.withEntry("someCar", "otherLink", (9 * 60 * 60) + 30, (9 * 60 * 60) + 30 + secondDelayRatio)
.build())
.build();
Table outputKpi = kpiCalculator.writeCongestionKpi(Path.of(tmpDir.getRoot().getAbsolutePath()));
Table outputKpi = kpiCalculator.writeCongestionKpi(
Path.of(tmpDir.getRoot().getAbsolutePath()),
linearScalingFactor
);

assertThat(outputKpi.rowCount()).isEqualTo(1).as("Congestion KPI table should include only one row/mode");
Row metrics = outputKpi.row(0);
assertThat(metrics.getString("mode")).isEqualTo("car").as("Mode should be car");
assertThat(metrics.getDouble("Mean [delayRatio]")).isEqualTo(averageDelayRatio)
.as("Mean delay should be the average of 5 and 10");
.as("Mean delay should be the average of 5 and 10.");
assertThat(metrics.getDouble("Normalised [Mean [delayRatio]]")).isEqualTo(averageDelayRatio * equivalentScalingFactor)
.as("Mean delay should be the average of 5 and 10, scaled by `equivalentScalingFactor`");
}

@Test
Expand All @@ -71,18 +82,30 @@ public void reportsDelayRatiosPerMode() {
.withEntry("someHorse", "otherLink", (9 * 60 * 60) + 25, (9 * 60 * 60) + 25 + horseDelayRatio)
.build())
.build();
Table outputKpi = kpiCalculator.writeCongestionKpi(Path.of(tmpDir.getRoot().getAbsolutePath()));
Table outputKpi = kpiCalculator.writeCongestionKpi(
Path.of(tmpDir.getRoot().getAbsolutePath()),
linearScalingFactor
);

assertThat(outputKpi.rowCount()).isEqualTo(3).as("Congestion KPI table should include three rows/modes");
Row carMetric = outputKpi.row(0);
assertThat(carMetric.getString("mode")).isEqualTo("car").as("Mode should be car");
assertThat(carMetric.getDouble("Mean [delayRatio]")).isEqualTo(carDelayRatio).as("Mean delay should be 15");
assertThat(carMetric.getDouble("Mean [delayRatio]")).isEqualTo(carDelayRatio)
.as("Mean delay should be 15");
assertThat(carMetric.getDouble("Normalised [Mean [delayRatio]]")).isEqualTo(carDelayRatio * equivalentScalingFactor)
.as("Mean delay should be 15, scaled by `equivalentScalingFactor`");
Row rocketMetric = outputKpi.row(1);
assertThat(rocketMetric.getString("mode")).isEqualTo("rocket").as("Mode should be rocket");
assertThat(rocketMetric.getDouble("Mean [delayRatio]")).isEqualTo(rocketDelayRatio).as("Mean delay should be 1");
assertThat(rocketMetric.getDouble("Mean [delayRatio]")).isEqualTo(rocketDelayRatio)
.as("Mean delay should be 1");
assertThat(rocketMetric.getDouble("Normalised [Mean [delayRatio]]")).isEqualTo(rocketDelayRatio * equivalentScalingFactor)
.as("Mean delay should be 1, scaled by `equivalentScalingFactor`");
Row horseMetric = outputKpi.row(2);
assertThat(horseMetric.getString("mode")).isEqualTo("horse").as("Mode should be horse");
assertThat(horseMetric.getDouble("Mean [delayRatio]")).isEqualTo(horseDelayRatio).as("Mean delay should be 30");
assertThat(horseMetric.getDouble("Mean [delayRatio]")).isEqualTo(horseDelayRatio)
.as("Mean delay should be 30");
assertThat(horseMetric.getDouble("Normalised [Mean [delayRatio]]")).isEqualTo(horseDelayRatio * equivalentScalingFactor)
.as("Mean delay should be 30, scaled by `equivalentScalingFactor`");
}

@Test
Expand All @@ -102,12 +125,18 @@ public void linkWithInfiniteSpeedDoesNotBreakCongestionKpi() {
.withEntry("someCar", "otherLink", (9 * 60 * 60) + 25, (9 * 60 * 60) + 30)
.build())
.build();
Table outputKpi = kpiCalculator.writeCongestionKpi(Path.of(tmpDir.getRoot().getAbsolutePath()));
Table outputKpi = kpiCalculator.writeCongestionKpi(
Path.of(tmpDir.getRoot().getAbsolutePath()),
linearScalingFactor
);

assertThat(outputKpi.rowCount()).isEqualTo(1).as("Congestion KPI table should include only one row/mode");
Row metrics = outputKpi.row(0);
assertThat(metrics.getString("mode")).isEqualTo("car").as("Mode should be car");
assertThat(metrics.getDouble("Mean [delayRatio]")).isEqualTo(5).as("Mean delay should be 5");
assertThat(metrics.getDouble("Mean [delayRatio]")).isEqualTo(5)
.as("Mean delay should be 5");
assertThat(metrics.getDouble("Normalised [Mean [delayRatio]]")).isEqualTo(5 * equivalentScalingFactor)
.as("Mean delay should be 5, scaled by `equivalentScalingFactor`");
}
}

Binary file not shown.
Binary file not shown.

0 comments on commit 4de1e03

Please sign in to comment.