-
Notifications
You must be signed in to change notification settings - Fork 15
/
grid_chain_complicated.py
432 lines (358 loc) · 12.3 KB
/
grid_chain_complicated.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# Import for I/O
import random
# import matplotlib
# matplotlib.use("Agg")
import matplotlib.pyplot as plt
import math
from functools import partial
import seaborn as sns
# Imports for GerryChain components
# You can look at the list of available functions in each
# corresponding .py file.
from gerrychain.proposals import recom
from gerrychain import MarkovChain
from gerrychain.constraints import (
Validator,
single_flip_contiguous,
within_percent_of_ideal_population,
)
from gerrychain.accept import always_accept
from gerrychain.updaters import Tally, cut_edges
from gerrychain.partition import Partition
import networkx as nx
def create_graph():
graph = nx.grid_graph([k * gn, k * gn])
for e in graph.edges():
graph[e[0]][e[1]]["shared_perim"] = 1
graph.remove_nodes_from(
[(0, 0), (0, k * gn - 1), (k * gn - 1, 0), (k * gn - 1, k * gn - 1)]
)
cddict = {x: int(x[0] / gn) for x in graph.nodes()}
for n in graph.nodes():
graph.nodes[n]["population"] = 1
graph.nodes[n]["part_sum"] = cddict[n]
graph.nodes[n]["last_flipped"] = 0
graph.nodes[n]["num_flips"] = 0
if random.random() < p:
graph.nodes[n]["pink"] = 1
graph.nodes[n]["purple"] = 0
else:
graph.nodes[n]["pink"] = 0
graph.nodes[n]["purple"] = 1
if 0 in n or k * gn - 1 in n:
graph.nodes[n]["boundary_node"] = True
graph.nodes[n]["boundary_perim"] = 1
else:
graph.nodes[n]["boundary_node"] = False
# this part adds queen adjacency
# for i in range(gn-1):
# for j in range(gn):
# if j<(gn-1):
# graph.add_edge((i,j),(i+1,j+1))
# graph[(i,j)][(i+1,j+1)]["shared_perim"]=0
# if j >0:
# graph.add_edge((i,j),(i+1,j-1))
# graph[(i,j)][(i+1,j-1)]["shared_perim"]=0
return graph, cddict
def step_num(partition):
parent = partition.parent
if not parent:
return 0
return parent["step_num"] + 1
def slow_reversible_propose(partition):
"""Proposes a random boundary flip from the partition in a reversible fasion
by selecting a boundary node at random and uniformly picking one of its
neighboring parts.
Temporary version until we make an updater for this set.
:param partition: The current partition to propose a flip from.
:return: a proposed next `~gerrychain.Partition`
"""
b_nodes = {x[0] for x in partition["cut_edges"]}.union(
{x[1] for x in partition["cut_edges"]}
)
flip = random.choice(list(b_nodes))
neighbor_assignments = list(
set(
[
partition.assignment[neighbor]
for neighbor in partition.graph.neighbors(flip)
]
)
)
neighbor_assignments.remove(partition.assignment[flip])
flips = {flip: random.choice(neighbor_assignments)}
return partition.flip(flips)
def reversible_propose(partition):
boundaries1 = {x[0] for x in partition["cut_edges"]}.union(
{x[1] for x in partition["cut_edges"]}
)
flip = random.choice(list(boundaries1))
return partition.flip({flip: -partition.assignment[flip]})
def cut_accept(partition):
boundaries1 = {x[0] for x in partition["cut_edges"]}.union(
{x[1] for x in partition["cut_edges"]}
)
boundaries2 = {x[0] for x in partition.parent["cut_edges"]}.union(
{x[1] for x in partition.parent["cut_edges"]}
)
bound = 1
if partition.parent is not None:
bound = (
base ** (-len(partition["cut_edges"]) + len(partition.parent["cut_edges"]))
) * (len(boundaries1) / len(boundaries2))
return random.random() < bound
def annealing_cut_accept(partition, t):
boundaries1 = {x[0] for x in partition["cut_edges"]}.union(
{x[1] for x in partition["cut_edges"]}
)
boundaries2 = {x[0] for x in partition.parent["cut_edges"]}.union(
{x[1] for x in partition.parent["cut_edges"]}
)
if t < 50000:
beta = 0
elif t < 200000:
beta = (t - 50000) / 50000
else:
beta = 3
bound = 1
if partition.parent is not None:
bound = (
base
** (
beta
* (-len(partition["cut_edges"]) + len(partition.parent["cut_edges"]))
)
) * (len(boundaries1) / len(boundaries2))
# bound = min(1, (how_many_seats_value(partition, col1="G17RATG",
# col2="G17DATG")/how_many_seats_value(partition.parent, col1="G17RATG",
# col2="G17DATG"))**2 )
# for some states/elections probably want to add 1 to denominator so
# you don't divide by zero
return random.random() < bound
def annealing_cut_accept2(partition):
boundaries1 = {x[0] for x in partition["cut_edges"]}.union(
{x[1] for x in partition["cut_edges"]}
)
boundaries2 = {x[0] for x in partition.parent["cut_edges"]}.union(
{x[1] for x in partition.parent["cut_edges"]}
)
t = partition["step_num"]
if t < 100000:
beta = 0
elif t < 400000:
beta = (t - 100000) / 100000 # was 50000)/50000
else:
beta = 3
bound = 1
if partition.parent is not None:
exponent = beta * (
-len(partition["cut_edges"]) + len(partition.parent["cut_edges"])
)
bound = (base ** (exponent)) * (len(boundaries1) / len(boundaries2))
# maybe_bound = (
# how_many_seats_value(partition, col1="G17RATG", col2="G17DATG")
# / how_many_seats_value(partition.parent, col1="G17RATG", col2="G17DATG")
# ) ** 2
# bound = min(1, maybe_bound)
# for some states/elections probably want to add 1 to denominator
# so you don't divide by zero
return random.random() < bound
gn = 10
k = 5
ns = 200
p = 0.4
for exp_num in [40, 20, 1]: # range(22,31):
for pop_bal in [5, 10, 50]: # [10,15,20,25,30,35,40,45,50]
base = exp_num / 10 # 1/math.pi
graph, cddict = create_graph()
# Necessary updaters go here
updaters = {
"population": Tally("population"),
"cut_edges": cut_edges,
"step_num": step_num,
}
# updaters={}
# election_updaters=dict()
# election_updaters["Pink-Purple"]=Election("Pink-Purple",{"Pink":"pink","Purple":"purple"},alias="Pink-Purple")
gp3 = Partition(graph, assignment=cddict, updaters=updaters)
ideal_population = sum(gp3["population"].values()) / len(gp3)
proposal = partial(
recom,
pop_col="population",
pop_target=ideal_population,
epsilon=0.02,
node_repeats=1,
)
popbound = within_percent_of_ideal_population(gp3, 0.05)
g3chain = MarkovChain(
proposal, # propose_chunk_flip,
Validator([popbound]),
accept=always_accept,
initial_state=gp3,
total_steps=100,
)
t = 0
for part3 in g3chain:
t += 1
print("finished tree walk")
pos_dict = {n: n for n in graph.nodes()}
pos = pos_dict
plt.figure()
plt.title("Starting Point")
nx.draw(
graph,
pos,
node_color=[part3.assignment[x] for x in graph.nodes()],
node_size=ns,
node_shape="s",
cmap="tab20",
)
plt.title("Starting Point")
plt.savefig(
"./plots/complicated/start_exp_"
+ str(exp_num)
+ "_"
+ str(pop_bal)
+ "pop.png"
)
plt.close()
gp = Partition(graph, assignment=dict(part3.assignment), updaters=updaters)
popbound = within_percent_of_ideal_population(gp, pop_bal / 100)
gchain = MarkovChain(
slow_reversible_propose, # propose_random_flip,#propose_chunk_flip, # ,
Validator([single_flip_contiguous, popbound]),
accept=annealing_cut_accept2, # aca,#cut_accept,#always_accept,#
initial_state=gp,
total_steps=500000,
)
pos_dict = {n: n for n in graph.nodes()}
pos = pos_dict
ce_hist = []
t = 0
cuts = []
for part in gchain:
cuts.append(len(part["cut_edges"]))
if part.flips is not None:
f = list(part.flips.keys())[0]
# graph.node[f]["part_sum"]=graph.node[f]["part_sum"]-part.assignment[f]*(t-graph.node[f]["last_flipped"])
# graph.node[f]["last_flipped"]=t
graph.node[f]["num_flips"] = graph.node[f]["num_flips"] + 1
# mm_hist.append(mean_median(part["Pink-Purple"]))
# ce_hist.append(len(part["cut_edges"]))
# print(len(part["cut_edges"]))
# if t % 200 == 0:
# plt.figure()
# nx.draw(
# graph,
# pos,
# node_color=[part.assignment[x] for x in graph.nodes()],
# node_size=ns,
# ) # ,cmap="tab20")
# # plt.savefig("./plots/GRIDn_"+str(int(t/1000))+".png")
# plt.show()
t += 1
if t % 50000 == 0:
print(t)
plt.figure()
plt.title(str(t) + "Steps")
nx.draw(
graph,
pos,
node_color=[part.assignment[x] for x in graph.nodes()],
node_size=ns,
node_shape="s",
cmap="tab20",
)
plt.title("Ending Point")
plt.savefig(
"./plots/complicated/middle"
+ str(t)
+ "2_"
+ str(exp_num)
+ "_"
+ str(pop_bal)
+ "pop.png"
)
plt.close()
# print(t)
for n in graph.nodes():
if graph.node[n]["last_flipped"] == 0:
graph.node[n]["part_sum"] = t * part.assignment[n]
graph.node[n]["num_flips"] = math.log(graph.node[n]["num_flips"] + 1)
print("finished flip")
# plt.figure()
# plt.title("Starting Point")
# nx.draw(
# graph,
# pos,
# node_color=[cddict[x] for x in graph.nodes()],
# node_size=ns,
# cmap="tab20",
# )
# plt.title("Starting Point")
# plt.show()
plt.figure()
plt.title("Ending Point")
nx.draw(
graph,
pos,
node_color=[part.assignment[x] for x in graph.nodes()],
node_size=ns,
node_shape="s",
cmap="tab20",
)
plt.title("Ending Point")
plt.savefig(
"./plots/complicated/newend2_"
+ str(exp_num)
+ "_"
+ str(pop_bal)
+ "pop.png"
)
# plt.show()
# plt.figure()
# plt.title("Weighted Community Assignment")
# nx.draw(
# graph,
# pos,
# node_color=[graph.nodes[x]["part_sum"] for x in graph.nodes()],
# node_size=ns,
# node_shape="s",
# cmap="jet",
# )
# plt.title("Weighted Community Assignment")
# plt.savefig(".//plotswca2_" + str(exp_num) + "_" + str(pop_bal) + "pop.png")
# # plt.show()
plt.figure()
plt.title("Flips")
nx.draw(
graph,
pos,
node_color=[graph.nodes[x]["num_flips"] for x in graph.nodes()],
node_size=ns,
node_shape="s",
cmap="jet",
)
plt.title("Flips")
plt.savefig(
"./plots/complicated/flips_" + str(exp_num) + "_" + str(pop_bal) + "pop.png"
)
plt.close()
plt.figure()
plt.title("Cut Lengths")
plt.plot(cuts)
plt.savefig(
"./plots/complicated/cuts_" + str(exp_num) + "_" + str(pop_bal) + "pop.png"
)
plt.close()
plt.figure()
plt.title("Cut Lengths")
sns.distplot(cuts, bins=100, kde=False)
plt.savefig(
"./plots/complicated/cuthist_"
+ str(exp_num)
+ "_"
+ str(pop_bal)
+ "pop.png"
)
plt.close()