Skip to content

Commit

Permalink
Trip Destination Alternatives Preprocessor (#865)
Browse files Browse the repository at this point in the history
* trip_destination alts preprocessor

* non_hh_veh cat, drop unused cols for alts

* blacken

* adding missed alts columns used in xborder model

* remove unneeded addition to categorical
  • Loading branch information
dhensle authored May 6, 2024
1 parent e309b84 commit 1cb48c7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
13 changes: 11 additions & 2 deletions activitysim/abm/models/trip_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class TripDestinationSettings(LocationComponentSettings, extra="forbid"):
PRIMARY_DEST: str = "tour_leg_dest" # must be created in preprocessor
REDUNDANT_TOURS_MERGED_CHOOSER_COLUMNS: list[str] | None = None
preprocessor: PreprocessorSettings | None = None
alts_preprocessor: PreprocessorSettings | None = None
CLEANUP: bool
fail_some_trips_for_testing: bool = False
"""This setting is used by testing code to force failed trip_destination."""
Expand Down Expand Up @@ -1245,6 +1246,7 @@ def run_trip_destination(
state.filesystem, model_settings_file_name
)
preprocessor_settings = model_settings.preprocessor
alts_preprocessor_settings = model_settings.alts_preprocessor
logsum_settings = state.filesystem.read_model_settings(
model_settings.LOGSUM_SETTINGS
)
Expand Down Expand Up @@ -1334,8 +1336,6 @@ def run_trip_destination(
# returns a series of size_terms for each chooser's dest_zone_id and purpose with chooser index
size_term_matrix = DataFrameMatrix(alternatives)

# don't need size terms in alternatives, just zone_id index
alternatives = alternatives.drop(alternatives.columns, axis=1)
alternatives.index.name = model_settings.ALT_DEST_COL_NAME

sample_list = []
Expand Down Expand Up @@ -1369,6 +1369,15 @@ def run_trip_destination(
trace_label=nth_trace_label,
)

if alts_preprocessor_settings:
expressions.assign_columns(
state,
df=alternatives,
model_settings=alts_preprocessor_settings,
locals_dict=locals_dict,
trace_label=tracing.extend_trace_label(nth_trace_label, "alts"),
)

if isinstance(
nth_trips["trip_period"].dtype, pd.api.types.CategoricalDtype
):
Expand Down
2 changes: 2 additions & 0 deletions activitysim/abm/models/vehicle_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ def vehicle_allocation(
choices.loc[choices["alt_choice"] == alt, "choice"] = choosers.loc[
choices["alt_choice"] == alt, alt
]

# set choice for non-household vehicle option
choices.loc[
choices["alt_choice"] == alts_from_spec[-1], "choice"
] = alts_from_spec[-1]
Expand Down
9 changes: 9 additions & 0 deletions activitysim/core/interaction_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ def _interaction_sample(
sharrow_enabled=sharrow_enabled,
)

alternatives = util.drop_unused_columns(
alternatives,
spec,
locals_d,
custom_chooser=None,
sharrow_enabled=sharrow_enabled,
additional_columns=["tdd", "origin_destination"],
)

if sharrow_enabled:
(
interaction_utilities,
Expand Down
9 changes: 9 additions & 0 deletions activitysim/core/interaction_sample_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ def _interaction_sample_simulate(
sharrow_enabled=sharrow_enabled,
)

alternatives = util.drop_unused_columns(
alternatives,
spec,
locals_d,
custom_chooser=None,
sharrow_enabled=sharrow_enabled,
additional_columns=["tdd", "origin_destination"],
)

interaction_df = alternatives.join(choosers, how="left", rsuffix="_chooser")
logger.info(
f"{trace_label} end merging choosers and alternatives to create interaction_df"
Expand Down

0 comments on commit 1cb48c7

Please sign in to comment.