Skip to content

Commit

Permalink
feat: simplify analysis with routing mode (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhoerl authored Mar 8, 2024
1 parent 7fa8f3f commit f4265df
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.network.io.MatsimNetworkReader;
import org.matsim.core.router.MainModeIdentifier;
import org.matsim.core.router.RoutingModeMainModeIdentifier;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.facilities.ActivityFacilities;
import org.matsim.facilities.MatsimFacilitiesReader;
Expand Down Expand Up @@ -66,8 +64,6 @@ public static void run(CommandLine cmd, PersonAnalysisFilter personAnalysisFilte

String outputPath = cmd.getOptionStrict("output-path");

MainModeIdentifier mainModeIdentifier = new RoutingModeMainModeIdentifier();

Collection<String> vehicleModes = Arrays.asList(cmd.getOption("vehicle-modes").orElse("car,pt").split(","))
.stream().map(s -> s.trim()).collect(Collectors.toSet());

Expand All @@ -79,7 +75,7 @@ public static void run(CommandLine cmd, PersonAnalysisFilter personAnalysisFilte
new MatsimNetworkReader(network).readFile(networkPath);

String eventsPath = cmd.getOptionStrict("events-path");
TripListener tripListener = new TripListener(network, mainModeIdentifier, personAnalysisFilter);
TripListener tripListener = new TripListener(network, personAnalysisFilter);
trips = new TripReaderFromEvents(tripListener).readTrips(eventsPath);
} else {
Network network = null;
Expand All @@ -102,8 +98,8 @@ public static void run(CommandLine cmd, PersonAnalysisFilter personAnalysisFilte
}

String populationPath = cmd.getOptionStrict("population-path");
trips = new TripReaderFromPopulation(vehicleModes, mainModeIdentifier, personAnalysisFilter,
Optional.ofNullable(network), Optional.ofNullable(facilities)).readTrips(populationPath);
trips = new TripReaderFromPopulation(vehicleModes, personAnalysisFilter, Optional.ofNullable(network),
Optional.ofNullable(facilities)).readTrips(populationPath);
}

DistanceUnit inputUnit = DistanceUnit.valueOf(cmd.getOption("input-distance-unit").orElse("meter"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
import org.matsim.api.core.v01.events.handler.PersonEntersVehicleEventHandler;
import org.matsim.api.core.v01.events.handler.PersonLeavesVehicleEventHandler;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.population.Leg;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.PopulationFactory;
import org.matsim.core.api.experimental.events.TeleportationArrivalEvent;
import org.matsim.core.api.experimental.events.handler.TeleportationArrivalEventHandler;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.router.MainModeIdentifier;
import org.matsim.core.router.TripStructureUtils;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.geometry.CoordUtils;
Expand All @@ -39,7 +37,6 @@
public class TripListener implements ActivityStartEventHandler, ActivityEndEventHandler, PersonDepartureEventHandler,
PersonEntersVehicleEventHandler, PersonLeavesVehicleEventHandler, LinkEnterEventHandler,
TeleportationArrivalEventHandler, GenericEventHandler {
final private MainModeIdentifier mainModeIdentifier;
final private Network network;
final private PopulationFactory factory;

Expand All @@ -50,9 +47,8 @@ public class TripListener implements ActivityStartEventHandler, ActivityEndEvent

final private PersonAnalysisFilter personFilter;

public TripListener(Network network, MainModeIdentifier mainModeIdentifier, PersonAnalysisFilter personFilter) {
public TripListener(Network network, PersonAnalysisFilter personFilter) {
this.network = network;
this.mainModeIdentifier = mainModeIdentifier;
this.factory = ScenarioUtils.createScenario(ConfigUtils.createConfig()).getPopulation().getFactory();
this.personFilter = personFilter;
}
Expand Down Expand Up @@ -93,26 +89,20 @@ public void handleEvent(ActivityEndEvent event) {
@Override
public void handleEvent(PersonDepartureEvent event) {
if (personFilter.analyzePerson(event.getPersonId())) {
Leg leg = factory.createLeg(event.getLegMode());
leg.setRoutingMode(event.getRoutingMode());
ongoing.get(event.getPersonId()).elements.add(leg);
ongoing.get(event.getPersonId()).mode = event.getRoutingMode();
}
}

@Override
public void handleEvent(ActivityStartEvent event) {
if (personFilter.analyzePerson(event.getPersonId())) {
if (TripStructureUtils.isStageActivityType(event.getActType())) {
ongoing.get(event.getPersonId()).elements
.add(factory.createActivityFromLinkId(event.getActType(), event.getLinkId()));
} else {
if (!TripStructureUtils.isStageActivityType(event.getActType())) {
TripListenerItem trip = ongoing.remove(event.getPersonId());

if (trip != null) {
trip.returning = event.getActType().equals("home");
trip.followingPurpose = event.getActType();
trip.travelTime = event.getTime() - trip.departureTime;
trip.mode = mainModeIdentifier.identifyMainMode(trip.elements);
trip.destination = network.getLinks().get(event.getLinkId()).getCoord();
trip.euclideanDistance = CoordUtils.calcEuclideanDistance(trip.origin, trip.destination);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package org.eqasim.core.analysis.trips;

import java.util.LinkedList;
import java.util.List;

import org.matsim.api.core.v01.Coord;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.population.Person;
import org.matsim.api.core.v01.population.PlanElement;

public class TripListenerItem extends TripItem {
public List<PlanElement> elements = new LinkedList<>();
public double lastAddedLinkDistance = 0.0;

public TripListenerItem(Id<Person> personId, int personTripId, Coord origin, double startTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.population.io.PopulationReader;
import org.matsim.core.router.MainModeIdentifier;
import org.matsim.core.router.TripStructureUtils;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.geometry.CoordUtils;
Expand All @@ -28,15 +27,13 @@

public class TripReaderFromPopulation {
final private Collection<String> networkModes;
final private MainModeIdentifier mainModeIdentifier;
final private PersonAnalysisFilter personFilter;
final private Optional<Network> network;
final private Optional<ActivityFacilities> facilities;

public TripReaderFromPopulation(Collection<String> networkModes, MainModeIdentifier mainModeIdentifier,
PersonAnalysisFilter personFilter, Optional<Network> network, Optional<ActivityFacilities> facilities) {
public TripReaderFromPopulation(Collection<String> networkModes, PersonAnalysisFilter personFilter,
Optional<Network> network, Optional<ActivityFacilities> facilities) {
this.networkModes = networkModes;
this.mainModeIdentifier = mainModeIdentifier;
this.personFilter = personFilter;
this.network = network;
this.facilities = facilities;
Expand Down Expand Up @@ -109,7 +106,7 @@ public Collection<TripItem> readTrips(Population population) {

tripItems.add(new TripItem(person.getId(), personTripIndex, originCoord, destinationCoord,
departureTime, duration, getVehicleDistance(trip), getRoutedDistance(trip),
mainModeIdentifier.identifyMainMode(trip.getTripElements()),
TripStructureUtils.getRoutingMode(trip.getLegsOnly().get(0)),
trip.getOriginActivity().getType(), trip.getDestinationActivity().getType(), isHomeTrip,
CoordUtils.calcEuclideanDistance(originCoord, destinationCoord)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,33 @@
import org.matsim.api.core.v01.network.Network;
import org.matsim.contrib.drt.run.MultiModeDrtConfigGroup;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.router.MainModeIdentifier;
import org.matsim.pt.transitSchedule.api.TransitSchedule;

import com.google.inject.Provides;
import com.google.inject.Singleton;


public class EqasimAnalysisModule extends AbstractModule {
@Override
public void install() {
addControlerListenerBinding().to(AnalysisOutputListener.class);
if(getConfig().getModules().containsKey(MultiModeDrtConfigGroup.GROUP_NAME)) {

if (getConfig().getModules().containsKey(MultiModeDrtConfigGroup.GROUP_NAME)) {
install(new DrtAnalysisModule());
} else {
// Would be better if there was a way to add the module above as an overriding module from this method.
// Would be better if there was a way to add the module above as an overriding
// module from this method.
// That way we could simply bind the two classes below before the if clause
bind(DefaultPersonAnalysisFilter.class);
bind(PersonAnalysisFilter.class).to(DefaultPersonAnalysisFilter.class);
}

install(new StuckAnalysisModule());
}

@Provides
@Singleton
public TripListener provideTripListener(Network network, MainModeIdentifier mainModeIdentifier,
PersonAnalysisFilter personFilter) {
return new TripListener(network, mainModeIdentifier, personFilter);
public TripListener provideTripListener(Network network, PersonAnalysisFilter personFilter) {
return new TripListener(network, personFilter);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package org.eqasim.ile_de_france.standalone_mode_choice;


import com.google.inject.Key;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.name.Names;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.eqasim.core.analysis.DefaultPersonAnalysisFilter;
import org.eqasim.core.analysis.DistanceUnit;
import org.eqasim.core.analysis.PersonAnalysisFilter;
Expand All @@ -31,19 +39,18 @@
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.router.MainModeIdentifier;
import org.matsim.core.router.util.TravelTime;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.trafficmonitoring.FreeSpeedTravelTime;
import org.matsim.core.utils.timing.TimeInterpretationModule;
import org.matsim.pt.transitSchedule.api.TransitSchedule;
import org.matsim.vehicles.Vehicle;

import java.io.*;
import java.nio.file.Paths;
import java.util.*;
import com.google.inject.Key;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.name.Names;

/**
* This class offers the functionality of running the discrete mode choice model on the whole population without having to go through the whole iterative MATSim process. It is also possible to filter-out the persons that do not have a valid alternative.
Expand Down Expand Up @@ -226,7 +233,7 @@ RecordedTravelTime provideRecordedTravelTime() {

Population population = injector.getInstance(Population.class);
// We initialize the TripReaderFromPopulation here as we might need it just below
TripReaderFromPopulation tripReader = new TripReaderFromPopulation(Arrays.asList("car,pt".split(",")), injector.getInstance(MainModeIdentifier.class), injector.getInstance(PersonAnalysisFilter.class), Optional.empty(), Optional.empty());
TripReaderFromPopulation tripReader = new TripReaderFromPopulation(Arrays.asList("car,pt".split(",")), injector.getInstance(PersonAnalysisFilter.class), Optional.empty(), Optional.empty());
PublicTransportLegReaderFromPopulation ptLegReader = new PublicTransportLegReaderFromPopulation(injector.getInstance(TransitSchedule.class), injector.getInstance(PersonAnalysisFilter.class));
OutputDirectoryHierarchy outputDirectoryHierarchy = injector.getInstance(Key.get(OutputDirectoryHierarchy.class, Names.named("StandaloneModeChoice")));

Expand Down

0 comments on commit f4265df

Please sign in to comment.