Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/algorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function tribulate() {
//for(var i=0; i<POPULATION_SIZE; i++) {
for(var i=population.length>>1; i<POPULATION_SIZE; i++) {
population[i] = randomIndivial(points.length);
}
}
}
function selection() {
var parents = new Array();
Expand All @@ -56,14 +56,14 @@ function crossover() {
if( Math.random() < CROSSOVER_PROBABILITY ) {
queue.push(i);
}
}
}
queue.shuffle();
for(var i=0, j=queue.length-1; i<j; i+=2) {
doCrossover(queue[i], queue[i+1]);
//oxCrossover(queue[i], queue[i+1]);
}
}
//function oxCrossover(x, y) {
//function oxCrossover(x, y) {
// //var px = population[x].roll();
// //var py = population[y].roll();
// var px = population[x].slice(0);
Expand Down Expand Up @@ -117,7 +117,7 @@ function mutation() {
}
}
}
function preciseMutate(orseq) {
function preciseMutate(orseq) {
var seq = orseq.clone();
if(Math.random() > 0.5){
seq.reverse();
Expand All @@ -133,7 +133,7 @@ function preciseMutate(orseq) {
//alert(bestv);
return seq;
}
function preciseMutate1(orseq) {
function preciseMutate1(orseq) {
var seq = orseq.clone();
var bestv = evaluate(seq);

Expand Down Expand Up @@ -246,7 +246,7 @@ function countDistances() {
for(var i=0; i<length; i++) {
dis[i] = new Array(length);
for(var j=0; j<length; j++) {
dis[i][j] = ~~distance(points[i], points[j]);
dis[i][j] = ~~distance(points[i].endsAt, points[j]);
}
}
}
51 changes: 34 additions & 17 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var population;
var values;
var fitnessValues;
var roulette;
var scaleFactor;

$(function() {

Expand Down Expand Up @@ -144,6 +145,17 @@ r.onload = function(e) {

console.log(notG0);

// find ending point of each segment
for (let some of allG0) {
for (let j = some.followingLines.length - 1; j >= 0; j--) {
let xy = getXY(some.followingLines[j]);
if (xy[0] !== false && xy[1] !== false) {
some.endsAt = { x: xy[0], y: xy[1] };
break;
}
}
}

// add notG0 to the followingLines for the last entry in allG0
// this gets the lines after the last G0 in the file
// we also need to check if the commands here are not G0, G1, G2, G3, or G4
Expand Down Expand Up @@ -196,16 +208,7 @@ r.onload = function(e) {
sf = yf;
}

for (var p=0; p<allG0.length; p++) {

// scale it
allG0[p].y = allG0[p].y*sf;
allG0[p].x = allG0[p].x*sf;

// flip the y axis because cnc and canvas world are opposite there
allG0[p].y = 600 - allG0[p].y;

}
ctx.setTransform(scaleFactor, 0, 0, -scaleFactor, 0, 600);

points = allG0;
draw();
Expand All @@ -215,7 +218,7 @@ r.onload = function(e) {
}
});

$('#start_btn').click(function() {
$('#start_btn').click(function() {
if(points.length >= 3) {
initData();
GAInitialize();
Expand Down Expand Up @@ -304,7 +307,8 @@ function initData() {
roulette = new Array(POPULATION_SIZE);
}

function drawCircle(point) {
function drawCircle(point, fillStyle = '#000') {
ctx.fillStyle = fillStyle;
ctx.fillStyle = '#000';
ctx.beginPath();
ctx.arc(point.x, point.y, 3, 0, Math.PI*2, true);
Expand All @@ -321,9 +325,15 @@ function drawLines(array) {
ctx.moveTo(points[array[0]].x, points[array[0]].y);

// loop through and draw lines to each other point
for(var i=1; i<array.length; i++) {
ctx.lineTo( points[array[i]].x, points[array[i]].y )
}
for (var i = 1; i < array.length; i++) {
ctx.lineTo(points[array[i]].x, points[array[i]].y)
let endpoint = points[array[i - 1]].endsAt;
if (endpoint) {
ctx.moveTo(endpoint.x, endpoint.y);
}
let movepoint = points[array[i]];
ctx.lineTo(movepoint.x, movepoint.y);
}
ctx.lineTo(points[array[0]].x, points[array[0]].y);

ctx.stroke();
Expand All @@ -350,6 +360,10 @@ function draw() {
drawCircle(points[i]);
}

for (var i = 0; i < points.length; i++) {
drawCircle(points[i].endsAt, "#00ff00");
}

// draw the path
if(best.length === points.length) {
drawLines(best);
Expand All @@ -359,5 +373,8 @@ function draw() {
}

function clearCanvas() {
ctx.clearRect(0, 0, WIDTH, HEIGHT);
}
let xform = ctx.getTransform();
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, WIDTH, HEIGHT);
ctx.setTransform(xform);
}