From 27a106d8c4d4e3a650c984d32f17983ef7160d26 Mon Sep 17 00:00:00 2001 From: tschlenther Date: Wed, 6 Nov 2024 14:47:58 +0100 Subject: [PATCH] Update mat sim (#90) * update MATSim: use DashboardUtils and new pt fare implementation (with old values and logic) * clean code * set version number to 2024.2 in java code * Revert "set version number to 2024.2 in java code" This reverts commit 9ea16e02379eb273563fe82c510607a1ce269a45. * change array types --------- Co-authored-by: sime94 Co-authored-by: simei94 <67737999+simei94@users.noreply.github.com> --- pom.xml | 6 +-- .../postAnalysis/NoiseAverageAnalysis.java | 16 ++++---- .../AverageKelheimEmissionsDashboard.java | 16 ++------ .../AverageKelheimNoiseDashboard.java | 30 +++----------- .../dashboard/KelheimEmissionsDashboard.java | 14 +++---- .../dashboard/KelheimSimWrapperRunner.java | 2 +- .../org/matsim/run/KelheimPtFareModule.java | 34 ---------------- .../org/matsim/run/RunKelheimScenario.java | 40 ++++++++----------- 8 files changed, 41 insertions(+), 117 deletions(-) delete mode 100644 src/main/java/org/matsim/run/KelheimPtFareModule.java diff --git a/pom.xml b/pom.xml index ac713775..28b6112f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ matsim-all - 2025.0-PR3402 + 2025.0-PR3501 @@ -15,7 +15,7 @@ 4.0.0 com.github.matsim-scenarios matsim-kelheim - 3.x-SNAPSHOT + 2024.2-SNAPSHOT MATSim Kelheim Model A transport model of the district of Kelheim and it's surroundings @@ -212,7 +212,7 @@ org.apache.avro avro - 1.11.3 + 1.12.0 diff --git a/src/main/java/org/matsim/analysis/postAnalysis/NoiseAverageAnalysis.java b/src/main/java/org/matsim/analysis/postAnalysis/NoiseAverageAnalysis.java index 2749a67a..e4526db4 100644 --- a/src/main/java/org/matsim/analysis/postAnalysis/NoiseAverageAnalysis.java +++ b/src/main/java/org/matsim/analysis/postAnalysis/NoiseAverageAnalysis.java @@ -4,9 +4,9 @@ import org.apache.avro.file.DataFileReader; import org.apache.avro.file.DataFileWriter; import org.apache.avro.file.FileReader; -import org.apache.avro.generic.GenericData; import org.apache.avro.generic.GenericDatumReader; import org.apache.avro.generic.GenericRecord; +import org.apache.avro.generic.PrimitivesArrays; import org.apache.avro.io.DatumWriter; import org.apache.avro.specific.SpecificDatumWriter; import org.apache.avro.util.Utf8; @@ -215,10 +215,8 @@ private XYTData calcAvroMeans(List recordList, String dataFieldNa getCoordData(object1, xCoords); getCoordData(object2, yCoords); - -// TODO: for the example data even for the hourly data there was only one time stamp. This might be different with real data. this needs to be checked - if (object3 instanceof GenericData.Array) { - List ints = new ArrayList<>((GenericData.Array) object3); + if (object3 instanceof PrimitivesArrays.IntArray intArray) { + List ints = new ArrayList<>(intArray); if (!timeStamps.equals(ints)) { if (timeStamps.isEmpty()) { @@ -235,8 +233,8 @@ private XYTData calcAvroMeans(List recordList, String dataFieldNa List values = new ArrayList<>(); for (Map.Entry entry : ((HashMap) object4).entrySet()) { - if (entry.getKey() instanceof Utf8 && entry.getKey().toString().equals(dataFieldName) && entry.getValue() instanceof GenericData.Array) { - values.addAll((GenericData.Array) entry.getValue()); + if (entry.getKey() instanceof Utf8 && entry.getKey().toString().equals(dataFieldName) && entry.getValue() instanceof PrimitivesArrays.FloatArray floatArray) { + values.addAll(floatArray); String entryString = ((Utf8) entry.getKey()).toString(); @@ -265,8 +263,8 @@ private XYTData calcAvroMeans(List recordList, String dataFieldNa } private void getCoordData(Object object, List target) { - if (object instanceof GenericData.Array) { - List floats = new ArrayList<>((GenericData.Array) object); + if (object instanceof PrimitivesArrays.FloatArray floatArray) { + List floats = new ArrayList<>(floatArray); if (!target.equals(floats)) { if (target.isEmpty()) { diff --git a/src/main/java/org/matsim/dashboard/AverageKelheimEmissionsDashboard.java b/src/main/java/org/matsim/dashboard/AverageKelheimEmissionsDashboard.java index fa369af5..b5263be8 100644 --- a/src/main/java/org/matsim/dashboard/AverageKelheimEmissionsDashboard.java +++ b/src/main/java/org/matsim/dashboard/AverageKelheimEmissionsDashboard.java @@ -21,17 +21,12 @@ package org.matsim.dashboard; import org.matsim.analysis.postAnalysis.emissions.EmissionsPostProcessingAverageAnalysis; -import org.matsim.simwrapper.Dashboard; -import org.matsim.simwrapper.Data; -import org.matsim.simwrapper.Header; -import org.matsim.simwrapper.Layout; +import org.matsim.simwrapper.*; import org.matsim.simwrapper.viz.*; import java.util.ArrayList; import java.util.List; -import static org.matsim.dashboard.AverageKelheimNoiseDashboard.*; - /** * Average emissions dashboard for several runs with the same config but a different random seed. */ @@ -130,21 +125,16 @@ public void configure(Header header, Layout layout) { layout.row("second").el(GridMap.class, (viz, data) -> { viz.title = "CO₂ Emissions"; viz.description = "per day. Be aware that CO2 values are provided in the simulation sample size!"; - setGridMapStandards(viz); + DashboardUtils.setGridMapStandards(viz, data, "EPSG:25832"); viz.file = postProcess(data, "mean_emissions_grid_per_day.xyt.csv"); }); layout.row("third") .el(GridMap.class, (viz, data) -> { viz.title = "CO₂ Emissions"; viz.description = "per hour. Be aware that CO2 values are provided in the simulation sample size!"; - setGridMapStandards(viz); + DashboardUtils.setGridMapStandards(viz, data, "EPSG:25832"); viz.file = postProcess(data, "mean_emissions_grid_per_hour.csv"); }); } - private static void setGridMapStandards(GridMap viz) { - viz.projection = "EPSG:25832"; - viz.setColorRamp(new double[]{30, 40, 50, 60, 70}, new String[]{DARK_BLUE, LIGHT_BLUE, YELLOW, SAND, ORANGE, RED}); - viz.height = 12.0; - } } diff --git a/src/main/java/org/matsim/dashboard/AverageKelheimNoiseDashboard.java b/src/main/java/org/matsim/dashboard/AverageKelheimNoiseDashboard.java index b4715dd9..d3d175e3 100644 --- a/src/main/java/org/matsim/dashboard/AverageKelheimNoiseDashboard.java +++ b/src/main/java/org/matsim/dashboard/AverageKelheimNoiseDashboard.java @@ -1,10 +1,7 @@ package org.matsim.dashboard; import org.matsim.analysis.postAnalysis.NoiseAverageAnalysis; -import org.matsim.simwrapper.Dashboard; -import org.matsim.simwrapper.Data; -import org.matsim.simwrapper.Header; -import org.matsim.simwrapper.Layout; +import org.matsim.simwrapper.*; import org.matsim.simwrapper.viz.ColorScheme; import org.matsim.simwrapper.viz.GridMap; import org.matsim.simwrapper.viz.MapPlot; @@ -23,12 +20,7 @@ public class AverageKelheimNoiseDashboard implements Dashboard { private final List dirs; private final Integer noRuns; private static final String NOISE = "noise"; - static final String DARK_BLUE = "#1175b3"; - static final String LIGHT_BLUE = "#95c7df"; - static final String ORANGE = "#f4a986"; - static final String RED = "#cc0c27"; - static final String SAND = "#dfb095"; - static final String YELLOW = "#dfdb95"; + public AverageKelheimNoiseDashboard(List dirs, Integer noRuns) { this.dirs = dirs; @@ -78,38 +70,28 @@ public void configure(Header header, Layout layout) { .el(GridMap.class, (viz, data) -> { viz.title = "Noise Immissions (Grid)"; viz.description = "Total Noise Immissions per day"; - setGridMapStandards(viz, data); + DashboardUtils.setGridMapStandards(viz, data, "EPSG:25832"); viz.file = postProcess(data, "mean_immission_per_day.avro"); }) .el(GridMap.class, (viz, data) -> { viz.title = "Hourly Noise Immissions (Grid)"; viz.description = "Noise Immissions per hour"; - setGridMapStandards(viz, data); + DashboardUtils.setGridMapStandards(viz, data, "EPSG:25832"); viz.file = postProcess(data, "mean_immission_per_hour.avro"); }); layout.row("damages") .el(GridMap.class, (viz, data) -> { viz.title = "Daily Noise Damages (Grid)"; viz.description = "Total Noise Damages per day [€]"; - setGridMapStandards(viz, data); + DashboardUtils.setGridMapStandards(viz, data, "EPSG:25832"); viz.file = postProcess(data, "mean_damages_receiverPoint_per_day.avro"); }) .el(GridMap.class, (viz, data) -> { viz.title = "Hourly Noise Damages (Grid)"; viz.description = "Noise Damages per hour [€]"; - setGridMapStandards(viz, data); + DashboardUtils.setGridMapStandards(viz, data, "EPSG:25832"); viz.file = postProcess(data, "mean_damages_receiverPoint_per_hour.avro"); }); } - private static void setGridMapStandards(GridMap viz, Data data) { - viz.height = 12.0; - viz.cellSize = 250; - viz.opacity = 0.1; - viz.maxHeight = 40; - viz.projection = "EPSG:25832"; - viz.center = data.context().getCenter(); - viz.zoom = data.context().mapZoomLevel; - viz.setColorRamp(new double[]{30, 40, 50, 60, 70}, new String[]{DARK_BLUE, LIGHT_BLUE, YELLOW, SAND, ORANGE, RED}); - } } diff --git a/src/main/java/org/matsim/dashboard/KelheimEmissionsDashboard.java b/src/main/java/org/matsim/dashboard/KelheimEmissionsDashboard.java index 80141b08..f59d55e0 100644 --- a/src/main/java/org/matsim/dashboard/KelheimEmissionsDashboard.java +++ b/src/main/java/org/matsim/dashboard/KelheimEmissionsDashboard.java @@ -23,6 +23,7 @@ import org.matsim.analysis.postAnalysis.emissions.KelheimOfflineAirPollutionAnalysisByEngineInformation; import org.matsim.application.prepare.network.CreateGeoJsonNetwork; import org.matsim.simwrapper.Dashboard; +import org.matsim.simwrapper.DashboardUtils; import org.matsim.simwrapper.Header; import org.matsim.simwrapper.Layout; import org.matsim.simwrapper.viz.GridMap; @@ -94,21 +95,16 @@ public void configure(Header header, Layout layout) { layout.row("second").el(GridMap.class, (viz, data) -> { viz.title = "CO₂ Emissions"; viz.description = "per day. Be aware that CO2 values are provided in the simulation sample size!"; - setGridMapStandards(viz, 12.0, data.compute(KelheimOfflineAirPollutionAnalysisByEngineInformation.class, "emissions_grid_per_day.xyt.csv", new String[0])); - + DashboardUtils.setGridMapStandards(viz, data, "EPSG:25832"); + viz.file = data.compute(KelheimOfflineAirPollutionAnalysisByEngineInformation.class, "emissions_grid_per_day.xyt.csv", new String[0]); }); layout.row("third") .el(GridMap.class, (viz, data) -> { viz.title = "CO₂ Emissions"; viz.description = "per hour. Be aware that CO2 values are provided in the simulation sample size!"; - setGridMapStandards(viz, 12., data.compute(KelheimOfflineAirPollutionAnalysisByEngineInformation.class, "emissions_grid_per_hour.csv")); + DashboardUtils.setGridMapStandards(viz, data, "EPSG:25832"); + viz.file = data.compute(KelheimOfflineAirPollutionAnalysisByEngineInformation.class, "emissions_grid_per_hour.csv"); }); } - private static void setGridMapStandards(GridMap viz, double height, String data) { - viz.height = height; - viz.projection = "EPSG:25832"; - viz.file = data; - viz.setColorRamp(new double[]{30, 40, 50, 60, 70}, new String[]{DARK_BLUE, LIGHT_BLUE, YELLOW, SAND, ORANGE, RED}); - } } diff --git a/src/main/java/org/matsim/dashboard/KelheimSimWrapperRunner.java b/src/main/java/org/matsim/dashboard/KelheimSimWrapperRunner.java index 01b3c3a6..7baec11c 100644 --- a/src/main/java/org/matsim/dashboard/KelheimSimWrapperRunner.java +++ b/src/main/java/org/matsim/dashboard/KelheimSimWrapperRunner.java @@ -106,7 +106,7 @@ public Integer call() throws Exception { new DrtDashboardProvider().getDashboards(config, sw).forEach(sw::addDashboard); } if (noise){ - sw.addDashboard(new NoiseDashboard()); + sw.addDashboard(new NoiseDashboard(config.global().getCoordinateSystem())); } try { diff --git a/src/main/java/org/matsim/run/KelheimPtFareModule.java b/src/main/java/org/matsim/run/KelheimPtFareModule.java deleted file mode 100644 index 2f823747..00000000 --- a/src/main/java/org/matsim/run/KelheimPtFareModule.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.matsim.run; - -import org.matsim.api.core.v01.TransportMode; -import org.matsim.core.config.ConfigUtils; -import org.matsim.core.controler.AbstractModule; -import playground.vsp.pt.fare.DistanceBasedPtFareHandler; -import playground.vsp.pt.fare.DistanceBasedPtFareParams; -import playground.vsp.pt.fare.PtFareConfigGroup; -import playground.vsp.pt.fare.PtFareUpperBoundHandler; - -/** - * Module to install classes for pt fare. - */ -public class KelheimPtFareModule extends AbstractModule { - @Override - public void install() { - // Set the money related thing in the config (planCalcScore) file to 0. - getConfig().scoring().getModes().get(TransportMode.pt).setDailyMonetaryConstant(0); - getConfig().scoring().getModes().get(TransportMode.pt).setMarginalUtilityOfDistance(0); - - // Initialize config group (and also write in the output config) - PtFareConfigGroup ptFareConfigGroup = ConfigUtils.addOrGetModule(this.getConfig(), PtFareConfigGroup.class); - DistanceBasedPtFareParams distanceBasedPtFareParams = ConfigUtils.addOrGetModule(this.getConfig(), DistanceBasedPtFareParams.class); - - - // Add bindings - addEventHandlerBinding().toInstance(new DistanceBasedPtFareHandler(distanceBasedPtFareParams)); - if (ptFareConfigGroup.getApplyUpperBound()) { - PtFareUpperBoundHandler ptFareUpperBoundHandler = new PtFareUpperBoundHandler(ptFareConfigGroup.getUpperBoundFactor()); - addEventHandlerBinding().toInstance(ptFareUpperBoundHandler); - addControlerListenerBinding().toInstance(ptFareUpperBoundHandler); - } - } -} diff --git a/src/main/java/org/matsim/run/RunKelheimScenario.java b/src/main/java/org/matsim/run/RunKelheimScenario.java index d146f381..0a243b6e 100644 --- a/src/main/java/org/matsim/run/RunKelheimScenario.java +++ b/src/main/java/org/matsim/run/RunKelheimScenario.java @@ -43,6 +43,7 @@ import org.matsim.contrib.dvrp.run.DvrpModule; import org.matsim.contrib.dvrp.run.DvrpQSimComponents; import org.matsim.contrib.dvrp.trafficmonitoring.DvrpModeLimitedMaxSpeedTravelTimeModule; +import org.matsim.contrib.vsp.pt.fare.PtFareModule; import org.matsim.contrib.vsp.scenario.SnzActivities; import org.matsim.core.api.experimental.events.EventsManager; import org.matsim.core.config.Config; @@ -62,8 +63,8 @@ import org.matsim.simwrapper.SimWrapperModule; import org.matsim.vehicles.VehicleType; import picocli.CommandLine; -import playground.vsp.pt.fare.DistanceBasedPtFareParams; -import playground.vsp.pt.fare.PtFareConfigGroup; +import org.matsim.contrib.vsp.pt.fare.DistanceBasedPtFareParams; +import org.matsim.contrib.vsp.pt.fare.PtFareConfigGroup; import playground.vsp.scoring.IncomeDependentUtilityOfMoneyPersonScoringParameters; import javax.annotation.Nullable; @@ -221,16 +222,18 @@ protected Config prepareConfig(Config config) { // Minimum fare (e.g. short trip or 1 zone ticket) distanceBasedPtFareParams.setMinFare(2.0); - // Division between long trip and short trip (unit: m) - distanceBasedPtFareParams.setLongDistanceTripThreshold(50000); - // y = ax + b --> a value, for short trips - distanceBasedPtFareParams.setNormalTripSlope(0.00017); - // y = ax + b --> b value, for short trips - distanceBasedPtFareParams.setNormalTripIntercept(1.6); - // y = ax + b --> a value, for long trips - distanceBasedPtFareParams.setLongDistanceTripSlope(0.00025); - // y = ax + b --> b value, for long trips - distanceBasedPtFareParams.setLongDistanceTripIntercept(30); + + distanceBasedPtFareParams.setTransactionPartner("pt-operator"); + DistanceBasedPtFareParams.DistanceClassLinearFareFunctionParams shortDistance = distanceBasedPtFareParams.getOrCreateDistanceClassFareParams(50000); + shortDistance.setFareIntercept(1.6); + shortDistance.setFareSlope(0.00017); + + DistanceBasedPtFareParams.DistanceClassLinearFareFunctionParams longDistance = distanceBasedPtFareParams.getOrCreateDistanceClassFareParams(Double.POSITIVE_INFINITY); + longDistance.setFareIntercept(30); + longDistance.setFareSlope(0.00025); + distanceBasedPtFareParams.setOrder(1); + + ptFareConfigGroup.addParameterSet(distanceBasedPtFareParams); //enable plan inheritance analysis config.planInheritance().setEnabled(true); @@ -291,7 +294,7 @@ protected void prepareControler(Controler controler) { controler.addOverridingModule(new AbstractModule() { @Override public void install() { - install(new KelheimPtFareModule()); + install(new PtFareModule()); install(new SwissRailRaptorModule()); install(new PersonMoneyEventsAnalysisModule()); install(new SimWrapperModule()); @@ -377,17 +380,6 @@ public void install() { // TODO: when to include AV? //estimatorConfig.addParameterSet(new DrtEstimatorConfigGroup("av")); - -// if (intermodal){ -// controler.addOverridingModule(new IntermodalTripFareCompensatorsModule()); -// controler.addOverridingModule(new PtIntermodalRoutingModesModule()); -// controler.addOverridingModule(new AbstractModule() { -// @Override -// public void install() { -// bind(RaptorIntermodalAccessEgress.class).to(EnhancedRaptorIntermodalAccessEgress.class); -// } -// }); -// } } } }