Repository navigation
Expand file tree
/
Copy pathschulzesimmulation.cpp
More file actions
158 lines (110 loc) · 4.42 KB
/
Copy pathschulzesimmulation.cpp
File metadata and controls
158 lines (110 loc) · 4.42 KB
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
#include "schulzesimmulation.h"
schulzesimmulation::schulzesimmulation() {
// initialize simulation ballot vector
simulationBallots = std::vector<std::shared_ptr<Ballot>>(this->numVoters);
// create graph controller object that will run schulze simulation
control = new GraphController(this->numCandidates);
}
schulzesimmulation::~schulzesimmulation() {
delete control;
}
void schulzesimmulation::BeginSimulation() {
// count ballots
CountPreferances(); // set up preferances for graph algo
CountVotes(); // necessary for second schulze simulation where majority matters
// update finalCandidates tuple with the winning candidates
DetermineWinners();
// determine if iteration was success
bool isSuccess = false;
if(std::get<0>(finalCandidates) != -1 && std::get<1>(finalCandidates) != -1) {
if(doesNeedMajority && IsMajority()) {
numberOfSuccesses += 1;
isSuccess = true;
}
else if(!doesNeedMajority) {
numberOfSuccesses += 1;
isSuccess = true;
}
}
// manipulate the current ballots based off winning candidates
Manipulation(firstWinCan, secondWinCan);
// !! calculate minorty probability
for(int i = 0; i < minorityCount.size(); ++i) {
if(minorityCount.at(i) < (int)(numVoters / 2)) {
minorityCount.at(i) = -1;
}
}
// check is first or second candidate is a minority
if(isSuccess && (minorityCount.at(firstWinCan) >= int(numVoters / 2) || minorityCount.at(secondWinCan) >= int(numVoters / 2))) {
minoritySuccesses += 1;
}
// !!
// reset necessary fields
voteCount = std::vector<int>(numCandidates, 0);
minorityCount = std::vector<int>(numCandidates, -1);
finalCandidates = std::make_tuple<int, int>(-1, -1);
++numberOfIterations;
}
void schulzesimmulation::DetermineWinners() {
control->CalculateStrongestPaths();
std::vector<int> candidateValue = std::vector<int>(numCandidates, -1);
std::vector<std::vector<int>> strongPaths = control->graph->getStrongPaths();
for(int i = 0; i < strongPaths.size(); ++i) {
int numGreater = 0;
for(int j = 0; j < strongPaths.at(0).size(); ++j) {
if(i != j) {
if(strongPaths.at(i).at(j) > strongPaths.at(j).at(i)) {
numGreater += 1;
}
}
}
// winning candidate is determined by number of strong paths greater than
// reverse path
candidateValue.at(i) = numGreater; // this will be sent to UpdateWinTuple
}
for(int i = 0; i < numCandidates; ++i) {
UpdateWinTuple(this->finalCandidates, candidateValue, i);
}
}
void schulzesimmulation::CountPreferances() {
// initialize vector that will count minority
minorityCount = std::vector<int>(numCandidates, -1);
// this method loops through the candidates and calculates the number of voters who
// prefer a certain candidate over another
for(int i = 0; i < numCandidates; ++i) {
// count minority votes for each candidate
int elfogadhatatlanSzam = 0;
for(int j = 0; j < numVoters; ++j) {
if(simulationBallots.at(j)->ballot.at(i) == 0) {
elfogadhatatlanSzam += 1;
}
}
// update minority count vector
minorityCount.at(i) = elfogadhatatlanSzam;
for(int j = i + 1; j < numCandidates; ++j) {
int iPreferance = 0;
int jPreferance = 0;
for(int k = 0; k < numVoters; ++k) {
// TODO: consider a voter using same rank on two different candidates
if(simulationBallots.at(k)->ballot.at(i) < simulationBallots.at(k)->ballot.at(j)) {
if(simulationBallots.at(k)->ballot.at(i) == 0) {
++jPreferance;
}
else {
++iPreferance;
}
}
else if(simulationBallots.at(k)->ballot.at(j) < simulationBallots.at(k)->ballot.at(i)){
if(simulationBallots.at(k)->ballot.at(j) == 0) {
++iPreferance;
}
else {
++jPreferance;
}
}
}
control->AddToAdjMatrix(i, j, iPreferance);
control->AddToAdjMatrix(j, i, jPreferance);
}
}
}