This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsp_worker.js
134 lines (115 loc) · 3.74 KB
/
tsp_worker.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
var util = require('util');
var _ = require('lodash-node');
var tsplib = require('./lib/tsplib.js');
var args = process.argv.splice(2);
if(args.length <= 0 || args.length > 1)
{
console.error('Usage: ' + process.argv[0] + ' ' + process.argv[1] + ' <port>');
return -1;
}
// Cleanup
process.on('SIGINT', function(){
console.log('Teardown connected clients...');
io.sockets.clients().forEach(function(client){client.disconnect();});
process.exit(-1);
});
var port = parseInt(args[0]);
var io = require('socket.io').listen(port);
var TSPSolver = null;
var Network = null;
var isStarted = false;
var ccSocket = null;
var sendPartial = null;
var currentProblem = null;
io.sockets.on('connection', function (socket) {
//socket.emit('get_problem');
// TODO: theoretically we could cluster nodes therefore we would have to handle more sockets
// But this may be some future. We could combine all local nodes to one.
// Because of not too many time to finish this app we will just spawn n worker with seperate ports for each worker.
// n will be the number of processors
ccSocket = socket;
socket.on('init', function(data){
// data.graph - Global Graph
// data.start - Where to start our travel
var graph = data.graph;
for(var idx in graph)
{
var value = graph[idx];
if(value !== -1)
graph[idx] = value;
else
graph[idx] = Infinity;
}
console.log('Init: Got graph', graph);
Network = tsplib.GraphNetwork().createNetworkFromGraph(graph);
TSPSolver = new tsplib.TravellingSalesmanSolver(Network, data.start);
TSPSolver.toggleDebug();
// TODO: optimalDepth should be generated through an plausible heuristic!
socket.emit('init', {done: true, optimalDepth: 2});
});
socket.on('problem', function(data){
// data.problem
isStarted = true;
currentProblem = data.problem;
TSPSolver.setPartialProblem(currentProblem);
console.log('Event.problem: ',data);
});
socket.on('solution', function(data){
// data.path
// data.cost
TSPSolver.setBestCost(data.path, data.cost);
console.log('Event.solution: ',data);
});
socket.on('start', function(){
isStarted = true;
socket.emit('start', true);
console.log('Event.start.');
});
socket.on('split', function(data){
// data.depth
sendPartial = data;
console.log('Event.split: ',data);
});
});
var solveFunctionThread = function(){
if(isStarted)
{
if(currentProblem)
{
TSPSolver.solve(TSPSolver.getNetworkSize(), function(){
ccSocket.emit('solution',{
path: tsplib.Util.nodePathToNamePath(TSPSolver.getBestPath()),
cost: TSPSolver.getBestCost()
});
});
}else{
ccSocket.emit('problem');
isStarted = false;
}
if(TSPSolver.isFinished())
{
currentProblem = null;
}else{
if(!_.isNull(sendPartial))
{
ccSocket.emit('split', {
problem: TSPSolver.getPartialProblem(sendPartial.depth)
});
sendPartial = null;
}
}
setImmediate(
solveFunctionThread
);
}else{
// Passively wait... else we burn our node to death with noop
setTimeout(solveFunctionThread, 1000);
}
};
setTimeout(solveFunctionThread, 1000);
/**
* TODO
* Split the Problem, so we can have multiple solver.
* Create the real worker application.
* Create the "master server" frontend.
*/