spopt.region.MaxPHeuristic

class spopt.region.MaxPHeuristic(gdf, w, attrs_name, threshold_name, threshold, top_n=2, max_iterations_construction=99, max_iterations_sa=10, verbose=False, policy='single')[source]

The max-p-regions involves the aggregation of n areas into an unknown maximum number of homogeneous regions, while ensuring that each region is contiguious and satisfies a minimum threshold value imposed on a predefined spatially extensive attribute.

Parameters:
gdfgeopandas.GeoDataFrame, required

Geodataframe containing original data.

wlibpysal.weights.W, required

Weights object created from given data.

attrs_namelist, required

Strings for attribute names (cols of geopandas.GeoDataFrame).

threshold_namestr, required

The name of the spatial extensive attribute variable.

threshold{int, float}, required

The threshold value.

top_nint, required

The number of top candidate regions to consider for enclave assignment.

max_iterations_constructionint

Max number of iterations for construction phase.

max_iterations_SAint

Max number of iterations for customized simulated annealing.

verbosebool

Set to True for reporting solution progress/debugging. Default is False.

policystr

Defaults to 'single' to attach infeasible components using a single linkage between the area in the infeasible component with the smallest nearest neighbor distance to an area in a feasible component. 'multiple' adds joins for each area in an infeasible component and their nearest neighbor area in a feasible component. 'keep' attempts to solve without modification (useful for debugging). 'drop' removes areas in infeasible components before solving.

Examples

>>> import numpy
>>> import libpysal
>>> import geopandas as gpd
>>> from spopt.region.maxp import MaxPHeuristic

Read the data.

>>> pth = libpysal.examples.get_path("mexicojoin.shp")
>>> mexico = gpd.read_file(pth)
>>> mexico["count"] = 1

Create the weight.

>>> w = libpysal.weights.Queen.from_dataframe(mexico)

Define the columns of geopandas.GeoDataFrame to be spatially extensive attribute.

>>> attrs_name = [f"PCGDP{year}" for year in range(1950, 2010, 10)]

Define the spatial extensive attribute variable and the threshold value.

>>> threshold_name = "count"
>>> threshold = 4

Run the max-p-regions algorithm.

>>> model = MaxPHeuristic(mexico, w, attrs_name, threshold_name, threshold)
>>> model.solve()

Get the number of regions and region IDs for unit areas.

>>> model.p
>>> model.labels_
Attributes:
max_pint

The number of regions.

labels_numpy.array

Region IDs for observations.

__init__(gdf, w, attrs_name, threshold_name, threshold, top_n=2, max_iterations_construction=99, max_iterations_sa=10, verbose=False, policy='single')[source]

Methods

__init__(gdf, w, attrs_name, threshold_name, ...)

solve()

Solve a max-p-regions problem and get back the results.

solve()[source]

Solve a max-p-regions problem and get back the results.