Accept multiple --ip arguments
[re6stnet.git] / simulation / realistic_dataset / latency.cpp
1 #include "main.h"
2
3 Latency::Latency(const char* filePath, int size) : size(size)
4 {
5 values = new int*[size];
6 avgLatencyToOthers = new double[size];
7 for(int i=0; i<size; i++)
8 {
9 values[i] = new int[size];
10 for(int j=0; j<size; j++)
11 values[i][j] = -1;
12 }
13
14
15 FILE* file = NULL;
16 file = fopen(filePath, "r");
17 int a, b;
18 double latency;
19
20 while(!feof(file))
21 {
22 fscanf(file, "%d %d %lf", &a, &b, &latency);
23 if(latency < 100)
24 latency = -1;
25
26 values[b-1][a-1] = latency;
27 values[a-1][b-1] = latency;
28 }
29
30 fclose(file);
31
32 for(int i=0; i<size; i++)
33 {
34 avgLatencyToOthers[i] = 0;
35 for(int j=0;j<size; j++)
36 avgLatencyToOthers[i] += values[i][j];
37 avgLatencyToOthers[i] /= size;
38 }
39
40 }
41
42 void Latency::Rewrite(int n)
43 {
44 int nat[size];
45 int nextId = 0;
46 for(int i=0; i<size; i++)
47 {
48 int nReachable = 0;
49 for(int j=0; j<size; j++)
50 {
51 if(j != i && values[i][j] >= 10)
52 nReachable++;
53 }
54
55 if(nReachable <= n)
56 nat[i] = -1;
57 else
58 {
59 nat[i] = nextId;
60 nextId++;
61 }
62 }
63
64 FILE* file = NULL;
65 file = fopen("latency/pw-1715/rewrite", "w");
66 for(int i=0; i<size-1; i++)
67 if(nat[i] != -1)
68 for(int j=i+1; j<size; j++)
69 if(nat[j] != -1)
70 fprintf(file, "%d %d %d\n", nat[i], nat[j], values[i][j]>=10?values[i][j]:-1);
71
72 fclose(file);
73 }
74
75 Latency::~Latency()
76 {
77 for(int i=0; i<size; i++)
78 delete[] values[i];
79 delete[] values;
80 delete[] avgLatencyToOthers;
81 }
82
83 double Latency::GetAverageDistance()
84 {
85 int size = 1555;
86 double** distances = new double*[size];
87 for(int i=0; i<size; i++)
88 distances[i] = new double[size];
89
90 for(int i=0; i<size; i++)
91 for(int j=0; j<size; j++)
92 if(i==j)
93 distances[i][j] = 0;
94 else if(values[i][j] > 0)
95 distances[i][j] = values[i][j];
96 else
97 distances[i][j] = numeric_limits<double>::infinity();
98
99 for(int i=0; i<size; i++)
100 for(int j=0; j<size; j++)
101 for(int k=0; k<size; k++)
102 distances[i][j] = min(distances[i][j], distances[i][k] + distances[k][j]);
103
104 double avg = 0;
105 for(int i=0; i<size; i++)
106 for(int j=0; j<size; j++)
107 avg += distances[i][j];
108
109 return avg / (size*size);
110 }
111
112 double Latency::GetAveragePing()
113 {
114 double out = 0;
115 double nPing = 0;
116 for(int i=0; i<size; i++)
117 for(int j=0; j<size; j++)
118 if(values[i][j] > 0)
119 {
120 nPing++;
121 out += values[i][j];
122 }
123
124 return out/nPing;
125 }