示例#1
0
    def __init__(self, initial_state, total_steps=1000):
        """
        :initial_state: the initial graph partition. Must have a cut_edges updater
        :total_steps: (defaults to 1000) the total number of steps that the random walk
        should perform.
        """
        if not initial_state['cut_edges']:
            raise ValueError(
                'BasicChain needs the Partition to have a cut_edges updater.')

        if not initial_state['population']:
            raise ValueError(
                'BasicChain needs the Partition to have a population updater.')

        population_constraint = within_percent_of_ideal_population(
            initial_state, 0.01)

        compactness_limit = L1_reciprocal_polsby_popper(initial_state)
        compactness_constraint = UpperBound(L1_reciprocal_polsby_popper,
                                            compactness_limit)

        validator = Validator(default_constraints +
                              [population_constraint, compactness_constraint])

        super().__init__(propose_random_flip,
                         validator,
                         always_accept,
                         initial_state,
                         total_steps=total_steps)
示例#2
0
# This builds the partition object
initial_partition = Partition(graph, assignment, updaters)

# Choose which binary constraints to enforce
# Options are in validity.py

pop_limit = .2
population_constraint = within_percent_of_ideal_population(
    initial_partition, pop_limit)

compactness_constraint_Lm1 = LowerBound(
    L_minus_1_polsby_popper, L_minus_1_polsby_popper(initial_partition))

#edge_constraint = UpperBound(number_cut_edges, 850)
edge_constraint = UpperBound(number_cut_edges,
                             2 * number_cut_edges(initial_partition))

#perim_constraint = LowerBound(perimeter, 1)
#county_constraint = refuse_new_splits("County_Splits")

validator = Validator(
    [single_flip_contiguous, population_constraint]
)  #([edge_constraint])#Validator([single_flip_contiguous, population_constraint,
#compactness_constraint_Lm1,county_constraint])#edge_constraint])

# Names of validators for output
# Necessary since bounds don't have __name__'s
list_of_validators = [
    single_flip_contiguous, within_percent_of_ideal_population,
    L_minus_1_polsby_popper, number_cut_edges
]
    updaters = {
        **updaters,
        **votes_updaters(election_columns[i], election_names[i])
    }

# This builds the partition object
initial_partition = Partition(graph, assignment, updaters)

# Desired validators go here
# Can change constants and bounds
pop_limit = .01
population_constraint = within_percent_of_ideal_population(
    initial_partition, pop_limit)

compactness_limit_L1 = 1.01 * L1_reciprocal_polsby_popper(initial_partition)
compactness_constraint_L1 = UpperBound(L1_reciprocal_polsby_popper,
                                       compactness_limit_L1)

compactness_limit_Lm1 = .99 * L_minus_1_polsby_popper(initial_partition)
compactness_constraint_Lm1 = LowerBound(L_minus_1_polsby_popper,
                                        compactness_limit_Lm1)

validator = Validator([
    refuse_new_splits, no_vanishing_districts, single_flip_contiguous,
    population_constraint, compactness_constraint_Lm1
])

# Names of validators for output
# Necessary since bounds don't have __name__'s
list_of_validators = [
    refuse_new_splits, no_vanishing_districts, single_flip_contiguous,
    within_percent_of_ideal_population, L_minus_1_polsby_popper