Beispiel #1
0
def test_data_tally_works_as_an_updater(three_by_three_grid):
    assignment = random_assignment(three_by_three_grid, 4)
    data = {node: random.randint(1, 100) for node in three_by_three_grid.nodes}
    parts = tuple(set(assignment.values()))
    updaters = {"tally": DataTally(data, alias="tally")}
    partition = Partition(three_by_three_grid, assignment, updaters)

    flip = {random.choice(list(partition.graph.nodes)): random.choice(parts)}
    new_partition = partition.flip(flip)

    assert new_partition["tally"]
Beispiel #2
0
def random_assignment(graph, num_districts):
    assignment = {
        node: random.choice(range(num_districts))
        for node in graph.nodes
    }
    # Make sure that there are cut edges:
    while len(set(assignment.values())) == 1:
        assignment = {
            node: random.choice(range(num_districts))
            for node in graph.nodes
        }
    return assignment
Beispiel #3
0
def update_walkers(state, walkers="walkers", target="target", times="times",
                   graph="graph"):
    not_done = []
    for i in range(len(state[walkers])):
        if state[walkers][i] in state[target]:
            not_done.append(False)
        else:
            state[times][i] += 1
            state[walkers][i] = random.choice(list(state[graph].neighbors(state[walkers][i])))
            not_done.append(True)
    return any(not_done)
def recom(partition, pop_col, pop_target, epsilon, node_repeats):
    """ReCom proposal.

    Description from MGGG's 2018 Virginia House of Delegates report:
    At each step, we (uniformly) randomly select a pair of adjacent districts and
    merge all of their blocks in to a single unit. Then, we generate a spanning tree
    for the blocks of the merged unit with the Kruskal/Karger algorithm. Finally,
    we cut an edge of the tree at random, checking that this separates the region
    into two new districts that are population balanced.

    Example usage::

        from functools import partial
        from gerrychain import MarkovChain
        from gerrychain.proposals import recom

        # ...define constraints, accept, partition, total_steps here...

        # Ideal population:
        pop_target = sum(partition["population"].values()) / len(partition)

        proposal = partial(
            recom, pop_col="POP10", pop_target=pop_target, epsilon=.05, node_repeats=10
        )

        chain = MarkovChain(proposal, constraints, accept, partition, total_steps)

    """
    edge = random.choice(tuple(partition["cut_edges"]))
    parts_to_merge = (partition.assignment[edge[0]],
                      partition.assignment[edge[1]])

    subgraph = partition.graph.subgraph(partition.parts[parts_to_merge[0]]
                                        | partition.parts[parts_to_merge[1]])

    flips = recursive_tree_part(
        subgraph,
        parts_to_merge,
        pop_col=pop_col,
        pop_target=pop_target,
        epsilon=epsilon,
        node_repeats=node_repeats,
    )

    return partition.flip(flips)
Beispiel #5
0
def random_assignment(graph, num_districts):
    return {node: random.choice(range(num_districts)) for node in graph.nodes}