Skip to content

Commit 53d97f4

Browse files
authored
Merge branch 'main' into feature/explicit-chunk-mode-choice
2 parents 8dc8ca4 + 52efeaf commit 53d97f4

65 files changed

Lines changed: 9526 additions & 898 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎activitysim/abm/models/disaggregate_accessibility.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
from activitysim.abm.models.util import tour_destination
1616
from activitysim.abm.tables import shadow_pricing
1717
from activitysim.core import estimation, los, tracing, util, workflow
18-
from activitysim.core.configuration.base import PreprocessorSettings, PydanticReadable
18+
from activitysim.core.configuration.base import (
19+
ComputeSettings,
20+
PreprocessorSettings,
21+
PydanticReadable,
22+
)
1923
from activitysim.core.configuration.logit import TourLocationComponentSettings
2024
from activitysim.core.expressions import assign_columns
2125

@@ -184,6 +188,8 @@ class DisaggregateAccessibilitySettings(PydanticReadable, extra="forbid"):
184188
If not supplied or None, will default to the chunk size in the location choice model settings.
185189
"""
186190

191+
compute_settings: ComputeSettings | None = None
192+
187193

188194
def read_disaggregate_accessibility_yaml(
189195
state: workflow.State, file_name
@@ -795,6 +801,11 @@ def get_disaggregate_logsums(
795801
if disagg_model_settings.explicit_chunk is not None:
796802
model_settings.explicit_chunk = disagg_model_settings.explicit_chunk
797803

804+
# Can set compute settings for disaggregate accessibility
805+
# Otherwise this will be set to whatever is in the location model settings
806+
if disagg_model_settings.compute_settings is not None:
807+
model_settings.compute_settings = disagg_model_settings.compute_settings
808+
798809
# Include the suffix tags to pass onto downstream logsum models (e.g., tour mode choice)
799810
if model_settings.LOGSUM_SETTINGS:
800811
suffixes = util.concat_suffix_dict(disagg_model_settings.suffixes)

‎activitysim/abm/models/joint_tour_participation.py‎

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
workflow,
2020
)
2121
from activitysim.core.configuration.base import ComputeSettings, PreprocessorSettings
22-
from activitysim.core.configuration.logit import LogitComponentSettings
23-
from activitysim.core.util import assign_in_place, reindex
22+
from activitysim.core.configuration.logit import LogitComponentSettings, LogitNestSpec
2423
from activitysim.core.exceptions import InvalidTravelError
24+
from activitysim.core.util import assign_in_place, reindex
2525

2626
logger = logging.getLogger(__name__)
2727

@@ -127,10 +127,11 @@ def get_tour_satisfaction(candidates, participate):
127127

128128
def participants_chooser(
129129
state: workflow.State,
130-
probs: pd.DataFrame,
130+
probs_or_utils: pd.DataFrame,
131131
choosers: pd.DataFrame,
132132
spec: pd.DataFrame,
133133
trace_label: str,
134+
nest_spec: dict | LogitNestSpec | None = None,
134135
) -> tuple[pd.Series, pd.Series]:
135136
"""
136137
custom alternative to logit.make_choices for simulate.simple_simulate
@@ -147,9 +148,10 @@ def participants_chooser(
147148
148149
Parameters
149150
----------
150-
probs : pandas.DataFrame
151+
probs_or_utils : pandas.DataFrame
151152
Rows for choosers and columns for the alternatives from which they
152-
are choosing. Values are expected to be valid probabilities across
153+
are choosing. If running with explicit_error_terms, these are utilities.
154+
Otherwise, values are expected to be valid probabilities across
153155
each row, e.g. they should sum to 1.
154156
choosers : pandas.dataframe
155157
simple_simulate choosers df
@@ -166,7 +168,7 @@ def participants_chooser(
166168
167169
"""
168170

169-
assert probs.index.equals(choosers.index)
171+
assert probs_or_utils.index.equals(choosers.index)
170172

171173
# choice is boolean (participate or not)
172174
model_settings = JointTourParticipationSettings.read_settings_file(
@@ -202,7 +204,7 @@ def participants_chooser(
202204
"%s max iterations exceeded (%s).", trace_label, MAX_ITERATIONS
203205
)
204206
diagnostic_cols = ["tour_id", "household_id", "composition", "adult"]
205-
unsatisfied_candidates = candidates[diagnostic_cols].join(probs)
207+
unsatisfied_candidates = candidates[diagnostic_cols].join(probs_or_utils)
206208
state.tracing.write_csv(
207209
unsatisfied_candidates,
208210
file_name="%s.UNSATISFIED" % trace_label,
@@ -215,9 +217,31 @@ def participants_chooser(
215217
f"Forcing joint tour participation for {num_tours_remaining} tours."
216218
)
217219
# anybody with probability > 0 is forced to join the joint tour
218-
probs[choice_col] = np.where(probs[choice_col] > 0, 1, 0)
219-
non_choice_col = [col for col in probs.columns if col != choice_col][0]
220-
probs[non_choice_col] = 1 - probs[choice_col]
220+
if state.settings.use_explicit_error_terms:
221+
# need "is valid choice" such that we certainly choose those with non-zero values,
222+
# and do not choose others.
223+
probs_or_utils[choice_col] = np.where(
224+
probs_or_utils[choice_col] > logit.UTIL_MIN,
225+
logit.UTIL_LARGE_ENOUGH,
226+
logit.UTIL_UNAVAILABLE,
227+
)
228+
non_choice_col = [
229+
col for col in probs_or_utils.columns if col != choice_col
230+
][0]
231+
probs_or_utils[non_choice_col] = np.where(
232+
probs_or_utils[choice_col] <= logit.UTIL_MIN,
233+
logit.UTIL_LARGE_ENOUGH,
234+
logit.UTIL_UNAVAILABLE,
235+
)
236+
else:
237+
probs_or_utils[choice_col] = np.where(
238+
probs_or_utils[choice_col] > 0, 1, 0
239+
)
240+
non_choice_col = [
241+
col for col in probs_or_utils.columns if col != choice_col
242+
][0]
243+
probs_or_utils[non_choice_col] = 1 - probs_or_utils[choice_col]
244+
221245
if iter > MAX_ITERATIONS + 1:
222246
raise InvalidTravelError(
223247
f"{num_tours_remaining} tours could not be satisfied even with forcing participation"
@@ -227,9 +251,21 @@ def participants_chooser(
227251
f"{num_tours_remaining} tours could not be satisfied after {iter} iterations"
228252
)
229253

230-
choices, rands = logit.make_choices(
231-
state, probs, trace_label=trace_label, trace_choosers=choosers
232-
)
254+
if state.settings.use_explicit_error_terms:
255+
choices, rands = logit.make_choices_utility_based(
256+
state,
257+
probs_or_utils,
258+
trace_label=trace_label,
259+
trace_choosers=choosers,
260+
nest_spec=nest_spec,
261+
)
262+
else:
263+
choices, rands = logit.make_choices(
264+
state,
265+
probs_or_utils,
266+
trace_label=trace_label,
267+
trace_choosers=choosers,
268+
)
233269
participate = choices == PARTICIPATE_CHOICE
234270

235271
# satisfaction indexed by tour_id
@@ -252,7 +288,7 @@ def participants_chooser(
252288
rands_list.append(rands[satisfied])
253289

254290
# remove candidates of satisfied tours
255-
probs = probs[~satisfied]
291+
probs_or_utils = probs_or_utils[~satisfied]
256292
candidates = candidates[~satisfied]
257293

258294
logger.debug(
@@ -401,6 +437,8 @@ def joint_tour_participation(
401437
if i not in model_settings.compute_settings.protect_columns:
402438
model_settings.compute_settings.protect_columns.append(i)
403439

440+
custom_chooser = participants_chooser
441+
404442
choices = simulate.simple_simulate_by_chunk_id(
405443
state,
406444
choosers=candidates,
@@ -409,7 +447,7 @@ def joint_tour_participation(
409447
locals_d=constants,
410448
trace_label=trace_label,
411449
trace_choice_name="participation",
412-
custom_chooser=participants_chooser,
450+
custom_chooser=custom_chooser,
413451
estimator=estimator,
414452
compute_settings=model_settings.compute_settings,
415453
)

0 commit comments

Comments
 (0)