Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

98 lines (76 sloc) 2.437 kb
<html>
<head>
<title>Travelling Salesman Problem</title>
<script src="../../lib/simplega.js" language="javascript"></script>
<script src="../tsp/tsp.js" language="javascript"></script>
<link href="../bootstrap.min.css" rel="stylesheet">
<link href="../bootstrap-theme.min.css" rel="stylesheet">
</head>
<body>
<h1>Travelling Salesman Problem</h1>
<div>
<input type="button" value="Relaunch" onclick="relaunch()">
</div>
<div>
<canvas id="canvas" width="600" height="500">
</canvas>
</div>
<script language="javascript">
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
function drawPoints()
{
context.clearRect(0, 0, canvas.width, canvas.height);
for (var k = 0; k < 5; k++)
for (var j = 0; j < 4; j++) {
context.beginPath();
context.arc(50 + 100 * k, 50 + 100 * j, 10, 0, Math.PI * 2, true);
context.closePath();
context.fill();
}
}
function drawValues(points, values)
{
context.beginPath();
var l = values.length;
var point = points[values[0]];
context.moveTo(50 + 100 * point.x, 50 + 100 * point.y);
for (var k = 1; k < l; k++) {
point = points[values[k]];
context.lineTo(50 + 100 * point.x, 50 + 100 * point.y);
}
context.closePath();
context.stroke();
}
var points = tsp.createPointRectangle(5, 4);
var maxlength = 5 * 4 * (5*5 + 4*4);
drawPoints();
var engine = new simplega.Engine();
var population = tsp.createPopulation(10000, points, maxlength);
engine.setMutators([new tsp.Mutator()]);
var bestvalue = 0;
var bestpath;
setTimeout(doStep, 10);
function doStep() {
engine.setPopulation(population);
population = engine.nextPopulation();
var newbestvalue = simplega.getBestValue(population);
if (newbestvalue >= bestvalue) {
bestvalue = newbestvalue;
var l = population.length;
for (var k = 0; k < l; k++)
if (population[k].evaluate() == newbestvalue) {
drawPoints();
drawValues(points, population[k].getValues());
}
}
setTimeout(doStep, 10);
}
function relaunch() {
var newpopulation = tsp.createPopulation(5000, points, maxlength);
bestvalue = 0;
population = newpopulation;
}
</script>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.