From f4a234a0dab9650008cc5648e7af3c6c96325df0 Mon Sep 17 00:00:00 2001 From: Kai Nagel Date: Wed, 10 Jan 2024 19:34:01 +0100 Subject: [PATCH] package reorganization --- .../PC2/GeneralParkingModule.java | 50 ++++++------ .../simulation/ParkingChoiceSimulation.java | 77 ++++++++++--------- .../ParkingInfrastructureManager.java | 13 ++-- .../{example => run}/ParkingBetaExample.java | 4 +- .../ParkingCostCalculatorExample.java | 4 +- .../run/RunParkingChoiceExample.java | 21 ++--- .../run/RunParkingChoiceExampleIT.java | 3 +- .../run/RunParkingSearchScenarioIT.java | 2 +- 8 files changed, 86 insertions(+), 88 deletions(-) rename contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/{example => run}/ParkingBetaExample.java (93%) rename contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/{example => run}/ParkingCostCalculatorExample.java (93%) rename contribs/parking/src/test/java/org/matsim/contrib/parking/{ => parkingchoice}/run/RunParkingChoiceExampleIT.java (94%) rename contribs/parking/src/test/java/org/matsim/contrib/parking/{ => parkingchoice}/run/RunParkingSearchScenarioIT.java (99%) diff --git a/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/PC2/GeneralParkingModule.java b/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/PC2/GeneralParkingModule.java index 78e2d0272d4..29b225d5397 100644 --- a/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/PC2/GeneralParkingModule.java +++ b/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/PC2/GeneralParkingModule.java @@ -11,18 +11,18 @@ public class GeneralParkingModule implements StartupListener, BeforeMobsimListener { - private Controler controler; + private final Controler controler; private ParkingScore parkingScoreManager; - public final ParkingScore getParkingScoreManager() { - return parkingScoreManager; - } +// public final ParkingScore getParkingScoreManager() { +// return parkingScoreManager; +// } public final void setParkingScoreManager(ParkingScore parkingScoreManager) { this.parkingScoreManager = parkingScoreManager; } private ParkingInfrastructure parkingInfrastructureManager; - private ParkingChoiceSimulation parkingSimulation; + private ParkingChoiceSimulation parkingChoiceSimulation; public GeneralParkingModule(Controler controler){ this.controler = controler ; @@ -32,40 +32,40 @@ public GeneralParkingModule(Controler controler){ @Override public void notifyStartup(StartupEvent event) { - parkingSimulation = new ParkingChoiceSimulation(controler.getScenario(), parkingInfrastructureManager); - controler.getEvents().addHandler(parkingSimulation); + parkingChoiceSimulation = new ParkingChoiceSimulation(controler.getScenario(), parkingInfrastructureManager); + controler.getEvents().addHandler( parkingChoiceSimulation ); // controler.addControlerListener(parkingSimulation); // was not doing anything there. kai, jul'15 } - public final ParkingInfrastructure getParkingInfrastructure() { - return parkingInfrastructureManager; - } - +// public final ParkingInfrastructure getParkingInfrastructure() { +// return parkingInfrastructureManager; +// } + public final void setParkingInfrastructurManager(ParkingInfrastructure parkingInfrastructureManager) { this.parkingInfrastructureManager = parkingInfrastructureManager; } - @Deprecated - // lower level objects may keep back pointers to higher level objects if they have to, but we prefer that they do not provide them - // as a service. kai, apr'15 - public final Controler getControler() { - return controler; - } +// @Deprecated +// // lower level objects may keep back pointers to higher level objects if they have to, but we prefer that they do not provide them +// // as a service. kai, apr'15 +// public final Controler getControler() { +// return controler; +// } @Override public void notifyBeforeMobsim(BeforeMobsimEvent event) { parkingScoreManager.prepareForNewIteration(); parkingInfrastructureManager.reset(); - parkingSimulation.prepareForNewIteration(); + parkingChoiceSimulation.prepareForNewIteration(); } - protected final ParkingInfrastructure getParkingInfrastructureManager() { - return parkingInfrastructureManager; - } - - protected final ParkingChoiceSimulation getParkingSimulation() { - return parkingSimulation; - } +// protected final ParkingInfrastructure getParkingInfrastructureManager() { +// return parkingInfrastructureManager; +// } +// +// protected final ParkingChoiceSimulation getParkingSimulation() { +// return parkingSimulation; +// } } diff --git a/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/PC2/simulation/ParkingChoiceSimulation.java b/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/PC2/simulation/ParkingChoiceSimulation.java index 739f596c79e..29bc9b7c65b 100644 --- a/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/PC2/simulation/ParkingChoiceSimulation.java +++ b/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/PC2/simulation/ParkingChoiceSimulation.java @@ -48,8 +48,8 @@ public final class ParkingChoiceSimulation - private ParkingInfrastructure parkingInfrastructureManager; - private Scenario scenario; + private final ParkingInfrastructure parkingInfrastructureManager; + private final Scenario scenario; private IntegerValueHashMap> currentPlanElementIndex; private HashMap, ParkingOperationRequestAttributes> parkingOperationRequestAttributes; private DoubleValueHashMap> firstDepartureTimeOfDay; @@ -59,10 +59,6 @@ public ParkingChoiceSimulation(Scenario scenario, ParkingInfrastructure parkingI this.parkingInfrastructureManager = parkingInfrastructureManager; } - @Override - public void reset(int iteration) { - } - @Override public void handleEvent(ActivityEndEvent event) { currentPlanElementIndex.increment(event.getPersonId()); @@ -70,14 +66,18 @@ public void handleEvent(ActivityEndEvent event) { @Override public void handleEvent(PersonDepartureEvent event) { + if (event.getLegMode().equalsIgnoreCase(TransportMode.car) && !event.getPersonId().toString().contains("pt") && isNotTransitAgent(event.getPersonId())) { + // (exclude some cases (in a brittle way, i.e. based on IDs)) + if (!firstDepartureTimeOfDay.containsKey(event.getPersonId())) { firstDepartureTimeOfDay.put(event.getPersonId(), event.getTime()); - // (I think that this is to remember the wrap-around activity. - // kai, jul'15) + // (I think that this is to remember the wrap-around activity. kai, jul'15) } if (isFirstCarDepartureOfDay(event.getPersonId())) { + // (special case, think about it later) + ParkingOperationRequestAttributes parkingAttributes = new ParkingOperationRequestAttributes(); parkingAttributes.personId = event.getPersonId(); // this is a trick to get the correct departure time @@ -85,19 +85,12 @@ public void handleEvent(PersonDepartureEvent event) { parkingAttributes.parkingDurationInSeconds = GeneralLib.getIntervalDuration(0, event.getTime()); parkingInfrastructureManager.personCarDepartureEvent(parkingAttributes); } else { - ParkingOperationRequestAttributes parkingAttributes = parkingOperationRequestAttributes - .get(event.getPersonId()); - parkingAttributes.parkingDurationInSeconds = GeneralLib - .getIntervalDuration(parkingAttributes.arrivalTime, event.getTime()); + ParkingOperationRequestAttributes parkingAttributes = parkingOperationRequestAttributes.get(event.getPersonId()); + parkingAttributes.parkingDurationInSeconds = GeneralLib.getIntervalDuration(parkingAttributes.arrivalTime, event.getTime()); if (parkingAttributes.parkingDurationInSeconds == 24 * 3600) { // (yyyy no idea what this is and why. kai, jul'15) - parkingAttributes.parkingDurationInSeconds = 1; // not zero, - // because - // this - // might - // lead to - // NaN + parkingAttributes.parkingDurationInSeconds = 1; // not zero, because this might lead to NaN } PC2Parking parking = parkingInfrastructureManager.personCarDepartureEvent(parkingAttributes); @@ -111,19 +104,25 @@ public void handleEvent(PersonDepartureEvent event) { public void handleEvent(PersonArrivalEvent event) { Id personId = event.getPersonId(); if (event.getLegMode().equalsIgnoreCase(TransportMode.car) && !event.getPersonId().toString().contains("pt") && isNotTransitAgent(event.getPersonId())) { - ParkingOperationRequestAttributes parkingAttributes = new ParkingOperationRequestAttributes(); - Link link = scenario.getNetwork().getLinks().get(event.getLinkId()); - Activity nextActivity = getNextActivity(personId); + // (exclude some cases (in a brittle way, i.e. based on IDs)) - parkingAttributes.destCoordinate = link.getCoord(); - parkingAttributes.arrivalTime = event.getTime(); - parkingAttributes.personId = personId; - parkingAttributes.facilityId = nextActivity.getFacilityId(); - parkingAttributes.actType = nextActivity.getType(); + // I think that this just packages some information together into the parkingAttributes: + ParkingOperationRequestAttributes parkingAttributes = new ParkingOperationRequestAttributes(); + { + Link link = scenario.getNetwork().getLinks().get( event.getLinkId() ); + Activity nextActivity = getNextActivity( personId ); + + parkingAttributes.destCoordinate = link.getCoord(); + parkingAttributes.arrivalTime = event.getTime(); + parkingAttributes.personId = personId; + parkingAttributes.facilityId = nextActivity.getFacilityId(); + parkingAttributes.actType = nextActivity.getType(); + } if (isLastCarLegOfDay(personId)) { - parkingAttributes.parkingDurationInSeconds = GeneralLib.getIntervalDuration(event.getTime(), - firstDepartureTimeOfDay.get(personId)); + // (special case, think about it later) + + parkingAttributes.parkingDurationInSeconds = GeneralLib.getIntervalDuration(event.getTime(), firstDepartureTimeOfDay.get(personId)); } else { Activity activityBeforeNextCarLeg = getActivityBeforeNextCarLeg(personId); @@ -131,6 +130,8 @@ public void handleEvent(PersonArrivalEvent event) { double parkingDuration=0; if (endTime==Double.NEGATIVE_INFINITY || endTime==Double.POSITIVE_INFINITY){ + // (I think that this _can_ happen, especially in context of within-day replanning, if departure time is unknown. (*)) + // try to estimate parking duration Person person = scenario.getPopulation().getPersons().get(personId); @@ -148,20 +149,22 @@ public void handleEvent(PersonArrivalEvent event) { } } - parkingAttributes.parkingDurationInSeconds = GeneralLib.getIntervalDuration(event.getTime(), - endTime); + parkingAttributes.parkingDurationInSeconds = GeneralLib.getIntervalDuration(event.getTime(), endTime); + // (This is the _estimated_ parking duration, since we are at arrival. This is needed to define the "best" parking + // location ... cf. short-term/long-term parking at airports. Could rename the attributed into "expected...", but we + // have seen at other places in the code that such attributes may change their interpretation based on context so will + // not do this here.) } parkingAttributes.legIndex = currentPlanElementIndex.get(personId); PC2Parking parking = parkingInfrastructureManager.parkVehicle(parkingAttributes); - // to me this looks like first the agent arrives at his/her - // activity. And then the negative parking score is added after the - // fact, however without consuming time. I.e. there is no physics. - // kai, jul'15 + // to me this looks like first the agent arrives at his/her activity. And then the negative parking score is added after the + // fact, however without consuming time. I.e. there is no physics. kai, jul'15 if (isLastCarLegOfDay(personId)) { parkingInfrastructureManager.scoreParkingOperation(parkingAttributes, parking); + // (The last arrival of the day is scored here, since there is no corresponding departure event.) } parkingOperationRequestAttributes.put(personId, parkingAttributes); @@ -184,10 +187,8 @@ public void prepareForNewIteration() { if (PopulationUtils.hasCarLeg(person.getSelectedPlan()) && isNotTransitAgent(person.getId())) { ParkingOperationRequestAttributes parkingAttributes = new ParkingOperationRequestAttributes(); - Activity firstActivityOfDayBeforeDepartingWithCar = PopulationUtils - .getFirstActivityOfDayBeforeDepartingWithCar(person.getSelectedPlan()); - Activity firstActivityAfterLastCarLegOfDay = PopulationUtils - .getFirstActivityAfterLastCarLegOfDay(person.getSelectedPlan()); + Activity firstActivityOfDayBeforeDepartingWithCar = PopulationUtils.getFirstActivityOfDayBeforeDepartingWithCar(person.getSelectedPlan()); + Activity firstActivityAfterLastCarLegOfDay = PopulationUtils.getFirstActivityAfterLastCarLegOfDay(person.getSelectedPlan()); parkingAttributes.destCoordinate = firstActivityAfterLastCarLegOfDay.getCoord(); // parkingAttributes.arrivalTime=firstActivityAfterLastCarLegOfDay.getStartTime(); diff --git a/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/PC2/simulation/ParkingInfrastructureManager.java b/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/PC2/simulation/ParkingInfrastructureManager.java index 5a16b94015f..72d6250a771 100644 --- a/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/PC2/simulation/ParkingInfrastructureManager.java +++ b/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/PC2/simulation/ParkingInfrastructureManager.java @@ -261,13 +261,13 @@ public synchronized PC2Parking parkVehicle(ParkingOperationRequestAttributes par } // put parking that was found into a sorted queue: - PriorityQueue> queue = new PriorityQueue>(); + PriorityQueue> queue = new PriorityQueue<>(); for (PC2Parking parking : collection) { double score = parkingScoreManager.calcScore(parkingOperationRequestAttributes.destCoordinate, parkingOperationRequestAttributes.arrivalTime, parkingOperationRequestAttributes.parkingDurationInSeconds, parking, parkingOperationRequestAttributes.personId, parkingOperationRequestAttributes.legIndex, false); - queue.add(new SortableMapObject(parking, -1.0 * score)); // score made positive, so that priority queue works properly + queue.add( new SortableMapObject<>( parking, -1.0 * score ) ); // score made positive, so that priority queue works properly } // TODO: should I make MNL only on top 5 here? @@ -298,8 +298,7 @@ public synchronized PC2Parking parkVehicle(ParkingOperationRequestAttributes par // this puts the personId (!!!! yyyyyy) at the parking location: parkedVehicles.put(parkingOperationRequestAttributes.personId, selectedParking.getId()); - // this tells the parking lot to decrease the number of - // available spaces: + // this tells the parking lot to decrease the number of available spaces: parkVehicle(selectedParking); // PC2Parking closestParking = @@ -364,10 +363,8 @@ public synchronized PC2Parking parkVehicle(ParkingOperationRequestAttributes par e.printStackTrace(); } - // yyyyyy the parking arrival event returns person id, not vehicle id. - // Do we change to vehicle id, or add the vehicle - // id to the event? In the code, the person id is used for the walk - // distance. kai, jul'15 + // yyyyyy the parking arrival event returns person id, not vehicle id. Do we change to vehicle id, or add the vehicle id to + // the event? In the code, the person id is used for the walk distance. kai, jul'15 return selectedParking; } diff --git a/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/example/ParkingBetaExample.java b/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/run/ParkingBetaExample.java similarity index 93% rename from contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/example/ParkingBetaExample.java rename to contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/run/ParkingBetaExample.java index b7e3b12c238..d1ab91f77ce 100644 --- a/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/example/ParkingBetaExample.java +++ b/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/run/ParkingBetaExample.java @@ -17,7 +17,7 @@ * See also COPYING, LICENSE and WARRANTY file * * * * *********************************************************************** */ -package org.matsim.contrib.parking.parkingchoice.example; +package org.matsim.contrib.parking.parkingchoice.run; import org.matsim.api.core.v01.population.Person; import org.matsim.contrib.parking.parkingchoice.PC2.scoring.AbstractParkingBetas; @@ -26,7 +26,7 @@ * @author jbischoff * example class for setting parking betas: we simply return 1 for Beta values. Typically those values should be person- and/or income depending */ -public class ParkingBetaExample extends AbstractParkingBetas { +class ParkingBetaExample extends AbstractParkingBetas { @Override diff --git a/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/example/ParkingCostCalculatorExample.java b/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/run/ParkingCostCalculatorExample.java similarity index 93% rename from contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/example/ParkingCostCalculatorExample.java rename to contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/run/ParkingCostCalculatorExample.java index 93e6310d8f6..96568a15fd7 100644 --- a/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/example/ParkingCostCalculatorExample.java +++ b/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/run/ParkingCostCalculatorExample.java @@ -17,7 +17,7 @@ * See also COPYING, LICENSE and WARRANTY file * * * * *********************************************************************** */ -package org.matsim.contrib.parking.parkingchoice.example; +package org.matsim.contrib.parking.parkingchoice.run; import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.population.Person; @@ -27,7 +27,7 @@ * @author jbischoff * */ -public class ParkingCostCalculatorExample implements ParkingCostModel { +class ParkingCostCalculatorExample implements ParkingCostModel { private double hourlyParkingCharge; diff --git a/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/run/RunParkingChoiceExample.java b/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/run/RunParkingChoiceExample.java index e53da13e52d..4ae2c556a7c 100644 --- a/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/run/RunParkingChoiceExample.java +++ b/contribs/parking/src/main/java/org/matsim/contrib/parking/parkingchoice/run/RunParkingChoiceExample.java @@ -33,8 +33,6 @@ import org.matsim.contrib.parking.parkingchoice.PC2.scoring.ParkingScoreManager; import org.matsim.contrib.parking.parkingchoice.PC2.simulation.ParkingInfrastructure; import org.matsim.contrib.parking.parkingchoice.PC2.simulation.ParkingInfrastructureManager; -import org.matsim.contrib.parking.parkingchoice.example.ParkingBetaExample; -import org.matsim.contrib.parking.parkingchoice.example.ParkingCostCalculatorExample; import org.matsim.core.config.Config; import org.matsim.core.config.ConfigUtils; import org.matsim.core.controler.Controler; @@ -47,7 +45,7 @@ * * */ -public class RunParkingChoiceExample { +class RunParkingChoiceExample { public static void main(String[] args) { Config config = ConfigUtils.loadConfig("parkingchoice/config.xml"); @@ -64,21 +62,24 @@ public static void run(Config config) { // we need some settings to walk from parking to destination: ParkingScore parkingScoreManager = new ParkingScoreManager(new WalkTravelTime(controler.getConfig().routing()), scenario); - parkingScoreManager.setParkingScoreScalingFactor(1); - parkingScoreManager.setParkingBetas(new ParkingBetaExample()); - + { + parkingScoreManager.setParkingScoreScalingFactor( 1 ); + parkingScoreManager.setParkingBetas( new ParkingBetaExample() ); + } // --- ParkingInfrastructure parkingInfrastructureManager = new ParkingInfrastructureManager(parkingScoreManager, controler.getEvents()); { - LinkedList publicParkings = new LinkedList(); + LinkedList publicParkings = new LinkedList<>(); + //parking 1: we place this near the workplace - publicParkings.add(new PublicParking(Id.create("workPark", PC2Parking.class), 98, new Coord((double) 10000, (double) 0), + publicParkings.add(new PublicParking(Id.create("workPark", PC2Parking.class), 98, new Coord( 10000, 0 ), new ParkingCostCalculatorExample(1), "park")); + //parking 2: we place this at home - final double x = -25000; - publicParkings.add(new PublicParking(Id.create("homePark", PC2Parking.class), 98, new Coord(x, (double) 0), + publicParkings.add(new PublicParking(Id.create("homePark", PC2Parking.class), 98, new Coord( -25000, 0 ), new ParkingCostCalculatorExample(0), "park")); + parkingInfrastructureManager.setPublicParkings(publicParkings); } diff --git a/contribs/parking/src/test/java/org/matsim/contrib/parking/run/RunParkingChoiceExampleIT.java b/contribs/parking/src/test/java/org/matsim/contrib/parking/parkingchoice/run/RunParkingChoiceExampleIT.java similarity index 94% rename from contribs/parking/src/test/java/org/matsim/contrib/parking/run/RunParkingChoiceExampleIT.java rename to contribs/parking/src/test/java/org/matsim/contrib/parking/parkingchoice/run/RunParkingChoiceExampleIT.java index c084bf7a434..e1d806174cf 100644 --- a/contribs/parking/src/test/java/org/matsim/contrib/parking/run/RunParkingChoiceExampleIT.java +++ b/contribs/parking/src/test/java/org/matsim/contrib/parking/parkingchoice/run/RunParkingChoiceExampleIT.java @@ -16,11 +16,10 @@ * See also COPYING, LICENSE and WARRANTY file * * * * *********************************************************************** */ -package org.matsim.contrib.parking.run; +package org.matsim.contrib.parking.parkingchoice.run; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; -import org.matsim.contrib.parking.parkingchoice.run.RunParkingChoiceExample; import org.matsim.core.config.Config; import org.matsim.core.config.ConfigUtils; import org.matsim.testcases.MatsimTestUtils; diff --git a/contribs/parking/src/test/java/org/matsim/contrib/parking/run/RunParkingSearchScenarioIT.java b/contribs/parking/src/test/java/org/matsim/contrib/parking/parkingchoice/run/RunParkingSearchScenarioIT.java similarity index 99% rename from contribs/parking/src/test/java/org/matsim/contrib/parking/run/RunParkingSearchScenarioIT.java rename to contribs/parking/src/test/java/org/matsim/contrib/parking/parkingchoice/run/RunParkingSearchScenarioIT.java index d6feb304be0..fa0dc4f5ffd 100644 --- a/contribs/parking/src/test/java/org/matsim/contrib/parking/run/RunParkingSearchScenarioIT.java +++ b/contribs/parking/src/test/java/org/matsim/contrib/parking/parkingchoice/run/RunParkingSearchScenarioIT.java @@ -17,7 +17,7 @@ * * * *********************************************************************** */ -package org.matsim.contrib.parking.run; +package org.matsim.contrib.parking.parkingchoice.run; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test;