Showing posts with label biomorphs. Show all posts
Showing posts with label biomorphs. Show all posts

Monday, 7 February 2011

Biomorphs and Javascript - a marriage made in purgatory.

Well it definitely isn't heaven, and it's not really hell (6502 assembly programming on a machine with no permanent memory and a dodgy power supply comes close).

I'm not sure I'm ready to devote a whole post to how crap Javascript is, I imagine that there are probably whole sites, probably complete universes, devoted to just that topic. But here's a handy hint -your variable isn't locally initialised unless you stick var in front of it, but your program will run anyway, perhaps almost correctly. Having been corrupted by languages where you don't declare variables, just use them, I got some unexpected side effects with the Biomorphs and the variables became global -even though they had been declared locally, inside a function scope. Enough, I've got to go and fill in the hole in the plaster where I was banging my head on the wall.

The biomorph program has had an overhaul and now produces, occasionally, things that look like biomorphs.  The main changes have been to switch from polar to Cartesian coordinates and to introduce randomness. The coordinate switch allows the introduction of a 'gene' as a string of x and y coordinates, and having this gene means that it can be populated randomly and thus gives us the potential for 'evolution'. Here's the guts :

function bio_morph()
{
        this.gene = new Array(9);

        //Generate a random gene 7 is the number of generations and gene 8 is the stem length
        for(var i = 0; i<7; i++) {
                this.gene[i] = Math.round((Math.random() * 20) -10);
        }
        this.gene[7] = Math.random() * 10;
        this.gene[8] = Math.round(Math.random() * 10);
}
Multiplying by 20 and taking away 10 allows us to have negative numbers in our random sequence, so the creatures can gro up and down, left and right.

All that remains is to produce an array of the critters and allow you to choose the one start new generations from.

As always take a look at the main biomorph page.

Sunday, 23 January 2011

Biomorphs - software genetics in HTML5 and Javascript.

'Pull  the switch Igor -we're frying tonight!'  Today we're going to create artificial life MWHAHAHA! Well not really, but we are going to have a play with genetics and software development, less fun than lightning and a methane atmosphere -but quicker and easier to arrange than a suitable planet.

Example Biomorphs


Richard Dawkins describes biomorphs in his book The Blind Watchmaker (chapter 3). Biomorphs are shapes that change by mutation and it is possible to build up creatures by selecting from the generated shapes. Pretty much a game really, although the book does carry a good description of n-dimensional spaces (9 in this instance) -maybe I finally get it.

As an exercise in brushing up my Javascript -and playing with HTML5 I am going to have a go at creating a version of his concept. The reason for Javascript and HTML5 is that I can host a page with the program on this blog and not have to set up a server somewhere.

In Dawkins original a biomorph is a tree structure that has 9 genes  controlling its growth, these genes control things like stem length, branch angle, number of iterations of breeding &c.. There isn't an algorithm as such in the book so this will be an 'Agile' (or suck it and see) development and I'll basically solve small problems and hope that I can bolt them all together for an overall solution.

Finally, as Biomorphs are tree structures the natural algorithm to draw them will be recursive, it'll be interesting to see if Javascript and the browsers can handle that.

The program will appear on a separate page available from the top menu, our first design decision!

linkedin