From d7edc53bd852dd8688d4f4e93d6840021f0514ea Mon Sep 17 00:00:00 2001 From: Chengqi Lu <43133404+luchengqi7@users.noreply.github.com> Date: Thu, 7 Apr 2022 12:30:32 +0200 Subject: [PATCH 1/3] Update Array based travel time data The travel time observer does not return the correct value for the travel time for the unused links. This difference is due to the discrete QSim time step. This fix incorporate the QSim config in the Travel Time data construction, such that the travel time for the unused link can be calculated more accurately. --- .../core/trafficmonitoring/TravelTimeDataArray.java | 13 ++++++++----- .../TravelTimeDataArrayFactory.java | 11 ++++++++++- .../trafficmonitoring/TravelTimeDataArrayTest.java | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArray.java b/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArray.java index d469ff9dac9..6d7f72a1a1a 100644 --- a/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArray.java +++ b/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArray.java @@ -58,10 +58,12 @@ class TravelTimeDataArray extends TravelTimeData { */ private final long[] data; private final Link link; + private final double timeStepSize; - TravelTimeDataArray(final Link link, final int numSlots) { + TravelTimeDataArray(final Link link, final int numSlots, double qsimTimeStepSize) { this.data = new long[numSlots]; this.link = link; + this.timeStepSize = qsimTimeStepSize; resetTravelTimes(); } @@ -111,10 +113,11 @@ public double getTravelTime(final int timeSlot, final double now) { double ttime = traveltime(val); if (ttime >= 0.0) return ttime; // negative values are invalid. - // ttime can only be <0 if it never accumulated anything, i.e. if cnt == 9, so just use freespeed - double freespeed = this.link.getLength() / this.link.getFreespeed(now); - this.data[timeSlot] = encode(0, freespeed); - return freespeed; + // ttime can only be <0 if it never accumulated anything, i.e. if cnt == 9, so just use freeSpeedTravelTime + double freeSpeedTravelTime = this.link.getLength() / this.link.getFreespeed(now); + freeSpeedTravelTime = (Math.floor(freeSpeedTravelTime / timeStepSize) + 1) * timeStepSize; + this.data[timeSlot] = encode(0, freeSpeedTravelTime); + return freeSpeedTravelTime; } /* package-private for debugging */ String cntToString(){ diff --git a/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayFactory.java b/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayFactory.java index db9b07a578c..638971aa636 100644 --- a/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayFactory.java +++ b/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayFactory.java @@ -23,20 +23,29 @@ import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.network.Link; import org.matsim.api.core.v01.network.Network; +import org.matsim.core.config.groups.QSimConfigGroup; class TravelTimeDataArrayFactory implements TravelTimeDataFactory { private final Network network; private final int numSlots; + private final double qSimTimeStepSize; public TravelTimeDataArrayFactory(final Network network, final int numSlots) { this.network = network; this.numSlots = numSlots; + this.qSimTimeStepSize = 1.0; + } + + public TravelTimeDataArrayFactory(final Network network, final int numSlots, QSimConfigGroup qSimConfigGroup) { + this.network = network; + this.numSlots = numSlots; + this.qSimTimeStepSize = qSimConfigGroup.getTimeStepSize(); } @Override public TravelTimeData createTravelTimeData(Id linkId) { - return new TravelTimeDataArray(this.network.getLinks().get(linkId), this.numSlots); + return new TravelTimeDataArray(this.network.getLinks().get(linkId), this.numSlots, qSimTimeStepSize); } } diff --git a/matsim/src/test/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayTest.java b/matsim/src/test/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayTest.java index 53a1ab360c9..4f0d6f7daec 100644 --- a/matsim/src/test/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayTest.java +++ b/matsim/src/test/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayTest.java @@ -21,7 +21,7 @@ public void test() { Node to = NetworkUtils.createNode( Id.createNodeId( "2" ) ); Link link = NetworkUtils.createLink( Id.createLinkId( "1-2" ), from, to, network, 10, 10, 10, 1 ); final int numSlots = 24; - TravelTimeDataArray abc = new TravelTimeDataArray( link, numSlots ); + TravelTimeDataArray abc = new TravelTimeDataArray( link, numSlots, 1.0 ); log.info( abc.ttToString() ); log.info( abc.cntToString() ); abc.resetTravelTimes(); From 91628212c99a89fbafb90855c37bac62476155f6 Mon Sep 17 00:00:00 2001 From: Chengqi Lu <43133404+luchengqi7@users.noreply.github.com> Date: Thu, 7 Apr 2022 12:40:35 +0200 Subject: [PATCH 2/3] Make the default constructor return the original value --- .../TravelTimeDataArray.java | 24 +++++++++++-------- .../TravelTimeDataArrayFactory.java | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArray.java b/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArray.java index 6d7f72a1a1a..54d4da17713 100644 --- a/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArray.java +++ b/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArray.java @@ -26,7 +26,7 @@ /** * Implementation of {@link TravelTimeData} that stores the data per time bin - * in simple arrays. Useful if not too many empty time bins (time bins with + * in simple arrays. Useful if not too many empty time bins (time bins with * no traffic on a link) exist, so no memory is wasted. * * @author mrieser @@ -109,15 +109,19 @@ public void addTravelTime(final int timeSlot, final double traveltime) { @Override public double getTravelTime(final int timeSlot, final double now) { - long val = this.data[timeSlot]; - double ttime = traveltime(val); - if (ttime >= 0.0) return ttime; // negative values are invalid. - - // ttime can only be <0 if it never accumulated anything, i.e. if cnt == 9, so just use freeSpeedTravelTime - double freeSpeedTravelTime = this.link.getLength() / this.link.getFreespeed(now); - freeSpeedTravelTime = (Math.floor(freeSpeedTravelTime / timeStepSize) + 1) * timeStepSize; - this.data[timeSlot] = encode(0, freeSpeedTravelTime); - return freeSpeedTravelTime; + long val = this.data[timeSlot]; + double ttime = traveltime(val); + if (ttime >= 0.0) return ttime; // negative values are invalid. + + // ttime can only be <0 if it never accumulated anything, i.e. if cnt == 9, so just use freeSpeedTravelTime + double freeSpeedTravelTime = this.link.getLength() / this.link.getFreespeed(now); + if (timeStepSize > 0) { + freeSpeedTravelTime = (Math.floor(freeSpeedTravelTime / timeStepSize) + 1) * timeStepSize; + this.data[timeSlot] = encode(0, freeSpeedTravelTime); + return freeSpeedTravelTime; + } + this.data[timeSlot] = encode(0, freeSpeedTravelTime); + return freeSpeedTravelTime; } /* package-private for debugging */ String cntToString(){ diff --git a/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayFactory.java b/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayFactory.java index 638971aa636..772d0295495 100644 --- a/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayFactory.java +++ b/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayFactory.java @@ -34,7 +34,7 @@ class TravelTimeDataArrayFactory implements TravelTimeDataFactory { public TravelTimeDataArrayFactory(final Network network, final int numSlots) { this.network = network; this.numSlots = numSlots; - this.qSimTimeStepSize = 1.0; + this.qSimTimeStepSize = -1.0; } public TravelTimeDataArrayFactory(final Network network, final int numSlots, QSimConfigGroup qSimConfigGroup) { From 432f25be4818b4d99f3437b252d3d44d0719527f Mon Sep 17 00:00:00 2001 From: Chengqi Lu <43133404+luchengqi7@users.noreply.github.com> Date: Thu, 7 Apr 2022 12:47:05 +0200 Subject: [PATCH 3/3] Make TravelTimeDataFactory public --- .../core/trafficmonitoring/TravelTimeDataArrayFactory.java | 2 +- .../matsim/core/trafficmonitoring/TravelTimeDataFactory.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayFactory.java b/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayFactory.java index 772d0295495..80d94c7e0db 100644 --- a/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayFactory.java +++ b/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataArrayFactory.java @@ -25,7 +25,7 @@ import org.matsim.api.core.v01.network.Network; import org.matsim.core.config.groups.QSimConfigGroup; -class TravelTimeDataArrayFactory implements TravelTimeDataFactory { +public class TravelTimeDataArrayFactory implements TravelTimeDataFactory { private final Network network; private final int numSlots; diff --git a/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataFactory.java b/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataFactory.java index fbe4aa0d726..4129542100f 100644 --- a/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataFactory.java +++ b/matsim/src/main/java/org/matsim/core/trafficmonitoring/TravelTimeDataFactory.java @@ -24,7 +24,7 @@ import org.matsim.api.core.v01.network.Link; import org.matsim.core.api.internal.MatsimFactory; -interface TravelTimeDataFactory extends MatsimFactory { +public interface TravelTimeDataFactory extends MatsimFactory { public TravelTimeData createTravelTimeData(final Id linkId);