-
Notifications
You must be signed in to change notification settings - Fork 2
/
schulze.go
502 lines (437 loc) · 14.2 KB
/
schulze.go
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
// Copyright (c) 2021, Janoš Guljaš <[email protected]>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package schulze implements the Schulze preferential voting method.
package schulze
import (
"fmt"
"sort"
"unsafe"
)
// NewPreferences initializes a fixed size slice that stores all pairwise
// preferences for voting. The resulting slice supposed to be updated by the
// Vote function with Ballot preferences and read by the Results function to
// order choices by their wins.
func NewPreferences(choicesLength int) []int {
return make([]int, choicesLength*choicesLength)
}
// Ballot represents a single vote with ranked choices. Lowest number represents
// the highest rank. Not all choices have to be ranked and multiple choices can
// have the same rank. Ranks do not have to be in consecutive order.
type Ballot[C comparable] map[C]int
// Record represents a single vote with ranked choices. It is a list of Ballot
// values. The first ballot is the list with the first choices, the second
// ballot is the list with the second choices, and so on. The last ballot is the
// list of choices that are not ranked, which can be an empty list.
type Record[C comparable] [][]C
// Vote updates the preferences passed as the first argument with the Ballot
// values. A record of a complete and normalized preferences is returned that
// can be used to unvote.
func Vote[C comparable](preferences []int, choices []C, b Ballot[C]) (Record[C], error) {
ranks, choicesCount, hasUnrankedChoices, err := ballotRanks(choices, b)
if err != nil {
return nil, fmt.Errorf("ballot ranks: %w", err)
}
for rank, choices1 := range ranks {
rest := ranks[rank+1:]
for _, i := range choices1 {
icc := int(i) * choicesCount
for _, choices1 := range rest {
for _, j := range choices1 {
preferences[icc+int(j)] += 1
}
}
}
}
ranksLen := len(ranks)
// set diagonal values as the values of the column of the least ranked
// choice to be able to have the correct preferences matrix when adding new
// choices
if hasUnrankedChoices {
// treat the diagonal values as one of the unranked choices,
// deprioritizing all choices except unranked as they are of the same
if ranksLen > 0 {
for _, choices1 := range ranks[:ranksLen-1] {
for _, i := range choices1 {
preferences[int(i)*choicesCount+int(i)] += 1
}
}
}
} else {
// all choices are ranked, tread diagonal values as a single not ranked
// choice, deprioritizing them for all existing choices
for i := 0; i < choicesCount; i++ {
preferences[int(i)*choicesCount+int(i)] += 1
}
}
// prepare results capacity to avoid allocation on appending the potential
// unranked choices
resultsCap := ranksLen
if !hasUnrankedChoices {
resultsCap++
}
r := make([][]C, ranksLen, resultsCap)
for rank, indexes := range ranks {
if r[rank] == nil {
r[rank] = make([]C, 0, len(indexes))
}
for _, index := range indexes {
r[rank] = append(r[rank], choices[index])
}
}
if !hasUnrankedChoices {
r = append(r, make([]C, 0))
}
return r, nil
}
// Unvote removes the Ballot values from the preferences.
func Unvote[C comparable](preferences []int, choices []C, r Record[C]) error {
choicesCount := len(choices)
recordLength := len(r)
if recordLength == 0 {
return nil
}
for rank, choices1 := range r {
rest := r[rank+1:]
for _, choice1 := range choices1 {
i := getChoiceIndex(choices, choice1)
if i < 0 {
continue
}
for _, choices1 := range rest {
for _, choice2 := range choices1 {
j := getChoiceIndex(choices, choice2)
if j < 0 {
continue
}
preferences[int(i)*choicesCount+int(j)] -= 1
}
}
}
}
knownChoices := newBitset(uint64(choicesCount))
rankedChoices := newBitset(uint64(choicesCount))
// remove voting from the ranked choices of the Record
//
// it is essential to have the last record ballot as
// unranked choices even if it is empty
for _, choices1 := range r[:recordLength-1] {
for _, choice1 := range choices1 {
i := getChoiceIndex(choices, choice1)
if i < 0 {
continue
}
preferences[int(i)*choicesCount+int(i)] -= 1
knownChoices.set(uint64(i))
rankedChoices.set(uint64(i))
}
}
// mark the rest of the known choices in the Record
for _, choice1 := range r[recordLength-1] {
i := getChoiceIndex(choices, choice1)
if i < 0 {
continue
}
knownChoices.set(uint64(i))
}
// remove votes of the choices that were added after the Record
for i := uint64(0); int(i) < choicesCount; i++ {
if rankedChoices.isSet(i) {
for j := uint64(0); int(j) < choicesCount; j++ {
if !knownChoices.isSet(j) {
preferences[int(i)*choicesCount+int(j)] -= 1
}
}
}
}
return nil
}
// SetChoices updates the preferences passed as the first argument by changing
// its values to accommodate the changes to the choices. It is required to
// pass the exact choices as the second parameter and complete updated choices
// as the third argument.
func SetChoices[C comparable](preferences []int, current, updated []C) []int {
currentLength := len(current)
updatedLength := len(updated)
updatedPreferences := NewPreferences(updatedLength)
for iUpdated := 0; iUpdated < updatedLength; iUpdated++ {
iCurrent := int(getChoiceIndex(current, updated[iUpdated]))
for j := 0; j < updatedLength; j++ {
if iUpdated < currentLength && updated[iUpdated] == current[iUpdated] && j < currentLength && updated[j] == current[j] {
updatedPreferences[iUpdated*updatedLength+j] = preferences[iUpdated*currentLength+j]
} else {
jCurrent := int(getChoiceIndex(current, updated[j]))
if iCurrent >= 0 {
if jCurrent >= 0 {
updatedPreferences[iUpdated*updatedLength+j] = preferences[iCurrent*currentLength+jCurrent]
} else {
// set the column of the new choice to the values of the
// preferences' diagonal values, just as nobody voted for the
// new choice to ensure consistency
updatedPreferences[iUpdated*updatedLength+j] = preferences[iCurrent*currentLength+iCurrent]
}
}
}
}
}
return updatedPreferences
}
type Choice[C comparable] struct {
// The choice value.
Value C
// 0-based ordinal number of the choice in the choice slice.
Index int
}
// Result represents a total number of wins for a single choice.
type Result[C comparable] struct {
// The choice value.
Choice C
// 0-based ordinal number of the choice in the choice slice.
Index int
// Number of wins in pairwise comparisons to other choices votings.
Wins int
// Total number of votes in the weakest link of the strongest path in wins
// in pairwise comparisons to other choices votings. Strength does not
// effect the winner, and may be less then the Strength of the choice with
// more wins.
Strength int
// Total number of preferred votes (difference between votes of the winner
// choice and the opponent choice) in the weakest link of the strongest path
// in wins in pairwise comparisons to other choices votings. Advantage does
// not effect the winner, and may be less then the Advantage of the choice
// with more wins. The code with less wins and greater Advantage had
// stronger but fewer wins and that information can be taken into the
// analysis of the results.
Advantage int
}
// Compute calculates a sorted list of choices with the total number of wins for
// each of them by reading preferences data previously populated by the Vote
// function. If there are multiple winners, tie boolean parameter is true.
func Compute[C comparable](preferences []int, choices []C) (results []Result[C], duels DuelsIterator[C], tie bool) {
strengths := calculatePairwiseStrengths(choices, preferences)
results, tie = calculateResults(choices, strengths)
return results, newDuelsIterator(choices, strengths), tie
}
// DuelsIterator is a function that returns the next Duel ordered by the choice indexes.
type DuelsIterator[C comparable] func() *Duel[C]
func newDuelsIterator[C comparable](choices []C, strengths []int) (duels DuelsIterator[C]) {
choicesCount := len(choices)
choiceIndexRow := 0
choiceIndexColumn := 1
return func() *Duel[C] {
if choiceIndexRow >= choicesCount || choiceIndexColumn >= choicesCount {
return nil
}
defer func() {
choiceIndexColumn++
if choiceIndexColumn >= choicesCount {
choiceIndexRow++
choiceIndexColumn = choiceIndexRow + 1
}
}()
return &Duel[C]{
Left: ChoiceStrength[C]{
Choice: choices[choiceIndexRow],
Index: choiceIndexRow,
Strength: strengths[choiceIndexRow*choicesCount+choiceIndexColumn],
},
Right: ChoiceStrength[C]{
Choice: choices[choiceIndexColumn],
Index: choiceIndexColumn,
Strength: strengths[choiceIndexColumn*choicesCount+choiceIndexRow],
},
}
}
}
// Duel represents a pairwise comparison between two choices that are compared
// by their strongest paths strengths (number of votes in the weakest link of
// the strongest path).
type Duel[C comparable] struct {
Left ChoiceStrength[C]
Right ChoiceStrength[C]
}
// Outcome returns the the winner and the defeated choice in the pairwise
// comparison of their strengths. If nils are returned, the outcome of the duel
// is a tie.
func (d Duel[C]) Outcome() (winner, defeated *ChoiceStrength[C]) {
if d.Left.Strength > d.Right.Strength {
return &d.Left, &d.Right
}
if d.Right.Strength > d.Left.Strength {
return &d.Right, &d.Left
}
return nil, nil // tie
}
// ChoiceStrength stores the strength of a choice. The strength is the number of
// votes in the weakest link of the strongest path between votes for different
// choices.
type ChoiceStrength[C comparable] struct {
// The choice value.
Choice C
// 0-based ordinal number of the choice in the choice slice.
Index int
Strength int
}
type choiceIndex int
func getChoiceIndex[C comparable](choices []C, choice C) choiceIndex {
for i, c := range choices {
if c == choice {
return choiceIndex(i)
}
}
return -1
}
func ballotRanks[C comparable](choices []C, b Ballot[C]) (ranks [][]choiceIndex, choicesLen int, hasUnrankedChoices bool, err error) {
choicesLen = len(choices)
ballotLen := len(b)
hasUnrankedChoices = ballotLen != choicesLen
ballotRanks := make(map[int][]choiceIndex, ballotLen)
var rankedChoices bitSet
if hasUnrankedChoices {
rankedChoices = newBitset(uint64(choicesLen))
}
choicesLen = len(choices)
for choice, rank := range b {
index := getChoiceIndex(choices, choice)
if index < 0 {
return nil, 0, false, &UnknownChoiceError[C]{Choice: choice}
}
ballotRanks[rank] = append(ballotRanks[rank], index)
if hasUnrankedChoices {
rankedChoices.set(uint64(index))
}
}
rankNumbers := make([]int, 0, len(ballotRanks))
for rank := range ballotRanks {
rankNumbers = append(rankNumbers, rank)
}
sort.Slice(rankNumbers, func(i, j int) bool {
return rankNumbers[i] < rankNumbers[j]
})
if hasUnrankedChoices {
ranks = make([][]choiceIndex, 0, len(rankNumbers)+1)
} else {
ranks = make([][]choiceIndex, 0, len(rankNumbers))
}
for _, rankNumber := range rankNumbers {
ranks = append(ranks, ballotRanks[rankNumber])
}
if hasUnrankedChoices {
unranked := make([]choiceIndex, 0, choicesLen-ballotLen)
for i := uint64(0); int(i) < choicesLen; i++ {
if !rankedChoices.isSet(i) {
unranked = append(unranked, choiceIndex(i))
}
}
if len(unranked) > 0 {
ranks = append(ranks, unranked)
}
}
return ranks, choicesLen, hasUnrankedChoices, nil
}
const intSize = unsafe.Sizeof(int(0))
func calculatePairwiseStrengths[C comparable](choices []C, preferences []int) []int {
choicesCount := uintptr(len(choices))
if choicesCount == 0 {
return nil
}
strengths := make([]int, choicesCount*choicesCount)
strengthsPtr := unsafe.Pointer(&strengths[0])
for i := uintptr(0); i < choicesCount; i++ {
icc := i * choicesCount
for j := uintptr(0); j < choicesCount; j++ {
ij := icc + j
ji := j*choicesCount + i
c := preferences[ij]
if c > preferences[ji] {
*(*int)(unsafe.Add(strengthsPtr, ij*intSize)) = c
}
}
}
// optimize most inner loop by loop unrolling
const step = 8
for i := uintptr(0); i < choicesCount; i++ {
icc := i * choicesCount
for j := uintptr(0); j < choicesCount; j++ {
jcc := j * choicesCount
ji := jcc + i
jip := *(*int)(unsafe.Add(strengthsPtr, ji*intSize))
ccMod := choicesCount % step
cc := choicesCount - ccMod
end := cc + icc
for ik, jk := icc, jcc; ik < end; ik, jk = ik+step, jk+step {
setStrengthValue(strengthsPtr, ik, jk, jip)
setStrengthValue(strengthsPtr, ik+1, jk+1, jip)
setStrengthValue(strengthsPtr, ik+2, jk+2, jip)
setStrengthValue(strengthsPtr, ik+3, jk+3, jip)
setStrengthValue(strengthsPtr, ik+4, jk+4, jip)
setStrengthValue(strengthsPtr, ik+5, jk+5, jip)
setStrengthValue(strengthsPtr, ik+6, jk+6, jip)
setStrengthValue(strengthsPtr, ik+7, jk+7, jip)
}
end = choicesCount + icc
for ik, jk := cc+icc, cc+jcc; ik < end; ik, jk = ik+1, jk+1 {
setStrengthValue(strengthsPtr, ik, jk, jip)
}
}
}
return strengths
}
func setStrengthValue(strengthsPtr unsafe.Pointer, ik, jk uintptr, jip int) {
m := min(
jip,
*(*int)(unsafe.Add(strengthsPtr, ik*intSize)),
)
jkp := (*int)(unsafe.Add(strengthsPtr, jk*intSize))
jkv := *jkp
if m > jkv {
*jkp = m
}
}
func calculateResults[C comparable](choices []C, strengths []int) (results []Result[C], tie bool) {
choicesCount := len(choices)
results = make([]Result[C], 0, choicesCount)
for i := 0; i < choicesCount; i++ {
var wins int
var strength int
var advantage int
for j := 0; j < choicesCount; j++ {
if i != j {
sij := strengths[i*choicesCount+j]
sji := strengths[j*choicesCount+i]
if sij > sji {
wins++
strength += sij
advantage += sij - sji
}
}
}
results = append(results, Result[C]{
Choice: choices[i],
Index: i,
Wins: wins,
Strength: strength,
Advantage: advantage,
})
}
sort.Slice(results, func(i, j int) bool {
if results[i].Wins != results[j].Wins {
return results[i].Wins > results[j].Wins
}
if results[i].Strength != results[j].Strength {
return results[i].Strength > results[j].Strength
}
return results[i].Index < results[j].Index
})
if len(results) >= 2 {
tie = results[0].Wins == results[1].Wins
}
return results, tie
}
func min(a, b int) int {
if a < b {
return a
}
return b
}