Skip to content

Park-and-ride lot choice and capacity#1001

Open
dhensle wants to merge 54 commits into
ActivitySim:mainfrom
RSGInc:pnr_capacity
Open

Park-and-ride lot choice and capacity#1001
dhensle wants to merge 54 commits into
ActivitySim:mainfrom
RSGInc:pnr_capacity

Conversation

@dhensle

@dhensle dhensle commented Oct 8, 2025

Copy link
Copy Markdown
Contributor

Code changes for #965

@jpn--

jpn-- commented Jun 23, 2026

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

@jpn-- jpn-- left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly good. That several agencies have collaborated and tested this already gives some extra confidence. Only a couple minor bugs/concerns to address.

persons = state.get_dataframe("persons")

# don't know a-priori how many park-and-ride tours there are at the start of the model run
# giving the buffer a size equal to the number of persons should be sufficient

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably fine most of the time, but in the outside chance that the number of PnR tours does exceed the number of persons, the model is going to crash hard later, probably just after line 159 where pad will become negative.

synced_choices = synced_choices.sort_index()

# now append any additional rows need to get size back to original length
pad = len(self.shared_pnr_choice) - len(synced_choices)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should intercept here if pad is negative, maybe crash gracefully explaining that the number of PnR tours was unexpectedly greater than the number of persons, instead of crashing with an arcane "negative dimensions are not allowed" error from deep in this code. (see initialization comment below)

.astype(int)
)

elif self.model_settings.RESAMPLE_STRATEGY == "random":

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no testing of RESAMPLE_STRATEGY == "random"


# count the total number of pnr choices being resimulated
pnr_counts = (
current_sample.pnr_zone_id.value_counts()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried making a simple test for "random" but it failed here, current_sample seems to be a pd.Series.

The correct code might be:

pnr_counts = (
    tours_in_cap_zones.loc[current_sample.index, "pnr_zone_id"]
    .value_counts()
    .reindex(self.shared_pnr_occupancy_df.index)
    .fillna(0)
    .astype(int)
)

----------
choices : pandas.Series
zone id of location choice indexed by person_id
segment_ids : pandas.Series

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function does not take segment_ids

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements explicit Park-and-Ride (PnR) lot choice and capacity-constrained iteration with tour mode choice, and extends skimming/logsum/matrix-writing infrastructure so PnR auto/transit legs can be modeled and reported without relying on expensive precomputed PnR skim sets.

Changes:

  • Added a new park_and_ride_lot_choice model plus ParkAndRideCapacity helper to iterate tour mode choice until PnR lots are within capacity.
  • Extended skim plumbing (flow, logsums, tour scheduling logsums) to support origin→lot and lot→destination skims and optional inclusion of PnR in logsum computation.
  • Updated trip matrix writing to support table-specific origin/destination columns (enabling PnR leg matrix outputs) and added tests for both PnR iteration and matrix writing behavior.

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
activitysim/core/util.py Keeps pnr_lot_dest_col_name columns when dropping unused chooser columns.
activitysim/core/steps/output.py Adjusts data dictionary text formatting width calculation.
activitysim/core/mp_tasks.py Allocates shared multiprocessing buffers for PnR capacity iteration when configured.
activitysim/core/flow.py Adds support for PnR lot destination column and additional PnR skim relationships.
activitysim/core/configuration/logit.py Adds settings flags for at-work PnR and optional PnR inclusion in logsums.
activitysim/abm/test/test_misc/test_tour_mode_choice_simulate.py New integration-style tests for tour mode choice with/without PnR iteration.
activitysim/abm/test/test_misc/test_park_and_ride_models.py New unit tests for PnR lot choice filtering, utilities, and capacity synchronization logic.
activitysim/abm/tables/disaggregate_accessibility.py Updates nearest accessibility zone helper signature to accept model settings.
activitysim/abm/models/util/vectorize_tour_scheduling.py Integrates optional PnR lot choice into tour scheduling logsum computation and centralizes skim setup.
activitysim/abm/models/util/test/test_write_matrices.py New tests validating matrix writing with custom OD columns for PnR legs.
activitysim/abm/models/util/school_escort_tours_trips.py Tightens integer casting to explicit int64 dtype.
activitysim/abm/models/util/park_and_ride_capacity.py New multiprocessing-aware PnR capacity accounting and chooser resampling logic.
activitysim/abm/models/util/logsums.py Adds shared setup_skims helper and optional PnR lot choice during logsum creation.
activitysim/abm/models/trip_matrices.py Supports per-table origin/destination columns and accumulating repeated table names into a single matrix.
activitysim/abm/models/tour_mode_choice.py Adds iterative PnR capacity-constrained tour mode choice and reuses shared skim setup.
activitysim/abm/models/park_and_ride_lot_choice.py New PnR lot choice model implementation and settings schema.
activitysim/abm/models/disaggregate_accessibility.py Adds NEAREST_ZONE_SKIM configuration for nearest-zone selection behavior.
activitysim/abm/models/atwork_subtour_mode_choice.py Adds optional at-work subtour PnR lot choice integration and shared skim setup.
activitysim/abm/models/init.py Registers the new park_and_ride_lot_choice model module.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

with open(state.get_output_file_path(txt_format), "w") as output_file:
# get max schema column widths from omnibus table
col_width = {c: schema_df[c].str.len().max() + 2 for c in schema_df}
col_width = {c: int(schema_df[c].str.len().max()) + 2 for c in schema_df}
Comment on lines +162 to +169
def return_no_choices(state, choosers: pd.DataFrame, original_index=None) -> pd.Series:
logger.debug(
"No choosers with transit accessible destinations found. Returning -1 as park-and-ride lot choice."
)
# need to drop rng channel that we created before trn_accessible_choosers
state.get_rn_generator().drop_channel("pnr_lot_choice")
index = choosers.index if original_index is None else original_index
return pd.Series(data=-1, index=index)
Comment on lines +69 to 85
if "pnr_zone_id" in subtours_merged.columns:
if model_settings.run_atwork_pnr_lot_choice:
subtours_merged["pnr_zone_id"] = run_park_and_ride_lot_choice(
state,
choosers=subtours_merged.copy(),
land_use=state.get_dataframe("land_use"),
network_los=network_los,
model_settings=None,
choosers_dest_col_name="destination",
choosers_origin_col_name="workplace_zone_id",
estimator=None,
pnr_capacity_cls=None,
trace_label=tracing.extend_trace_label(trace_label, "pnr_lot_choice"),
)
else:
subtours_merged["pnr_zone_id"].fillna(-1, inplace=True)

Comment on lines +158 to +165
# now append any additional rows need to get size back to original length
pad = len(self.shared_pnr_choice) - len(synced_choices)
new_arr_values = np.concatenate(
[
synced_choices["pnr_zone_id"].to_numpy(np.int64),
np.zeros(pad, dtype=np.int64),
]
)
Comment on lines 18 to 20
def find_nearest_accessibility_zone(
state: workflow.State, choosers, accessibility_df, method="skims"
state: workflow.State, model_settings, choosers, accessibility_df, method="skims"
):
model_settings_file_name: str = "park_and_ride_lot_choice.yaml",
pnr_capacity_cls: ParkAndRideCapacity | None = None,
trace_label: str = "park_and_ride_lot_choice",
) -> None:
Comment on lines +191 to +196
if model_settings is None:
model_settings = ParkAndRideLotChoiceSettings.read_settings_file(
state.filesystem,
"park_and_ride_lot_choice.yaml",
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

4 participants