-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
378 lines (339 loc) · 15.4 KB
/
index.html
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Agent Consensus</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"
integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"
integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
</head>
<style type="text/css">
body {
padding-top: 1rem;
}
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
.form-row {
padding-top: 0.5rem;
}
.row-brake {
padding-top: 1.5rem;
}
a, a:visited, a:hover, a:active {
color: inherit;
}
i {
padding-top: 2px;
}
</style>
<body>
<main role="main" class="container">
<div class="row">
<div class="col-sm col-sm-4">
<div class="row form-row">
<div class="col-sm col-sm-2">
<a href="https://github.com/dragoslav/consensus">
<i class="fa fa-github fa-2x" aria-hidden="true"></i>
</a>
</div>
<div class="col-sm col-sm-10">
<button type="button" class="btn btn-primary btn-block" id="buttonRun" disabled>Run Simulation</button>
</div>
</div>
<div class="row form-row">
<div class="input-group" data-toggle="tooltip" data-placement="bottom"
title="Random number generator, leave it empty for time based seed.">
<span class="input-group-addon col-6" id="basic-addon1">seed</span>
<input type="number" class="form-control col-6" id="seed" placeholder="" value="0"
aria-label="seed" aria-describedby="basic-addon1">
</div>
</div>
<div class="row form-row">
<div class="input-group" data-toggle="tooltip" data-placement="bottom" title="Number of rounds to run.">
<span class="input-group-addon col-6" id="basic-addon2">rounds</span>
<input type="number" class="form-control col-6" id="rounds" placeholder="10" value="10" min="1"
aria-label="rounds" aria-describedby="basic-addon2">
</div>
</div>
<div class="row form-row">
<div class="input-group" data-toggle="tooltip" data-placement="bottom" title="Total number of agents.">
<span class="input-group-addon col-6" id="basic-addon3"># agents</span>
<input type="number" class="form-control col-6" id="na" placeholder="100" min="1" value="100"
aria-label="# agents" aria-describedby="basic-addon3">
</div>
</div>
<div class="row form-row">
<div class="input-group" data-toggle="tooltip" data-placement="bottom"
title="Total number of unique messages.">
<span class="input-group-addon col-6" id="basic-addon4"># messages</span>
<input type="number" class="form-control col-6" id="nm" placeholder="500" min="1" value="500"
aria-label="# messages" aria-describedby="basic-addon4">
</div>
</div>
<div class="row form-row">
<div class="input-group" data-toggle="tooltip" data-placement="bottom"
title="Probability of an edge between agents.">
<span class="input-group-addon col-6" id="basic-addon5">connection %</span>
<input type="number" class="form-control col-6" id="cp" placeholder="0.1"
min="0.01" value="0.1" step="0.01" max="1"
aria-label="connection %" aria-describedby="basic-addon5">
</div>
</div>
<div class="row form-row">
<div class="input-group" data-toggle="tooltip" data-placement="bottom"
title="Probability of an erroneous agent.">
<span class="input-group-addon col-6" id="basic-addon6">error %</span>
<input type="number" class="form-control col-6" id="ep" placeholder="0.3"
min="0" value="0.3" step="0.01" max="1"
aria-label="error %" aria-describedby="basic-addon6">
</div>
</div>
<div class="row form-row">
<div class="input-group" data-toggle="tooltip" data-placement="bottom"
title="Percentage of messages initially distributed per agent.">
<span class="input-group-addon col-6" id="basic-addon7">message %</span>
<input type="number" class="form-control col-6" id="mp"
placeholder="0.01" min="0.001" value="0.01" step="0.001" max="1"
aria-label="message %" aria-describedby="basic-addon7">
</div>
</div>
<div class="row form-row row-brake">
<div class="form-group col-12">
<label for="cn">Compliant Agent</label>
<select id="cn" class="form-control" aria-label="compliant agent">
<option value="0">Collect Only</option>
<option value="1">Collect/Reduce</option>
</select>
</div>
</div>
<div class="row form-row">
<div class="form-group col-12">
<label>Erroneous Agents</label>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" id="en0">
Quiet
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" id="en1">
Send Initial Only
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" id="en2">
Send One Each Round
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" id="en3">
Flush at Finish
</label>
</div>
</div>
</div>
</div>
<div class="col-sm col-sm-8">
<div class="row">
<canvas id="agentCanvas"></canvas>
</div>
<div class="row">
<canvas id="messageCanvas"></canvas>
</div>
</div>
</div>
</main>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"
integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"
integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ"
crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>
<script type="text/javascript" src="./target/scala-2.12/consensus-opt.js"></script>
<script type="text/javascript">
let config = {
type: 'line',
data: {
labels: [],
datasets: [{
label: "",
backgroundColor: '',
borderColor: '',
data: [],
fill: false,
lineTension: 0
}]
},
options: {
responsive: true,
title:{
display: false,
text:''
},
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Round'
}
}],
yAxes: [{
id: '1',
display: true,
scaleLabel: {
display: true,
labelString: ''
},
stacked: true
}]
}
}
};
let rounds;
let simulator;
let currentRound;
let running = false;
let agentChartConfig = JSON.parse(JSON.stringify(config));
agentChartConfig.data.datasets[0].label = '# Agents with Consensus';
agentChartConfig.data.datasets[0].borderColor = 'rgb(54, 162, 235)';
agentChartConfig.data.datasets[0].backgroundColor = 'rgb(54, 162, 235)';
agentChartConfig.options.scales.yAxes[0].scaleLabel.labelString = '# Agents';
let messageChartConfig = JSON.parse(JSON.stringify(config));
messageChartConfig.data.datasets[0].label = '# Consensus Messages';
messageChartConfig.data.datasets[0].borderColor = 'rgb(75, 192, 192)';
messageChartConfig.data.datasets[0].backgroundColor = 'rgb(75, 192, 192)';
messageChartConfig.options.scales.yAxes[0].scaleLabel.labelString = '# Messages';
function run() {
rndSeed = $('#seed').val() || Date.now();
rounds = $('#rounds').val() || 10;
numberOfAgents = $('#na').val() || 100;
numberOfMessages = $('#nm').val() || 500;
connectionProbability = $('#cp').val() || 0.1;
erroneousAgentProbability = $('#ep').val() || 0.3;
initialMessageDistributionProbability = $('#mp').val() || 0.01;
compliantAgent = $('#cn').val() || 0;
erroneousAgents = [];
for (let i = 0; i < 4; i++)
if ($('#en' + i).prop('checked')) erroneousAgents.push(i);
if (erroneousAgents.length === 0) erroneousAgentProbability = 0;
currentRound = 0;
simulator = new JsSimulator(rndSeed, rounds, numberOfAgents, numberOfMessages, connectionProbability,
erroneousAgentProbability, initialMessageDistributionProbability, compliantAgent, erroneousAgents);
agentChartConfig.data.labels.length = 0;
messageChartConfig.data.labels.length = 0;
agentChartConfig.data.datasets[0].data.length = 0;
messageChartConfig.data.datasets[0].data.length = 0;
for (let i = 1; i <= rounds; i++) {
agentChartConfig.data.labels.push(i);
messageChartConfig.data.labels.push(i);
}
window.agentChart.update(0);
window.messageChart.update(0);
running = true;
update();
}
function update() {
setTimeout(function() {
let next = simulator.next();
agentChartConfig.data.datasets[0].data.push(next[0]);
messageChartConfig.data.datasets[0].data.push(next[1]);
window.agentChart.update(0);
window.messageChart.update(0);
if (currentRound++ <= rounds)
update();
else
finished();
}, 0);
}
function finished() {
running = false;
document.getElementById('buttonRun').disabled = false;
addHistory();
}
function addHistory() {
let index = location.href.indexOf('?');
let url = index >= 0 ? location.href.substring(0, index) : location.href;
let params = [];
params.push('seed=' + $('#seed').val());
params.push('rounds=' + $('#rounds').val());
params.push('na=' + $('#na').val());
params.push('nm=' + $('#nm').val());
params.push('cp=' + $('#cp').val());
params.push('ep=' + $('#ep').val());
params.push('mp=' + $('#mp').val());
params.push('cn=' + $('#cn').val());
let erroneousAgents = [];
for (let i = 0; i < 4; i++)
if ($('#en' + i).prop('checked')) erroneousAgents.push(i);
params.push('en=' + erroneousAgents.join(','));
if (JSON.stringify(history.state) !== JSON.stringify(params))
history.pushState(params, document.title, url + '?' + params.join('&'));
}
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function updateConfiguration() {
['seed', 'rounds', 'na', 'nm', 'cp', 'ep', 'mp'].forEach(function(input) {
let formInput = $('#' + input);
formInput.on('input', function() {
let button = $('#buttonRun');
button.prop('disabled', running || !formInput[0].validity.valid);
});
let value = getParameterByName(input);
if (value) formInput.val(value);
});
let cnValue = getParameterByName('cn');
if (cnValue) $('#cn').val(cnValue);
let enValue = getParameterByName('en');
if (enValue) {
for (let i = 0; i < 5; i++)
$('#en' + i).prop('checked', false);
enValue.split(',').forEach(function (index) {
$('#en' + index).prop('checked', true);
});
}
}
$(function () {
$('[data-toggle="tooltip"]').tooltip();
$('#buttonRun').click(function(e) {
e.target.disabled = true;
run();
});
updateConfiguration();
window.agentChart = new Chart($('#agentCanvas')[0].getContext("2d"), agentChartConfig);
window.messageChart = new Chart($('#messageCanvas')[0].getContext("2d"), messageChartConfig);
window.onpopstate = function(event) {
updateConfiguration();
if (!running) run();
};
run();
});
</script>
</body>
</html>