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..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 @@ -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(); } @@ -107,14 +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 freespeed - double freespeed = this.link.getLength() / this.link.getFreespeed(now); - this.data[timeSlot] = encode(0, freespeed); - return freespeed; + 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 db9b07a578c..80d94c7e0db 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 { +public 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/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); 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();