Exemple #1
0
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)
Exemple #2
0
def auto_ownership_simulate(set_random_seed, households_merged,
                            auto_ownership_spec, auto_ownership_settings,
                            trace_hh_id):
    """
    Auto ownership is a standard model which predicts how many cars a household
    with given characteristics owns
    """

    logger.info("Running auto_ownership_simulate with %d households" %
                len(households_merged))

    nest_spec = get_logit_model_settings(auto_ownership_settings)
    constants = get_model_constants(auto_ownership_settings)

    choices = asim.simple_simulate(choosers=households_merged.to_frame(),
                                   spec=auto_ownership_spec,
                                   nest_spec=nest_spec,
                                   locals_d=constants,
                                   trace_label=trace_hh_id
                                   and 'auto_ownership',
                                   trace_choice_name='auto_ownership')

    tracing.print_summary('auto_ownership', choices, value_counts=True)

    orca.add_column('households', 'auto_ownership', choices)

    add_dependent_columns('households', 'households_autoown')

    if trace_hh_id:
        trace_columns = ['auto_ownership'
                         ] + orca.get_table('households_autoown').columns
        tracing.trace_df(orca.get_table('households').to_frame(),
                         label='auto_ownership',
                         columns=trace_columns,
                         warn_if_empty=True)
Exemple #3
0
def cdap_simulate(persons_merged, cdap_settings, cdap_indiv_spec,
                  cdap_interaction_coefficients,
                  cdap_fixed_relative_proportions, hh_chunk_size, trace_hh_id):
    """
    CDAP stands for Coordinated Daily Activity Pattern, which is a choice of
    high-level activity pattern for each person, in a coordinated way with other
    members of a person's household.

    Because Python requires vectorization of computation, there are some specialized
    routines in the cdap directory of activitysim for this purpose.  This module
    simply applies those utilities using the simulation framework.
    """

    persons_df = persons_merged.to_frame()

    constants = get_model_constants(cdap_settings)

    logger.info("Running cdap_simulate with %d persons" %
                len(persons_df.index))

    choices = cdap.run_cdap(
        persons=persons_df,
        cdap_indiv_spec=cdap_indiv_spec,
        cdap_interaction_coefficients=cdap_interaction_coefficients,
        cdap_fixed_relative_proportions=cdap_fixed_relative_proportions,
        locals_d=constants,
        chunk_size=hh_chunk_size,
        trace_hh_id=trace_hh_id,
        trace_label='cdap')

    tracing.print_summary('cdap_activity',
                          choices.cdap_activity,
                          value_counts=True)

    print pd.crosstab(persons_df.ptype, choices.cdap_activity, margins=True)

    choices = choices.reindex(persons_merged.index)
    orca.add_column("persons", "cdap_activity", choices.cdap_activity)
    orca.add_column("persons", "cdap_rank", choices.cdap_rank)

    if trace_hh_id:

        tracing.trace_df(orca.get_table('persons_merged').to_frame(),
                         label="cdap",
                         columns=['ptype', 'cdap_rank', 'cdap_activity'],
                         warn_if_empty=True)
def mandatory_tour_frequency(set_random_seed, persons_merged,
                             mandatory_tour_frequency_spec,
                             mandatory_tour_frequency_settings, trace_hh_id):
    """
    This model predicts the frequency of making mandatory trips (see the
    alternatives above) - these trips include work and school in some combination.
    """

    choosers = persons_merged.to_frame()
    # filter based on results of CDAP
    choosers = choosers[choosers.cdap_activity == 'M']
    logger.info("Running mandatory_tour_frequency with %d persons" %
                len(choosers))

    nest_spec = get_logit_model_settings(mandatory_tour_frequency_settings)
    constants = get_model_constants(mandatory_tour_frequency_settings)

    choices = asim.simple_simulate(
        choosers,
        spec=mandatory_tour_frequency_spec,
        nest_spec=nest_spec,
        locals_d=constants,
        trace_label=trace_hh_id and 'mandatory_tour_frequency',
        trace_choice_name='mandatory_tour_frequency')

    # convert indexes to alternative names
    choices = pd.Series(mandatory_tour_frequency_spec.columns[choices.values],
                        index=choices.index).reindex(
                            persons_merged.local.index)

    tracing.print_summary('mandatory_tour_frequency',
                          choices,
                          value_counts=True)

    orca.add_column("persons", "mandatory_tour_frequency", choices)

    if trace_hh_id:
        trace_columns = ['mandatory_tour_frequency']
        tracing.trace_df(orca.get_table('persons_merged').to_frame(),
                         label="mandatory_tour_frequency",
                         columns=trace_columns,
                         warn_if_empty=True)
Exemple #5
0
def non_mandatory_scheduling(set_random_seed, non_mandatory_tours_merged,
                             tdd_alts, tdd_non_mandatory_spec,
                             non_mandatory_scheduling_settings, chunk_size,
                             trace_hh_id):
    """
    This model predicts the departure time and duration of each activity for
    non-mandatory tours
    """

    tours = non_mandatory_tours_merged.to_frame()

    logger.info("Running non_mandatory_scheduling with %d tours" % len(tours))

    constants = get_model_constants(non_mandatory_scheduling_settings)

    spec = tdd_non_mandatory_spec.to_frame()
    alts = tdd_alts.to_frame()

    choices = vectorize_tour_scheduling(tours,
                                        alts,
                                        spec,
                                        constants,
                                        chunk_size,
                                        trace_label='non_mandatory_scheduling')

    tracing.print_summary(
        'non_mandatory_scheduling tour_departure_and_duration',
        choices,
        describe=True)

    orca.add_column("non_mandatory_tours", "tour_departure_and_duration",
                    choices)

    if trace_hh_id:
        tracing.trace_df(orca.get_table('non_mandatory_tours').to_frame(),
                         label="non_mandatory_tours",
                         slicer='person_id',
                         index_label='tour_id',
                         columns=None,
                         warn_if_empty=True)
Exemple #6
0
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 school_location_simulate(set_random_seed, persons_merged,
                             school_location_spec, school_location_settings,
                             skims, destination_size_terms, chunk_size,
                             trace_hh_id):
    """
    The school location model predicts the zones in which various people will
    go to school.
    """

    choosers = persons_merged.to_frame()
    alternatives = destination_size_terms.to_frame()

    constants = get_model_constants(school_location_settings)

    logger.info("Running school_location_simulate with %d persons" %
                len(choosers))

    # set the 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.set_keys("TAZ", "TAZ_r")

    locals_d = {'skims': skims}
    if constants is not None:
        locals_d.update(constants)

    choices_list = []
    for school_type in ['university', 'highschool', 'gradeschool']:

        locals_d['segment'] = school_type

        choosers_segment = choosers[choosers["is_" + school_type]]

        # FIXME - no point in considering impossible alternatives
        alternatives_segment = alternatives[alternatives[school_type] > 0]

        logger.info(
            "school_type %s:  %s persons %s alternatives" %
            (school_type, len(choosers_segment), len(alternatives_segment)))

        if len(choosers_segment.index) > 0:

            choices = asim.interaction_simulate(
                choosers_segment,
                alternatives_segment,
                spec=school_location_spec[[school_type]],
                skims=skims,
                locals_d=locals_d,
                sample_size=50,
                chunk_size=chunk_size,
                trace_label='school_location.%s' % school_type,
                trace_choice_name='school_location')

            choices_list.append(choices)

    choices = pd.concat(choices_list)

    # We only chose school locations for the subset of persons who go to school
    # so we backfill the empty choices with -1 to code as no school location
    choices = choices.reindex(persons_merged.index).fillna(-1)

    tracing.print_summary('school_taz', choices, describe=True)

    orca.add_column("persons", "school_taz", choices)
    add_dependent_columns("persons", "persons_school")

    if trace_hh_id:
        trace_columns = ['school_taz'
                         ] + orca.get_table('persons_school').columns
        tracing.trace_df(orca.get_table('persons_merged').to_frame(),
                         label="school_location",
                         columns=trace_columns,
                         warn_if_empty=True)
Exemple #8
0
def trip_mode_choice_simulate(tours_merged, trip_mode_choice_spec,
                              trip_mode_choice_settings, skims, stacked_skims,
                              omx_file, trace_hh_id):

    # FIXME - running the trips model on tours
    logging.error('trips not implemented running the trips model on tours')

    trips = tours_merged.to_frame()

    nest_spec = get_logit_model_settings(trip_mode_choice_settings)
    constants = get_model_constants(trip_mode_choice_settings)

    logger.info("Running trip_mode_choice_simulate with %d trips" % len(trips))

    choices_list = []

    for tour_type, segment in trips.groupby('tour_type'):

        logger.info("running %s tour_type '%s'" % (
            len(segment.index),
            tour_type,
        ))

        orig_key = 'TAZ'
        dest_key = 'destination'

        # name index so tracing knows how to slice
        segment.index.name = 'tour_id'

        # FIXME - check that destination is not null (patch_mandatory_tour_destination not run?)

        # FIXME - no point in printing verbose dest_taz value_counts now that we have tracing?
        # tracing.print_summary('trip_mode_choice_simulate %s dest_taz' % tour_type,
        #                       segment[dest_key], value_counts=True)

        trace_label = trace_hh_id and ('trip_mode_choice_%s' % tour_type)

        choices = _mode_choice_simulate(segment,
                                        skims,
                                        stacked_skims,
                                        orig_key=orig_key,
                                        dest_key=dest_key,
                                        spec=get_segment_and_unstack(
                                            trip_mode_choice_spec, tour_type),
                                        constants=constants,
                                        nest_spec=nest_spec,
                                        omx=omx_file,
                                        trace_label=trace_label,
                                        trace_choice_name='trip_mode_choice')

        # FIXME - no point in printing verbose value_counts now that we have tracing?
        tracing.print_summary('trip_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('trip_mode_choice_simulate all tour type choices',
                          choices,
                          value_counts=True)

    # FIXME - is this a NOP if trips table doesn't exist
    orca.add_column("trips", "mode", choices)

    if trace_hh_id:
        logger.warn("can't dump trips table because it doesn't exist")
        # FIXME - commented out because trips table doesn't really exist
        # trace_columns = ['mode']
        # tracing.trace_df(orca.get_table('trips').to_frame(),
        #                  label = "mode",
        #                  slicer='tour_id',
        #                  index_label='tour_id',
        #                  columns = trace_columns,
        #                  warn_if_empty=True)

    # FIXME - this forces garbage collection
    asim.memory_info()
Exemple #9
0
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()
def workplace_location_simulate(set_random_seed, persons_merged,
                                workplace_location_spec,
                                workplace_location_settings, skims,
                                destination_size_terms, chunk_size,
                                trace_hh_id):
    """
    The workplace location model predicts the zones in which various people will
    work.
    """

    # for now I'm going to generate a workplace location for everyone -
    # presumably it will not get used in downstream models for everyone -
    # it should depend on CDAP and mandatory tour generation as to whether
    # it gets used
    choosers = persons_merged.to_frame()
    alternatives = destination_size_terms.to_frame()

    constants = get_model_constants(workplace_location_settings)

    logger.info("Running workplace_location_simulate with %d persons" %
                len(choosers))

    # set the 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.set_keys("TAZ", "TAZ_r")

    locals_d = {'skims': skims}
    if constants is not None:
        locals_d.update(constants)

    # FIXME - HACK - only include columns actually used in spec (which we pathologically know)
    choosers = choosers[["income_segment", "TAZ", "mode_choice_logsums"]]

    choices = asim.interaction_simulate(choosers,
                                        alternatives,
                                        spec=workplace_location_spec,
                                        skims=skims,
                                        locals_d=locals_d,
                                        sample_size=50,
                                        chunk_size=chunk_size,
                                        trace_label=trace_hh_id
                                        and 'workplace_location',
                                        trace_choice_name='workplace_location')

    # FIXME - no need to reindex?
    choices = choices.reindex(persons_merged.index)

    logger.info("%s workplace_taz choices min: %s max: %s" %
                (len(choices.index), choices.min(), choices.max()))

    tracing.print_summary('workplace_taz', choices, describe=True)

    orca.add_column("persons", "workplace_taz", choices)

    add_dependent_columns("persons", "persons_workplace")

    if trace_hh_id:
        trace_columns = ['workplace_taz'
                         ] + orca.get_table('persons_workplace').columns
        tracing.trace_df(orca.get_table('persons_merged').to_frame(),
                         label="workplace_location",
                         columns=trace_columns,
                         warn_if_empty=True)