I am implementing a Random Sampling / Montecarlo heuristic for the Travelling Salesman Problem.
I want to perform maximum c
iterations, while being able to stop the search when I want by Ctrl+C or sending SIGINT
to my process.
I know this has been asked before (related: Quitting node.js gracefully) but the given solution is not working for me. The process just doesn't exit when I give Ctrl+C and if I kill it, the finalizing code is not executed.
My code:
var tsp = require("./Tsp.js");
var data = tsp.load("./input/it16862.tsp");
var starting_point = data.splice(10927, 1)[0];
var c = 0;
var cost = Number.POSITIVE_INFINITY;
var new_cost, solution, candidate_solution;
var interrupt = false;
var c = 0, interrupt = false;
process.on('SIGINT', function() {
interrupt = true;
});
while(c < 1000000000) {
if (interrupt) {
break;
}
candidate_solution = shuffle(data);
new_cost = tsp.cost(candidate_solution, starting_point);
if (new_cost < cost) {
cost = new_cost;
solution = candidate_solution;
console.log("Found a new better solution! %d", cost);
}
c++;
}
if (interrupt) {
console.log("Caught interrupt signal");
}
console.log("Examined %d solutions", c);
console.log("Best: %j", cost);
console.log("Solution written to: %s", tsp.write(solution, starting_point, cost));
I am on Ubuntu 14.04.1
, Nodejs 4.2.4
. Any idea what might be wrong?