-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagerank.cpp
More file actions
257 lines (186 loc) · 4.87 KB
/
Copy pathpagerank.cpp
File metadata and controls
257 lines (186 loc) · 4.87 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
#include "PageRank.h"
#include "Matrice.h"
#include <fstream>
using std::ostream; using std::endl; using std::cout;
PageRank::PageRank() {
Matrix vp;
std::vector<std::string> all;
std::vector<std::pair<double, std::string>> rank;
}
//calcule du pagerank avec random walk + display
void PageRank::calculate(nlohmann::json j, double b, int iteration) {
Matrix M;
M = Matrix::createSquare(j.size());
M.getMatrixM(j);
all = M.getAll();
Vp = Matrix(j.size(), 1, (1.0 / (j.size())));
Matrix E;
E = Matrix::createSquare(j.size(), 1);
for (int i(0); i < iteration; ++i) {
Vp = b * M*Vp + ((1.0 - b) / (M.getNbCols()))*E;
}
}
void PageRank::calculate(const char* path, double b, int iteration) {
std::ifstream file(path);
nlohmann::json j;
file >> j;
Matrix M;
M = Matrix::createSquare(j.size());
M.getMatrixM(j);
all = M.getAll();
Vp = Matrix(j.size(), 1, (1.0 / (j.size())));
Matrix E;
E = Matrix::createSquare(j.size(), 1);
for (int i(0); i < iteration; ++i) {
Vp = b * M*Vp + ((1.0 - b) / (M.getNbCols()))*E;
}
}
//pour simulation cas avec probleme
void PageRank::calculateFaulty(const char* path, int iteration) {
std::ifstream file(path);
nlohmann::json j;
file >> j;
Matrix M;
M = Matrix::createSquare(j.size());
M.getMatrixMFaulty(j);
all = M.getAll();
Vp = Matrix(j.size(), 1, (1.0 / (j.size())));
for (int i(0); i < iteration; ++i) {
Vp = M*Vp;
}
}
void PageRank::calculateFaulty(nlohmann::json j, int iteration) {
Matrix M;
M = Matrix::createSquare(j.size());
M.getMatrixMFaulty(j);
all = M.getAll();
Vp = Matrix(j.size(), 1, (1.0 / (j.size())));
for (int i(0); i < iteration; ++i) {
Vp = M * Vp;
}
}
//calcule avec comparation par rapport a epsilon
int PageRank::calculateEpsilon(nlohmann::json j, double b, double eps) {
Matrix M;
M = Matrix::createSquare(j.size());
M.getMatrixM(j);
all = M.getAll();
Vp = Matrix(j.size(), 1, (1.0 / (j.size())));
Matrix Vp0(Vp);
Matrix E;
E = Matrix::createSquare(j.size(), 1);
int i(0);
while (Vp.diffVecteur(Vp0, eps) != true) {
Vp0 = Vp;
Vp = b * M*Vp0 + ((1.0 - b) / (M.getNbCols()))*E;
i += 1;
}
return i;
}
int PageRank::calculateEpsilon(const char* path, double b, double eps) {
std::ifstream file(path);
nlohmann::json j;
file >> j;
Matrix M;
M = Matrix::createSquare(j.size());
M.getMatrixM(j);
all = M.getAll();
Vp = Matrix(j.size(), 1, (1.0 / (j.size())));
Matrix Vp0(Vp);
Matrix E;
E = Matrix::createSquare(j.size(), 1);
int i(0);
while (Vp.diffVecteur(Vp0, eps)!=true) {
Vp0 = Vp;
Vp = b * M*Vp0 + ((1.0 - b) / (M.getNbCols()))*E;
i += 1;
}
return i;
}
//pour simulation cas avec probleme avec epsilon
void PageRank::calculateEpsilonFaulty(nlohmann::json j, double b, double eps) {
Matrix M;
M = Matrix::createSquare(j.size());
M.getMatrixMFaulty(j);
all = M.getAll();
Vp = Matrix(j.size(), 1, (1.0 / (j.size())));
Matrix Vp0(Vp);
while (Vp.diffVecteur(Vp0, eps) != true) {
Vp0 = Vp;
Vp = M * Vp;
}
}
void PageRank::calculateEpsilonFaulty(const char* path, double b, double eps) {
std::ifstream file(path);
nlohmann::json j;
file >> j;
Matrix M;
M = Matrix::createSquare(j.size());
M.getMatrixMFaulty(j);
all = M.getAll();
Vp = Matrix(j.size(), 1, (1.0 / (j.size())));
Matrix Vp0(Vp);
while (Vp.diffVecteur(Vp0, eps) != true) {
Vp0 = Vp;
Vp = M * Vp;
}
}
//affichage sans ordre
void PageRank::display() const {
for (size_t i(0); i < all.size(); ++i) {
cout << all[i] << " influence is " << Vp(i, 0) << endl;
}
}
//affichage avec ordre
void PageRank::displayRank() const {
for (size_t i = 0; i < rank.size(); i++)
{
cout << rank[i].second << " influence is "
<< rank[i].first << endl;
}
}
//fill et sort rank pour avoir du plus petit au plus grand
void PageRank::fillRank() {
for (size_t i(0); i < all.size(); ++i) {
rank.push_back(std::make_pair(Vp(i,0), all[i]));
}
}
void PageRank::sortRankDescending() {
std::sort(rank.rbegin(), rank.rend());
}
void PageRank::sortRankAscending() {
std::sort(rank.begin(), rank.end());
}
void PageRank::calculateRankDescending() {
fillRank();
sortRankDescending();
}
void PageRank::calculateRankAscending() {
fillRank();
sortRankAscending();
}
//getters
Matrix PageRank::getVp() {
return Vp;
}
std::vector<std::string> PageRank::getAll() {
return all;
}
std::vector<std::pair<double, std::string>> PageRank::getRank() {
return rank;
}
double PageRank::getVp(int i) {
return Vp(i, 0);
}
std::string PageRank::getAll(int i) {
return all[i];
}
double PageRank::getAllFirst(int i) {
return rank[i].first;
}
std::string PageRank::getAllSecond(int i) {
return rank[i].second;
}
int PageRank::getSize() {
return all.size();
}