Skip to content

Commit

Permalink
test speedup ideas for simulated annealing
Browse files Browse the repository at this point in the history
  • Loading branch information
formularin committed Feb 24, 2023
1 parent bb3bcf1 commit f57faec
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 188 deletions.
191 changes: 28 additions & 163 deletions examples/ensemble_analysis.ipynb

Large diffs are not rendered by default.

70 changes: 59 additions & 11 deletions examples/optimization.ipynb

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions rba/data/2010/vra_md.json
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": 4,
"opportunity_threshold": 0.51
}
35 changes: 21 additions & 14 deletions rba/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from gerrychain import Partition, Graph, MarkovChain, updaters, constraints, accept
from gerrychain.constraints import Validator
from gerrychain.proposals import recom
from gerrychain.tree import recursive_tree_part, bipartition_tree
from gerrychain.tree import recursive_tree_part, bipartition_tree, uniform_spanning_tree
import gerrychain.random
import networkx as nx
import pandas as pd
Expand All @@ -24,6 +24,7 @@
from .district_quantification import quantify_gerrymandering
from .util import (get_num_vra_districts, load_districts, get_county_spanning_forest,
save_assignment, choose_cut)
from .visualization import visualize_partition_geopandas


class SimulatedAnnealingChain(MarkovChain):
Expand Down Expand Up @@ -179,25 +180,31 @@ def generate_districts_simulated_annealing(graph, edge_lifetimes, num_vra_distri

initial_partition = Partition(graph, initial_assignment, sa_updaters)

county_recom_proposal = partial(
recom,
visualize_partition_geopandas(initial_partition)

# county_recom_proposal = partial(
# recom,
# pop_col="total_pop",
# pop_target=ideal_population,
# epsilon=constants.POP_EQUALITY_THRESHOLD,
# node_repeats=2,
# method=partial(
# bipartition_tree,
# spanning_tree_fn=get_county_spanning_forest,
# choice=partial(choose_cut, graph=graph))
# )

recom_proposal = partial(recom,
pop_col="total_pop",
pop_target=ideal_population,
epsilon=constants.POP_EQUALITY_THRESHOLD,
node_repeats=2,
method=partial(
bipartition_tree,
spanning_tree_fn=get_county_spanning_forest,
choice=partial(choose_cut, graph=graph))
spanning_tree_fn=lambda G: Graph(uniform_spanning_tree(G, lambda x: random.choice(tuple(x))))
)
)

# recom_proposal = partial(recom,
# pop_col="total_pop",
# pop_target=ideal_population,
# epsilon=constants.POP_EQUALITY_THRESHOLD,
# node_repeats=2
# )

pop_constraint = constraints.within_percent_of_ideal_population(initial_partition,
pop_equality_threshold)

Expand All @@ -221,8 +228,8 @@ def generate_districts_simulated_annealing(graph, edge_lifetimes, num_vra_distri
get_temperature=partial(
SimulatedAnnealingChain.COOLING_SCHEDULES[cooling_schedule],
num_steps=num_steps),
proposal=county_recom_proposal,
# proposal=recom_proposal,
# proposal=county_recom_proposal,
proposal=recom_proposal,
constraints=[
pop_constraint,
# compactness_bound
Expand Down

0 comments on commit f57faec

Please sign in to comment.