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.') if not initial_state['polsby_popper']: raise ValueError( 'BasicChain needs the Partition to have a polsby_popper updater.' ) threshold = L1_reciprocal_polsby_popper(initial_state) polsby_popper_constraint = LowerBound(L1_reciprocal_polsby_popper, threshold) population_constraint = within_percent_of_ideal_population( initial_state, 0.05) validator = Validator( default_constraints + [polsby_popper_constraint, population_constraint]) super().__init__(propose_random_flip, validator, always_accept, initial_state, total_steps=total_steps)
updaters = { **updaters, **votes_updaters(election_columns[i], election_names[i]) } # 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
# 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 ] # Add cyclic updaters :( # updaters['metagraph_degree'] = MetagraphDegree(validator, "metagraph_degree")