-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/gcrsef-gang/the-rebalancing…
…-act into main
- Loading branch information
Showing
8 changed files
with
463 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,5 @@ out.mp4 | |
|
||
district_assignment.csv | ||
maup_concated.csv | ||
|
||
examples/.ipynb_checkpoints |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"black": 0, | ||
"hispanic": 0, | ||
"asian": 0, | ||
"native": 0, | ||
"islander": 0, | ||
"combined": 0, | ||
"opportunity_threshold": 0.51 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""Arbitrarily decided parameters for all algorithms. | ||
""" | ||
|
||
|
||
# Edges between nodes in the same county should be weighted less than those that cross, because | ||
# the maximum spanning tree should be made more likely to choose an edge crossing county lines. | ||
SAME_COUNTY_PENALTY = 0.5 | ||
|
||
POP_EQUALITY_THRESHOLD = 0.005 | ||
|
||
MINORITY_NAMES = ["black", "hispanic", "asian", "native", "islander"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
Given supercommunity edge lifetimes, uses simulated annealing to generate a map that minimizes | ||
the average border edge lifetime while conforming to redistricting requirements. | ||
""" | ||
|
||
from functools import partial | ||
import random | ||
|
||
from gerrychain import (GeographicPartition, Partition, Graph, MarkovChain, | ||
proposals, updaters, constraints, accept, Election) | ||
from gerrychain.proposals import recom | ||
from gerrychain.tree import bipartition_tree | ||
from networkx import tree | ||
|
||
from .util import get_num_vra_districts, get_county_weighted_random_spanning_tree | ||
|
||
|
||
class SimulatedAnnealingChain(MarkovChain): | ||
"""Augments gerrychain.MarkovChain to take both the current state and proposal in the `accept` | ||
function. | ||
""" | ||
def __next__(self): | ||
if self.counter == 0: | ||
self.counter += 1 | ||
return self.state | ||
|
||
while self.counter < self.total_steps: | ||
proposed_next_state = self.proposal(self.state) | ||
# Erase the parent of the parent, to avoid memory leak | ||
if self.state is not None: | ||
self.state.parent = None | ||
|
||
if self.is_valid(proposed_next_state): | ||
if self.accept(self.state, proposed_next_state): | ||
self.state = proposed_next_state | ||
self.counter += 1 | ||
return self.state | ||
raise StopIteration | ||
|
||
|
||
def accept_proposal(temperature, current_energy, proposed_energy): | ||
"""Simple simulated-annealing acceptance function. | ||
""" | ||
if current_energy > proposed_energy or random.random() < temperature: | ||
return True | ||
return False | ||
|
||
|
||
def generate_districts_simulated_annealing(graph, edge_lifetimes, num_vra_districts, vra_threshold, | ||
pop_equality_threshold): | ||
"""Returns the 10 best maps and a dataframe of statistics for the entire chain. | ||
""" | ||
|
||
weighted_recom_proposal = partial( | ||
recom, | ||
method=partial( | ||
bipartition_tree, | ||
spanning_tree_fn=get_county_weighted_random_spanning_tree) | ||
) | ||
|
||
|
||
def optimize(): | ||
""" | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters