-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
166 lines (139 loc) · 5.62 KB
/
tools.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
# copyright 2020 Andrzej Kaczmarczyk (andrzej >dot> kaczmarczyk <at> agh.edu.pl; a <dot> kaczmarczyk <at> tu-berlin.de)
# This file is part of mul-win-just-pub.
# mul-win-just-pub is licensed under the terms of MIT license
# see LICENSE.txt for the text of the lincense
def committeApprovalAndCoverage(candidates, voters, committee):
commSet = set(committee)
coverageCounter=0
for voter in voters.values():
if len(commSet & set(voter))>0:
coverageCounter = coverageCounter + 1
approvalCounter = 0
for cand in committee:
for voter in voters.values():
if cand in voter:
approvalCounter = approvalCounter + 1
return coverageCounter, approvalCounter
def loadPrefLibPartialOrder(filepath, groupsApproved):
profile = {}
multiplicities = {}
with open(filepath, 'r') as infile:
counter = 0
votersnr = None
candnr = None
for line in infile:
counter = counter + 1
if counter == 1:
candnr = int(line.strip())
continue
if counter > 1 and counter <= candnr + 1:
continue
if counter == candnr + 2:
votersnr = int(line.strip().split(",")[0])
continue
lineparts = line.strip().split(",")
multiplicity = int(lineparts[0])
groups = {}
partnr = 1
# print lineparts
while partnr <= len(lineparts[1:]):
part = lineparts[partnr]
if part == "{}" or part == "":
groupApproved = []
elif part.startswith("{"):
endpartnr = partnr
while not lineparts[endpartnr].endswith("}"):
endpartnr = endpartnr + 1
groupApproved = reduce(lambda acc, elem: acc + [int(elem)-1],
(",".join(lineparts[partnr:endpartnr+1])[1:-1]).split(","), [])
partnr = endpartnr
else:
groupApproved = [int(part)-1]
groups[len(groups)] = groupApproved
partnr = partnr + 1
approved = reduce(lambda acc, elem: acc + elem, [g for gnr, g in
groups.items() if gnr<groupsApproved], [])
nextVoteNr = len(profile)
profile[nextVoteNr] = approved
multiplicities[nextVoteNr] = multiplicity
return profile, range(candnr), multiplicities
class Mesh(object):
def _computeRanges(self, minVal, maxVal, parts):
step = maxVal//parts
lowerBounds = range(minVal, maxVal+1, step)
ranges = [(lowerBound,lowerBound+step-1) for lowerBound in lowerBounds]
if ranges[-1][1] != maxVal:
ranges[-1] = (ranges[-1][0], maxVal)
return ranges
def _initializeCells(self):
coverageRanges = self._computeRanges(1, self._maxCoverage, self.coverageParts)
approvalRanges = self._computeRanges(1, self._maxApproval, self.approvalParts)
self._cells = {cR + aR: self._initVal for cR in coverageRanges for aR in approvalRanges}
self._cellStatus = {cell: True for cell in self._cells}
def _initializeCellsCoordinates(self):
""" Starting from 1 (not from 0)"""
self._cellCoordinate = {}
colCounter = 0
rowCounter = self.coverageParts
for cell in self._sortCells_Fragile(self._cells):
self._cellCoordinate[cell] = (rowCounter, colCounter + 1)
colCounter = (colCounter + 1) % self.approvalParts
if colCounter == 0:
rowCounter = rowCounter - 1
def _sortCells_Fragile(self, cells):
return sorted(cells, key = lambda cell: (-cell[0], cell[2]))
def _sortCells(self, cells):
return self._sortCells_Fragile(cells)
def getClippedCells(self):
return self._sortCells([c for c in self._cells if self._cellStatus[c] is False])
def getUnclippedCells(self):
return self._sortCells([c for c in self._cells if self._cellStatus[c] is True])
def getAllCells(self):
return self._sortCells(self._cells)
def setValueOfCell(self, cell, value):
self._cells[cell] = value
def getValueOfCell(self, cell, value):
return self._cells[cell]
def findAndSetValue(self, coverage, approval, value):
cell = self.getCellFromValues(coverage, approval)
self.setValueOfCell(cell, value)
def getCellFromValues(self, coverage, approval):
for cell in self._cells.keys():
cCLB, cCUB, cALB, cAUB = cell
if cCLB <= coverage and cCUB >= coverage and cALB <= approval and cAUB >= approval:
return cell
def getCellCoordinate(self, cell):
return self._cellCoordinate[cell]
def clipCell(self, cell):
self._cellStatus[cell] = False
def unclipCell(self, cell):
self._cellStatus[cell] = True
def clipMesh(self, fromDownC, fromUpC, fromDownA, fromUpA):
for cell, coordinate in self._cellCoordinate.items():
covCoor, appCoor = coordinate
if covCoor <= fromDownC or covCoor + fromUpC > self.coverageParts or \
appCoor <= fromDownA or appCoor + fromUpA > self.approvalParts:
self.clipCell(cell)
def clipMeshByValues(self, minC, maxC, minApp, maxApp):
for cell in self._cells.keys():
cCLB, cCUB, cALB, cAUB = cell
if cCUB < minC or cCLB > maxC or cAUB < minApp or cALB > maxApp:
self.clipCell(cell)
def depict(self, outStream):
columnCounter = 0
for cell in self.getAllCells():
outStream.write(str(self._cells[cell]))
columnCounter = (columnCounter+1) % self.approvalParts
if columnCounter == 0:
outStream.write("\n")
def __init__(self, candidatesNr, votersNr, committeeSize, coverageParts, approvalParts, initVal="."):
self.candidatesNr = candidatesNr
self.committeeSize = committeeSize
self.coverageParts = coverageParts
self.approvalParts = approvalParts
self.votersNr = votersNr
self._initVal = initVal
self._maxApproval = committeeSize*votersNr
self._maxCoverage = votersNr
self._initializeCells()
self._initializeCellsCoordinates()