示例#1
0
def _mode_choice_simulate(tours, skims, spec, additional_constants, omx=None):
    """
    This is a utility to run a mode choice model for each segment (usually
    segments are trip purposes).  Pass in the tours that need a mode,
    the Skim object, the spec to evaluate with, and any additional expressions
    you want to use in the evaluation of variables.
    """

    # FIXME - this is only really going for the workplace trip
    in_skims = askim.Skims3D(skims.set_keys("TAZ", "workplace_taz"),
                             "in_period", -1)
    out_skims = askim.Skims3D(skims.set_keys("workplace_taz", "TAZ"),
                              "out_period", -1)
    skims.set_keys("TAZ", "workplace_taz")

    if omx:
        in_skims.set_omx(omx)
        out_skims.set_omx(omx)

    locals_d = {"in_skims": in_skims, "out_skims": out_skims, "skims": skims}
    locals_d.update(additional_constants)

    choices, _ = asim.simple_simulate(tours,
                                      spec,
                                      skims=[in_skims, out_skims, skims],
                                      locals_d=locals_d)

    alts = spec.columns
    choices = choices.map(dict(zip(range(len(alts)), alts)))

    return choices
示例#2
0
文件: mode.py 项目: amos5/activitysim
def mode_choice_simulate(tours_merged,
                         mode_choice_spec,
                         mode_choice_settings,
                         skims):

    tours = tours_merged.to_frame()
    tours = tours[tours.tour_type == "work"]

    mode_choice_spec = mode_choice_spec.head(33)

    # the skims will be available under the name "skims" for any @ expressions
    in_skims = askim.Skims3D(skims.set_keys("TAZ", "workplace_taz"),
                             "in_period", -1)
    out_skims = askim.Skims3D(skims.set_keys("workplace_taz", "TAZ"),
                              "out_period", -1)
    locals_d = {
        "in_skims": in_skims,
        "out_skims": out_skims
    }
    locals_d.update(mode_choice_settings['CONSTANTS'])

    # FIXME lots of other segments here - for now running the mode choice for
    # FIXME work on all kinds of tour types
    # FIXME note that in particular the spec above only has work tours in it

    choices, _ = asim.simple_simulate(tours,
                                      get_segment_and_unstack(mode_choice_spec,
                                                              'work'),
                                      skims=[in_skims, out_skims],
                                      locals_d=locals_d)

    print "Choices:\n", choices.value_counts()
    orca.add_column("tours", "mode", choices)
示例#3
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)
def mandatory_tour_frequency(set_random_seed,
                             persons_merged,
                             mandatory_tour_frequency_spec):
    """
    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 == 'Mandatory']
    logger.info("%d persons run for mandatory tour model" % len(choosers))

    # trace.print_summary('mandatory_tour_frequency choosers',
    #                     choosers.workplace_taz, describe=True)

    choices, _ = asim.simple_simulate(choosers, mandatory_tour_frequency_spec)

    # 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)
示例#5
0
def pet_activity_simulate(set_random_seed, pets_merged, pet_spec):

    # choosers: the choice model will be applied to each row of the choosers table (a pandas.DataFrame)
    choosers = pets_merged.to_frame()

    # spec: table of variable specifications and coefficient values of alternatives (a pandas.DataFrame table
    spec = pet_spec

    # locals whose values will be accessible to the execution context  when the expressions in spec are applied to choosers
    locals_d=None

    choices, model_design = asim.simple_simulate(choosers, spec)

    print "\n### model_design - results of the expressions for each row in choosers"
    print model_design

    print "\n### choices - choices expressed as zero-based index into set of alternatives"
    print choices

    # convert choice indexes to spec column names
    activity_names = spec.columns.values
    choice_names = choices.apply(lambda x : activity_names[x])

    print "\n### choice_names - choices expressed as names of columns of alternatives in spec"
    print choice_names

    # store the results so they are available to subsequent models, etc
    orca.add_column("pets", "pet_activity", choices)
    orca.add_column("pets", "pet_activity_names", choice_names)
示例#6
0
def _mode_choice_simulate(tours, skims, spec, additional_constants, omx=None):
    """
    This is a utility to run a mode choice model for each segment (usually
    segments are trip purposes).  Pass in the tours that need a mode,
    the Skim object, the spec to evaluate with, and any additional expressions
    you want to use in the evaluation of variables.
    """

    # FIXME - this is only really going for the workplace trip
    in_skims = askim.Skims3D(skims.set_keys("TAZ", "workplace_taz"),
                             "in_period", -1)
    out_skims = askim.Skims3D(skims.set_keys("workplace_taz", "TAZ"),
                              "out_period", -1)
    skims.set_keys("TAZ", "workplace_taz")

    if omx:
        in_skims.set_omx(omx)
        out_skims.set_omx(omx)

    locals_d = {
        "in_skims": in_skims,
        "out_skims": out_skims,
        "skims": skims
    }
    locals_d.update(additional_constants)

    choices, _ = asim.simple_simulate(tours,
                                      spec,
                                      skims=[in_skims, out_skims, skims],
                                      locals_d=locals_d)

    alts = spec.columns
    choices = choices.map(dict(zip(range(len(alts)), alts)))

    return choices
示例#7
0
def auto_ownership_simulate(set_random_seed, households_merged,
                            auto_ownership_spec):

    choices, _ = asim.simple_simulate(households_merged.to_frame(),
                                      auto_ownership_spec)

    print "Choices:\n", choices.value_counts()

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

    add_dependent_columns("households", "households_autoown")
def auto_ownership_simulate(set_random_seed, households_merged,
                            auto_ownership_spec):

    choices, _ = asim.simple_simulate(
        households_merged.to_frame(), auto_ownership_spec)

    print "Choices:\n", choices.value_counts()

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

    add_dependent_columns("households", "households_autoown")
示例#9
0
def _mode_choice_simulate(tours,
                          skims,
                          stack,
                          orig_key,
                          dest_key,
                          spec,
                          constants,
                          nest_spec,
                          omx=None,
                          trace_label=None,
                          trace_choice_name=None):
    """
    This is a utility to run a mode choice model for each segment (usually
    segments are trip purposes).  Pass in the tours that need a mode,
    the Skim object, the spec to evaluate with, and any additional expressions
    you want to use in the evaluation of variables.
    """

    # FIXME - check that periods are in time_periods?

    in_skims = askim.Skims3D(stack=stack,
                             left_key=orig_key,
                             right_key=dest_key,
                             skim_key="in_period",
                             offset=-1)
    out_skims = askim.Skims3D(stack=stack,
                              left_key=dest_key,
                              right_key=orig_key,
                              skim_key="out_period",
                              offset=-1)

    if omx is not None:
        in_skims.set_omx(omx)
        out_skims.set_omx(omx)

    skims.set_keys(orig_key, dest_key)

    locals_d = {"in_skims": in_skims, "out_skims": out_skims, "skims": skims}
    if constants is not None:
        locals_d.update(constants)

    choices = asim.simple_simulate(tours,
                                   spec,
                                   nest_spec,
                                   skims=[in_skims, out_skims, skims],
                                   locals_d=locals_d,
                                   trace_label=trace_label,
                                   trace_choice_name=trace_choice_name)

    alts = spec.columns
    choices = choices.map(dict(zip(range(len(alts)), alts)))

    return choices
示例#10
0
def _mode_choice_simulate(tours,
                          skims,
                          stack,
                          orig_key,
                          dest_key,
                          spec,
                          additional_constants,
                          omx=None):
    """
    This is a utility to run a mode choice model for each segment (usually
    segments are trip purposes).  Pass in the tours that need a mode,
    the Skim object, the spec to evaluate with, and any additional expressions
    you want to use in the evaluation of variables.
    """

    # FIXME - log
    # print "Skims3D %s skim_key2 values = %s" % ('in_period', tours['in_period'].unique())
    # print "Skims3D %s skim_key2 values = %s" % ('out_period', tours['out_period'].unique())

    # FIXME - check that periods are in time_periods?

    in_skims = askim.Skims3D(stack=stack,
                             left_key=orig_key, right_key=dest_key,
                             skim_key="in_period",
                             offset=-1)
    out_skims = askim.Skims3D(stack=stack,
                              left_key=dest_key, right_key=orig_key,
                              skim_key="out_period",
                              offset=-1)

    if omx is not None:
        in_skims.set_omx(omx)
        out_skims.set_omx(omx)

    skims.set_keys(orig_key, dest_key)

    locals_d = {
        "in_skims": in_skims,
        "out_skims": out_skims,
        "skims": skims
    }
    locals_d.update(additional_constants)

    choices, _ = asim.simple_simulate(tours,
                                      spec,
                                      skims=[in_skims, out_skims, skims],
                                      locals_d=locals_d)

    alts = spec.columns
    choices = choices.map(dict(zip(range(len(alts)), alts)))

    return choices
示例#11
0
def auto_ownership_simulate(set_random_seed, households_merged,
                            auto_ownership_spec):
    """
    Auto ownership is a standard model which predicts how many cars a household
    with given characteristics owns
    """

    choices, _ = asim.simple_simulate(
        households_merged.to_frame(), auto_ownership_spec)

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

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

    add_dependent_columns("households", "households_autoown")
def mandatory_tour_frequency(set_random_seed, persons_merged,
                             mandatory_tour_frequency_spec):

    choosers = persons_merged.to_frame()
    # filter based on results of CDAP
    choosers = choosers[choosers.cdap_activity == 'Mandatory']
    print "%d persons run for mandatory tour model" % len(choosers)

    print choosers.workplace_taz.describe()

    choices, _ = asim.simple_simulate(choosers, mandatory_tour_frequency_spec)

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

    print "Choices:\n", choices.value_counts()
    orca.add_column("persons", "mandatory_tour_frequency", choices)
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)
def mandatory_tour_frequency(set_random_seed,
                             persons_merged,
                             mandatory_tour_frequency_spec):

    choosers = persons_merged.to_frame()
    # filter based on results of CDAP
    choosers = choosers[choosers.cdap_activity == 'Mandatory']
    print "%d persons run for mandatory tour model" % len(choosers)

    print choosers.workplace_taz.describe()

    choices, _ = asim.simple_simulate(choosers, mandatory_tour_frequency_spec)

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

    print "Choices:\n", choices.value_counts()
    orca.add_column("persons", "mandatory_tour_frequency", choices)
示例#15
0
def test_simple_simulate(random_seed, data, spec):
    choices, _ = asim.simple_simulate(data, spec)
    expected = pd.Series([1, 1, 1], index=data.index)
    pdt.assert_series_equal(choices, expected)