-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.h
More file actions
432 lines (305 loc) · 13.9 KB
/
Simulation.h
File metadata and controls
432 lines (305 loc) · 13.9 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
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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <QTableWidget>
#include "Ballot.h"
#include "Graph.h"
#include "GraphController.h"
#define MAXWEIGHT 500
#ifndef SIMULATION_H
#define SIMULATION_H
class Simulation {
public:
std::vector<int> voteCount;
std::vector<int> minorityCount;
bool isSchulze;
bool isOne;
bool isGettingFullBallot;
bool isDone = false;
// necessary variables for analysis
long double numberOfManipulations;
int numberOfSuccesses;
int minoritySuccesses; // only for schulze
int manipulationCount = -1;
int numberOfIterations = 0;
// winning candidates
std::tuple<int, int> finalCandidates;
Simulation(int numVoters, int numCandidates) {
srand(time(0));
this->numCandidates = numCandidates;
this->numVoters = numVoters;
numberOfManipulations = 0;
numberOfSuccesses = 0;
minoritySuccesses = 0;
simulationBallots = std::vector<std::shared_ptr<Ballot>>(numVoters);
// create the instance of graphController
control = std::make_unique<GraphController>(this->numCandidates);
}
~Simulation() = default;
// create 1d array of probailities and attach to 2d array of all probabilities
std::vector<double> getProbabilities() {
std::vector<double> probabilities = std::vector<double>();
probabilities.push_back((long double)((this->numberOfManipulations / (long double)(numberOfIterations)) * 100));
probabilities.push_back((long double)((this->numberOfSuccesses / (long double)(numberOfIterations)) * 100));
probabilities.push_back((long double)((this->minoritySuccesses / (long double)(numberOfIterations)) * 100));
return probabilities;
}
// getters
int getNumberOfManipulations() {
return this->numberOfManipulations;
}
// !! negy kulonbozo eljaras !!
void StartIgenNemSimulation() {
manipulationCount = 0;
// count ballots
CountVotes();
auto winningCandidates = DetermineWinners();
finalCandidates = winningCandidates;
if(std::get<0>(winningCandidates) != -1 && std::get<1>(winningCandidates) != -1) {
numberOfSuccesses += 1;
}
// manipulate current ballots and rerun the simulation
Manipulation(std::get<0>(winningCandidates), std::get<1>(winningCandidates));
// clear the count array
voteCount = std::vector<int>(numCandidates, 0);
// increment iteration counter
++numberOfIterations;
}
void StartSchulzeSimulation() {
manipulationCount = 0;
// count the ballots
CountPreferances();
CountVotes();
// calculate strongest paths
auto finalRank = FindWinningSchulzeCandidates();
std::get<0>(finalCandidates) = finalRank.at(0);
std::get<1>(finalCandidates) = finalRank.at(1);
bool isSuccess = false;
if(finalRank.at(0) != -1 && finalRank.at(1) != -1) {
if(!isGettingFullBallot) {
if(voteCount.at(finalRank.at(0)) > int(numVoters / 2) && voteCount.at(finalRank.at(1) > int(numVoters / 2))) {
numberOfSuccesses += 1;
isSuccess = true;
}
}
else {
numberOfSuccesses += 1;
isSuccess = true;
}
}
// manipulate the current ballots and rerun the simulation
Manipulation(finalRank.at(0), finalRank.at(1));
// !! calculate minorty probability
for(int i = 0; i < minorityCount.size(); ++i) {
if(minorityCount.at(i) < (int)(numVoters / 2)) {
minorityCount.at(i) = -1;
}
}
// check if first or second place winner was a minority count leader
if(isSuccess && (minorityCount.at(finalRank.at(0)) >= int(numVoters / 2) || minorityCount.at(finalRank.at(1)) >= int(numVoters / 2))) {
minoritySuccesses += 1;
}
// reset minority count vector
minorityCount = std::vector<int>(numCandidates, -1);
++numberOfIterations;
}
void GenerateRandomBallots() {
std::cout << "printing out ballots: " << std::endl;
for(int i = 0; i < numVoters; ++i) {
std::shared_ptr<Ballot> newBallot = std::make_shared<Ballot>(numCandidates, isGettingFullBallot);
simulationBallots.at(i) = newBallot;
for(int j = 0; j < numCandidates; ++j) {
std::cout << simulationBallots.at(i)->ballot.at(j) << " ";
}
std::cout << std::endl;
}
}
// two candidates in the tuple are the winning candidate (-1 if there are none in the majority scenario)
void CountVotes() {
// count the votes
for(int i = 0; i < numVoters; ++i) {
for(int j = 0; j < numCandidates; ++j) {
if(simulationBallots.at(i)->ballot.at(j) != 0) {
voteCount.at(j) += 1;
}
}
}
}
std::tuple<int, int> DetermineWinners() {
int firstWinningCandidate = -1;
int secondWinningCandidate = -1;
// find the winning candidates by looping through the number of voters
// that voted for a certain candidate and determining candidate with most votes
for(int i = 0; i < voteCount.size(); ++i) {
if(voteCount.at(i) >= (int)(numVoters / 2) || !isOne) {
if(firstWinningCandidate == -1) {
firstWinningCandidate = i;
}
else if(secondWinningCandidate == -1) {
secondWinningCandidate = i;
}
else {
// holtverseny eseten egy random szam alapitja meg a nyertest
int random = rand() % 2;
if(voteCount.at(i) > voteCount.at(firstWinningCandidate)) {
secondWinningCandidate = firstWinningCandidate;
firstWinningCandidate = i;
}
else if(voteCount.at(i) > voteCount.at(secondWinningCandidate)) {
secondWinningCandidate = i;
}
else if(voteCount.at(i) == voteCount.at(firstWinningCandidate)) {
if(random) {
secondWinningCandidate = firstWinningCandidate;
firstWinningCandidate = i;
}
else {
secondWinningCandidate = i;
}
}
else if(voteCount.at(i) == voteCount.at(secondWinningCandidate)) {
if(random) {
secondWinningCandidate = i;
}
}
}
}
}
auto winningCandidates = std::make_tuple(firstWinningCandidate, secondWinningCandidate);
return winningCandidates;
/*
else {
for(int i = 0; i < voteCount.size(); ++i) {
if(voteCount.at(i) > firstWinningCandidate) {
firstWinningCandidate = voteCount.at(i);
}
else if(voteCount.at(i) > secondWinningCandidate) {
secondWinningCandidate = voteCount.at(i);
}
}
auto winningCandidates = std::make_tuple(firstWinningCandidate, secondWinningCandidate);
return winningCandidates;
}
*/
}
void 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);
}
}
}
// function will return a vector that will contain all the candidates in order of winning
std::vector<int> FindWinningSchulzeCandidates() {
control->CalculateStrongestPaths();
std::vector<int> finalRank = 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;
}
}
}
// count the number of wins a certain row number has over its column associates
// that number minus the number of candidates gives the ranking for that row candidate
finalRank.at((numCandidates - 1) - numGreater) = i;
/*
int index = (numCandidates - 1) - numGreater;
if(finalRank.at(index) == -1) {
finalRank.at(index) = i;
}
else { // else is considered for ties
srand(time(0));
int random = rand() % 2;
if(finalRank.at(index - 1) == -1) {
if(random) finalRank.at(index - 1) = i;
else {
finalRank.at(index - 1) = std::move(finalRank.at(index));
finalRank.at(index) = i;
}
}
else {
int rank = finalRank.at(index);
}
}
*/
}
return finalRank;
}
// same manipulations for each simulation
void Manipulation(int winningCandidateOne, int winningCandidateTwo) {
int manipulations = 0;
for(int i = 0; i < numVoters; ++i) {
std::shared_ptr<Ballot> currentVoter = simulationBallots.at(i);
bool hasManipulated = false;
for(int j = 0; j < numCandidates; ++j) {
// R(k - 1) iteracio szerint elokelobb helyen vegzett, de a szavazo sajat rangsoraban hatrebb sorolt szituacio
if((j == winningCandidateOne || j == winningCandidateTwo) &&
currentVoter->ballot.at(j) != 1 && currentVoter->ballot.at(j) != 2 && currentVoter->ballot.at(j) != 0) {
currentVoter->ballot.at(j) = 0;
if(!hasManipulated) {
manipulations += 1;
hasManipulated = true;
}
}
}
}
// add up probability from each simulation and average after each iteration
manipulationCount = manipulations;
numberOfManipulations += (long double)(manipulations / (long double)(numVoters));
}
void PrintBallots() {
for(int i = 0; i < numVoters; ++i) {
for(int j = 0; j < numCandidates; ++j) {
std::cout << simulationBallots.at(i)->ballot.at(j) << " ";
}
std::cout << std::endl;
}
}
private:
int numCandidates;
int numVoters;
std::vector<std::shared_ptr<Ballot>> simulationBallots;
std::unique_ptr<GraphController> control;
};
#endif // SIMULATION_H