Skip to content

Commit 59afcf7

Browse files
authored
Merge pull request opentripplanner#7206 from entur/baselineDuration_of_carpool_trip_fix
Baseline duration of carpool trip fix
2 parents 17bb5c4 + 33001d8 commit 59afcf7

4 files changed

Lines changed: 139 additions & 86 deletions

File tree

application/src/ext/java/org/opentripplanner/ext/carpooling/routing/InsertionCandidate.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ public record InsertionCandidate(
2222
int pickupPosition,
2323
int dropoffPosition,
2424
List<GraphPath<State, Edge, Vertex>> routeSegments,
25-
Duration baselineDuration,
25+
Duration durationBetweenOriginAndDestination,
2626
Duration totalDuration
2727
) {
2828
/**
29-
* Calculates the additional duration caused by inserting this passenger.
29+
* Calculates the difference between the total duration when inserting this passenger,
30+
* and the duration when driving directly from the start to the end of the trip.
3031
*/
3132
public Duration additionalDuration() {
32-
return totalDuration.minus(baselineDuration);
33+
return totalDuration.minus(durationBetweenOriginAndDestination);
3334
}
3435

3536
/**

application/src/ext/java/org/opentripplanner/ext/carpooling/routing/InsertionEvaluator.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.opentripplanner.ext.carpooling.routing;
22

33
import static org.opentripplanner.ext.carpooling.util.GraphPathUtils.calculateCumulativeDurations;
4+
import static org.opentripplanner.ext.carpooling.util.GraphPathUtils.calculateDuration;
45

56
import java.time.Duration;
67
import java.util.ArrayList;
@@ -57,24 +58,18 @@ public InsertionEvaluator(
5758
}
5859

5960
/**
60-
* Routes all baseline segments and caches the results.
61+
* Routes all segments of routePoints.
6162
*
6263
* @return Array of routed segments, or null if any segment fails to route
6364
*/
6465
@SuppressWarnings("unchecked")
65-
private GraphPath<State, Edge, Vertex>[] routeBaselineSegments(List<WgsCoordinate> routePoints) {
66+
private GraphPath<State, Edge, Vertex>[] routeSegments(List<WgsCoordinate> routePoints) {
6667
GraphPath<State, Edge, Vertex>[] segments = new GraphPath[routePoints.size() - 1];
6768

6869
for (int i = 0; i < routePoints.size() - 1; i++) {
6970
var fromCoord = routePoints.get(i);
7071
var toCoord = routePoints.get(i + 1);
71-
GenericLocation from = GenericLocation.fromCoordinate(
72-
fromCoord.latitude(),
73-
fromCoord.longitude()
74-
);
75-
GenericLocation to = GenericLocation.fromCoordinate(toCoord.latitude(), toCoord.longitude());
76-
77-
GraphPath<State, Edge, Vertex> segment = routingFunction.route(from, to, linkingContext);
72+
GraphPath<State, Edge, Vertex> segment = routeSegment(fromCoord, toCoord);
7873
if (segment == null) {
7974
LOG.debug("Baseline routing failed for segment {} → {}", i, i + 1);
8075
return null;
@@ -86,6 +81,18 @@ private GraphPath<State, Edge, Vertex>[] routeBaselineSegments(List<WgsCoordinat
8681
return segments;
8782
}
8883

84+
private GraphPath<State, Edge, Vertex> routeSegment(WgsCoordinate from, WgsCoordinate to) {
85+
GenericLocation fromGenericLocation = GenericLocation.fromCoordinate(
86+
from.latitude(),
87+
from.longitude()
88+
);
89+
GenericLocation toGenericLocation = GenericLocation.fromCoordinate(
90+
to.latitude(),
91+
to.longitude()
92+
);
93+
return routingFunction.route(fromGenericLocation, toGenericLocation, linkingContext);
94+
}
95+
8996
/**
9097
* Evaluates pre-filtered insertion positions using A* routing.
9198
* <p>
@@ -107,17 +114,30 @@ public InsertionCandidate findBestInsertion(
107114
WgsCoordinate passengerPickup,
108115
WgsCoordinate passengerDropoff
109116
) {
110-
GraphPath<State, Edge, Vertex>[] baselineSegments = routeBaselineSegments(trip.routePoints());
117+
GraphPath<State, Edge, Vertex>[] baselineSegments = routeSegments(trip.routePoints());
111118
if (baselineSegments == null) {
112119
LOG.warn("Could not route baseline for trip {}", trip.getId());
113120
return null;
114121
}
115122

116123
Duration[] cumulativeDurations = calculateCumulativeDurations(baselineSegments);
117124

125+
GraphPath<State, Edge, Vertex> pathBetweenOriginAndDestination = routeSegment(
126+
trip.stops().getFirst().getCoordinate(),
127+
trip.stops().getLast().getCoordinate()
128+
);
129+
130+
if (pathBetweenOriginAndDestination == null) {
131+
LOG.warn("Could not create route between start and stop for trip {}", trip.getId());
132+
return null;
133+
}
134+
135+
Duration durationBetweenOriginAndDestination = calculateDuration(
136+
pathBetweenOriginAndDestination
137+
);
138+
118139
InsertionCandidate bestCandidate = null;
119140
Duration minAdditionalDuration = INITIAL_ADDITIONAL_DURATION;
120-
Duration baselineDuration = cumulativeDurations[cumulativeDurations.length - 1];
121141

122142
for (InsertionPosition position : viablePositions) {
123143
InsertionCandidate candidate = evaluateInsertion(
@@ -128,7 +148,7 @@ public InsertionCandidate findBestInsertion(
128148
passengerDropoff,
129149
baselineSegments,
130150
cumulativeDurations,
131-
baselineDuration
151+
durationBetweenOriginAndDestination
132152
);
133153

134154
if (candidate == null) {
@@ -168,7 +188,7 @@ private InsertionCandidate evaluateInsertion(
168188
WgsCoordinate passengerDropoff,
169189
GraphPath<State, Edge, Vertex>[] baselineSegments,
170190
Duration[] originalCumulativeDurations,
171-
Duration baselineDuration
191+
Duration durationBetweenOriginAndDestination
172192
) {
173193
// Build modified route segments by reusing cached baseline segments
174194
List<GraphPath<State, Edge, Vertex>> modifiedSegments = buildModifiedSegments(
@@ -217,7 +237,7 @@ private InsertionCandidate evaluateInsertion(
217237
pickupPos,
218238
dropoffPos,
219239
modifiedSegments,
220-
baselineDuration,
240+
durationBetweenOriginAndDestination,
221241
totalDuration
222242
);
223243
}

application/src/ext/java/org/opentripplanner/ext/carpooling/util/GraphPathUtils.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ public static Duration[] calculateCumulativeDurations(GraphPath<State, Edge, Ver
1616
cumulativeDurations[0] = Duration.ZERO;
1717

1818
for (int i = 0; i < segments.length; i++) {
19-
Duration segmentDuration = Duration.between(
20-
segments[i].states.getFirst().getTime(),
21-
segments[i].states.getLast().getTime()
22-
);
19+
Duration segmentDuration = calculateDuration(segments[i]);
2320
cumulativeDurations[i + 1] = cumulativeDurations[i].plus(segmentDuration);
2421
}
2522

2623
return cumulativeDurations;
2724
}
25+
26+
/**
27+
* Calculates duration for a segment
28+
*/
29+
public static Duration calculateDuration(GraphPath<State, Edge, Vertex> segment) {
30+
return Duration.between(
31+
segment.states.getFirst().getTime(),
32+
segment.states.getLast().getTime()
33+
);
34+
}
2835
}

0 commit comments

Comments
 (0)