diff --git a/src/main/java/org/matsim/analysis/StuckEventHandler.java b/src/main/java/org/matsim/analysis/StuckEventHandler.java new file mode 100644 index 000000000..32eb7c284 --- /dev/null +++ b/src/main/java/org/matsim/analysis/StuckEventHandler.java @@ -0,0 +1,70 @@ +package org.matsim.analysis; + +import org.matsim.api.core.v01.Coord; +import org.matsim.api.core.v01.Id; +import org.matsim.api.core.v01.events.PersonStuckEvent; +import org.matsim.api.core.v01.events.handler.PersonStuckEventHandler; +import org.matsim.api.core.v01.network.Link; +import org.matsim.api.core.v01.network.Network; +import org.matsim.api.core.v01.population.Person; +import org.matsim.core.api.experimental.events.EventsManager; +import org.matsim.core.events.EventsUtils; +import org.matsim.core.events.MatsimEventsReader; +import org.matsim.core.network.NetworkUtils; +import org.matsim.core.utils.io.IOUtils; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class StuckEventHandler implements PersonStuckEventHandler { + + private final List stuckEventsData; + + public StuckEventHandler(List stuckEventsData) { + this.stuckEventsData = stuckEventsData; + } + + public static void main (String args []) throws IOException { + + EventsManager manager = EventsUtils.createEventsManager(); + List listOfStuckEvents = new ArrayList<>(); + manager.addHandler(new StuckEventHandler(listOfStuckEvents)); + manager.initProcessing(); + MatsimEventsReader matsimEventsReader = new MatsimEventsReader(manager); + matsimEventsReader.readFile("/Users/gregorr/Desktop/Test/TryToWriteAPaper/KlimaTaler/KlimaTaler1000/KlimaTaler1000.output_events.xml.gz"); + manager.finishProcessing(); + writeResults("/Users/gregorr/Desktop/Test/TryToWriteAPaper/KlimaTaler/KlimaTaler1000/", listOfStuckEvents); + } + + + @Override + public void handleEvent(PersonStuckEvent personStuckEvent) { + StuckEventsData stuckEvent = new StuckEventsData(personStuckEvent.getPersonId(), personStuckEvent.getLinkId()); + stuckEventsData.add(stuckEvent); + } + + @Override + public void reset(int iteration) { + PersonStuckEventHandler.super.reset(iteration); + } + + + record StuckEventsData (Id personId, Id linkId) {} + + + private static void writeResults(String output, List stuckEventsData) throws IOException { + BufferedWriter writer = IOUtils.getBufferedWriter(output + "/stuckEvents.tsv"); + writer.write("personId"+ "\t" +"linkId" ); + writer.newLine(); + for (int i = 0; i < stuckEventsData.size(); i++) { + StuckEventsData se = stuckEventsData.get(i); + writer.write(se.personId + "\t" + se.linkId); + writer.newLine(); + } + writer.close(); + System.out.println("Nr of stuck Agents: " + stuckEventsData.size()); + } + +} \ No newline at end of file diff --git a/src/main/java/org/matsim/policies/MobilityBudgetEventHandler.java b/src/main/java/org/matsim/policies/MobilityBudgetEventHandler.java index 58a2d7cfd..d2ccd900e 100644 --- a/src/main/java/org/matsim/policies/MobilityBudgetEventHandler.java +++ b/src/main/java/org/matsim/policies/MobilityBudgetEventHandler.java @@ -5,20 +5,25 @@ import org.matsim.api.core.v01.TransportMode; import org.matsim.api.core.v01.events.PersonDepartureEvent; import org.matsim.api.core.v01.events.PersonMoneyEvent; +import org.matsim.api.core.v01.events.PersonStuckEvent; import org.matsim.api.core.v01.events.handler.PersonDepartureEventHandler; +import org.matsim.api.core.v01.events.handler.PersonStuckEventHandler; import org.matsim.api.core.v01.population.Person; import org.matsim.core.controler.events.AfterMobsimEvent; import org.matsim.core.controler.listener.AfterMobsimListener; import org.matsim.core.utils.misc.Time; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; -public class MobilityBudgetEventHandler implements PersonDepartureEventHandler, AfterMobsimListener { +public class MobilityBudgetEventHandler implements PersonDepartureEventHandler, AfterMobsimListener, PersonStuckEventHandler { private final Map, Double> person2MobilityBudget; private final Map, Double> currentIterationMobilityBudget = new HashMap<>(); + private final List> personWhoAreStuck= new ArrayList<>(); public MobilityBudgetEventHandler(Map, Double> personsEligibleForMobilityBudget2MoneyValue) { this.person2MobilityBudget = personsEligibleForMobilityBudget2MoneyValue; @@ -28,6 +33,7 @@ public MobilityBudgetEventHandler(Map, Double> personsEligibleForMobi public void reset(int iteration) { currentIterationMobilityBudget.clear(); currentIterationMobilityBudget.putAll(person2MobilityBudget); + personWhoAreStuck.clear(); } @Override @@ -47,7 +53,19 @@ public void notifyAfterMobsim(AfterMobsimEvent event) { Double mobilityBudget = idDoubleEntry.getValue(); event.getServices().getEvents().processEvent(new PersonMoneyEvent(Time.MIDNIGHT, person, mobilityBudget, "mobilityBudget", null)); totalSumMobilityBudget = totalSumMobilityBudget + mobilityBudget; - //} } + + for (Id personId: personWhoAreStuck) { + event.getServices().getEvents().processEvent(new PersonMoneyEvent(Time.MIDNIGHT, personId, 10000, "punishmentForBeingStuck", null)); + } + } + + /* + This is here to add an extra punishment to people who get stuck and benefit from getting the mob budget and thus obtaining a high score + */ + @Override + public void handleEvent(PersonStuckEvent personStuckEvent) { + personWhoAreStuck.add(personStuckEvent.getPersonId()); + } } \ No newline at end of file diff --git a/src/main/java/org/matsim/run/RunBerlinWithMobBudget.java b/src/main/java/org/matsim/run/RunBerlinWithMobBudget.java index cd8f72c94..890c83cc9 100644 --- a/src/main/java/org/matsim/run/RunBerlinWithMobBudget.java +++ b/src/main/java/org/matsim/run/RunBerlinWithMobBudget.java @@ -86,9 +86,7 @@ public static void main(String[] args) { DistanceBasedMoneyReward klimaTaler = new DistanceBasedMoneyReward(controler.getScenario().getConfig().plansCalcRoute().getBeelineDistanceFactors().get(TransportMode.walk), controler.getScenario().getNetwork(), distanceBasedMoneyReward); addKlimaTaler(controler, klimaTaler); } - controler.run(); - }