Skip to content

Commit

Permalink
dvrp: skip computing sparse TT matrix when not needed
Browse files Browse the repository at this point in the history
When max duration and distance are set to zero, there is no point in computing te sparce matrix,
which may be very time consuming for very large networks.
  • Loading branch information
michalmac committed Nov 12, 2023
1 parent a39662a commit 0ee1ec7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
public class FreeSpeedTravelTimeMatrix implements TravelTimeMatrix {
public static FreeSpeedTravelTimeMatrix createFreeSpeedMatrix(Network dvrpNetwork, DvrpTravelTimeMatrixParams params, int numberOfThreads,
double qSimTimeStepSize) {
double qSimTimeStepSize) {
return new FreeSpeedTravelTimeMatrix(dvrpNetwork, params, numberOfThreads, new QSimFreeSpeedTravelTime(qSimTimeStepSize));
}

Expand All @@ -48,17 +48,19 @@ public FreeSpeedTravelTimeMatrix(Network dvrpNetwork, DvrpTravelTimeMatrixParams
var routingParams = new TravelTimeMatrices.RoutingParams(dvrpNetwork, travelTime, travelDisutility, numberOfThreads);
freeSpeedTravelTimeMatrix = TravelTimeMatrices.calculateTravelTimeMatrix(routingParams, centralNodes, 0);
freeSpeedTravelTimeSparseMatrix = TravelTimeMatrices.calculateTravelTimeSparseMatrix(routingParams, params.maxNeighborDistance,
params.maxNeighborTravelTime, 0);
params.maxNeighborTravelTime, 0).orElse(null);
}

@Override
public int getTravelTime(Node fromNode, Node toNode, double departureTime) {
if (fromNode == toNode) {
return 0;
}
int time = freeSpeedTravelTimeSparseMatrix.get(fromNode, toNode);
if (time >= 0) {// value is present
return time;
if (freeSpeedTravelTimeSparseMatrix != null) {
int time = freeSpeedTravelTimeSparseMatrix.get(fromNode, toNode);
if (time >= 0) {// value is present
return time;
}
}
return freeSpeedTravelTimeMatrix.get(gridSystem.getZone(fromNode), gridSystem.getZone(toNode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.IntStream;

import org.matsim.api.core.v01.network.Network;
Expand Down Expand Up @@ -56,7 +57,7 @@ public static Matrix calculateTravelTimeMatrix(RoutingParams params, Map<Zone, N
}

private static void computeForDepartureZone(Zone fromZone, Map<Zone, Node> centralNodes, double departureTime, Matrix travelTimeMatrix,
LeastCostPathTree lcpTree) {
LeastCostPathTree lcpTree) {
Node fromNode = centralNodes.get(fromZone);
lcpTree.calculate(fromNode.getId().index(), departureTime, null, null);

Expand All @@ -65,26 +66,31 @@ private static void computeForDepartureZone(Zone fromZone, Map<Zone, Node> centr
int nodeIndex = toNode.getId().index();
OptionalTime currOptionalTime = lcpTree.getTime(nodeIndex);
double currTime = currOptionalTime.orElseThrow(() -> new RuntimeException(
"Undefined Time. Reason could be that the dvrp network is not fully connected. Please check and/or clean."));
"Undefined Time. Reason could be that the dvrp network is not fully connected. Please check and/or clean."));
double tt = currTime - departureTime;
travelTimeMatrix.set(fromZone, toZone, tt);
}
}

public static SparseMatrix calculateTravelTimeSparseMatrix(RoutingParams params, double maxDistance, double maxTravelTime, double departureTime) {
public static Optional<SparseMatrix> calculateTravelTimeSparseMatrix(RoutingParams params, double maxDistance, double maxTravelTime,
double departureTime) {
SparseMatrix travelTimeMatrix = new SparseMatrix();
if (maxDistance == 0 && maxTravelTime == 0) {
return Optional.empty();
}

var nodes = params.routingNetwork.getNodes().values();
var counter = "DVRP free-speed TT sparse matrix: node ";
Calculation<Node> calculation = (lcpTree, n) -> computeForDepartureNode(n, nodes, departureTime, travelTimeMatrix, lcpTree, maxDistance,
maxTravelTime);
maxTravelTime);
calculate(params, nodes, calculation, counter);
return travelTimeMatrix;
return Optional.of(travelTimeMatrix);
}

private static void computeForDepartureNode(Node fromNode, Collection<? extends Node> nodes, double departureTime, SparseMatrix sparseMatrix,
LeastCostPathTree lcpTree, double maxDistance, double maxTravelTime) {
LeastCostPathTree lcpTree, double maxDistance, double maxTravelTime) {
lcpTree.calculate(fromNode.getId().index(), departureTime, null, null,
(nodeIndex, arrivalTime, travelCost, distance, departTime) -> distance >= maxDistance && arrivalTime >= departTime + maxTravelTime);
(nodeIndex, arrivalTime, travelCost, distance, departTime) -> distance >= maxDistance && arrivalTime >= departTime + maxTravelTime);

List<NodeAndTime> neighborNodes = new ArrayList<>();
for (Node toNode : nodes) {
Expand All @@ -109,8 +115,8 @@ private interface Calculation<E> {

private static <E> void calculate(RoutingParams params, Collection<? extends E> elements, Calculation<E> calculation, String counterPrefix) {
var trees = IntStream.range(0, params.numberOfThreads)
.mapToObj(i -> new LeastCostPathTree(new SpeedyGraph(params.routingNetwork), params.travelTime, params.travelDisutility))
.toList();
.mapToObj(i -> new LeastCostPathTree(new SpeedyGraph(params.routingNetwork), params.travelTime, params.travelDisutility))
.toList();
var executorService = new ExecutorServiceWithResource<>(trees);
var counter = new Counter(counterPrefix, " / " + elements.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void travelTimeSparseMatrix_maxDistance() {
NetworkUtils.createAndAddLink(network, Id.createLinkId("CA"), nodeC, nodeA, 600, 15, 80, 1);

double maxDistance = 300;// B->A->C and C->A->B are pruned by the limit
var matrix = TravelTimeMatrices.calculateTravelTimeSparseMatrix(routingParams(network), maxDistance, 0, 0);
var matrix = TravelTimeMatrices.calculateTravelTimeSparseMatrix(routingParams(network), maxDistance, 0, 0).orElseThrow();

assertThat(matrix.get(nodeA, nodeA)).isEqualTo(0);
assertThat(matrix.get(nodeA, nodeB)).isEqualTo(10);
Expand Down Expand Up @@ -97,7 +97,7 @@ public void travelTimeSparseMatrix_maxTravelTime() {

// 20 s (max TT) corresponds to 300 m (max distance) in another test (see: travelTimeSparseMatrix_maxDistance())
double maxTravelTime = 20;// B->A->C and C->A->B are pruned by the limit
var matrix = TravelTimeMatrices.calculateTravelTimeSparseMatrix(routingParams(network), 0, maxTravelTime, 0);
var matrix = TravelTimeMatrices.calculateTravelTimeSparseMatrix(routingParams(network), 0, maxTravelTime, 0).orElseThrow();

assertThat(matrix.get(nodeA, nodeA)).isEqualTo(0);
assertThat(matrix.get(nodeA, nodeB)).isEqualTo(10);
Expand Down

0 comments on commit 0ee1ec7

Please sign in to comment.