From 76dbc472d3e7d52f090071b84e9c96da5244122a Mon Sep 17 00:00:00 2001 From: Ricardo Ewert Date: Wed, 17 Apr 2024 14:32:07 +0200 Subject: [PATCH 1/5] add carrierFile as possible input parameter --- .../GenerateSmallScaleCommercialTrafficDemand.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java index dbcb160f0d0..7f55b39c7f2 100644 --- a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java +++ b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java @@ -118,6 +118,9 @@ private enum SmallScaleCommercialTrafficType { @CommandLine.Option(names = "--pathToCommercialFacilities", description = "Path to the commercial facilities.") private Path pathToCommercialFacilities; + @CommandLine.Option(names = "--carrierFilePath", description = "Path to the carrier file.") + private Path carrierFilePath; + @CommandLine.Option(names = "--sample", description = "Scaling factor of the small scale commercial traffic (0, 1)", required = true) private double sample; @@ -189,6 +192,8 @@ public Integer call() throws Exception { throw new Exception( "You set that existing models should included to the new model. This is only possible for a creation of the new carrier file and not by using an existing."); freightCarriersConfigGroup = ConfigUtils.addOrGetModule(config, FreightCarriersConfigGroup.class); + if (freightCarriersConfigGroup.getCarriersFile() == null) + freightCarriersConfigGroup.setCarriersFile(carrierFilePath.toString()); if (config.vehicles() != null && freightCarriersConfigGroup.getCarriersVehicleTypesFile() == null) freightCarriersConfigGroup.setCarriersVehicleTypesFile(config.vehicles().getVehiclesFile()); log.info("Load carriers from: {}", freightCarriersConfigGroup.getCarriersFile()); From dc5515c39ce2e4b2c59073bea74b158ae1f98552 Mon Sep 17 00:00:00 2001 From: Ricardo Ewert Date: Wed, 17 Apr 2024 14:32:33 +0200 Subject: [PATCH 2/5] remove assert --- .../GenerateSmallScaleCommercialTrafficDemand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java index 7f55b39c7f2..b066678a1d2 100644 --- a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java +++ b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java @@ -475,7 +475,7 @@ private Config readAndCheckConfig(Path configPath, String modelName, String samp new OutputDirectoryHierarchy(config.controller().getOutputDirectory(), config.controller().getRunId(), config.controller().getOverwriteFileSetting(), ControllerConfigGroup.CompressionType.gzip); - assert(new File(Path.of(config.controller().getOutputDirectory()).resolve("calculatedData").toString()).mkdir()); + new File(Path.of(config.controller().getOutputDirectory()).resolve("calculatedData").toString()).mkdir(); MatsimRandom.getRandom().setSeed(config.global().getRandomSeed()); rnd = MatsimRandom.getRandom(); if (config.network().getInputFile() == null) From 71f79bd762aee599077f225cf9aad2d1db0dd04f Mon Sep 17 00:00:00 2001 From: Ricardo Ewert Date: Wed, 17 Apr 2024 14:37:37 +0200 Subject: [PATCH 3/5] use ModeNetworkFilter and exactly Link finder --- ...rateSmallScaleCommercialTrafficDemand.java | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java index b066678a1d2..55a0f9cb232 100644 --- a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java +++ b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java @@ -47,6 +47,7 @@ import org.matsim.core.gbl.Gbl; import org.matsim.core.gbl.MatsimRandom; import org.matsim.core.network.NetworkUtils; +import org.matsim.core.network.algorithms.TransportModeNetworkFilter; import org.matsim.core.population.routes.NetworkRoute; import org.matsim.core.replanning.GenericPlanStrategyImpl; import org.matsim.core.replanning.selectors.ExpBetaPlanChanger; @@ -78,7 +79,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.*; -import java.util.stream.Collectors; import static org.matsim.core.controler.OutputDirectoryHierarchy.OverwriteFileSetting.overwriteExistingFiles; import static org.matsim.smallScaleCommercialTrafficGeneration.SmallScaleCommercialTrafficUtils.readDataDistribution; @@ -867,32 +867,27 @@ static Map, Link>> filterLinksForZones(Scenario scenario, I Map>> facilitiesPerZone, String shapeFileZoneNameColumn) throws URISyntaxException { Map, Link>> linksPerZone = new HashMap<>(); - List links; log.info("Filtering and assign links to zones. This take some time..."); - String networkPath; - if (scenario.getConfig().network().getInputFile().startsWith("https:")) - networkPath = scenario.getConfig().network().getInputFile(); - else - networkPath = scenario.getConfig().getContext().toURI().resolve(scenario.getConfig().network().getInputFile()).getPath(); - - Network networkToChange = NetworkUtils.readNetwork(networkPath); - NetworkUtils.runNetworkCleaner(networkToChange); + TransportModeNetworkFilter filter = new TransportModeNetworkFilter(scenario.getNetwork()); + Set modes = new HashSet<>(); + modes.add("car"); + Network filteredNetwork = NetworkUtils.createNetwork(scenario.getConfig().network()); + filter.filter(filteredNetwork, modes); CoordinateTransformation ct = indexZones.getShp().createTransformation(ProjectionUtils.getCRS(scenario.getNetwork())); - - links = networkToChange.getLinks().values().stream().filter(l -> l.getAllowedModes().contains("car")) - .collect(Collectors.toList()); + //TODO possible check if newCoord attribute is really needed (find better way) + List links = new ArrayList<>(filteredNetwork.getLinks().values()); links.forEach(l -> l.getAttributes().putAttribute("newCoord", CoordUtils.round(ct.transform(l.getCoord())))); links.forEach(l -> l.getAttributes().putAttribute("zone", indexZones.query((Coord) l.getAttributes().getAttribute("newCoord")))); - links = links.stream().filter(l -> l.getAttributes().getAttribute("zone") != null).collect(Collectors.toList()); + links = links.stream().filter(l -> l.getAttributes().getAttribute("zone") != null).toList(); links.forEach(l -> linksPerZone .computeIfAbsent((String) l.getAttributes().getAttribute("zone"), (k) -> new HashMap<>()) .put(l.getId(), l)); if (linksPerZone.size() != indexZones.size()) - findNearestLinkForZonesWithoutLinks(networkToChange, linksPerZone, indexZones, facilitiesPerZone, shapeFileZoneNameColumn); + findNearestLinkForZonesWithoutLinks(filteredNetwork, linksPerZone, indexZones, facilitiesPerZone, shapeFileZoneNameColumn); return linksPerZone; } @@ -909,7 +904,7 @@ private static void findNearestLinkForZonesWithoutLinks(Network networkToChange, if (!linksPerZone.containsKey(zoneID) && facilitiesPerZone.get(zoneID) != null) { for (List buildingList : facilitiesPerZone.get(zoneID).values()) { for (ActivityFacility building : buildingList) { - Link l = NetworkUtils.getNearestLink(networkToChange, building.getCoord()); + Link l = NetworkUtils.getNearestLinkExactly(networkToChange, building.getCoord()); assert l != null; linksPerZone .computeIfAbsent(zoneID, (k) -> new HashMap<>()) From 09f1e966f94985eca40a6fb6c729fc5aa58d97c2 Mon Sep 17 00:00:00 2001 From: Ricardo Ewert Date: Wed, 17 Apr 2024 14:37:58 +0200 Subject: [PATCH 4/5] change time format --- .../GenerateSmallScaleCommercialTrafficDemand.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java index 55a0f9cb232..00c5496cf8a 100644 --- a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java +++ b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java @@ -78,6 +78,8 @@ import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; +import java.time.LocalDate; +import java.time.LocalTime; import java.util.*; import static org.matsim.core.controler.OutputDirectoryHierarchy.OverwriteFileSetting.overwriteExistingFiles; @@ -448,7 +450,7 @@ private Config readAndCheckConfig(Path configPath, String modelName, String samp if (output == null || output.toString().isEmpty()) config.controller().setOutputDirectory(Path.of(config.controller().getOutputDirectory()).resolve(modelName) .resolve(usedSmallScaleCommercialTrafficType.toString() + "_" + sampleName + "pct" + "_" - + java.time.LocalDate.now() + "_" + java.time.LocalTime.now().toSecondOfDay() + "_" + resistanceFactor) + + LocalDate.now() + "_" + LocalTime.now().toSecondOfDay() + "_" + resistanceFactor) .toString()); else config.controller().setOutputDirectory(output.toString()); From db13a2c65eed7ff1f190054d366b989221238303 Mon Sep 17 00:00:00 2001 From: Ricardo Ewert Date: Wed, 17 Apr 2024 14:44:56 +0200 Subject: [PATCH 5/5] add reduction of unused vehicleTypes --- .../GenerateSmallScaleCommercialTrafficDemand.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java index 00c5496cf8a..e33e6248b16 100644 --- a/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java +++ b/contribs/small-scale-traffic-generation/src/main/java/org/matsim/smallScaleCommercialTrafficGeneration/GenerateSmallScaleCommercialTrafficDemand.java @@ -200,6 +200,17 @@ public Integer call() throws Exception { freightCarriersConfigGroup.setCarriersVehicleTypesFile(config.vehicles().getVehiclesFile()); log.info("Load carriers from: {}", freightCarriersConfigGroup.getCarriersFile()); CarriersUtils.loadCarriersAccordingToFreightConfig(scenario); + + // Remove vehicle types which are not used by the carriers + Map, VehicleType> readVehicleTypes = CarriersUtils.getCarrierVehicleTypes(scenario).getVehicleTypes(); + List> usedCarrierVehicleTypes = CarriersUtils.getCarriers(scenario).getCarriers().values().stream() + .flatMap(carrier -> carrier.getCarrierCapabilities().getCarrierVehicles().values().stream()) + .map(vehicle -> vehicle.getType().getId()) + .distinct() + .toList(); + + readVehicleTypes.keySet().removeIf(vehicleType -> !usedCarrierVehicleTypes.contains(vehicleType)); + if (Objects.requireNonNull(usedCreationOption) == CreationOption.useExistingCarrierFileWithoutSolution) { solveSeparatedVRPs(scenario, null); }