spopt.region.RandomRegion

class spopt.region.RandomRegion(area_ids, num_regions=None, cardinality=None, contiguity=None, maxiter=1000, compact=False, max_swaps=1000000)[source]

Randomly combine a given set of areas into two or more regions based on various constraints.

Parameters:
area_idslist

The IDs indexing the areas to be grouped into regions (must be in the same order as spatial weights matrix if this is provided).

num_regionsint (default None)

The number of regions to generate (if None then this is chosen randomly from 2 to \(n\) where \(n\) is the number of areas).

cardinalitylist (default None)

A list containing the number of areas to assign to regions (if num_regions is also provided then len(cardinality) must equal num_regions; if None then a list of length num_regions will be generated randomly).

contiguitylibpysal.weights.W (default None)

A spatial weights object (if None then contiguity will be ignored).

maxiterint (default 100)

The maximum number attempts (for each permutation) at finding a feasible solution (only affects contiguity constrained regions).

compactbool (default False)

Attempt to build compact regions (only affects contiguity constrained regions).

max_swapsint (default 1000000)

The maximum number of swaps to find a feasible solution (only affects contiguity constrained regions).

Examples

Setup the data.

>>> import libpysal
>>> import numpy
>>> from spopt.region import RandomRegions, RandomRegion
>>> nregs = 13
>>> cards = list(range(2,14)) + [10]
>>> w = libpysal.weights.lat2W(10,10,rook=True)
>>> ids = w.id_order

Unconstrained:

>>> numpy.random.seed(10)
>>> t0 = RandomRegion(ids)
>>> t0.regions[0]
[19, 14, 43, 37, 66, 3, 79, 41, 38, 68, 2, 1, 60]

Cardinality and contiguity constrained (num_regions implied):

>>> numpy.random.seed(60)
>>> t1 = RandomRegion(ids, num_regions=nregs, cardinality=cards, contiguity=w)
>>> t1.regions[0]
[62, 61, 81, 71, 64, 90, 72, 51, 80, 63, 50, 73, 52]

Cardinality constrained (num_regions implied):

>>> numpy.random.seed(100)
>>> t2 = RandomRegion(ids, num_regions=nregs, cardinality=cards)
>>> t2.regions[0]
[37, 62]

Number of regions and contiguity constrained:

>>> numpy.random.seed(100)
>>> t3 = RandomRegion(ids, num_regions=nregs, contiguity=w)
>>> t3.regions[1]
[62, 52, 51, 63, 61, 73, 41, 53, 60, 83, 42, 31, 32]

Cardinality and contiguity constrained:

>>> numpy.random.seed(60)
>>> t4 = RandomRegion(ids, cardinality=cards, contiguity=w)
>>> t4.regions[0]
[62, 61, 81, 71, 64, 90, 72, 51, 80, 63, 50, 73, 52]

Number of regions constrained:

>>> numpy.random.seed(100)
>>> t5 = RandomRegion(ids, num_regions=nregs)
>>> t5.regions[0]
[37, 62, 26, 41, 35, 25, 36]

Cardinality constrained:

>>> numpy.random.seed(100)
>>> t6 = RandomRegion(ids, cardinality=cards)
>>> t6.regions[0]
[37, 62]

Contiguity constrained:

>>> numpy.random.seed(100)
>>> t7 = RandomRegion(ids, contiguity=w)
>>> t7.regions[0]
[37, 36, 38, 39]
Attributes:
feasiblebool

If True then solution was found.

regionslist

A list of lists of regions where each list has the IDs of areas in that region.

__init__(area_ids, num_regions=None, cardinality=None, contiguity=None, maxiter=1000, compact=False, max_swaps=1000000)[source]

Methods

__init__(area_ids[, num_regions, ...])

build_contig_regions(num_regions, ...)

build_noncontig_regions(num_regions, ...)

cards2breaks(cards)

get_cards(num_regions)

get_num_regions()

get_region_breaks(num_regions)

grow_compact(w, test_card, region, ...)

grow_free(w, test_card, region, candidates, ...)

build_contig_regions(num_regions, cardinality, w, maxiter, compact, max_swaps)[source]
build_noncontig_regions(num_regions, region_breaks)[source]
cards2breaks(cards)[source]
get_cards(num_regions)[source]
get_num_regions()[source]
get_region_breaks(num_regions)[source]
grow_compact(w, test_card, region, candidates, potential)[source]
grow_free(w, test_card, region, candidates, potential)[source]