Skip to content

Commit

Permalink
estimation sampling for non-mandatory and joint tours
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensle committed Oct 8, 2024
1 parent 8d80e2e commit 1459e48
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
49 changes: 42 additions & 7 deletions activitysim/abm/models/util/tour_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from activitysim.abm.models.util import logsums as logsum
from activitysim.abm.tables.size_terms import tour_destination_size_terms
from activitysim.core import config, los, simulate, tracing, workflow
from activitysim.core import estimation, config, los, simulate, tracing, workflow
from activitysim.core.configuration.logit import TourLocationComponentSettings
from activitysim.core.interaction_sample import interaction_sample
from activitysim.core.interaction_sample_simulate import interaction_sample_simulate
Expand Down Expand Up @@ -87,15 +87,17 @@ def _destination_sample(
logger.info("running %s with %d tours", trace_label, len(choosers))

sample_size = model_settings.SAMPLE_SIZE
if state.settings.disable_destination_sampling or (
estimator and estimator.want_unsampled_alternatives
):
# FIXME interaction_sample will return unsampled complete alternatives with probs and pick_count
if estimator:
sample_size = model_settings.ESTIMATION_SAMPLE_SIZE
logger.info(
"Estimation mode for %s using unsampled alternatives short_circuit_choices"
% (trace_label,)
f"Estimation mode for {trace_label} using sample size of {sample_size}"
)

if state.settings.disable_destination_sampling:
sample_size = 0
logger.info(
f"SAMPLE_SIZE set to 0 for {trace_label} because disable_destination_sampling is set"
)

locals_d = {
"skims": skims,
Expand Down Expand Up @@ -179,6 +181,39 @@ def destination_sample(
trace_label=trace_label,
)

# adding observed choice to alt set when running in estimation mode
if estimator:
# grabbing survey values
survey_tours = estimation.manager.get_survey_table("tours")
survey_choices = survey_tours[["destination", "person_id"]].reset_index()
survey_choices.columns = ["tour_id", alt_dest_col_name, "person_id"]
survey_choices = survey_choices[
survey_choices["tour_id"].isin(choices.index)
& (survey_choices[alt_dest_col_name] > 0)
]
# merging survey destination into table if not available
joined_data = survey_choices.merge(
choices,
on=["tour_id", alt_dest_col_name, "person_id"],
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[
["tour_id", alt_dest_col_name, "prob", "pick_count", "person_id"]
].set_index("tour_id")
choices = choices.append(new_choices, ignore_index=False).sort_index()
# making prob 0 for missing rows so it does not influence model decision
choices["prob"] = choices["prob"].fillna(0)
# sort by tour_id and alt_dest
choices = (
choices.reset_index()
.sort_values(by=["tour_id", alt_dest_col_name])
.set_index("tour_id")
)

return choices


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
EDB_ALTS_FILE_FORMAT: compact
EDB_FILETYPE: csv
EDB_ALTS_FILE_FORMAT: compact # options: compact, verbose
EDB_FILETYPE: csv # options: csv, parquet, pkl

enable: True

Expand Down

0 comments on commit 1459e48

Please sign in to comment.