Beispiel #1
0
def run_replanning_loop(settings, forecast_year):

    replan_iters = settings['replan_iters']
    replan_hh_samp_size = settings['replan_hh_samp_size']
    activity_demand_model = settings['activity_demand_model']
    image_names = settings['docker_images']
    activity_demand_image = image_names[activity_demand_model]
    region = settings['region']
    asim_subdir = settings['region_to_asim_subdir'][region]
    asim_workdir = os.path.join('/activitysim', asim_subdir)
    asim_docker_vols = get_asim_docker_vols(settings)
    base_asim_cmd = get_base_asim_cmd(settings, replan_hh_samp_size)
    docker_stdout = settings.get('docker_stdout', False)
    last_asim_step = settings['replan_after']

    for i in range(replan_iters):
        replanning_iteration_number = i + 1
        print_str = (
            'Replanning Iteration {0}'.format(replanning_iteration_number))
        formatted_print(print_str)

        # a) format new skims for asim
        asim_pre.create_skims_from_beam(settings, year, overwrite=True)

        # b) replan with asim
        print_str = (
            "Replanning {0} households with ActivitySim".format(
                replan_hh_samp_size))
        formatted_print(print_str)
        asim = client.containers.run(
            activity_demand_image, working_dir=asim_workdir,
            volumes=asim_docker_vols,
            command=base_asim_cmd + ' -r ' + last_asim_step,
            stdout=docker_stdout,
            stderr=True,
            detach=True,
            remove=True)
        for log in asim.logs(
                stream=True, stderr=True, stdout=docker_stdout):
            print(log)

        # e) run BEAM
        run_traffic_assignment(
            settings, year, client, replanning_iteration_number)

    return
Beispiel #2
0
def generate_activity_plans(
        settings, year, forecast_year, client,
        resume_after=None,
        warm_start=False,
        overwrite_skims=True):
    """
    Parameters
    ----------
    year : int
        Start year for the simulation iteration.
    forecast_year : int
        Simulation year for which activities are generated. If `forecast_year`
        is the start year of the whole simulation, then we are probably
        generating warm start activities based on the base year input data in
        order to generate "warm start" skims.
    """

    # 1. PARSE SETTINGS
    activity_demand_model = settings['activity_demand_model']
    land_use_model = settings['land_use_model']
    image_names = settings['docker_images']
    activity_demand_image = image_names[activity_demand_model]
    region = settings['region']
    asim_subdir = settings['region_to_asim_subdir'][region]
    asim_workdir = os.path.join('/activitysim', asim_subdir)
    asim_docker_vols = get_asim_docker_vols(settings)
    asim_cmd = get_base_asim_cmd(settings)
    docker_stdout = settings.get('docker_stdout', False)

    # If this is the first iteration, skims should only exist because
    # they were created during the warm start activities step. The skims
    # haven't been updated since then so we don't need to re-create them.
    if year == settings['start_year']:
        overwrite_skims = False

    # 2. PREPROCESS DATA FOR ACTIVITY DEMAND MODEL
    print_str = "Creating {0} input data from {1} outputs".format(
        activity_demand_model,
        land_use_model)
    formatted_print(print_str)
    asim_pre.create_skims_from_beam(settings, year, overwrite=overwrite_skims)
    asim_pre.create_asim_data_from_h5(
        settings, year=forecast_year, warm_start=warm_start)

    # 3. GENERATE ACTIVITY PLANS
    print_str = (
        "Generating activity plans for the year "
        "{0} with {1}".format(
            forecast_year, activity_demand_model))
    formatted_print(print_str)
    if resume_after:
        asim_cmd += ' -r {0}'.format(resume_after)
    asim = client.containers.run(
        activity_demand_image,
        working_dir=asim_workdir,
        volumes=asim_docker_vols,
        command=asim_cmd,
        stdout=docker_stdout,
        stderr=True,
        detach=True)
    for log in asim.logs(
            stream=True, stderr=True, stdout=docker_stdout):
        print(log)

    # 4. COPY ACTIVITY DEMAND OUTPUTS --> LAND USE INPUTS
    # If generating activities for the base year (i.e. warm start),
    # then we don't want to overwrite urbansim input data. Otherwise
    # we want to set up urbansim for the next simulation iteration
    if (settings['land_use_enabled']) and (not warm_start):
        print_str = (
            "Generating {0} {1} input data from "
            "{2} outputs".format(
                forecast_year, land_use_model, activity_demand_model))
        formatted_print(print_str)
        asim_post.create_next_iter_inputs(settings, year, forecast_year)

    # 6. CLEANUP
    asim.remove()

    logger.info('Done!')

    return
Beispiel #3
0
def warm_start_activities(settings, year, client):
    """
    Run ActivitySim to update UrbanSim inputs with long-term
    choices it needs: workplace location, school location, and
    auto ownership.
    """

    # 1. PARSE SETTINGS
    activity_demand_model = settings['activity_demand_model']
    land_use_model = settings['land_use_model']
    travel_model = settings['travel_model']
    image_names = settings['docker_images']
    activity_demand_image = image_names[activity_demand_model]
    region = settings['region']
    asim_subdir = settings['region_to_asim_subdir'][region]
    asim_workdir = os.path.join('/activitysim', asim_subdir)
    asim_docker_vols = get_asim_docker_vols(settings)
    base_asim_cmd = get_base_asim_cmd(settings)
    docker_stdout = settings.get('docker_stdout', False)

    print_str = "Initializing {0} warm start sequence".format(
        activity_demand_model)
    formatted_print(print_str)

    # 2. CREATE DATA FROM BASE YEAR SKIMS AND URBANSIM INPUTS

    # skims
    logger.info("Creating {0} skims from {1}".format(
        activity_demand_model,
        travel_model).upper())
    asim_pre.create_skims_from_beam(settings, year)

    # data tables
    logger.info("Creating {0} input data from {1} outputs".format(
        activity_demand_model,
        land_use_model).upper())
    asim_pre.create_asim_data_from_h5(settings, year, warm_start=True)

    # 3. RUN ACTIVITYSIM IN WARM START MODE
    logger.info("Running {0} in warm start mode".format(
        activity_demand_model).upper())
    ws_asim_cmd = base_asim_cmd + ' -w'  # warm start flag

    asim = client.containers.run(
        activity_demand_image,
        working_dir=asim_workdir,
        volumes=asim_docker_vols,
        command=ws_asim_cmd,
        stdout=docker_stdout,
        stderr=True,
        detach=True)
    for log in asim.logs(stream=True, stderr=True, stdout=docker_stdout):
        print(log)

    # 4. UPDATE URBANSIM BASE YEAR INPUT DATA
    logger.info((
        "Appending warm start activities/choices to "
        " {0} base year input data").format(land_use_model).upper())
    asim_post.update_usim_inputs_after_warm_start(settings)

    # 5. CLEANUP
    asim.remove()
    logger.info('Done!')

    return