-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudit_n_seats_fwc.py
executable file
·1700 lines (1236 loc) · 55.1 KB
/
audit_n_seats_fwc.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (C) 2021 Michelle Blom
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse
import os
import numpy as np
import sys
from functools import partial
from multiprocessing import Pool
from multiprocessing.pool import ThreadPool
from utils import read_outcome, sample_size, ssm_sample_size,\
tally_vs_tally_sample_size, read_ballots_txt, index_of, next_cand, \
read_ballots_json, read_ballots_txt, read_ballots_blt, \
read_ballots_stv, simulate_stv
# Determine if there is a candidate 'd' for which 'd' AG loser, based
# on the AG relationships in neb_matrix, preferenced before 'loser' in
# the preference ranking 'prefs'. Return a boolean indicating whether
# there is such a candidate 'd', and the ASN of the cheapest AG
# relationship (if there are multiple such 'd's).
#
# ag_matrix[d][loser] will give ASN of 'd' AG loser or None if
# such an assertion does not exist.
#
# The function returns pair:
#
# ag_present, ag_min_ss
#
# where neb_present is a boolean indicating whether we found a
# 'd' such that 'd' AG loser, and 'd' is preferenced before loser
# in prefs.
#
# If loser is not present in the ranking 'prefs' the function will
# return:
#
# False, np.inf
def rule_out_for_max(prefs, loser, ag_matrix, nl_matrix, winners, candidates):
ag_min_ss = np.inf
ag_present = False
ags_used = set()
for p in prefs:
if p == loser:
return ag_present, ag_min_ss, ags_used
if p in winners:
# 'loser' could still get this vote as it could skip over 'p'
continue
ag = ag_matrix[p][loser]
nl,nl_supp = nl_matrix[p][loser] if nl_matrix != None else (None,set())
if ag != None and ag != np.inf:
ag_present = True
ags_used.add((p,"AG",loser, ag))
ag_min_ss = min(ag_min_ss, ag)
elif nl != None and nl != np.inf:
ag_present = True
ags_used.update(nl_supp)
ag_min_ss = min(ag_min_ss, nl)
return False, np.inf, set()
# Merge a sequence of AG relationships that could be used to increase the
# assorter margin of an NL.
#
# Input list 'helpful_ags' will be a list of (asn, extra),
# where 'asn' is the cost of auditing the AG assertion, and 'extra' is
# the increase to the assorter total if we incorporate those AGs. This
# function takes a list of these AGs, and merges consecutive entries if: the
# ASN is the same.
def merge_helpful_ags(helpful_ags, exp_merged_total):
helpful_ags.sort()
merged_helpful_ags = []
merged_total = 0
if helpful_ags != []:
cntr = 1
curr_ag_asn = helpful_ags[0][0]
curr_extra = helpful_ags[0][1]
curr_desc = helpful_ags[0][2]
lhelpfuls = len(helpful_ags)
to_add = False
while cntr < lhelpfuls:
ag_asn, extra, desc = helpful_ags[cntr]
if ag_asn != curr_ag_asn:
merged_helpful_ags.append((curr_ag_asn, curr_extra, curr_desc))
merged_total += curr_extra
curr_ag_asn = ag_asn
curr_extra = extra
curr_desc = desc
else:
curr_extra += extra
curr_desc.update(desc)
cntr += 1
merged_helpful_ags.append((curr_ag_asn, curr_extra, curr_desc))
merged_total += curr_extra
assert(merged_total >= exp_merged_total-0.00001 \
and merged_total <= exp_merged_total+0.00001)
return merged_helpful_ags
def compute_ag_matrix(candidates, ballots, ag_matrix, \
INVALID, args, log=None):
for cand_w in candidates:
w = cand_w.num
for cand_c in candidates:
c = cand_c.num
if c == w:
continue
min_w = 0
max_c = 0
# assertion: fpc(w) > maxc
assorter = INVALID*0.5 # h(b) = ((b_w - b_c) + 1)/2
for b in ballots:
if b.prefs != []:
if b.prefs[0] == w:
# assorter value is 1 per vote
assorter += b.votes
min_w += b.votes
continue
if b.prefs[0] == c:
# assorter value is 0 per vote
max_c += b.votes
continue
# Default contribution of each instance of ballot type
# to assorter.
contrib = 0.5 * b.votes
for p in b.prefs:
if p == w:
break
if p == c:
contrib = 0
max_c += b.votes
break
assorter += contrib
# Compute assorter margin
amean = assorter/args.voters
if amean > 0.5:
ss = sample_size(amean, args)
ag_matrix[cand_w.num][c] = ss
if log != None:
print("AG({},{}) = {}".format(cand_w.id, cand_c.id, \
ss), file=log)
def compute_ag_stars(winners, losers, candidates, ballots, ag_matrix, \
first_winners, min_tvs, aud_tvs, INVALID, args, desc):
for w in winners:
cand_w = candidates[w]
for c in losers:
if c == w:
continue
cand_c = candidates[c]
min_w = 0
max_c = 0
# assertion: fpc(w) > maxc
assorter = INVALID*0.5 # h(b) = ((b_w - b_c) + 1)/2
for b in ballots:
if b.prefs != []:
if b.prefs[0] == w:
# assorter value is 1 per vote
assorter += b.votes
min_w += b.votes
continue
if b.prefs[0] == c:
# assorter value is 0 per vote
max_c += b.votes
continue
# Default contribution of each instance of ballot type
# to assorter.
contrib = 0.5 * b.votes
counted = False
if w in b.prefs:
# Check if all preceding candidates are first winners
prior = b.prefs[:b.prefs.index(w)]
if prior != [] and all([p in first_winners for p in prior]):
# Candidate c gets votes at smallest of transfer
# values across winneres in prior
min_tv = min([min_tvs[p] for p in prior])
min_w += min_tv * b.votes
contrib = b.votes * ((1 + min_tv)/2.0)
counted = True
if not counted:
weight = 1
if c in b.prefs:
prior = b.prefs[:b.prefs.index(c)]
atvs = [aud_tvs[p] for p in prior if p in first_winners]
if atvs != []:
weight = max(atvs)
for p in b.prefs:
if p == w:
break
if p == c:
contrib = b.votes * ((1 - weight)/2.0)
max_c += weight*b.votes
break
assorter += contrib
# Compute assorter margin
amean = assorter/args.voters
if amean > 0.5:
ss = sample_size(amean, args)
ag_matrix[w][c] = ss
desc += "AG*({},{}) = {}\n".format(cand_w.id, \
cand_c.id, ss)
def get_max_ballot_weight(prefs, first_winners, aud_tv, elected, maxtv,\
eliminated, standing):
weights = []
fw_prefix = []
if prefs[0] in elected:
return maxtv, [] # Reducing this will be helpful!
for p in prefs:
if p in eliminated:
continue
if p in standing:
return 0, []
if p in first_winners:
weights.append(aud_tv[p])
fw_prefix.append(p)
else:
break
if weights == []:
return 1, []
else:
return weights[0], fw_prefix
def establish_max_tvalue_nonfw(w, candidates, ballots, winners_on_fp, \
min_tvs, aud_tvs, ag_matrix, maxtv, INVALID, args, curr_asn):
info = " curr_asn = {}\n".format(curr_asn)
# Verify that 'w' did not receive a quota on first preference NIQ(w)
# We need to do this as this approach of bounding w's transfer value
# relies on them receiving a quota by the distribution of votes from
# another candidate (either via a surplus or elimination).
cand_w = candidates[w]
if cand_w.fp_votes >= args.quota:
info = "Cannot form NIQ({}) as they have a quota on FPs\n".format(\
cand_w.id)
return maxtv, 0, set(), info
# Show that total of everyone ELSE's fp votes is more than seats-1 quotas.
thresh = 1 - (1.0/(args.seats + 1));
valid_ballots = args.voters - INVALID
others_total = valid_ballots - cand_w.fp_votes
asn = ssm_sample_size(thresh, others_total, INVALID, args)
if asn >= args.voters:
info = "Cannot form NIQ({}); too expensive.\n".format(cand_w.id)
return maxtv, 0, set(), info
# Find how much each candidate would transfer to w on their elimination
# (if they are not a candidate in winners_on_fp, or election otherwise).
max_surpluses = {}
max_overall = 0
tot_vote = 0
# Assuming a context where each ballot may have a value < 1
weighted_ballots = []
for b in ballots:
if b.prefs == []:
continue
p = b.prefs[0]
weight = aud_tvs[p] if p in winners_on_fp else 1
tot_vote += weight*b.votes
weighted_ballots.append((b, weight))
for cand in candidates:
c = cand.num
if c == w:
continue
# Use aud_tvs as maximum transfer value for any ballot that
# has a candidate in winners_on_fp as first preference and 1
# otherwise.
transfer = 0
tot_not_t = 0
# Record contribution of ballot type to "Not transfer" category, to
# total vote, and number of ballots of that type.
details = []
for (b,v) in weighted_ballots:
if b.prefs == []:
continue
win = b.prefs.index(w) if w in b.prefs else np.inf
cin = b.prefs.index(c) if c in b.prefs else np.inf
if w in b.prefs and c in b.prefs:
widx = b.prefs.index(w)
cidx = b.prefs.index(c)
if cidx < widx:
details.append((0, v, b.votes))
transfer += v * b.votes
else:
details.append((v, v, b.votes))
tot_not_t += v * b.votes
else:
details.append((v, v, b.votes))
tot_not_t += v * b.votes
max_surpluses[c] = (transfer, tot_not_t, details)
max_overall = max(max_overall, transfer)
# Try and establish a max_tr_tv that would cost less than or equal
# to 'curr_asn' to verify in an audit.
delta = 50
max_tr_tv_ss = np.inf
bar = max_overall
aset = set()
desc = ""
#valid_vote = args.voters - INVALID
#while max_tr_tv_ss > max(asn, curr_asn) and bar + delta < valid_vote: #tot_vote:
while max_tr_tv_ss > max(asn, curr_asn) and bar + delta < tot_vote:
bar += delta
# should we use total original vote or total vote in the context
# we are interested in? (ie. with first winners seated and their
# transfer values set to their maximum value).
thresh = (tot_vote - bar)/tot_vote
# We want to show that the bucket of votes in the category "will
# transfer to w in the event of election/elimination" is less than
# bar.
max_tr_tv_ss = 0
aset = set()
desc += "MAX SURPLUS ASSERTIONS:\n"
for c,(s,tnt,details) in max_surpluses.items():
ss = ssm_sample_size(thresh, tnt, INVALID, args)
# We want to show that p_s < thresh
# so p_tnt > t where t = 1 - thresh
#amean = 0.5*INVALID
#for cont_tnt, cont_v, num in details:
# amean += num*(((cont_tnt - thresh*cont_v) + thresh)/(2*thresh))
#amean /= args.voters
#ss = sample_size(amean, args)
max_tr_tv_ss = max(ss, max_tr_tv_ss)
aset.add((c, "SP[{}]".format(bar), None, ss))
desc += "SP[{},{}]({}) with ASN {}\n".format(bar, s, \
candidates[c].id, ss)
# if max_tr_tv_ss != np.inf and max_tr_tv_ss < 2*max(asn, curr_asn):
# break
max_tr_tv_ss = max(asn, max_tr_tv_ss)
max_tr_tv = maxtv
if max_tr_tv_ss <= curr_asn:
max_tr_tv = (bar/(bar+args.quota))
info += desc
info += "Reduce maxtv for {} to {} ASN {}\n".format(cand_w.id, \
max_tr_tv, max_tr_tv_ss)
if max_tr_tv < maxtv:
return max_tr_tv, max_tr_tv_ss, aset, info
# Return established maximum on transfer value, asn to verify this,
# set of assertions required to establish this, logging info
return maxtv, 0, set(), info
def form_NL(candidates, c, ow_i, ags, nls, ballots, INVALID, winners_on_fp, \
min_tvs, aud_tvs, ag_matrix, nl_matrix, winners, maxtv, elected, \
eliminated,standing):
cand_c = candidates[c]
min_ow_i = 0 # Min tally for ow_i.
max_c = 0 # Max tally for candidate c.
# Keep running tally of total votes we can increase the
# margin of assertion 'ow_i' NL 'c' with 'AG*'
# relationships
pot_margin_inc = 0
helpful_ags = []
nl_assertion_set = set()
# Assertion: min_ow_i > maxc
assorter = INVALID*0.5
for b in ballots:
if b.prefs != []:
if b.prefs[0] == ow_i.num:
assorter += b.votes
min_ow_i += b.votes
continue
if b.prefs[0] == c:
max_c += b.votes
continue
# is this a ballot for 'c' over 'ow_i'?
c_in = c in b.prefs
ow_in = ow_i.num in b.prefs
# Reducing upper bound on 'elected's transfer values down
# from maxtv will likely be helpful.
weight, fwprefix = get_max_ballot_weight(b.prefs,\
winners_on_fp, aud_tvs, elected, maxtv, \
eliminated, standing)
c_idx=b.prefs.index(c) if c_in else np.inf
o_idx=b.prefs.index(ow_i.num) if ow_in else np.inf
if c_in and c_idx < o_idx:
# These ballots could not go to 'ow' but may go to 'c'.
if weight == 0:
# There is someone still assumed to be standing before 'c'
assorter += 0.5*b.votes
else:
# Could we exclude ballots by using an AG*?
is_ag, ag_asn, descs = rule_out_for_max(\
b.prefs, c, ag_matrix, nl_matrix, winners, candidates)
contrib = b.votes*((1-weight)/2.0)
if is_ag:
alt_contrib = b.votes*0.5
helpful_ags.append((ag_asn, alt_contrib - contrib, descs))
pot_margin_inc += alt_contrib-contrib
assorter += contrib
max_c += b.votes
elif ow_in:
# Check if all candidates before ow_i are
# first winners.
exprefs = [p for p in b.prefs if not p in fwprefix and \
not p in eliminated]
if exprefs[0] == ow_i.num and fwprefix == []:
# Only candidates before ow_i are assumed to be eliminated
min_ow_i += b.votes
assorter += b.votes
elif exprefs[0] == ow_i.num and b.prefs[0] in fwprefix:
minval = min_tvs[fwprefix[0]]
min_ow_i += minval * b.votes
assorter += b.votes * ((1 + minval)/2.0)
else:
# If we remove all cand 'd' for which ow AG*
# d that appear before ow in the ballot,
# will 'ow' be the first ranked cand? If so,
# we could add these ballots to the min
# tally of 'ow'.
prefs = [p for p in b.prefs if not p in eliminated\
and not p in winners_on_fp]
descs = set()
max_ags_here = 0
minval = 1
if b.prefs != [] and b.prefs[0] in winners_on_fp:
minval = min_tvs[b.prefs[0]]
other_winners = [w for w in winners if not w in winners_on_fp]
skip_winner = False
for d,dval in ags.items():
if d in prefs:
idx_d = prefs.index(d)
if idx_d < o_idx:
prefs.remove(d)
o_idx -= 1
rag = (ow_i.num,"AG*",d,dval)
descs.add(rag)
max_ags_here=max(max_ags_here,dval)
if d in other_winners:
skip_winner = True
break
for d,(dval,dset) in nls.items():
if d in prefs:
idx_d = prefs.index(d)
if idx_d < o_idx:
prefs.remove(d)
o_idx -= 1
descs.update(dset)
max_ags_here=max(max_ags_here,dval)
if d in other_winners:
skip_winner = True
break
assorter += 0.5*b.votes
if not skip_winner:
if prefs != [] and prefs[0] == ow_i.num:
base_contrib = 0.5*b.votes
alt_contrib = b.votes * ((1 + minval)/2.0)
dconfig = alt_contrib-base_contrib
if dconfig > 0:
helpful_ags.append((max_ags_here, dconfig, descs))
pot_margin_inc += dconfig
#else:
# fwprefix = []
# minws = {}
# for p in prefs:
# if p in winners_on_fp:
# fwprefix.append(p)
# minws[p] = min_tvs[p]
# else:
# break
# if fwprefix != [] and prefs[len(fwprefix)] == ow_i.num:
# minval = minws[b.prefs[0]] if b.prefs[0]\
# in winners_on_fp else 1
# base_contrib = 0.5*b.votes
# alt_contrib = b.votes * ((1 + minval)/2.0)
# dconfig = alt_contrib-base_contrib
# if dconfig > 0:
# helpful_ags.append((max_ags_here, dconfig, descs))
# pot_margin_inc += dconfig
else:
assorter += 0.5*b.votes
# Max ASN of any AG*'s used to increase assorter
# margins when forming NLs.
max_ags_used = 0
merged_helpful_ags = merge_helpful_ags(helpful_ags, pot_margin_inc)
# Incorporate use of AG*'s that either make the
# assertion possible, or whose ASN is already
# within/equal to current lower bound on audit
# difficulty.
while assorter/args.voters <= 0.5 and merged_helpful_ags != []:
ag_asn, extra_contrib, descs = merged_helpful_ags.pop(0)
assorter += extra_contrib
max_ags_used = max(max_ags_used, ag_asn)
nl_assertion_set.update(descs)
# Can we reduce the sample size required for the
# assertion by including more AG*'s?
amean = assorter/args.voters
if amean > 0.5:
# Current sample size for assertion
ss = sample_size(amean, args)
# Go through remaining AG*'s
for ag_asn, extra_contrib, descs in \
merged_helpful_ags:
if ss < ag_asn:
break
# would reducing margin by votes reduce sample
# size by more than increase caused by including
# AG*?
assorter += extra_contrib
amean = assorter/args.voters
ss = sample_size(amean, args)
max_ags_used = max(max_ags_used, ag_asn)
nl_assertion_set.update(descs)
asn = max(ss, max_ags_used)
if asn >= args.voters:
return False, np.inf, None, "NL would require full hand count"
desc = "NL({},{}) = {}, AG*'s used {}\n".format(\
ow_i.id, cand_c.id, ss, max_ags_used)
nl_assertion_set.add((ow_i.num, "NL", c, \
max(ss,max_ags_used)))
return True, max(max_ags_used,ss), nl_assertion_set, desc
else:
desc = "NL({},{}) NOT POSSIBLE, AMEAN {}\n".format(\
ow_i.id, cand_c.id, amean)
return False, np.inf, None, desc
def compute_iqx(candidates, ow_i, ag_matrix, ballots, INVALID, args, min_tvs,
winners_on_fp, winners):
desc = ""
qthresh = 1.0/(args.seats + 1);
ags = {c : ag_matrix[ow_i.num][c] for c in losers \
if ag_matrix[ow_i.num][c] != None}
# Can we show that ow_i's first preferences plus all the
# ballots that would flow from candidates l for which
# AG*(ow_i, l) is greater than a quota?
totvotes = 0
ss_ag_iqx = 0
ss_iqx = np.inf
iqx_assertions = set()
helpful_ags = []
pot_margin_inc = 0
winners_not_fp = [w for w in winners if not w in winners_on_fp]
for b in ballots:
if b.prefs[0] == ow_i.num:
totvotes += b.votes
continue
if not ow_i.num in b.prefs:
continue
value = 1
if b.prefs[0] in winners_on_fp:
value = min_tvs[b.prefs[0]]
prefs = [p for p in b.prefs if not p in winners_on_fp]
if prefs[0] == ow_i.num:
totvotes += value*b.votes
continue
descs = set()
max_ags_here = 0
o_idx = prefs.index(ow_i.num)
for d,dval in ags.items():
if d in prefs:
idx_d = prefs.index(d)
if idx_d < o_idx:
prefs.remove(d)
o_idx -= 1
rag = (ow_i.num,"AG*",d,dval)
descs.add(rag)
max_ags_here=max(max_ags_here,dval)
if d in winners_not_fp:
# value of ballot uncertain
value = 0
break
if value > 0 and prefs[0] == ow_i.num:
helpful_ags.append((max_ags_here, value*b.votes, descs))
pot_margin_inc += value*b.votes
ss_ag_iqx = 0
merged_helpful_ags = merge_helpful_ags(helpful_ags, \
pot_margin_inc)
while totvotes < args.quota and \
merged_helpful_ags != []:
ag_asn, extra_contrib, descs = merged_helpful_ags.pop(0)
totvotes += extra_contrib
ss_ag_iqx = max(ss_ag_iqx, ag_asn)
iqx_assertions.update(descs)
if totvotes > args.quota:
ss = ssm_sample_size(qthresh, totvotes, INVALID, args)
for ag_asn, extra_contrib, descs in merged_helpful_ags:
if ss < ag_asn:
break
totvotes += extra_contrib
ss = ssm_sample_size(qthresh, totvotes, INVALID, args)
ss_ag_iqx = max(ss_ag_iqx, ag_asn)
iqx_assertions.update(descs)
iqx_assertions.add((ow_i.num, "IQX", None, ss))
desc = "Can form IQX({}) with sample size {}, AG*s {}\n".format(\
ow_i.id, ss, ss_ag_iqx)
ss_iqx = max(ss, ss_ag_iqx)
return True, ss_iqx, iqx_assertions, desc
return False, ss_iqx, set(), desc
def form_SB(nv, ow, candidates, ballots, winners_on_fp, winners, args, INVALID):
max_v_ow = 0
desc = ""
# These are winners that while they may have won on first preferences,
# we have not demonstrated that they have done so. Excluding nv and ow.
winners_not_on_fp = [w for w in winners if not w in winners_on_fp \
and w != nv and w != ow]
for b in ballots:
if b.prefs == []:
continue
ow_in = ow in b.prefs
if not ow_in:
continue
nv_in = nv in b.prefs
value = 1
if b.prefs[0] in winners_on_fp:
value = min_tvs[b.prefs[0]]
if ow_in and not nv_in:
max_v_ow += value*b.votes
else:
ow_idx = b.prefs.index(ow)
nv_idx = b.prefs.index(nv)
if ow_in < nv_in:
max_v_ow += value*b.votes
else:
# could ballot have skipped over 'nv'?
for wnfp in winners_not_on_fp:
if wnfp in b.prefs:
idx = b.prefs.index(wnfp)
if idx < ow_idx:
max_v_ow += value*b.votes
break
# if max_v_ow < args.quota:
#todo
# else:
# desc += "Cannot show that {} SB {}.\n".format(candidates[nv].id,\
# candidates[ow].id)
# return None, set(), desc
def inner_loop(winners_on_fp, args, candidates, cands, valid_ballots,\
INVALID,max_sample_size,mintv_ss,ballots,min_tvs,ows,fws,losers,winners, \
straight_iqx_verified, runner_up, aud_tvs):
np.seterr(all='ignore')
ag_matrix = [[None for c in candidates] for o in candidates]
desc = "---------------------------------------------\n"
desc += "START INNER LOOP\n"
inner_loop_assertions = set()
max_this_loop = 0
max_ss_mt = 0
winners_verified = winners_on_fp[:]
ut_asns = {}
# Check that TVs of the first winners i are at most aud_TV[i]
for f in winners_on_fp:
fw_i = candidates[f]
aud_tv_i = aud_tvs[f]
a = 1 / (1 - aud_tv_i)
thresholdT = a * valid_ballots / (args.seats + 1)
thresholdProp = thresholdT / valid_ballots
threshold = 1 - thresholdProp
tally_others = valid_ballots - fw_i.fp_votes
ss_i = ssm_sample_size(threshold,tally_others,INVALID,args)
ut_asns[f] = (aud_tv_i, ss_i)
desc += "AUD TV for {}, {}, ss {}\n".format(fw_i.id, \
aud_tv_i, ss_i)
inner_loop_assertions.add((fw_i.num, "MT", None, \
(aud_tv_i, ss_i)))
max_ss_mt = max(max_ss_mt, ss_i)
partial_ss = 0
# For remaining winners, is it the case that they cannot be
# eliminated prior to all reported losers? We will
# look at this from TWO perspectives. (1) compute AG*'s
# between the remaining winner r and opponents using our upper
# and lower bounds on the initial winner's transfer value. If
# we can form enough AG*'s, we can verify the winner.
# (2) Forming NL assertions, potentially making use of our
# set of AG* relationships. We will use the
# cheaper of the two options.
compute_ag_stars([ow_i.num for ow_i in ows], losers, \
candidates, ballots, ag_matrix, winners_on_fp, min_tvs, \
aud_tvs, INVALID, args, desc)
compute_ag_stars(losers, losers, candidates, ballots, \
ag_matrix, winners_on_fp, min_tvs, aud_tvs, INVALID, \
args, desc)
qthresh = 1.0/(args.seats + 1);
newly_verified = []
could_not_verify = []
nl_matrix = [[(None,set()) for c in candidates] for o in candidates]
for ow_i in ows:
ags = {c : ag_matrix[ow_i.num][c] for c in losers \
if ag_matrix[ow_i.num][c] != None}
success, ss_iqx, iqx_assertions, info = compute_iqx(candidates, \
ow_i, ag_matrix, ballots, INVALID, args, min_tvs, winners_on_fp,\
winners)
desc += "{}-{}\n".format(success, ss_iqx)
desc += info
nl_assertion_set = set()
max_with_nls_i = 0
# Determine NL's between original losers and winner ow_i
for c in cands:
if c in winners:
continue
successNL, asn, aset, info = form_NL(candidates, c, ow_i, ags, \
{}, ballots,INVALID,winners_on_fp, min_tvs,aud_tvs,ag_matrix,\
None, winners, maxtv, [], [], [])
desc += info
max_with_nls_i = max(max_with_nls_i, asn)
if successNL:
nl_assertion_set.update(aset)
nl_matrix[ow_i.num][c] = (asn, aset)
straight_iqx_asn = straight_iqx_verified[ow_i.num][0] if \
ow_i.num in straight_iqx_verified else np.inf
if ss_iqx < args.voters or max_with_nls_i < args.voters or \
straight_iqx_asn < args.voters:
winners_verified.append(ow_i.num)
newly_verified.append(ow_i.num)
partial_ss = max(partial_ss, min([ss_iqx, max_with_nls_i,\
straight_iqx_asn]))
desc += "{}-{}-{}\n".format(ss_iqx,max_with_nls_i,straight_iqx_asn)
if straight_iqx_asn < ss_iqx and straight_iqx_asn < max_with_nls_i:
desc += "CHOOSE STRAIGHT IQX\n"
max_this_loop = max(max_this_loop, straight_iqx_asn)
partial_ss = max(partial_ss, straight_iqx_asn)
inner_loop_assertions.update(straight_iqx_verified[ow_i.num][1])
elif ss_iqx < max_with_nls_i:
desc += "CHOOSE IQX over NLs\n"
max_this_loop = max(max_this_loop, ss_iqx)
partial_ss = max(partial_ss, ss_iqx)
inner_loop_assertions.update(iqx_assertions)
else:
max_this_loop = max(max_this_loop, max_with_nls_i)
inner_loop_assertions.update(nl_assertion_set)
else:
could_not_verify.append(ow_i.num)
nv_maxtvs = {}
ub = max(partial_ss, max_sample_size)
outer_asn = max(mintv_ss, max_ss_mt)
if outer_asn < args.voters:
ub = max(ub, outer_asn)
for nv in newly_verified:
max_tr_tv, max_tr_tv_asn, max_tr_tv_aset, max_tr_tv_info = \
establish_max_tvalue_nonfw(nv, candidates, ballots, winners_on_fp,\
min_tvs, aud_tvs, ag_matrix, maxtv, INVALID, args, ub)
desc += max_tr_tv_info
nv_maxtvs[nv] = (max_tr_tv, max_tr_tv_asn, max_tr_tv_aset)
if could_not_verify != [] and newly_verified != []:
max_this_loop = partial_ss
# Can we show that any of the candidates in could_not_verify
# cannot achieve a quota before one of the candidates in
# newly_verified is seated?
#for ow in could_not_verify:
# for nv in newly_verified:
# Compute max vote of ow in context where
# nv is still standing.
# form_SB(nv, ow, candidates, ballots, )
improvement = True
while improvement and could_not_verify != []:
improvement = False