-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvent.cpp
More file actions
165 lines (133 loc) · 3.84 KB
/
Event.cpp
File metadata and controls
165 lines (133 loc) · 3.84 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
#include <algorithm>
#include <exception>
#include <iostream>
#include <random>
#include <math.h>
#include "Event.h"
Event::Event(unsigned short maxRank) {
this->maxRank = maxRank;
}
Event::~Event() {};
void Event::addMatch(
unsigned short red1,
unsigned short red2,
unsigned short red3,
unsigned short blue1,
unsigned short blue2,
unsigned short blue3,
double redWinProbability) {
matches.push_back(Match(
red1,
red2,
red3,
blue1,
blue2,
blue3,
redWinProbability));
}
void Event::addTeam(
unsigned short number,
unsigned char qs,
unsigned short firstSort,
unsigned short secondSort,
unsigned short thirdSort,
unsigned short fourthSort) {
teams.push_back(Team(
number,
qs,
firstSort,
secondSort,
thirdSort,
fourthSort));
}
void Event::winTeam(
std::vector<Team> &teams,
unsigned short number) {
for (auto &team : teams) {
if (team.getNumber() == number) {
team.win();
return;
}
}
std::cerr << "Couldn't find " << number << " in the rankings." << std::endl;
std::cerr << "Are you sure that the input files are correct?" << std::endl;
throw std::exception();
}
void Event::calculate() {
size_t numBranches = pow(2, matches.size());
#pragma omp parallel for
for (size_t i = 0; i < numBranches; i++) {
this->calculatePossibility(i);
}
this->root.log();
std::cout << std::flush;
}
void Event::calculate(size_t numSims) {
std::random_device rand_dev;
std::mt19937_64 generator(rand_dev());
std::uniform_int_distribution<unsigned long> distribution(0x00000000, 0xffffffff);
unsigned long randomPossibility = 0;
for (size_t i = 0; i < numSims; i++) {
do {
randomPossibility = distribution(generator);
randomPossibility << 32;
randomPossibility |= distribution(generator);
} while (randomPossibility >= pow(2, matches.size()));
this->calculatePossibility(randomPossibility);
}
this->root.logMonteCarlo();
std::cout << std::flush;
}
void Event::calculatePossibility(unsigned long i) {
auto percent = (i + 1) / (pow(2, matches.size()));
std::cerr.precision(2);
std::cerr << "\r" << (percent * 100) << "%" << std::flush;
auto currentNumber = i;
auto teams = this->teams;
double probability = 1;
for (size_t m = 0; m < matches.size(); m++) {
auto outcomeNumber = currentNumber % 2;
if (outcomeNumber == 0) {
probability = probability * this->matches[m].getRedWinProbability();
this->winTeam(teams, this->matches[m].getRed1());
this->winTeam(teams, this->matches[m].getRed2());
this->winTeam(teams, this->matches[m].getRed3());
} else {
probability = probability * this->matches[m].getBlueWinProbability();
this->winTeam(teams, this->matches[m].getBlue1());
this->winTeam(teams, this->matches[m].getBlue2());
this->winTeam(teams, this->matches[m].getBlue3());
}
currentNumber /= 2;
}
std::sort(teams.begin(), teams.end());
#pragma omp critical
{
this->updateMonteCarloSummary(teams, probability);
}
}
void Event::updateSummary(
std::vector<Team> &teams,
double probability) {
root.addChild(teams[0].getNumber(), 0);
Node* currentNode = root.getChild(teams[0].getNumber());
currentNode->updateProbability(probability);
for (size_t i = 1; i < this->maxRank; i++) {
currentNode->addChild(teams[i].getNumber(), 0);
currentNode = currentNode->getChild(teams[i].getNumber());
currentNode->updateProbability(probability);
}
}
void Event::updateMonteCarloSummary(
std::vector<Team> &teams,
double probability) {
root.addChild(teams[0].getNumber(), 0);
root.updateProbability(probability);
Node* currentNode = root.getChild(teams[0].getNumber());
currentNode->updateProbability(probability);
for (size_t i = 1; i < this->maxRank; i++) {
currentNode->addChild(teams[i].getNumber(), 0);
currentNode = currentNode->getChild(teams[i].getNumber());
currentNode->updateProbability(probability);
}
}