Exemplo n.º 1
0
def maxfield(filename, num_agents=1, num_field_iterations=1000,
             num_cpus=1, max_route_solutions=1000,
             max_route_runtime=60,
             outdir='.', skip_plots=False, skip_step_plots=False,
             res_colors=False, google_api_key=None,
             google_api_secret=None, output_csv=False, verbose=False, max_outgoing_links=8, count_already_made_links=True):
    """
    Given a portal list file, determine the optimal linking and
    fielding strategy to maximize AP, minimize walking distance, and
    minimize the number of keys needed.

    Inputs:
      filename :: string
        The filename containing the properly-formatted portal list.
      num_agents :: integer
        The number of agents in this fielding operation
      num_field_iterations :: integer
        The number of random field plans to generate before choosing
        the best. A larger number of iterations will mean the plan is
        more likely, or closer to, optimal, but it also increases
        runtime.
      num_cpus :: integer
        The number of CPUs used to generate field plans.
        If 1, do not use multiprocessing.
        If < 1, use maximum available CPUs.
        Otherwise, use this many CPUs.
      max_route_solutions :: integer
        The maximum number of agent routing solutions to generate
        before choosing the best. Once max_route_solutions or
        max_route_runtime is reached, the best routing plan is
        selected.
      max_route_runtime :: integer
        The maximum runtime of the agent routing algorithm (seconds).
        Once max_route_solutions or max_route_runtime is reached, the
        best routing plan is selected.
      outdir :: string
        The directory where results are stored. Created if it doesn't
        exist. Default is current directory.
      skip_plots :: boolean
        If True, don't generate any figures
      skip_step_plots :: boolean
        If True, don't generate link-by-link figures
      res_colors :: boolean
        If True, use resistance color scheme, otherwise enlightened
      google_api_key :: string
        If not None, use this as an API key for google maps. If None,
        do not use google maps.
      google_api_secret :: string
        If not None, use this as a signature secret for google maps.
        If None, do not use a google API signature.
      output_csv :: boolean
        If True, also output machine readable CSV files.
      verbose :: boolean
        If True, display helpful information along the way

    Returns: Nothing
    """
    start_time = time.time()
    #
    # Read portal file
    #
    portals = read_portal_file(filename)
    if verbose:
        print("Found {0} portals in portal file: {1}".
              format(len(portals), filename))
        print()
    #
    # Initialize Plan
    #
    plan = Plan(portals, num_agents=num_agents, verbose=verbose)
    #
    # Optimize Plan
    #
    plan.optimize(num_field_iterations=num_field_iterations,
                  num_cpus=num_cpus, max_outgoing_links=max_outgoing_links)
    #
    # Determine agent link assignments
    #
    plan.route_agents(max_route_solutions=max_route_solutions,
                      max_route_runtime=max_route_runtime)
    #
    # Generate plan output files and plots
    #
    results = Results(plan, outdir=outdir, res_colors=res_colors,
                      google_api_key=google_api_key,
                      google_api_secret=google_api_secret,
                      output_csv=output_csv,verbose=verbose)
    results.key_prep()
    results.ownership_prep()
    results.agent_key_prep()
    results.agent_assignments()
    if not skip_plots:
        results.portal_map()
        results.link_map()
        if not skip_step_plots:
            results.step_plots()
    end_time = time.time()
    if verbose:
        print("Total maxfield runtime: {0:.1f} seconds".
              format(end_time-start_time))