From 1cb48c7acc2c377f4822e2521a19503842aaa495 Mon Sep 17 00:00:00 2001 From: David Hensle <51132108+dhensle@users.noreply.github.com> Date: Mon, 6 May 2024 15:22:08 -0700 Subject: [PATCH] Trip Destination Alternatives Preprocessor (#865) * 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 --- activitysim/abm/models/trip_destination.py | 13 +++++++++++-- activitysim/abm/models/vehicle_allocation.py | 2 ++ activitysim/core/interaction_sample.py | 9 +++++++++ activitysim/core/interaction_sample_simulate.py | 9 +++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/activitysim/abm/models/trip_destination.py b/activitysim/abm/models/trip_destination.py index 9789f805e..8a3320a20 100644 --- a/activitysim/abm/models/trip_destination.py +++ b/activitysim/abm/models/trip_destination.py @@ -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.""" @@ -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 ) @@ -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 = [] @@ -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 ): diff --git a/activitysim/abm/models/vehicle_allocation.py b/activitysim/abm/models/vehicle_allocation.py index 9dcaf8c71..632839b11 100644 --- a/activitysim/abm/models/vehicle_allocation.py +++ b/activitysim/abm/models/vehicle_allocation.py @@ -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] diff --git a/activitysim/core/interaction_sample.py b/activitysim/core/interaction_sample.py index 5a05b7e8b..6f4c9a6ea 100644 --- a/activitysim/core/interaction_sample.py +++ b/activitysim/core/interaction_sample.py @@ -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, diff --git a/activitysim/core/interaction_sample_simulate.py b/activitysim/core/interaction_sample_simulate.py index 3d729ad49..a317d92af 100644 --- a/activitysim/core/interaction_sample_simulate.py +++ b/activitysim/core/interaction_sample_simulate.py @@ -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"