-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.js
50 lines (40 loc) · 1.51 KB
/
server.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
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server)
, simplega = require('../../')
, tsp = require('../tsp/tsp.js');
server.listen(8080);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
var population;
var engine = new simplega.Engine();
var mutator = new tsp.Mutator();
engine.setMutators([mutator]);
var stopped = false;
io.sockets.on('connection', function (socket) {
socket.on('newproblem', function (data) {
var points = tsp.createPointRectangle(data.width, data.height);
var maxlength = data.width * data.height * (data.width*data.width + data.height*data.height);
population = tsp.createPopulation(5000, points, maxlength);
engine.setPopulation();
stopped = false;
function doStep() {
engine.setPopulation(population);
population = engine.nextPopulation();
var bestvalue = simplega.getBestValue(population);
var bestpath = maxlength - bestvalue;
var l = population.length;
console.log(l + ': ' + bestpath);
for (var k = 0; k < l; k++)
if (population[k].evaluate() == bestvalue)
socket.emit('newresult', { value: bestpath, values: population[k].getValues() });
if (!stopped)
process.nextTick(doStep);
}
process.nextTick(doStep);
});
socket.on('stop', function() {
stopped = true;
});
});