-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.cpp
More file actions
285 lines (256 loc) · 7.05 KB
/
sim.cpp
File metadata and controls
285 lines (256 loc) · 7.05 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
#include "sim.h"
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#define NUMBEROFUSERS 50000
#define NUMBEROFSIMS 1
/* Simulator consisting of 4 cities (1,2,3,4) and 4 edges.
a
10 ->/ \ <- 25
b--c
30 > / ^15
d
5000 users initialized with:
bandwidth = 10
adjacent matrix repesentation
a b c d
a|0|10|25|12
b|10|0|15|30
c|25|15|0|15
d|12|30|15|0
*/
Sim::Sim(){
}
Sim::~Sim(){
}
void Sim::initialize_Simulator(){
//Initialize listOfCities
City a;
a.setCityNo(0);
a.setPercent(50);
add_city(a);
City b;
b.setCityNo(1);
b.setPercent(20);
add_city(b);
City c;
c.setCityNo(2);
c.setPercent(20);
add_city(c);
City d;
d.setCityNo(3);
d.setPercent(10);
add_city(d);
std::vector<City> tempCityList;
tempCityList.push_back(a);
tempCityList.push_back(b);
tempCityList.push_back(c);
tempCityList.push_back(d);
server.init_cities(tempCityList);
server.updateMatrix(10,a,b);
server.updateMatrix(25,a,c);
server.updateMatrix(15,b,c);
server.updateMatrix(30,b,d);
server.updateMatrix(12,a,d);
server.updateMatrix(15,c,d);
server.init_city_adjMatrix();
}
void Sim::create_users(){
outputFile.open("groups_of_users.txt");
auto t1 = std::chrono::high_resolution_clock::now();
for(unsigned int i=0; i<NUMBEROFUSERS; i++){
std::vector<int> newGroup;
int chooser;
chooser = std::rand()%100;
if(chooser >= 50){
User addedUser(i%NUMBEROFUSERS,std::rand()%3+1,&server,0);
newGroup = server.addUser(addedUser,mode);
if(newGroup.size() > 0){
for (auto i = newGroup.begin(); i != newGroup.end(); i++){
outputFile << *i << " ";
}
outputFile << std::endl;
}
listOfUsers.push_back(addedUser);
}
else if(chooser >= 30 && chooser < 50){
User addedUser(i%NUMBEROFUSERS,std::rand()%3+1,&server,1);
newGroup = server.addUser(addedUser,mode);
if(newGroup.size() > 0){
for (auto i = newGroup.begin(); i != newGroup.end(); i++){
outputFile << *i << " ";
}
outputFile << std::endl;
}
listOfUsers.push_back(addedUser);
}
else if(chooser >= 10 && chooser < 30){
User addedUser(i%NUMBEROFUSERS,std::rand()%3+1,&server,2);
newGroup = server.addUser(addedUser,mode);
if(newGroup.size() > 0){
for (auto i = newGroup.begin(); i != newGroup.end(); i++){
outputFile << *i << " ";
}
outputFile << std::endl;
}
listOfUsers.push_back(addedUser);
}
else if(chooser < 10){
User addedUser(i%NUMBEROFUSERS,std::rand()%3+1,&server,3);
newGroup = server.addUser(addedUser,mode);
if(newGroup.size() > 0){
for (auto i = newGroup.begin(); i != newGroup.end(); i++){
outputFile << *i << " ";
}
outputFile << std::endl;
}
listOfUsers.push_back(addedUser);
}
}
outputFile.close();
outputFile.open("Average_time.csv");
auto t2 = std::chrono::high_resolution_clock::now();
<<<<<<< HEAD
outputFile << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count() <<",";
outputFile.close();
// std::cout << "PART E\n";
=======
>>>>>>> 582adba165ba1be529b7403ec3ebf366658a5d57
}
void Sim::add_city(City newCity){
listOfCities.push_back(newCity);
}
void Sim::add_user(User newUser){
listOfUsers.push_back(newUser);
}
void Sim::print_cities(){
for(unsigned int i=0; i<server.getCityList().size();i++){
printf("City %d:", server.getCityList().at(i).getCityNo());
server.getCityList().at(i).printUsers();
std::cout << std::endl;
}
}
void Sim::print_users(){
printf("User List:\n");
for (unsigned int i = 0; i < listOfUsers.size();i++){
printf("%d\n",listOfUsers[i].getID());
}
}
void Sim::print_Uservector(std::vector<User> printing){
for(unsigned int i=0; i<printing.size();i++){
printf("UserID %d with these desired hosts:", printing[i].getID());
std::cout << std::endl;
}
}
void Sim::print_adjMatrix(){
for (unsigned int i = 0; i < adjMatrix.size(); ++i){
for (unsigned int j = 0; j < adjMatrix[0].size(); ++j){
std::cout << adjMatrix[i][j] << ' ';
}
std::cout << std::endl;
}
}
double Sim::averagePing(){
std::string line;
int count = 0;
double sum = 0;
double temp = 0;
inputFile.open("groups_of_users.txt");
outputFile.open("pingValues.csv");
if (inputFile.is_open()){
while(getline(inputFile, line)){
temp = calculate_ping(line);
count++;
sum+= temp;
outputFile << std::to_string(temp) + ","<< std::endl;
}
inputFile.close();
}
outputFile.close();
return sum/count;
}
double Sim::calculate_ping(std::string input){
int temp= 0;
std::vector<int> users;
double sum = 0;
for (unsigned int i = 0; i < input.size();i++){
if(input[i] == ' '){
users.push_back(temp);
temp = 0;
}
else{
temp = temp * 10 + (input[i] - '0');
}
}
for (unsigned int i = 1; i < users.size();i++){
sum += listOfUsers[users[0]].ping(listOfUsers[users[i]]);
}
return sum/4;
}
void Sim::processTime(){
inputFile.open("Average_time.csv");
std::string line;
getline(inputFile,line);
switch (mode) {
case 0:
outputFile.open("Average_time_normal.csv",std::ios::app);
break;
case 1:
outputFile.open("Average_time_random.csv",std::ios::app);
break;
case 2:
outputFile.open("Average_time_geo.csv",std::ios::app);
break;
}
outputFile<<line<<std::endl;
outputFile.close();
inputFile.close();
}
int main(){
std::ofstream outputFile1;
double sum = 0.0;
outputFile1.open("Average_ping_normal.csv");
for (int i = 0; i < NUMBEROFSIMS; i++){
std::srand((unsigned)time(0)+i);
Sim simulators;
simulators.setMode(1);
simulators.initialize_Simulator();
simulators.create_users();
double ping = simulators.averagePing();
outputFile1 << ping << "," << std::endl;
sum += ping;
simulators.processTime();
}
std::cout<<"Normal method average ping is: " << sum/NUMBEROFSIMS << std::endl;
outputFile1.close();
sum = 0.0;
outputFile1.open("Average_ping_random.csv");
for(int i = 0; i < NUMBEROFSIMS; i++){
std::srand((unsigned)time(0)+i);
Sim simulators;
simulators.setMode(2);
simulators.initialize_Simulator();
simulators.create_users();
double ping = simulators.averagePing();
outputFile1 << ping << "," << std::endl;
sum += ping;
simulators.processTime();
}
std::cout<<"Random method average ping is: " << sum/NUMBEROFSIMS << std::endl;
outputFile1.close();
sum = 0.0;
outputFile1.open("Average_ping_geo.csv");
for (int i = 0; i < NUMBEROFSIMS; i++){
std::srand((unsigned)time(0)+i);
Sim simulators;
simulators.setMode(0);
simulators.initialize_Simulator();
simulators.create_users();
double ping = simulators.averagePing();
outputFile1 << ping << "," << std::endl;
sum += ping;
simulators.processTime();
}
std::cout<<"Geolocation method average ping is: " << sum/NUMBEROFSIMS << std::endl;
outputFile1.close();
}