Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
tschlenther authored Nov 14, 2023
2 parents 51dc190 + ce0528f commit 671c1c0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 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
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void configure(Header header, Layout layout) {
viz.title = "Score";
viz.dataset = data.output("*.scorestats.csv");
viz.description = "per Iteration";
viz.x = "ITERATION";
viz.x = "iteration";
viz.columns = List.of("avg_executed", "avg_worst", "avg_best");
viz.xAxisName = "Iteration";
viz.yAxisName = "Score";
Expand All @@ -88,7 +88,7 @@ public void configure(Header header, Layout layout) {
viz.title = "Mode Share Progression";
viz.description = "per Iteration";
viz.dataset = data.output("*.modestats.csv");
viz.x = "Iteration";
viz.x = "iteration";
viz.xAxisName = "Iteration";
viz.yAxisName = "Share";
viz.width = 2d;
Expand Down

0 comments on commit 671c1c0

Please sign in to comment.