-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
vse.py
167 lines (138 loc) · 5.23 KB
/
vse.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
from imp import reload
from mydecorators import autoassign, cached_property, setdefaultattr, timeit
import random
from numpy.lib.scimath import sqrt
from numpy.core.fromnumeric import mean, std
from numpy.lib.function_base import median
from numpy.ma.core import floor
from test.test_binop import isnum
from debugDump import *
from voterModels import *
from stratFunctions import *
from methods import *
from uuid import uuid4
import csv, os
join = os.path.join
def uniquify(seq):
# order preserving
checked = []
for e in seq:
if e not in checked:
checked.append(e)
return checked
class CsvBatch:
@timeit
@autoassign
def __init__(self, model, methods, nvot, ncand, niter,
baseName = None, media=truth, seed=None, force=False):
"""A harness function which creates niter elections from model and finds three kinds
of utility for all methods given.
for instance:
>>> csvs = CsvBatch(PolyaModel(), [[Score(), baseRuns], [Mav(), medianRuns]], nvot=5, ncand=4, niter=3)
>>> len(csvs.rows)
54
"""
rows = []
emodel = str(model)
if (seed is None):
seed = (baseName or '') + str(niter)
self.seed = seed
random.seed(seed)
try:
from git import Repo
repo = Repo(os.getcwd())
if not force:
assert not repo.is_dirty()
self.repo_version = repo.head.commit.hexsha
except:
self.repo_version = 'unknown repo version'
for i in range(niter):
eid = uuid4()
electorate = model(nvot, ncand)
for method, chooserFuns in methods:
results = method.resultsTable(eid, emodel, ncand, electorate, chooserFuns, media=media)
rows.extend(results)
debug(i,results[1:3])
self.rows = rows
if baseName:
self.saveFile(baseName)
def saveFile(self, baseName="SimResults"):
"""print the result of doVse in an accessible format.
for instance:
csvs.saveFile()
"""
i = 1
while os.path.isfile(baseName + str(i) + ".csv"):
i += 1
keys = ["vse", "method", "chooser", *list(self.rows[0].keys())]
for n in range(4):
keys.extend([f"tallyName{str(n)}", f"tallyVal{str(n)}"])
keys = uniquify(keys)
with open(baseName + str(i) + ".csv", "w") as myFile:
print(
f"# {dict(media=self.media.__name__, version=self.repo_version, seed=self.seed, model=self.model, methods=self.methods, nvot=self.nvot, ncand=self.ncand, niter=self.niter)}",
file=myFile,
)
dw = csv.DictWriter(myFile, keys, restval = "NA")
dw.writeheader()
for r in self.rows:
dw.writerow(r)
medianRuns = [
OssChooser([beHon,ProbChooser([(1/2, beStrat), (1/2, beHon)])]),
ProbChooser([(1/4, beX), (3/4, beHon)]),
ProbChooser([(1/2, beX), (1/2, beHon)]),
ProbChooser([(3/4, beX), (1/4, beHon)]),
ProbChooser([(0.5, beStrat), (0.5, beHon)]),
ProbChooser([(1/3, beStrat), (1/3, beHon), (1/3, beX)]),
LazyChooser(),
ProbChooser([(1/2, LazyChooser()), (1/2, beHon)]),
]
baseRuns = [
OssChooser([beHon,ProbChooser([(1/2, beStrat), (1/2, beHon)])]),
ProbChooser([(1/4, beStrat), (3/4, beHon)]),
ProbChooser([(1/2, beStrat), (1/2, beHon)]),
ProbChooser([(3/4, beStrat), (1/4, beHon)]),
]
allSystems = [[Score(1000), baseRuns],
[Score(10), baseRuns],
[Score(2), baseRuns],
[Score(1), baseRuns],
[BulletyApprovalWith(.6), baseRuns],
[Srv(10), baseRuns],
[Srv(2), baseRuns],
[Plurality(), baseRuns],
[Borda(), baseRuns],
[Irv(), baseRuns],
[IrvPrime(), baseRuns],
[Schulze(), baseRuns],
[Rp(), baseRuns],
[V321(), baseRuns],
[Mav(), medianRuns],
[Mj(), medianRuns],
[IRNR(), baseRuns],
]
#request from Mark: "SRV0-2, SRV0-3, SRV0-4, SRV0-5, SRV0-6, SRV0-7, SRV0-8, SRV0-9, SRV0-10, Score0-10, 321, Approval, IRV and plurality"
markMethods = [
[Srv(2), baseRuns],
[Srv(3), baseRuns],
[Srv(4), baseRuns],
[Srv(5), baseRuns],
[Srv(6), baseRuns],
[Srv(7), baseRuns],
[Srv(8), baseRuns],
[Srv(9), baseRuns],
[Score(10), baseRuns],
[V321(), baseRuns],
[BulletyApprovalWith(.6), baseRuns],
[Irv(), baseRuns],
[Plurality(), baseRuns],
]
#usage example:
#>>> from vse import *
#>>> vses = CsvBatch(KSModel(dcdecay=(1,3),wcdecay=(1.5,3), dccut = .2, wcalpha=1.5),
# allSystems, nvot=40, ncand=6, niter=15000, baseName="target",
# media=fuzzyMediaFor())
if __name__ == "__main__":
import doctest
setDebug( False)
doctest.testmod()