You are here

Evolving "Hello World!"

Benjamin's Personal Blog - Tue, 14/12/2010 - 5:14am
puremango has put together a nice little writeup on using genetic algorithms to generate the classic "Hello World!" He has an attached webpage that lets you play with the parameters and generate a nice graph.

A few years ago I put together a generic genetic javascript algorithm webpage. While it doesn't have the nice writeup or the cool graph (only a basic one) the cool feature was that you could modify the input box to put in your problem. Taking a few minutes here is the hello world! version that you can put in the text box and run to have it discover hello world.

function salesman() { = "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', '!', ' ' ]; = function(chromosome) { // higher fitness is better var f = 0; // start at 0; the best fitness for(var i=0,; i f -= Math.abs( - this.points[chromosome[i]].charCodeAt(0)); } return Math.abs(f); };
// the size of values that should be passed to fitness this.numberOfArgs = function() { return; };
// 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 <> canvasContext.strokeText(this.points[values[i]], i*10, 10); } } }}salesman;