def run_simple2(graph): election = Election("2014 Senate", { "Democratic": "sen_blue", "Republican": "sen_red" }, alias="2014_Senate") initial_partition = Partition(graph, assignment="con_distri", updaters={ "2014_Senate": election, "population": Tally("population", alias="population"), "exterior_boundaries": exterior_boundaries, "interior_boundaries": interior_boundaries, "perimeter": perimeter, "boundary_nodes": boundary_nodes, "cut_edges": cut_edges, "area": Tally("area", alias="area"), "cut_edges_by_part": cut_edges_by_part, "county_split": county_splits('county_split', "COUNTY_ID"), "black_population": Tally("black_pop", alias="black_population"), }) districts_within_tolerance_2 = lambda part: districts_within_tolerance( part, 'population', 0.3) is_valid = Validator( [single_flip_contiguous, districts_within_tolerance_2]) chain = MarkovChain( proposal=propose_random_flip, is_valid=is_valid, accept=always_accept, # accept=accept, #THe acceptance criteria is what needs to be defined ourselves - to match the paper initial_state=initial_partition, total_steps=30, ) efficiency_gaps = [] wins = [] for partition in chain: if (hasattr(partition, 'accepted') and partition.accepted): efficiency_gaps.append( gerrychain.scores.efficiency_gap(partition["2014_Senate"])) wins.append(partition["2014_Senate"].wins("Democratic")) return (efficiency_gaps, wins, partition)
def test_districts_within_tolerance_returns_true_if_districts_are_within_tolerance(): # 100 and 100.1 are not within 1% of each other, so we should expect True mock_partition = {"population": {0: 100.0, 1: 100.1}} result = districts_within_tolerance( mock_partition, attribute_name="population", percentage=0.01 ) assert result is True
def test_districts_within_tolerance_returns_false_if_districts_are_not_within_tolerance( ): # 100 and 1 are not within 1% of each other, so we should expect False mock_partition = {'population': {0: 100.0, 1: 1.0}} result = districts_within_tolerance(mock_partition, attribute_name='population', percentage=0.01) assert result is False
def run_simple(graph, num_samples = 80000, wp = 3000, wi = 2.5, wc = 0.4, wm = 800, get_parts = 5): election = Election( "2014 Senate", {"Democratic": "sen_blue", "Republican": "sen_red"}, alias="2014_Senate" ) initial_partition = Partition( graph, assignment="2014_CD", updaters={ "2014_Senate": election, "population": Tally("population", alias="population"), "exterior_boundaries": exterior_boundaries, "interior_boundaries": interior_boundaries, "perimeter": perimeter, "boundary_nodes": boundary_nodes, "cut_edges": cut_edges, "area": Tally("area", alias="area"), "cut_edges_by_part": cut_edges_by_part, "county_split" : county_splits( 'county_split', "COUNTY_ID"), "black_population": Tally("black_pop", alias = "black_population"), } ) def vra_validator(part): if(vra_district_requirement(part, 2, [0.4, 0.335]) > 0): return False return True districts_within_tolerance_2 = lambda part : districts_within_tolerance(part, 'population', 0.12) is_valid = Validator([single_flip_contiguous, districts_within_tolerance_2, vra_validator ]) def accept(partition, counter): if(counter < 40000): beta = 0 elif(counter < 60000): beta = (counter - 40000) /(60000-40000) else: beta = 1 return(metro_scoring_prob(partition, beta, wp, wi, wc, wm)) chain = MarkovChainAnneal( proposal=propose_random_flip, is_valid=is_valid, accept=accept, initial_state=initial_partition, total_steps= num_samples * 100, ) # going to show 5 partitions from this sample part_con = max(1, num_samples / get_parts) efficiency_gaps = [] wins = [] voting_percents = [] sample_parts = [] tbefore = time.time() for partition in chain: if(hasattr(partition, 'accepted') and partition.accepted): efficiency_gaps.append(gerrychain.scores.efficiency_gap(partition["2014_Senate"])) wins.append(partition["2014_Senate"].wins("Democratic")) voting_percents.append(partition["2014_Senate"].percents("Democratic")) num_s = len(wins) if(num_s % part_con) == 0: sample_parts.append(partition) if(num_s> num_samples): break if(num_s % 1000 == 0): tnext = time.time() print("It took %i time to get 1000 samples" % (tnext - tbefore)) tbefore = time.time() return(efficiency_gaps, wins, voting_percents, sample_parts)