Skip to content

Commit

Permalink
sampling in EDB for location choice
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensle committed Dec 16, 2023
1 parent a8e755f commit 564f07a
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions activitysim/abm/models/location_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,8 @@ def _location_sample(
logger.info("Running %s with %d persons" % (trace_label, len(choosers.index)))

sample_size = model_settings["SAMPLE_SIZE"]
if config.setting("disable_destination_sampling", False) or (
estimator and estimator.want_unsampled_alternatives
):
# FIXME interaction_sample will return unsampled complete alternatives with probs and pick_count
logger.info(
"Estimation mode for %s using unsampled alternatives short_circuit_choices"
% (trace_label,)
)
sample_size = 0
if estimator:
sample_size = model_settings.get("ESTIMATION_SAMPLE_SIZE", 0)

locals_d = {
"skims": skims,
Expand Down Expand Up @@ -470,6 +463,37 @@ def run_location_sample(
trace_label=trace_label,
)

# Hack to get shorter run times when you don't care about creating EDB for location choice models
if estimator:
# grabbing survey values
survey_persons = estimation.manager.get_survey_table("persons")
if "school_location" in trace_label:
survey_choices = survey_persons["school_zone_id"].reset_index()
elif ("workplace_location" in trace_label) and ("external" not in trace_label):
survey_choices = survey_persons["workplace_zone_id"].reset_index()
else:
return choices
survey_choices.columns = ["person_id", "alt_dest"]
survey_choices = survey_choices[
survey_choices["person_id"].isin(choices.index)
& (survey_choices.alt_dest > 0)
]
# merging survey destination into table if not available
joined_data = survey_choices.merge(
choices, on=["person_id", "alt_dest"], how="left", indicator=True
)
missing_rows = joined_data[joined_data["_merge"] == "left_only"]
missing_rows["pick_count"] = 1
if len(missing_rows) > 0:
new_choices = missing_rows[
["person_id", "alt_dest", "prob", "pick_count"]
].set_index("person_id")
choices = choices.append(new_choices, ignore_index=False).sort_index()
# making probability the mean of all other sampled destinations by person
choices["prob"] = choices["prob"].fillna(
choices.groupby("person_id")["prob"].transform("mean")
)

return choices


Expand Down

0 comments on commit 564f07a

Please sign in to comment.