-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
292 lines (239 loc) · 8.67 KB
/
index.js
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
let g = new graphlib.Graph();
for (let i = 1; i <= 20; i += 1) {
g.setNode(`${i}`);
}
for (let i = 1; i < 20; i += 1) {
g.setEdge(`${i}`, `${i + 1}`, { h: 0.95 });
}
function connectionChance(grph, tries = 10000) {
const jsonDump = graphlib.json.write(grph);
let graph = graphlib.json.read(jsonDump);
let successes = 0;
for (var i = 0; i < tries; i += 1) {
graph.edges().forEach(element => {
if (Math.random() > graph.edge(element).h) {
graph.removeEdge(element);
}
});
/*
Finds all connected components in a graph and returns an array of these components. Each component is itself an array that contains the ids of nodes in the component.
This function takes O(|V|) time.
*/
if (graphlib.alg.components(graph).length > 1) {
successes++;
}
// clone
graph = graphlib.json.read(jsonDump);
}
return (1 - successes / tries) * 100;
}
function showConvergence(targetId, value) {
var selector = `#${targetId} + .convergence`;
var element = document.querySelector(selector);
element.innerHTML = `Graph consistency: ${value}%`;
}
/*
Napisz program szacujący niezawodność (rozumianą jako prawdopodobieństwo nierozspójnienia) takiej sieci w dowolnym interwale.
*/
var graphConvergence = connectionChance(g);
console.log(`L Graph Convergence: ${graphConvergence}%`);
drawGraph(g, g.nodes(), g.edges(), 'L');
showConvergence('L', graphConvergence);
/*
Jak zmieni się niezawodność tej sieci po dodaniu krawędzi e(1,20) takiej, że h(e(1,20))=0.95
*/
g.setEdge('20', '1', { h: .95 });
graphConvergence = connectionChance(g);
console.log(`C Graph Convergence: ${graphConvergence}%`);
drawGraph(g, g.nodes(), g.edges(), 'C');
showConvergence('C', graphConvergence);
/*
A jak zmieni się niezawodność tej sieci gdy dodatkowo dodamy jeszcze krawędzie e(1,10) oraz e(5,15) takie, że: h(e(1,10))=0.8, a h(e(5,15))=0.7.
*/
g.setEdge('1', '10', { h: .95 });
g.setEdge('5', '15', { h: .95 });
graphConvergence = connectionChance(g);
console.log(`C+ (0.95) Graph Convergence: ${graphConvergence}%`);
drawGraph(g, g.nodes(), g.edges(), 'C22_95');
showConvergence('C22_95', graphConvergence);
g.setEdge('1', '10', { h: .8 });
g.setEdge('5', '15', { h: .7 });
graphConvergence = connectionChance(g);
console.log(`C+ Graph Convergence: ${graphConvergence}%`);
drawGraph(g, g.nodes(), g.edges(), 'C22');
showConvergence('C22', graphConvergence);
/*
A jak zmieni się niezawodność tej sieci gdy dodatkowo dodamy jeszcze 4 krawedzie pomiedzy losowymi wierzchołkami o h=0.4.
*/
for (var i = 0; i < 4; i += 1) {
g.setEdge(`${i + 7}`, `${i + 15}`, { h: .4 });
}
graphConvergence = connectionChance(g);
console.log(`C++ Graph Convergence: ${graphConvergence}%`);
drawGraph(g, g.nodes(), g.edges(), 'C26');
showConvergence('C26', graphConvergence);
// ================================================================================================================
function range(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function intensityMatrix(graph, min, max) {
const nodes = graph.nodes();
const matrix = [];
for (var i = 0; i < nodes.length; i += 1) {
for (var j = 0; j < nodes.length; j += 1) {
matrix.push(range(min, max));
}
}
return matrix;
}
function graphSettings(minBand, maxBand, chance) {
return {
h: chance,
c: range(minBand, maxBand),
a: 0,
weight: 1,
}
}
function newPetersen(min1, max1, chance) {
const graph = new graphlib.Graph({ directed: false, multigraph: false });
for (var i = 1; i <= 10; i += 1) {
graph.setNode(i + '');
}
for (var i = 1; i <= 5; i += 1) {
var setting = graphSettings(min1, max1, chance);
graph.setEdge(`${i}`, `${(i % 5 + 1)}`, setting);
// graph.setEdge(`${(i%5+1)}`, `${i}`, setting);
setting = graphSettings(min1, max1, chance);
graph.setEdge(`${i}`, `${i + 5}`, setting);
// graph.setEdge(`${i+5}`, `${i}`, setting);
setting = graphSettings(min1, max1, chance);
graph.setEdge(`${i + 5}`, `${(i + 1) % 5 + 6}`, setting);
// graph.setEdge(`${(i+1)%5+6}`, `${i+5}`, setting);
}
// +warkocz
for (var i = 1; i <= 4; i += 1) {
setting = graphSettings(min1, max1, chance);
graph.setEdge(`${i}`, `${((i + 1) % 5 + 1)}`, setting);
// graph.setEdge(`${((i+1)%5+1)}`, `${i}`, setting);
}
return graph;
}
function showMatrix(targetId, m) {
var element = document.querySelector('#'+targetId);
element.innerHTML = m;
}
const petersen = newPetersen(20000, 40000, 0.95);
const matrix = intensityMatrix(petersen, 10, 50);
showMatrix('intensityMatrix', matrix);
graphConvergence = connectionChance(petersen);
console.log(`Petersen Graph Convergence: ${graphConvergence}%`);
showConvergence('Petersen', graphConvergence);
function findRoute(graph, start, endpoint, stack, routes) {
var vertice = routes[endpoint];
if (vertice.predecessor === undefined) {
stack.push(start);
return stack.reverse();
} else {
graph.edge(vertice.predecessor, endpoint).weight += 1;
stack.push(endpoint);
return findRoute(graph, start, vertice.predecessor, stack, routes)
}
}
//rating edge
//check that it is possible to send package this way
function canGo(graph, route, dataStream, m) {
for (var i = 0; i < route.length - 1; i += 1) {
if (graph.edge(route[i], route[i + 1]).c / m - graph.edge(route[i], route[i + 1]).a < dataStream) {
return false;
}
}
return true;
}
var packetSize = 200;
function simulate(graph, mtx, pSize) {
function rateEdge(edge) {
return graph.edge(edge).weight;
}
var routess;
routess = graphlib.alg.dijkstraAll(graph, rateEdge, function (v) {
return graph.nodeEdges(v);
});
for (let i = 1; i <= graph.nodeCount(); i += 1) {
for (let j = 1; j <= graph.nodeCount(); j += 1) {
// prevent sent to ourselves
if (i == j)
continue;
for (var l = 0; l < 10; l += 1) {
let ds = mtx[(i - 1) * graph.nodeCount() + j];
//finding the shorthes path from A to B
let route = findRoute(graph, i + '', j + '', [], routess[i]);
// checking that we can send packadge this way
if (canGo(graph, route, ds, pSize)) {
// set a for founds route
for (let k = 0; k < route.length - 1; k += 1) {
graph.edge(route[k], route[k + 1]).a += ds;
}
//clearing weights
graph.edges().forEach((e) => {
graph.edge(e).weight = 1;
});
break;
}
// if(l === 9){
// console.log("rejected");
// }
}
}
}
}
simulate(petersen, matrix, packetSize);
drawGraph(petersen, petersen.nodes(), petersen.edges(), "Petersen");
//===================================================================
function SUM_e(m, graph) {
var result = 0;
graph.edges().forEach(e => {
var a = graph.edge(e).a
var c = graph.edge(e).c
result += (a / (c / m - a));
});
return result;
}
function T(matrix, sum_e) {
var G = matrix.reduce((prev, curr, index, arr) => {
return prev + curr;
});
return 1 / G * sum_e;
}
var tMax = T(matrix, SUM_e(packetSize, petersen));
document.querySelector('#meanLatency').innerHTML = (tMax*1000).toFixed(2);
console.log(tMax);
//===================================================================
function test(tries) {
var graphh = newPetersen(20000, 40000, 0.7);
var successes = 0;
const jsonDumpp = graphlib.json.write(graphh);
for (var i = 0; i < tries; i++) {
//zrobic kopie grafu
graphh = graphlib.json.read(jsonDumpp);
//usunac krawedzzie
graphh.edges().forEach(element => {
if (Math.random() > graphh.edge(element).h) {
graphh.removeEdge(element);
}
});
//sprawdzic czy jest spojny
if (graphlib.alg.components(graphh).length > 1) {
//jesli nie to continue
continue;
}
simulate(graphh, matrix, packetSize);
if (tMax <= T(matrix, SUM_e(packetSize, graphh))) {
successes += 1;
}
//jesli tak test macierzy dla tego grafu
}
return successes / tries;
}
var reliability = test(1000);
document.querySelector('#reliability').innerHTML = (reliability*100).toFixed(2);
console.log(reliability);