From ce1b77b07ea878def5a7456881961aa4c1da74bb Mon Sep 17 00:00:00 2001 From: tschlenther Date: Fri, 1 Sep 2023 15:25:08 +0200 Subject: [PATCH] extract PRStation class --- .../run/replaceCarByDRT/DrtStopsWriter.java | 2 +- .../DrtVehicleCreatorForBanScenario.java | 12 +--- .../matsim/run/replaceCarByDRT/PRStation.java | 49 +++++++++++++ .../PrActivityEventHandler.java | 4 +- .../run/replaceCarByDRT/ReplaceCarByDRT.java | 34 +++------ .../replaceCarByDRT/ReplaceCarByDRTTest.java | 72 ++++++++++--------- 6 files changed, 102 insertions(+), 71 deletions(-) create mode 100644 src/main/java/org/matsim/run/replaceCarByDRT/PRStation.java diff --git a/src/main/java/org/matsim/run/replaceCarByDRT/DrtStopsWriter.java b/src/main/java/org/matsim/run/replaceCarByDRT/DrtStopsWriter.java index 1c34278..cb73158 100644 --- a/src/main/java/org/matsim/run/replaceCarByDRT/DrtStopsWriter.java +++ b/src/main/java/org/matsim/run/replaceCarByDRT/DrtStopsWriter.java @@ -113,7 +113,7 @@ private void writeTransitStops(String outputFile) throws IOException { //write a stop for each P+R link Set prStations = ReplaceCarByDRT.readPRStationFile(url2PRStations); for (PRStation prStation : prStations) { - writeStop(csvWriter, network.getLinks().get(prStation.linkId)); + writeStop(csvWriter, network.getLinks().get(prStation.getLinkId())); } } catch (IOException e){ e.printStackTrace(); diff --git a/src/main/java/org/matsim/run/replaceCarByDRT/DrtVehicleCreatorForBanScenario.java b/src/main/java/org/matsim/run/replaceCarByDRT/DrtVehicleCreatorForBanScenario.java index 9a9bbd0..f6c13fe 100644 --- a/src/main/java/org/matsim/run/replaceCarByDRT/DrtVehicleCreatorForBanScenario.java +++ b/src/main/java/org/matsim/run/replaceCarByDRT/DrtVehicleCreatorForBanScenario.java @@ -20,17 +20,14 @@ package org.matsim.run.replaceCarByDRT; import com.opencsv.CSVWriter; -import org.apache.commons.math3.distribution.EnumeratedDistribution; import org.apache.commons.math3.util.Pair; import org.apache.log4j.Logger; import org.locationtech.jts.geom.Point; import org.matsim.api.core.v01.Coord; import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.Scenario; -import org.matsim.api.core.v01.TransportMode; import org.matsim.api.core.v01.network.Link; import org.matsim.api.core.v01.network.Network; -import org.matsim.api.core.v01.population.Activity; import org.matsim.contrib.dvrp.fleet.DvrpVehicle; import org.matsim.contrib.dvrp.fleet.DvrpVehicleSpecification; import org.matsim.contrib.dvrp.fleet.FleetWriter; @@ -41,16 +38,11 @@ import org.matsim.core.network.NetworkUtils; import org.matsim.core.network.algorithms.TransportModeNetworkFilter; import org.matsim.core.network.io.NetworkWriter; -import org.matsim.core.population.PopulationUtils; -import org.matsim.core.population.io.PopulationReader; -import org.matsim.core.router.StageActivityTypeIdentifier; -import org.matsim.core.router.TripStructureUtils; import org.matsim.core.scenario.ScenarioUtils; import org.matsim.core.utils.geometry.CoordinateTransformation; import org.matsim.core.utils.geometry.geotools.MGC; import org.matsim.core.utils.geometry.transformations.TransformationFactory; import org.matsim.core.utils.io.IOUtils; -import org.matsim.facilities.MatsimFacilitiesReader; import org.matsim.prepare.drt.DrtVehicleCreator; import org.matsim.run.drt.BerlinShpUtils; import org.matsim.run.drt.RunDrtOpenBerlinScenario; @@ -245,9 +237,9 @@ public final void createVehiclesByRandomPointInShape(int amount, int seats, List public final void createVehiclesAtPRStation(PRStation station, int amount, int seats, List vehicles) { for (int i = 1; i <= amount; i++) { - Link link = drtNetwork.getLinks().get(station.linkId); + Link link = drtNetwork.getLinks().get(station.getLinkId()); - if(link == null) log.warn("will crash. station link id was " + station.linkId); + if(link == null) log.warn("will crash. station link id was " + station.getLinkId()); vehicles.add(ImmutableDvrpVehicleSpecification.newBuilder().id(Id.create("drt-" + station.getName() + "-" + i, DvrpVehicle.class)) .startLinkId(link.getId()) diff --git a/src/main/java/org/matsim/run/replaceCarByDRT/PRStation.java b/src/main/java/org/matsim/run/replaceCarByDRT/PRStation.java new file mode 100644 index 0000000..b96052e --- /dev/null +++ b/src/main/java/org/matsim/run/replaceCarByDRT/PRStation.java @@ -0,0 +1,49 @@ +/* *********************************************************************** * + * project: org.matsim.* + * Controler.java + * * + * *********************************************************************** * + * * + * copyright : (C) 2007 by the members listed in the COPYING, * + * LICENSE and WARRANTY file. * + * email : info at matsim dot org * + * * + * *********************************************************************** * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * See also COPYING, LICENSE and WARRANTY file * + * * + * *********************************************************************** */ + +package org.matsim.run.replaceCarByDRT; + +import org.matsim.api.core.v01.Coord; +import org.matsim.api.core.v01.Id; +import org.matsim.api.core.v01.network.Link; +class PRStation { + + private final String name; + private final Id linkId; + private final Coord coord; + + PRStation(String name, Id linkId, Coord coord) { + this.name = name; + this.linkId = linkId; + this.coord = coord; + } + + protected Coord getCoord() { + return coord; + } + + protected String getName() { + return name; + } + + protected Id getLinkId() { + return linkId; + } +} diff --git a/src/main/java/org/matsim/run/replaceCarByDRT/PrActivityEventHandler.java b/src/main/java/org/matsim/run/replaceCarByDRT/PrActivityEventHandler.java index 9685339..e9e59d9 100644 --- a/src/main/java/org/matsim/run/replaceCarByDRT/PrActivityEventHandler.java +++ b/src/main/java/org/matsim/run/replaceCarByDRT/PrActivityEventHandler.java @@ -135,7 +135,7 @@ public void handleEvent(ActivityEndEvent event) { @Nullable private PRStation getPRStationWithCoord(Coord coord){ for (PRStation prStation : activitiesPerPRStation.keySet()) { - if(prStation.coord.equals(coord)) return prStation; + if(prStation.getCoord().equals(coord)) return prStation; } return null; } @@ -209,7 +209,7 @@ private void writeAgentsPerPRStation(String outputFileName) throws IOException { MutableInt agentsPerPRStation = entry.getValue(); - writer.writeNext(new String[]{station.getName(),String.valueOf(agentsPerPRStation),String.valueOf(station.coord.getX()),String.valueOf(station.coord.getY())}); + writer.writeNext(new String[]{station.getName(),String.valueOf(agentsPerPRStation),String.valueOf(station.getCoord().getX()),String.valueOf(station.getCoord().getY())}); } writer.close(); } diff --git a/src/main/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRT.java b/src/main/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRT.java index 6f27c8f..181bb0d 100644 --- a/src/main/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRT.java +++ b/src/main/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRT.java @@ -34,6 +34,7 @@ import org.matsim.api.core.v01.network.Network; import org.matsim.api.core.v01.population.*; import org.matsim.contrib.common.util.StraightLineKnnFinder; +import org.matsim.core.gbl.Gbl; import org.matsim.core.network.algorithms.MultimodalNetworkCleaner; import org.matsim.core.population.PopulationUtils; import org.matsim.core.population.routes.NetworkRoute; @@ -56,6 +57,8 @@ class ReplaceCarByDRT { static final String TRIP_TYPE_ATTR_KEY = "tripType"; static final String PR_ACTIVITY_TYPE = "P+R"; + private static boolean hasWarnedHardcodedChainMode = false; + /** * * @param scenario @@ -247,7 +250,7 @@ static void prepareInputPlansForCarProhibitionWithPRLogic(Scenario scenario, Activity parkAndRideAct = fac.createActivityFromCoord(PR_ACTIVITY_TYPE, prStation.getCoord()); parkAndRideAct.setMaximumDuration(5 * 60); - parkAndRideAct.setLinkId(prStation.linkId); + parkAndRideAct.setLinkId(prStation.getLinkId()); newTrip = new ArrayList<>(); Leg l1 = fac.createLeg(replacingMode); @@ -290,7 +293,7 @@ static void prepareInputPlansForCarProhibitionWithPRLogic(Scenario scenario, // Activity parkAndRideAct = fac.createActivityFromLinkId(PR_ACTIVITY_TYPE, prStation); Activity parkAndRideAct = fac.createActivityFromCoord(PR_ACTIVITY_TYPE, prStation.getCoord()); parkAndRideAct.setMaximumDuration(5 * 60); - parkAndRideAct.setLinkId(prStation.linkId); + parkAndRideAct.setLinkId(prStation.getLinkId()); newTrip = new ArrayList<>(); @@ -362,7 +365,11 @@ private static Plan createPTOnlyPlan(Plan originalPlan, boolean enforceMassConse .findAny().isPresent(); if(subtourTouchesProhibitionZoneWithCarOrRide){ - log.warn("assuming (with hardcoding) that ride is considered as non-chain-based and car is chain-based"); + if(!hasWarnedHardcodedChainMode){ + log.warn("assuming (with hardcoding) that ride is considered as non-chain-based and car is chain-based"); + log.warn(Gbl.ONLYONCE); + hasWarnedHardcodedChainMode = true; + } //if car is used, change all trips to pt if(subtour.getTripsWithoutSubSubtours().stream() .filter(trip -> mainModeIdentifier.identifyMainMode(trip.getTripElements()).equals(TransportMode.car)) @@ -558,24 +565,3 @@ enum PRStationChoice{ } -//TODO extract class -class PRStation { - - private String name; - Id linkId; - Coord coord; - - PRStation(String name, Id linkId, Coord coord){ - this.name = name; - this.linkId = linkId; - this.coord = coord; - } - - public Coord getCoord() { - return coord; - } - - public String getName() { - return name; - } -} diff --git a/src/test/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRTTest.java b/src/test/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRTTest.java index b0a14ef..a0157c0 100644 --- a/src/test/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRTTest.java +++ b/src/test/java/org/matsim/run/replaceCarByDRT/ReplaceCarByDRTTest.java @@ -8,7 +8,10 @@ import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.Scenario; import org.matsim.api.core.v01.TransportMode; -import org.matsim.api.core.v01.population.*; +import org.matsim.api.core.v01.population.Activity; +import org.matsim.api.core.v01.population.Leg; +import org.matsim.api.core.v01.population.Person; +import org.matsim.api.core.v01.population.Plan; import org.matsim.core.config.ConfigUtils; import org.matsim.core.network.NetworkUtils; import org.matsim.core.population.PopulationUtils; @@ -20,7 +23,8 @@ import java.util.*; import java.util.stream.Collectors; -import static org.matsim.run.replaceCarByDRT.ReplaceCarByDRT.*; +import static org.matsim.run.replaceCarByDRT.ReplaceCarByDRT.PR_ACTIVITY_TYPE; +import static org.matsim.run.replaceCarByDRT.ReplaceCarByDRT.readPRStationFile; import static org.matsim.run.replaceCarByDRT.RunBerlinNoInnerCarTripsScenario.URL_2_PR_STATIONS; /** @@ -173,15 +177,15 @@ public void testBrandenburgerWith2PRActivities(){ Assert.assertEquals(2, prActs.size()); //nr of PR acts if(scenario.equals(SCENARIO_CLOSEST_INSIDE)) { - Assert.assertEquals(PR_STATIONS.get("Innsbrucker").linkId,prActs.get(0).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Innsbrucker").coord,prActs.get(0).getCoord()); - Assert.assertEquals(PR_STATIONS.get("Innsbrucker").linkId,prActs.get(1).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Innsbrucker").coord,prActs.get(1).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Innsbrucker").getLinkId(),prActs.get(0).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Innsbrucker").getCoord(),prActs.get(0).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Innsbrucker").getLinkId(),prActs.get(1).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Innsbrucker").getCoord(),prActs.get(1).getCoord()); } else if(scenario.equals(SCENARIO_CLOSEST_OUTSIDE)) { - Assert.assertEquals(PR_STATIONS.get("Bundesplatz").linkId,prActs.get(0).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Bundesplatz").coord,prActs.get(0).getCoord()); - Assert.assertEquals(PR_STATIONS.get("Bundesplatz").linkId,prActs.get(1).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Bundesplatz").coord,prActs.get(1).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Bundesplatz").getLinkId(),prActs.get(0).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Bundesplatz").getCoord(),prActs.get(0).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Bundesplatz").getLinkId(),prActs.get(1).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Bundesplatz").getCoord(),prActs.get(1).getCoord()); } @@ -211,14 +215,14 @@ public void testBrandenburgerWith4PRActivities(){ if(!plan.getType().equals("ptOnly")){ Assert.assertEquals(4, prActs.size()); //nr of PR acts - Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").linkId,prActs.get(0).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").coord,prActs.get(0).getCoord()); - Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").linkId,prActs.get(1).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").coord,prActs.get(1).getCoord()); - Assert.assertEquals(PR_STATIONS.get("Wedding").linkId,prActs.get(2).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Wedding").coord,prActs.get(2).getCoord()); - Assert.assertEquals(PR_STATIONS.get("Wedding").linkId,prActs.get(3).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Wedding").coord,prActs.get(3).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").getLinkId(),prActs.get(0).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").getCoord(),prActs.get(0).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").getLinkId(),prActs.get(1).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").getCoord(),prActs.get(1).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Wedding").getLinkId(),prActs.get(2).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Wedding").getCoord(),prActs.get(2).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Wedding").getLinkId(),prActs.get(3).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Wedding").getCoord(),prActs.get(3).getCoord()); } else { Assert.assertEquals(0, prActs.size()); //nr of PR acts @@ -245,14 +249,14 @@ public void testInhabitantOfProhibitionZoneWithMultipleBorderCrossings(){ if(!plan.getType().equals("ptOnly")){ Assert.assertEquals(4, prActs.size()); //nr of PR acts - Assert.assertEquals(PR_STATIONS.get("Jungfernheide").linkId,prActs.get(0).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Jungfernheide").coord,prActs.get(0).getCoord()); - Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").linkId,prActs.get(1).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").coord,prActs.get(1).getCoord()); - Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").linkId,prActs.get(2).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").coord,prActs.get(2).getCoord()); - Assert.assertEquals(PR_STATIONS.get("Jungfernheide").linkId,prActs.get(3).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Jungfernheide").coord,prActs.get(3).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Jungfernheide").getLinkId(),prActs.get(0).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Jungfernheide").getCoord(),prActs.get(0).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").getLinkId(),prActs.get(1).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").getCoord(),prActs.get(1).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").getLinkId(),prActs.get(2).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Beusselstrasse").getCoord(),prActs.get(2).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Jungfernheide").getLinkId(),prActs.get(3).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Jungfernheide").getCoord(),prActs.get(3).getCoord()); } else { Assert.assertEquals(0, prActs.size()); //nr of PR acts @@ -366,15 +370,15 @@ public void testRiderWithDifferentPRStations(){ if(scenario.equals(SCENARIO_CLOSEST_INSIDE)) { // here it´s actually same PRStation. TODO: find better agent to test? - Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").linkId,prActs.get(0).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").coord,prActs.get(0).getCoord()); - Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").linkId,prActs.get(1).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").coord,prActs.get(1).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").getLinkId(),prActs.get(0).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").getCoord(),prActs.get(0).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").getLinkId(),prActs.get(1).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").getCoord(),prActs.get(1).getCoord()); } else if(scenario.equals(SCENARIO_CLOSEST_OUTSIDE)) { - Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").linkId,prActs.get(0).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").coord,prActs.get(0).getCoord()); - Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").linkId,prActs.get(1).getLinkId()); - Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").coord,prActs.get(1).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").getLinkId(),prActs.get(0).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Westkreuz/ZOB").getCoord(),prActs.get(0).getCoord()); + Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").getLinkId(),prActs.get(1).getLinkId()); + Assert.assertEquals(PR_STATIONS.get("Gesundbrunnen").getCoord(),prActs.get(1).getCoord()); }