1919 workflow ,
2020)
2121from 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
2423from activitysim .core .exceptions import InvalidTravelError
24+ from activitysim .core .util import assign_in_place , reindex
2525
2626logger = logging .getLogger (__name__ )
2727
@@ -127,10 +127,11 @@ def get_tour_satisfaction(candidates, participate):
127127
128128def 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