27 ms·
A few years ago I made up this [generic] genetic algorithm page in javascript. http://icefox.github.com/javascript_genetic_algorithm/?program=JavaScript%2520Ge
by icefox 16y ago
A few years ago I made up this [generic] genetic algorithm page in javascript. http://icefox.github.com/javascript_genetic_algorithm/?program=JavaScript%2520Genetic%2520Algorithm http://icefox.github.com/javascript_genetic_algorithm/?progr...
The neat part about it was that you could put in your own problem and have it solve it. I took a minute and slapped together the Hello World! version of this problem. You can copy it into the text box and play with the parameters (and resulting graph output. The output isn't a pretty as the link and it runs slower, but still neat.
function salesman() {
this.target = "hello world!";
this.points = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '!', ' ' ];
this.fitness = function(chromosome) {
// higher fitness is better
var f = 0; // start at 0; the best fitness
for(var i=0, c=this.target.length; i<c ; i++) {
f -= Math.abs(this.target.charCodeAt(i) - this.points[chromosome[i]].charCodeAt(0));
}
return Math.abs(f);
};
// the size of values that should be passed to fitness
this.numberOfArgs = function() { return this.target.length; };
// the max value needed for the arguments
this.maxArg = function() { return this.points.length; };
// convert the current chromosome value which can have a maxValue
// into something fitness can use.
this.getArg = function(value, maxValue) {
return Math.round(value * (this.points.length - 1) / maxValue);
};
// Paint the solution onto bestimage
this.paint = function(values) {
var canvas = document.getElementById('bestimage');
if (canvas.getContext){
var w = canvas.width;
var h = canvas.height;
var canvasContext = canvas.getContext('2d');
canvasContext.clearRect(0, 0, w, h);
canvasContext.font = "italic 200 12px/2 Unknown Font, sans-serif";
canvasContext.strokeStyle = "blue";
for (var i = 0; i < values.length; ++i){
canvasContext.strokeText(this.points[values[i]], i*10, 10);
}
}
}
}
salesman;
- icefox 16y agoA change to the code so it didn't hard code the letters, but just use ascii: http://benjamin-meyer.blogspot.com/2010/12/evolving-hello-world.html http://benjamin-meyer.blogspot.com/2010/12/evolving-hello-wo...
- user24 16y agoNice! Yeah I was thinking about trying to genericise it so you could plug your own fitness function in, but didn't get around to it. I got an error when playing with the values though; pop 400, mutation 0.2, crossover 0.8: "When executing function:this.people[peopleSize - i] is undefined"
- icefox 16y agoIn mine the two options have be less than 1.0 when combined. So out of all genomes, 20% (.2) goes to crossover, 80% (.8) goes to crossover and the rest live. The two numbers have to be less than 1.0 for some to remain alive from one round to the next. This gives the option to also specify how many survive from one round to the next v.s (correct me if this is wrong) a random amount each time in your example. For what it is worth you graph looks much nicer :) Back when I first made mine the canvas tag wasn't around and js was much slower. Really should remove the 'you might get an image!' text before the graph.
- user24 16y agoAh, right I see :) > This gives the option to also specify how many survive from one round to the next v.s (correct me if this is wrong) a random amount each time in your example. in mine the population is constant. A set of two parents always have exactly two children. Breeding occurs until there are exactly as many children as there are parents, then the children mutate, and murder their parents. Genetics is fun! Yeah I'm really happy with the graph, thanks! Canvas API is a bit clunky, but powerful (especially now we're allowed to write text into it!) edit: what selection method are you using?
- icefox 16y agoSay 10% mutate and 60% crossover, that leaves 30% to live another round and be the parents for the crossover and mutation. After the fitness function is run the results are sorted by the fitness value. The lowest get to live and be part of the 30% the live/mutate/crossover. Link to the js: http://icefox.github.com/javascript_genetic_algorithm/javascriptgeneticalgorithm.js http://icefox.github.com/javascript_genetic_algorithm/javasc...