Skip to content

Commit

Permalink
add money event handler and ride into mobBudget
Browse files Browse the repository at this point in the history
  • Loading branch information
GregorRyb committed Jul 11, 2024
1 parent 89bfeec commit df2ba4f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
73 changes: 73 additions & 0 deletions src/main/java/org/matsim/analysis/MyPersonMoneyEventHandler.java
Original file line number Diff line number Diff line change
@@ -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<PersonMoneyEvent>());
myEventsmanager.addHandler(myEventHandler);
EventsUtils.readEvents(myEventsmanager, inputPath);
writeAllPersonMoneyEvents(myEventHandler.personMoneyEventList, outputPath);
}

static void writeAllPersonMoneyEvents(List<PersonMoneyEvent> 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<PersonMoneyEvent> personMoneyEventList;
TestPersonMoneyEventHandler( List<PersonMoneyEvent> personMoneyEventList) {
this.personMoneyEventList = personMoneyEventList;
}

@Override
public void handleEvent(PersonMoneyEvent personMoneyEvent) {
personMoneyEventList.add(personMoneyEvent);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -124,7 +123,6 @@ public void install() {
});
}


private static class DistanceBasedMoneyReward implements PersonDepartureEventHandler,
PersonArrivalEventHandler,
AfterMobsimListener,
Expand Down Expand Up @@ -247,7 +245,6 @@ public void handleEvent(ActivityStartEvent event) {
}
if (!distanceTravelledPt.containsKey(event.getPersonId())) {
distanceTravelledPt.put(personId, distance);

}
personDepartureCoordMap.remove(personId);
personArrivalCoordMap.remove(personId);
Expand All @@ -256,9 +253,6 @@ public void handleEvent(ActivityStartEvent event) {
}
}




private static class MobilityBudgetEventHandler implements PersonDepartureEventHandler, AfterMobsimListener, PersonStuckEventHandler {

private final Map<Id<Person>, Double> person2MobilityBudget;
Expand All @@ -279,7 +273,7 @@ public void reset(int iteration) {
@Override
public void handleEvent(PersonDepartureEvent personDepartureEvent) {
Id<Person> 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);
}
}
Expand Down

0 comments on commit df2ba4f

Please sign in to comment.