def mandatory_scheduling(set_random_seed, mandatory_tours_merged, tdd_alts, tdd_school_spec, tdd_work_spec, mandatory_scheduling_settings, chunk_size, trace_hh_id): """ This model predicts the departure time and duration of each activity for mandatory tours """ tours = mandatory_tours_merged.to_frame() alts = tdd_alts.to_frame() if trace_hh_id: # register non_mandatory_tours so we can slice utilities tracing.register_tours(tours, trace_hh_id) constants = get_model_constants(mandatory_scheduling_settings) school_spec = tdd_school_spec.to_frame() school_tours = tours[tours.tour_type == "school"] logger.info("Running mandatory_scheduling school_tours with %d tours" % len(school_tours)) school_choices = vectorize_tour_scheduling( school_tours, alts, school_spec, constants, chunk_size, trace_label='mandatory_scheduling.school') work_spec = tdd_work_spec.to_frame() work_tours = tours[tours.tour_type == "work"] logger.info("Running %d work tour scheduling choices" % len(work_tours)) work_choices = vectorize_tour_scheduling( work_tours, alts, work_spec, constants, chunk_size, trace_label='mandatory_scheduling.work') choices = pd.concat([school_choices, work_choices]) tracing.print_summary('mandatory_scheduling tour_departure_and_duration', choices, describe=True) orca.add_column("mandatory_tours", "tour_departure_and_duration", choices) if trace_hh_id: tracing.trace_df(orca.get_table('mandatory_tours').to_frame(), label="mandatory_tours", slicer='person_id', index_label='tour', columns=None, warn_if_empty=True)
def destination_choice(set_random_seed, non_mandatory_tours_merged, skim_dict, destination_choice_spec, destination_choice_settings, destination_size_terms, chunk_size, trace_hh_id): """ Given the tour generation from the above, each tour needs to have a destination, so in this case tours are the choosers (with the associated person that's making the tour) """ # choosers are tours - in a sense tours are choosing their destination choosers = non_mandatory_tours_merged.to_frame() alternatives = destination_size_terms.to_frame() spec = destination_choice_spec.to_frame() constants = get_model_constants(destination_choice_settings) if trace_hh_id: # register non_mandatory_tours so we can slice utilities tracing.register_tours(choosers, trace_hh_id) # create wrapper with keys for this lookup - in this case there is a TAZ in the choosers # and a TAZ in the alternatives which get merged during interaction # the skims will be available under the name "skims" for any @ expressions skims = skim_dict.wrap("TAZ", "TAZ_r") locals_d = { 'skims': skims } if constants is not None: locals_d.update(constants) logger.info("Running destination_choice with %d non_mandatory_tours" % len(choosers.index)) choices_list = [] # segment by trip type and pick the right spec for each person type for name, segment in choosers.groupby('tour_type'): # FIXME - there are two options here escort with kids and without kludge_name = name if name == "escort": logging.error("destination_choice escort not implemented - running shopping instead") kludge_name = "shopping" # the segment is now available to switch between size terms locals_d['segment'] = kludge_name # FIXME - no point in considering impossible alternatives alternatives_segment = alternatives[alternatives[kludge_name] > 0] logger.info("Running segment '%s' of %d tours %d alternatives" % (name, len(segment), len(alternatives_segment))) # name index so tracing knows how to slice segment.index.name = 'tour_id' choices = asim.interaction_simulate(segment, alternatives_segment, spec[[kludge_name]], skims=skims, locals_d=locals_d, sample_size=50, chunk_size=chunk_size, trace_label='destination.%s' % name) choices_list.append(choices) choices = pd.concat(choices_list) # FIXME - can there be null destinations? if choices.isnull().any(): logger.error("destination_choice had %s null destinations" % choices.isnull().sum()) assert choices.isnull().sum() == 0 tracing.print_summary('destination', choices, describe=True) # every trip now has a destination which is the index from the # alternatives table - in this case it's the destination taz orca.add_column("non_mandatory_tours", "destination", choices) if trace_hh_id: tracing.trace_df(orca.get_table('non_mandatory_tours').to_frame(), label="destination", slicer='person_id', index_label='tour', columns=None, warn_if_empty=True)
def tour_mode_choice_simulate(tours_merged, tour_mode_choice_spec, tour_mode_choice_settings, skims, stacked_skims, omx_file, trace_hh_id): trace_label = trace_hh_id and 'tour_mode_choice' tours = tours_merged.to_frame() nest_spec = get_logit_model_settings(tour_mode_choice_settings) constants = get_model_constants(tour_mode_choice_settings) if trace_hh_id: tracing.register_tours(tours, trace_hh_id) logger.info("Running tour_mode_choice_simulate with %d tours" % len(tours.index)) tracing.print_summary('tour_mode_choice_simulate tour_type', tours.tour_type, value_counts=True) if trace_hh_id: tracing.trace_df(tour_mode_choice_spec, tracing.extend_trace_label(trace_label, 'spec'), slicer='NONE', transpose=False) choices_list = [] for tour_type, segment in tours.groupby('tour_type'): # if tour_type != 'work': # continue logger.info("running tour_type '%s'" % tour_type) orig_key = 'TAZ' dest_key = 'destination' # name index so tracing knows how to slice segment.index.name = 'tour_id' logger.info("tour_mode_choice_simulate running %s tour_type '%s'" % ( len(segment.index), tour_type, )) # FIXME - check that destination is not null (patch_mandatory_tour_destination not run?) spec = get_segment_and_unstack(tour_mode_choice_spec, tour_type) if trace_hh_id: tracing.trace_df(spec, tracing.extend_trace_label( trace_label, 'spec.%s' % tour_type), slicer='NONE', transpose=False) choices = _mode_choice_simulate(segment, skims, stacked_skims, orig_key=orig_key, dest_key=dest_key, spec=spec, constants=constants, nest_spec=nest_spec, omx=omx_file, trace_label=tracing.extend_trace_label( trace_label, tour_type), trace_choice_name='tour_mode_choice') tracing.print_summary('tour_mode_choice_simulate %s choices' % tour_type, choices, value_counts=True) choices_list.append(choices) # FIXME - force garbage collection mem = asim.memory_info() logger.info('memory_info tour_type %s, %s' % (tour_type, mem)) choices = pd.concat(choices_list) tracing.print_summary('tour_mode_choice_simulate all tour type choices', choices, value_counts=True) orca.add_column("tours", "mode", choices) if trace_hh_id: trace_columns = ['mode'] tracing.trace_df(orca.get_table('tours').to_frame(), label=tracing.extend_trace_label(trace_label, 'mode'), slicer='tour_id', index_label='tour', columns=trace_columns, warn_if_empty=True) # FIXME - this forces garbage collection asim.memory_info()