From df2ba4f04ae7c184495226ea7799e740587d7408 Mon Sep 17 00:00:00 2001 From: GregorRyb Date: Wed, 10 Jul 2024 19:06:26 +0200 Subject: [PATCH] add money event handler and ride into mobBudget --- .../analysis/MyPersonMoneyEventHandler.java | 73 +++++++++++++++++++ .../run/OpenBerlinWithMonetaryIncentive.java | 10 +-- 2 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/matsim/analysis/MyPersonMoneyEventHandler.java diff --git a/src/main/java/org/matsim/analysis/MyPersonMoneyEventHandler.java b/src/main/java/org/matsim/analysis/MyPersonMoneyEventHandler.java new file mode 100644 index 00000000..b9824ee7 --- /dev/null +++ b/src/main/java/org/matsim/analysis/MyPersonMoneyEventHandler.java @@ -0,0 +1,73 @@ +package org.matsim.analysis; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import org.apache.logging.log4j.LogManager; +import org.matsim.api.core.v01.events.PersonMoneyEvent; +import org.matsim.core.api.experimental.events.EventsManager; +import org.matsim.core.events.EventsUtils; +import org.matsim.core.utils.io.IOUtils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + +/** + * Quick person money analysis handler + */ +public class MyPersonMoneyEventHandler { + + public static void main (String[] args) { + String inputPath = args[0]; + String outputPath = args[1]; + EventsManager myEventsmanager = EventsUtils.createEventsManager(); + TestPersonMoneyEventHandler myEventHandler = new TestPersonMoneyEventHandler(new ArrayList()); + myEventsmanager.addHandler(myEventHandler); + EventsUtils.readEvents(myEventsmanager, inputPath); + writeAllPersonMoneyEvents(myEventHandler.personMoneyEventList, outputPath); + } + + static void writeAllPersonMoneyEvents(List personMoneyEventList, String outputFilename) { + try { + CSVPrinter csvPrinter = new CSVPrinter(IOUtils.getBufferedWriter(outputFilename), CSVFormat.DEFAULT); + + try { + csvPrinter.printRecord("time", "person", "amount", "purpose", "transactionPartner", "reference"); + Iterator var3 = personMoneyEventList.iterator(); + + while (var3.hasNext()) { + PersonMoneyEvent personMoneyEvent = (PersonMoneyEvent) var3.next(); + csvPrinter.printRecord(personMoneyEvent.getTime(), personMoneyEvent.getPersonId(), personMoneyEvent.getAmount(), personMoneyEvent.getPurpose(), personMoneyEvent.getTransactionPartner(), personMoneyEvent.getReference()); + } + } catch (Throwable var6) { + try { + csvPrinter.close(); + } catch (Throwable var5) { + var6.addSuppressed(var5); + } + throw var6; + } + csvPrinter.close(); + } catch (IOException var7) { + LogManager.getLogger("Could not write " + outputFilename + "."); + } + } + +} + +class TestPersonMoneyEventHandler implements org.matsim.api.core.v01.events.handler.PersonMoneyEventHandler { + + List personMoneyEventList; + TestPersonMoneyEventHandler( List personMoneyEventList) { + this.personMoneyEventList = personMoneyEventList; + } + + @Override + public void handleEvent(PersonMoneyEvent personMoneyEvent) { + personMoneyEventList.add(personMoneyEvent); + } +} + + diff --git a/src/main/java/org/matsim/run/OpenBerlinWithMonetaryIncentive.java b/src/main/java/org/matsim/run/OpenBerlinWithMonetaryIncentive.java index 81dc4032..abe8c986 100644 --- a/src/main/java/org/matsim/run/OpenBerlinWithMonetaryIncentive.java +++ b/src/main/java/org/matsim/run/OpenBerlinWithMonetaryIncentive.java @@ -83,7 +83,6 @@ protected void prepareControler(Controler controler) { // teleported beeline is the same for bike and walk if (distanceBasedReward > 0.0) { - DistanceBasedMoneyReward distanceBasedMoneyReward = new DistanceBasedMoneyReward( controler.getScenario().getConfig().routing().getBeelineDistanceFactors().get(TransportMode.walk), controler.getScenario().getNetwork(), distanceBasedReward); @@ -98,8 +97,8 @@ protected void prepareControler(Controler controler) { if (simpleMobilityBudget != 0.0) { double oldDailyMonetaryConstant = controler.getScenario().getConfig().scoring().getModes().get(TransportMode.car).getDailyMonetaryConstant(); controler.getScenario().getConfig().scoring().getModes().get(TransportMode.car).setDailyMonetaryConstant(oldDailyMonetaryConstant + simpleMobilityBudget); + controler.getScenario().getConfig().scoring().getModes().get(TransportMode.ride).setDailyMonetaryConstant(simpleMobilityBudget); } - } private static void addKlimaTaler(Controler controler, DistanceBasedMoneyReward klimaTaler) { @@ -124,7 +123,6 @@ public void install() { }); } - private static class DistanceBasedMoneyReward implements PersonDepartureEventHandler, PersonArrivalEventHandler, AfterMobsimListener, @@ -247,7 +245,6 @@ public void handleEvent(ActivityStartEvent event) { } if (!distanceTravelledPt.containsKey(event.getPersonId())) { distanceTravelledPt.put(personId, distance); - } personDepartureCoordMap.remove(personId); personArrivalCoordMap.remove(personId); @@ -256,9 +253,6 @@ public void handleEvent(ActivityStartEvent event) { } } - - - private static class MobilityBudgetEventHandler implements PersonDepartureEventHandler, AfterMobsimListener, PersonStuckEventHandler { private final Map, Double> person2MobilityBudget; @@ -279,7 +273,7 @@ public void reset(int iteration) { @Override public void handleEvent(PersonDepartureEvent personDepartureEvent) { Id personId = personDepartureEvent.getPersonId(); - if (this.currentIterationMobilityBudget.containsKey(personId) && personDepartureEvent.getLegMode().equals(TransportMode.car)) { + if (this.currentIterationMobilityBudget.containsKey(personId) && (personDepartureEvent.getLegMode().equals(TransportMode.car)) || personDepartureEvent.getLegMode().equals(TransportMode.ride) ) { this.currentIterationMobilityBudget.replace(personId, 0.0); } }