Skip to content

Commit

Permalink
Merge pull request #3007 from matsim-org/feature/improve-transfer-times
Browse files Browse the repository at this point in the history
Feature/improve transfer times
  • Loading branch information
jfbischoff authored Dec 21, 2023
2 parents dbb186e + 23d96b3 commit 360db13
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
class MinimalTransferTimesImpl implements MinimalTransferTimes {

private Map<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>> minimalTransferTimes = new ConcurrentHashMap<>();
private final Map<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>> minimalTransferTimes = new ConcurrentHashMap<>();

@Override
public double set(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toStop, double seconds) {
Expand All @@ -59,15 +59,21 @@ public double get(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toSt
public double get(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toStop, double defaultSeconds) {
Map<Id<TransitStopFacility>, Double> innerMap = this.minimalTransferTimes.get(fromStop);
if (innerMap == null) {
return defaultSeconds;
return getInnerStopTransferTime(toStop,defaultSeconds);
}
Double value = innerMap.get(toStop);
if (value == null) {
return defaultSeconds;
return Math.max(getInnerStopTransferTime(toStop,defaultSeconds),getInnerStopTransferTime(fromStop,defaultSeconds));
}
return value;
}

private double getInnerStopTransferTime(Id<TransitStopFacility> stopId, double defaultSeconds){
Map<Id<TransitStopFacility>, Double> innerMap = this.minimalTransferTimes.get(stopId);
return innerMap!=null?innerMap.getOrDefault(stopId,defaultSeconds):defaultSeconds;

}

@Override
public double remove(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toStop) {
Map<Id<TransitStopFacility>, Double> innerMap = this.minimalTransferTimes.get(fromStop);
Expand All @@ -94,7 +100,7 @@ private static class MinimalTransferTimesIteratorImpl implements MinimalTransfe
private double seconds = Double.NaN;
private boolean hasElement = false;

private Iterator<Map.Entry<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>>> outerIterator;
private final Iterator<Map.Entry<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>>> outerIterator;
private Iterator<Map.Entry<Id<TransitStopFacility>, Double>> innerIterator;

MinimalTransferTimesIteratorImpl(Map<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>> values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@
*/
public class MinimalTransferTimesImplTest {

private Id<TransitStopFacility> stopId1 = Id.create(1, TransitStopFacility.class);
private Id<TransitStopFacility> stopId2 = Id.create(2, TransitStopFacility.class);
private Id<TransitStopFacility> stopId3 = Id.create(3, TransitStopFacility.class);
private Id<TransitStopFacility> stopId4 = Id.create(4, TransitStopFacility.class);
private Id<TransitStopFacility> stopId5 = Id.create(5, TransitStopFacility.class);
private final Id<TransitStopFacility> stopId1 = Id.create(1, TransitStopFacility.class);
private final Id<TransitStopFacility> stopId2 = Id.create(2, TransitStopFacility.class);
private final Id<TransitStopFacility> stopId3 = Id.create(3, TransitStopFacility.class);
private final Id<TransitStopFacility> stopId4 = Id.create(4, TransitStopFacility.class);

@Test
void testSetGet() {
Expand All @@ -55,6 +54,19 @@ void testSetGet() {
Assertions.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId4), 0.0);
}

@Test
void testGetNonSetConnection() {
MinimalTransferTimes mtt = new MinimalTransferTimesImpl();
mtt.set(this.stopId1, this.stopId1, 300.0);
mtt.set(this.stopId3, this.stopId3, 240.0);

Assertions.assertEquals(300.0, mtt.get(this.stopId1, this.stopId1), 0.0);
Assertions.assertEquals(240.0, mtt.get(this.stopId3, this.stopId3), 0.0);

Assertions.assertEquals(300.0, mtt.get(this.stopId1, this.stopId3), 0.0);

}

@Test
void testGetWithDefault() {
MinimalTransferTimes mtt = new MinimalTransferTimesImpl();
Expand Down Expand Up @@ -259,4 +271,4 @@ void testIterator_withRemove() {
iter = mtt.iterator();
Assertions.assertFalse(iter.hasNext());
}
}
}

0 comments on commit 360db13

Please sign in to comment.