Exemplo n.º 1
0
def main():
    """
    Save figures showing the results of running the mulit-objective optimisation (NSGA2)
    with two objectives.
    """
    print("Saving two-objective network figures...")
    set_fig_style()
    config = get_config()
    figs_dir = get_figures_save_dir(config)

    networks_path = get_two_objs_filepath(config)
    networks = load_pickle(networks_path)
    theta, n_sensors = get_default_optimisation_params(config)
    n = networks[f"theta{theta}"][f"{n_sensors}sensors"]
    scores, solutions = extract_all(n)
    scores = -scores

    population_groups, all_groups = get_objectives(config)
    plot_objs = config["optimisation"]["two_objectives"]["objectives"]
    lad20cd = lad20nm_to_lad20cd(config["la"])
    inputs = get_multi_obj_inputs(lad20cd, population_groups)

    fig_obj1_vs_obj2(plot_objs, scores, all_groups, theta, n_sensors, figs_dir)
    fig_two_objs_spectrum(
        lad20cd,
        plot_objs,
        scores,
        solutions,
        inputs,
        all_groups,
        theta,
        n_sensors,
        figs_dir,
    )
Exemplo n.º 2
0
def main():
    """Save figures showing the pre-existing Urban Observatory network of sensors and
    comparisons with optimised networks using our approach.
    """
    print("Saving Urban Observatory figures...")
    set_fig_style()

    config = get_config()
    lad20cd = lad20nm_to_lad20cd(config["la"])
    networks_path = get_single_obj_filepath(config)
    networks = load_pickle(networks_path)
    uo_sensors = load_uo_sensors(config)

    figs_dir = get_figures_save_dir(config)

    population_groups, all_groups = get_objectives(config)
    oa_weights = get_weights(lad20cd, population_groups)
    theta, _ = get_default_optimisation_params(config)

    uo_sensor_dict = get_uo_sensor_dict(lad20cd, uo_sensors=uo_sensors)
    uo_coverage = get_uo_coverage_oa(lad20cd, uo_sensor_dict, theta,
                                     all_groups, oa_weights)

    fig_uo_sensor_locations(lad20cd, uo_sensors, figs_dir)
    fig_uo_coverage_grid(lad20cd, uo_sensors, theta, figs_dir)
    fig_uo_coverage_grid_diff(lad20cd, uo_sensors, theta, all_groups, networks,
                              figs_dir)
    fig_uo_coverage_oa(uo_coverage, theta, all_groups, figs_dir)
    fig_uo_coverage_oa_diff(lad20cd, uo_coverage, theta, all_groups, networks,
                            figs_dir)
Exemplo n.º 3
0
def main():
    """Save figures showing the results of running the multi-objective optimisation
    (with the NSGA2 algorithm)
    """
    print("Saving multi-objective network figures...")
    set_fig_style()
    config = get_config()

    lad20cd = lad20nm_to_lad20cd(config["la"])
    networks_path = get_multi_objs_filepath(config)
    networks = load_pickle(networks_path)

    figs_dir = get_figures_save_dir(config)

    theta, n_sensors = get_default_optimisation_params(config)
    n = networks[f"theta{theta}"][f"{n_sensors}sensors"]
    scores, solutions = extract_all(n)
    scores = -scores

    population_groups, all_groups = get_objectives(config)
    objs = [g["title"] for g in all_groups.values()]

    threshold = config["figures"]["multi_objectives"]["all_coverage_threshold"]
    fig_all_above_threshold(scores, objs, threshold, theta, n_sensors,
                            figs_dir)

    threshold = config["figures"]["multi_objectives"][
        "work_coverage_threshold"]
    work_name = config["objectives"]["workplace"]["title"]
    fig_work_above_threshold(scores, objs, threshold, theta, n_sensors,
                             work_name, figs_dir)

    child_name = config["objectives"]["population_groups"]["pop_children"][
        "title"]
    inputs = get_multi_obj_inputs(lad20cd, population_groups)
    fig_max_child_work_above_threshold(
        lad20cd,
        scores,
        objs,
        threshold,
        theta,
        n_sensors,
        solutions,
        inputs,
        work_name,
        child_name,
        figs_dir,
    )

    uo_sensors = load_uo_sensors(config)
    uo_coverage = get_uo_coverage_oa(lad20cd, None, theta, all_groups,
                                     inputs["oa_weight"])
    n_uo_oa = uo_sensors["oa11cd"].nunique()
    fig_coverage_above_uo(uo_coverage, scores, objs, theta, n_uo_oa,
                          all_groups, figs_dir)

    fig_max_min_coverage(lad20cd, scores, objs, theta, n_sensors, solutions,
                         inputs, figs_dir)
Exemplo n.º 4
0
def main():
    """
    Generate single objective networks for a local authority and save them to the path
    specified in config.yml.
    """
    print("Generating single objective networks...")
    config = get_config()
    lad20cd = lad20nm_to_lad20cd(config["la"])
    save_path = get_single_obj_filepath(config)
    population_groups, _ = get_objectives(config)
    thetas, n_sensors = get_all_optimisation_params(config)

    results = make_single_obj_networks(lad20cd, population_groups, thetas,
                                       n_sensors)
    with open(save_path, "wb") as f:
        pickle.dump(results, f)
Exemplo n.º 5
0
def main():
    """Save figures showing the distribution of differrent sub-populations around the
    local authority.
    """
    print("Saving demographic figures...")
    set_fig_style()

    config = get_config()
    lad20cd = lad20nm_to_lad20cd(config["la"])
    figs_dir = get_figures_save_dir(config)

    theta, _ = get_default_optimisation_params(config)
    population_groups, all_groups = get_objectives(config)

    oa_weights = get_weights(lad20cd, population_groups)
    fig_importance(lad20cd, all_groups, oa_weights, theta, figs_dir)
    oa = calc_oa_density(lad20cd, all_groups, population_groups)
    fig_density(lad20cd, oa, all_groups, figs_dir)
Exemplo n.º 6
0
def main():
    """
    Save figures showing the results of single-objective networks generated with a
    greedy algorithm.
    """
    print("Saving single objective network figures...")
    set_fig_style()

    config = get_config()
    networks_path = get_single_obj_filepath(config)
    results = load_pickle(networks_path)

    figs_dir = get_figures_save_dir(config)

    thetas, n_sensors = get_all_optimisation_params(config)
    _, all_groups = get_objectives(config)
    fig_single_obj(thetas, n_sensors, results, all_groups, figs_dir)

    theta, n_sensors = get_default_optimisation_params(config)
    fig_coverage_vs_sensors(results, theta, n_sensors, all_groups, figs_dir)
Exemplo n.º 7
0
def main():
    """
    Generate two-objective networks for a local authority and save them to the path
    specified in config.yml.
    """
    print("Generating two-objective networks...")
    config = get_config()
    lad20cd = lad20nm_to_lad20cd(config["la"])
    save_path = get_two_objs_filepath(config)

    population_groups, _ = get_objectives(config)
    thetas, n_sensors = get_all_optimisation_params(config)
    gen = config["optimisation"]["two_objectives"]["gen"]
    population_size = config["optimisation"]["two_objectives"]["population_size"]
    seed = config["optimisation"]["two_objectives"]["seed"]
    pg.set_global_rng_seed(seed=seed)
    objectives = config["optimisation"]["two_objectives"]["objectives"]
    inputs = get_two_obj_inputs(lad20cd, objectives, population_groups)

    results = make_two_obj_networks(inputs, thetas, n_sensors, gen, population_size)
    with open(save_path, "wb") as f:
        pickle.dump(results, f)
Exemplo n.º 8
0
def main():
    """
    Creates a formatted report including all the generated figures for a local auhtority
    and decriptions of what each figure shows (using the template reoprt_template.md).
    Both a Markdown (report.md) and HTML (report.html) version of the report are saved.
    """
    print("Generating formatted reports...")
    with open("report_template.md") as f:
        template = Template(f.read())

    config = get_config()
    fig_dir = get_figures_save_dir(config)
    report_dir = fig_dir.parent
    rel_fig_dir = fig_dir.stem

    la_name = config["la"]
    la_code = lad20nm_to_lad20cd(la_name)
    fig_density = Path(rel_fig_dir, "demographics_density.png")
    fig_importance = Path(rel_fig_dir, "demographics_importance.png")
    fig_coverage_vs_nsensors = Path(rel_fig_dir, "coverage_vs_nsensors.png")

    _, all_groups = get_objectives(config)
    total_pop_name = all_groups["pop_total"]["title"]
    fig_totalpop = find_fig_path("pop_total_theta*_nsensors*.png", fig_dir)
    children_name = all_groups["pop_children"]["title"]
    fig_children = find_fig_path("pop_children_theta*_nsensors*.png", fig_dir)
    older_name = all_groups["pop_elderly"]["title"]
    fig_older = find_fig_path("pop_elderly_theta*_nsensors*.png", fig_dir)
    work_name = all_groups["workplace"]["title"]
    fig_workers = find_fig_path("workplace_theta*_nsensors*.png", fig_dir)

    fig_urb_obs_sensors = find_fig_path("urb_obs_sensors_nsensors_*.png", fig_dir)
    fig_urb_obs_coverage_grid = find_fig_path(
        "urb_obs_coverage_grid_theta_*_nsensors_*.png", fig_dir
    )
    fig_urb_obs_coverage_diff_grid = find_fig_path(
        "urb_obs_coverage_difference_grid_theta_*_nsensors_*.png", fig_dir
    )
    fig_urb_obs_coverage_oa = find_fig_path(
        "urb_obs_coverage_oa_theta_*_nsensors_*.png", fig_dir
    )
    fig_urb_obs_coverage_diff_oa = find_fig_path(
        "urb_obs_coverage_difference_grid_theta_*_nsensors_*.png", fig_dir
    )
    all_threshold = config["figures"]["multi_objectives"]["all_coverage_threshold"]
    fig_all_above_threshold = find_fig_path(
        f"multiobj_theta*_*sensors_above{round(all_threshold * 100)}cov.png",
        fig_dir,
    )
    work_threshold = config["figures"]["multi_objectives"]["work_coverage_threshold"]
    fig_work_above_threshold = find_fig_path(
        f"multiobj_theta*_*sensors_workabove{round(work_threshold * 100)}cov.png",
        fig_dir,
    )
    fig_max_child_work_above_threshold = find_fig_path(
        "multiobj_wplace*_child*_theta*_*sensors.png", fig_dir
    )
    fig_coverage_above_uo = find_fig_path(
        "multiobj_theta*_*sensors_above_urbobs.png", fig_dir
    )
    fig_max_min_coverage = find_fig_path(
        "multiobj_compromise_theta*_*sensors_cov*.png", fig_dir
    )
    obj_1 = config["optimisation"]["two_objectives"]["objectives"][0]
    obj_2 = config["optimisation"]["two_objectives"]["objectives"][1]
    obj_1 = all_groups[obj_1]["title"]
    obj_2 = all_groups[obj_2]["title"]
    fig_obj1_vs_obj2 = find_fig_path("2obj_theta*_*sensors.png", fig_dir)
    fig_spectrum = find_fig_path("2obj_spectrum_theta*_*sensors.png", fig_dir)
    fig_width = config["report"]["fig_width"]

    filled_template = template.render(
        la_name=la_name,
        la_code=la_code,
        fig_density=fig_density,
        fig_importance=fig_importance,
        fig_coverage_vs_nsensors=fig_coverage_vs_nsensors,
        total_pop_name=total_pop_name,
        fig_totalpop=fig_totalpop,
        children_name=children_name,
        fig_children=fig_children,
        older_name=older_name,
        fig_older=fig_older,
        work_name=work_name,
        fig_workers=fig_workers,
        fig_urb_obs_sensors=fig_urb_obs_sensors,
        fig_urb_obs_coverage_grid=fig_urb_obs_coverage_grid,
        fig_urb_obs_coverage_diff_grid=fig_urb_obs_coverage_diff_grid,
        fig_urb_obs_coverage_oa=fig_urb_obs_coverage_oa,
        fig_urb_obs_coverage_diff_oa=fig_urb_obs_coverage_diff_oa,
        all_threshold=all_threshold,
        fig_all_above_threshold=fig_all_above_threshold,
        work_threshold=work_threshold,
        fig_work_above_threshold=fig_work_above_threshold,
        fig_max_child_work_above_threshold=fig_max_child_work_above_threshold,
        fig_coverage_above_uo=fig_coverage_above_uo,
        fig_max_min_coverage=fig_max_min_coverage,
        obj_1=obj_1,
        obj_2=obj_2,
        fig_obj1_vs_obj2=fig_obj1_vs_obj2,
        fig_spectrum=fig_spectrum,
        fig_width=fig_width,
    )

    with open(Path(report_dir, "report.md"), "w") as f:
        f.write(filled_template)

    html = markdown(filled_template)
    with open(Path(report_dir, "report.html"), "w") as f:
        f.write(html)
    print("Reports saved to", report_dir)