Skip to content

Commit

Permalink
Merge pull request #3651 from moia-oss/updateFreespeedCachePath
Browse files Browse the repository at this point in the history
Use URL instead of path string in matrix cache
  • Loading branch information
nkuehnel authored Jan 6, 2025
2 parents b757f83 + 0c1ca6b commit 33b2be4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.matsim.contrib.dvrp.router;

import java.io.File;
import java.net.URL;
import java.util.Collections;
import java.util.Set;

Expand Down Expand Up @@ -94,7 +95,7 @@ public void install() {
return FreeSpeedTravelTimeMatrix.createFreeSpeedMatrix(network, zoneSystem, matrixParams, globalConfigGroup.getNumberOfThreads(),
qSimConfigGroup.getTimeStepSize());
} else {
File cachePath = new File(ConfigGroup.getInputFileURL(getConfig().getContext(), matrixParams.cachePath).getPath());
URL cachePath = ConfigGroup.getInputFileURL(getConfig().getContext(), matrixParams.cachePath);
return FreeSpeedTravelTimeMatrix.createFreeSpeedMatrixFromCache(network, zoneSystem, matrixParams, globalConfigGroup.getNumberOfThreads(),
qSimConfigGroup.getTimeStepSize(), cachePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.matsim.contrib.dvrp.run;

import java.io.File;
import java.net.URL;

import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.common.zones.ZoneSystem;
Expand Down Expand Up @@ -98,7 +98,7 @@ public TravelTimeMatrix get() {
return FreeSpeedTravelTimeMatrix.createFreeSpeedMatrix(network, zoneSystem, params, numberOfThreads,
qSimConfigGroup.getTimeStepSize());
} else {
File cachePath = new File(ConfigGroup.getInputFileURL(getConfig().getContext(), params.cachePath).getPath());
URL cachePath = ConfigGroup.getInputFileURL(getConfig().getContext(), params.cachePath);
return FreeSpeedTravelTimeMatrix.createFreeSpeedMatrixFromCache(network, zoneSystem, params, numberOfThreads,
qSimConfigGroup.getTimeStepSize(), cachePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@

package org.matsim.contrib.zone.skims;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.IdSet;
import org.matsim.api.core.v01.network.Network;
Expand All @@ -43,15 +40,20 @@
import org.matsim.contrib.dvrp.trafficmonitoring.QSimFreeSpeedTravelTime;
import org.matsim.contrib.zone.skims.SparseMatrix.NodeAndTime;
import org.matsim.contrib.zone.skims.SparseMatrix.SparseRow;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.router.util.TravelTime;

import com.google.common.base.Verify;
import com.google.common.collect.Sets;
import org.matsim.core.utils.io.IOUtils;

/**
* @author Michal Maciejewski (michalm)
*/
public class FreeSpeedTravelTimeMatrix implements TravelTimeMatrix {

private final static Logger log = LogManager.getLogger(FreeSpeedTravelTimeMatrix.class);

public static FreeSpeedTravelTimeMatrix createFreeSpeedMatrix(Network dvrpNetwork, ZoneSystem zoneSystem, DvrpTravelTimeMatrixParams params, int numberOfThreads,
double qSimTimeStepSize) {
return new FreeSpeedTravelTimeMatrix(dvrpNetwork, zoneSystem, params, numberOfThreads, new QSimFreeSpeedTravelTime(qSimTimeStepSize));
Expand Down Expand Up @@ -89,24 +91,25 @@ public int getZonalTravelTime(Node fromNode, Node toNode, double departureTime)
return freeSpeedTravelTimeMatrix.get(zoneSystem.getZoneForNodeId(fromNode.getId()).orElseThrow(), zoneSystem.getZoneForNodeId(toNode.getId()).orElseThrow());
}

public static FreeSpeedTravelTimeMatrix createFreeSpeedMatrixFromCache(Network dvrpNetwork, ZoneSystem zoneSystem, DvrpTravelTimeMatrixParams params, int numberOfThreads, double qSimTimeStepSize, File cachePath) {
boolean exists = cachePath.exists();

final FreeSpeedTravelTimeMatrix matrix;
if (exists) {
matrix = new FreeSpeedTravelTimeMatrix(dvrpNetwork, zoneSystem, cachePath);
} else {
matrix = createFreeSpeedMatrix(dvrpNetwork, zoneSystem, params, numberOfThreads, qSimTimeStepSize);
matrix.write(cachePath, dvrpNetwork);
}

public static FreeSpeedTravelTimeMatrix createFreeSpeedMatrixFromCache(Network dvrpNetwork, ZoneSystem zoneSystem, DvrpTravelTimeMatrixParams params, int numberOfThreads, double qSimTimeStepSize, URL cachePath) {

FreeSpeedTravelTimeMatrix matrix;
if (cachePath != null) {
try {
return new FreeSpeedTravelTimeMatrix(dvrpNetwork, zoneSystem, cachePath);
} catch (FileNotFoundException e) {
log.warn("Freespeed matrix cache file not found, will use as output path for on-the-fly creation.");
}
}
matrix = createFreeSpeedMatrix(dvrpNetwork, zoneSystem, params, numberOfThreads, qSimTimeStepSize);
matrix.write(cachePath, dvrpNetwork);
return matrix;
}

public FreeSpeedTravelTimeMatrix(Network dvrpNetwork, ZoneSystem zoneSystem, File cachePath) {
public FreeSpeedTravelTimeMatrix(Network dvrpNetwork, ZoneSystem zoneSystem, URL cachePath) throws FileNotFoundException {
this.zoneSystem = zoneSystem;

try (DataInputStream inputStream = new DataInputStream(new FileInputStream(cachePath))) {
try (DataInputStream inputStream = new DataInputStream(IOUtils.getInputStream(cachePath))) {
// number of zones
int numberOfZones = inputStream.readInt();
Verify.verify(numberOfZones == zoneSystem.getZones().size());
Expand Down Expand Up @@ -175,13 +178,15 @@ public FreeSpeedTravelTimeMatrix(Network dvrpNetwork, ZoneSystem zoneSystem, Fil
}
}
}
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

public void write(File outputPath, Network dvrpNetwork) {
try (DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(outputPath))) {
public void write(URL outputPath, Network dvrpNetwork) {
try (DataOutputStream outputStream = new DataOutputStream(IOUtils.getOutputStream(outputPath, false))) {
// obtain fixed order of zones
List<Zone> zones = new ArrayList<>(zoneSystem.getZones().values());
outputStream.writeInt(zones.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -56,7 +58,7 @@ public FreeSpeedTravelTimeMatrixTest() {
}

@Test
void matrix() {
void matrix() throws MalformedURLException {
DvrpTravelTimeMatrixParams params = new DvrpTravelTimeMatrixParams();
params.maxNeighborDistance = 0;
ZoneSystem zoneSystem = new SquareGridZoneSystem(network, 100.);
Expand All @@ -75,7 +77,7 @@ void matrix() {
assertThat(matrix.getTravelTime(nodeC, nodeB, 0)).isEqualTo(10 + 1); // 1 s for moving over nodes

// write and read cache
File cachePath = new File(utils.getOutputDirectory(), "cache.bin");
URL cachePath = new File(utils.getOutputDirectory(), "cache.bin").toURI().toURL();
matrix.write(cachePath, network);
matrix = FreeSpeedTravelTimeMatrix.createFreeSpeedMatrixFromCache(network, zoneSystem, null, 1, 1, cachePath);

Expand All @@ -93,7 +95,7 @@ void matrix() {
}

@Test
void sparseMatrix() {
void sparseMatrix() throws MalformedURLException {
DvrpTravelTimeMatrixParams params = new DvrpTravelTimeMatrixParams();
params.maxNeighborDistance = 9999;

Expand All @@ -113,7 +115,7 @@ void sparseMatrix() {
assertThat(matrix.getTravelTime(nodeC, nodeB, 0)).isEqualTo(10 + 9 + 2); // 2 s for moving over nodes

// write and read cache
File cachePath = new File(utils.getOutputDirectory(), "cache.bin");
URL cachePath = new File(utils.getOutputDirectory(), "cache.bin").toURI().toURL();
matrix.write(cachePath, network);
matrix = FreeSpeedTravelTimeMatrix.createFreeSpeedMatrixFromCache(network, zoneSystem, null, 1, 1, cachePath);

Expand Down

0 comments on commit 33b2be4

Please sign in to comment.