Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Generating PT legs during mode choice + skipping routing during mode choice + update to Eqasim 1.5.0 #229

Closed
wants to merge 10 commits into from
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
- uses: actions/setup-java@v4
with:
distribution: "adopt"
java-version: "11"
distribution: "corretto"
java-version: "17"
- uses: actions/cache@v2
env:
CACHE_NUMBER: 0
Expand Down
4 changes: 2 additions & 2 deletions matsim/runtime/eqasim.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import matsim.runtime.java as java
import matsim.runtime.maven as maven

DEFAULT_EQASIM_VERSION = "1.3.1"
DEFAULT_EQASIM_VERSION = "1.5.0"
DEFAULT_EQASIM_BRANCH = "develop"
DEFAULT_EQASIM_COMMIT = "7cbe85b"
DEFAULT_EQASIM_COMMIT = "f1af717"

def configure(context):
context.stage("matsim.runtime.git")
Expand Down
35 changes: 22 additions & 13 deletions matsim/simulation/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,35 @@ def execute(context):
"--factor", str(0.8)
])

# Route population
eqasim.run(context, "org.eqasim.core.scenario.routing.RunPopulationRouting", [
"--config-path", "%sconfig.xml" % context.config("output_prefix"),
"--output-path", "%spopulation.xml.gz" % context.config("output_prefix"),
"--threads", context.config("processes"),
"--config:plans.inputPlansFile", "prepared_population.xml.gz"
])
assert os.path.exists("%s/%spopulation.xml.gz" % (context.path(), context.config("output_prefix")))

# Optionally, perform mode choice
if context.config("mode_choice"):
eqasim.run(context, "org.eqasim.ile_de_france.RunModeChoice", [
eqasim.run(context, "org.eqasim.ile_de_france.standalone_mode_choice.RunStandaloneModeChoice", [
"--config-path", "%sconfig.xml" % context.config("output_prefix"),
"--output-plans-path", "%spopulation.xml.gz" % context.config("output_prefix"),
"--config:standaloneModeChoice.outputDirectory", "mode_choice",
"--config:standaloneModeChoice.removePersonsWithNoValidAlternatives", "true",
"--config:global.numberOfThreads", context.config("processes"),
"--output-csv-path", "%stripModes.csv" % context.config("output_prefix")
"--write-output-csv-trips", "true",
"--skip-scenario-check", "true",
"--config:plans.inputPlansFile", "prepared_population.xml.gz"
])

assert os.path.exists("%s/mode_choice/output_plans.xml.gz" % context.path())
assert os.path.exists("%s/mode_choice/output_trips.csv" % context.path())
assert os.path.exists("%s/mode_choice/output_pt_legs.csv" % context.path())

shutil.copy("%s/mode_choice/output_plans.xml.gz" % context.path(),
"%s/%spopulation.xml.gz" % (context.path(), context.config("output_prefix")))
else:
# Route population
eqasim.run(context, "org.eqasim.core.scenario.routing.RunPopulationRouting", [
"--config-path", "%sconfig.xml" % context.config("output_prefix"),
"--output-path", "%spopulation.xml.gz" % context.config("output_prefix"),
"--threads", context.config("processes"),
"--config:plans.inputPlansFile", "prepared_population.xml.gz"
])

assert os.path.exists("%s/%stripModes.csv" % (context.path(), context.config("output_prefix")))
assert os.path.exists("%s/%spopulation.xml.gz" % (context.path(), context.config("output_prefix")))
assert os.path.exists("%s/%spopulation.xml.gz" % (context.path(), context.config("output_prefix")))

# Validate scenario
eqasim.run(context, "org.eqasim.core.scenario.validation.RunScenarioValidator", [
Expand Down
13 changes: 10 additions & 3 deletions matsim/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,18 @@ def end_person(self):
self._write_line('</person>')

def start_attributes(self):
self._require_scope(self.PERSON_SCOPE)
# We don't require any scope here because attributes can be almost anywhere
self._write_line('<attributes>')
self.indent += 1
# And we need to remember which scope we were in before starting the attributes
self._pre_attributes_scope = self.scope
self.scope = self.ATTRIBUTES_SCOPE

def end_attributes(self):
self._require_scope(self.ATTRIBUTES_SCOPE)
self.indent -= 1
self.scope = self.PERSON_SCOPE
# Resetting the scope that we were in before starting the attributes
self.scope = self._pre_attributes_scope
self._write_line('</attributes>')

def add_attribute(self, name, type, value):
Expand Down Expand Up @@ -143,7 +146,11 @@ def add_leg(self, mode, departure_time, travel_time):
self._write('mode="%s" ' % mode)
self._write('dep_time="%s" ' % self.time(departure_time))
self._write('trav_time="%s" ' % self.time(travel_time))
self._write('/>\n')
self._write('>\n')
self.start_attributes()
self.add_attribute('routingMode', 'java.lang.String', mode)
self.end_attributes()
self._write_line('</leg>')

class HouseholdsWriter(XmlWriter):
HOUSEHOLDS_SCOPE = 0
Expand Down
15 changes: 10 additions & 5 deletions synthesis/output.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shutil
import geopandas as gpd
import pandas as pd
import shapely.geometry as geo
Expand Down Expand Up @@ -131,15 +132,19 @@ def execute(context):

if context.config("mode_choice"):
df_mode_choice = pd.read_csv(
"{}/{}tripModes.csv".format(context.path("matsim.simulation.prepare"), output_prefix),
"{}/mode_choice/output_trips.csv".format(context.path("matsim.simulation.prepare"), output_prefix),
delimiter = ";")

df_mode_choice = df_mode_choice.rename(columns = {
"personId": "person_id", "tripId": "trip_index", "mode" : "mode"})


df_mode_choice = df_mode_choice.rename(columns={"person_trip_id": "trip_index"})
columns_to_keep = ["person_id", "trip_index"]
columns_to_keep.extend([c for c in df_trips.columns if c not in df_mode_choice.columns])
df_trips = df_trips[columns_to_keep]
df_trips = pd.merge(df_trips, df_mode_choice, on = [
"person_id", "trip_index"], how="left", validate = "one_to_one")

shutil.copy("%s/mode_choice/output_pt_legs.csv" % (context.path("matsim.simulation.prepare")),
"%s/%spt_legs.csv" % (output_path, output_prefix))

assert not np.any(df_trips["mode"].isna())

df_trips.to_csv("%s/%strips.csv" % (output_path, output_prefix), sep = ";", index = None, lineterminator = "\n")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_determinism.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _test_determinism_matsim(index, data_path, tmpdir):
#"ile_de_france_network.xml.gz": "5f10ec295b49d2bb768451c812955794",
"ile_de_france_households.xml.gz": "64a0c9fab72aad51bc6adb926a1c9d44",
#"ile_de_france_facilities.xml.gz": "5ad41afff9ae5c470082510b943e6778",
"ile_de_france_config.xml": "f374807f12a5151fe1efb6e9904e1a56"
"ile_de_france_config.xml": "481fac5fb3e7b90810caa38ff460c00a"
}

# activities.gpkg, trips.gpkg, meta.json,
Expand Down
Loading