Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Array based travel time data #1935

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Link> linkId) {
return new TravelTimeDataArray(this.network.getLinks().get(linkId), this.numSlots);
return new TravelTimeDataArray(this.network.getLinks().get(linkId), this.numSlots, qSimTimeStepSize);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Link> linkId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down