## Evolving solutions

User projects written in or related to FreeBASIC.
BasicCoder2
Posts: 3421
Joined: Jan 01, 2009 7:03

### Evolving solutions

Reading in another post about GA to generate an algorithm reminded me of the biomorphs I first read about in "The Blind Watchmaker" by Richard Dawkins. Computer programs are a useful way to illustrate and make use of the ideas of non-random selection of random variations to incrementally move toward more complex working solutions in working stages.

This is a rewrite of an old program I wrote inspired by the biomorphs to generate random patterns that could be evolved. The example in the animal kingdom is groups of apparently different animals really being made up of the same components modified. The skeleton being a good visual example. My patterns consists of 10 lines of random length which are connected end to end with different angles. So I guess you could say it has a gene for each segment specifying a length and a connection angle. Even with only two variables (animals have many more) a lot of visually pleasing patterns can be generated.

The program below generates a random pattern and then 8 variants. There appears to be something wrong with the code that generates the variants in that the bilateral pattern appears to be assymetrical. With a selecting agent one pattern can evolve into another pattern.

Sexual reproduction would require parents swapping genes allowing large but stable changes as oppposed to the incremental changes possible with mutations.

Code: Select all

`'some useful definesConst Pi = 4 * Atn(1)Dim Shared As Double TwoPi = 8 * Atn(1)Dim Shared As Double RtoD = 180 / Pi   ' radians * RtoD = degreesDim Shared As Double DtoR = Pi / 180   ' degrees * DtoR = radiansscreenres 960,480,32type PATTERN    as double x(10)    as double x2(10)    as double y(10)    as double y2(10)    as double s(10)    as double s2(10)    as double a(10)    as double a2(10)end typedim as PATTERN p(8)dim as PATTERN p1     'parent patternp1.x(0)=240p1.y(0)=150p1.x2(0)=240p1.y2(0)=150for j as integer = 0 to 7    p(j).x(0)  = j*100+100    p(j).y(0)  = 300    p(j).x2(0) = j*100+100    p(j).y2(0) = 300next j'start with parent patternfor i as integer = 0 to 9    p1.s(i)=int(rnd(1)*10)+10    'length    p1.a(i)=int(rnd(1)*360)*DtoR 'angle    p1.a2(i)=(180-(p1.a(i)*RtoD))*DtoR 'horizontal flip degrees    p1.s2(i)=p1.s(i)next idim as integer selectiondo    cls        'generate variants of parent pattern    for j as integer = 0 to 7        for i as integer = 0 to 9            p(j).s(i)=  p1.s(i) + int(rnd(1)*3)-1              'length            p(j).a(i)= (p1.a(i)*RtoD + (int(rnd(1)*3)-1))*DtoR 'angle            p(j).a2(i)= (180-(p1.a(i)*RtoD))*DtoR   'horizontal flip degrees            p(j).s2(i)= p1.s(i)        next i    next j        screenlock()        'draw parent pattern    circle (p1.x(0),p1.y(0)),3,rgb(255,255,0)    for i as integer = 1 to 9  'for each segement        p1.x(i) = p1.x(i-1) + cos(p1.a(i)) * p1.s(i)        p1.y(i) = p1.y(i-1) + sin(p1.a(i)) * p1.s(i)        line (p1.x(i-1),p1.y(i-1))-(p1.x(i),p1.y(i)),rgb(255,255,255)        p1.x2(i) = p1.x2(i-1) + cos(p1.a2(i)) * p1.s2(i)        p1.y2(i) = p1.y2(i-1) + sin(p1.a2(i)) * p1.s2(i)        line (p1.x2(i-1),p1.y2(i-1))-(p1.x2(i),p1.y2(i)),rgb(255,255,255)    next i    'draw variants    for j as integer = 0 to 7  'for each pattern        circle (p(j).x(0),p(j).y(0)),3,rgb(255,255,0)             for i as integer = 1 to 9  'for each segment                        p(j).x(i) = p(j).x(i-1) + cos(p(j).a(i)) * p(j).s(i)            p(j).y(i) = p(j).y(i-1) + sin(p(j).a(i)) * p(j).s(i)            line (p(j).x(i-1),p(j).y(i-1)) - (p(j).x(i),p(j).y(i)),rgb(0,255,0)            p(j).x2(i) = p(j).x2(i-1) + cos(p(j).a2(i)) * p(j).s2(i)            p(j).y2(i) = p(j).y2(i-1) + sin(p(j).a2(i)) * p(j).s2(i)            line (p(j).x2(i-1),p(j).y2(i-1)) - (p(j).x2(i),p(j).y2(i)),rgb(0,0,255)                    next i    next j        locate 2,2    print "Hit space bar to generate new variation"    locate 4,2    print "Hit ESC key to exit program"    locate 50,2     print "           1            2           3            4            5           6           7            8"    screenunlock()    locate 6,2        'input "select variation 1 to 8 or 0 to exit";selection    'dim as integer selection,choice    'selection = choice+1    'copy to new parent    'if selection >0 and selection < 9 then    '    for i as integer = 0 to 9    '        p1.s(i) = p(selection-1).s(i)    '        p1.a(i) = p(selection-1).a(i)    '        p1.s2(i)= p(selection-1).s2(i)    '        p1.a2(i)= p(selection-1).a2(i)    '    next i    'end if    'print "CHOICE = ";choice        for i as integer = 0 to 9        p1.s(i)=int(rnd(1)*10)+10    'length        p1.a(i)=int(rnd(1)*360)*DtoR 'angle        p1.a2(i)=(180-(p1.a(i)*RtoD))*DtoR 'horizontal flip degrees        p1.s2(i)=p1.s(i)    next i    sleep   loop until multikey(&H01)end`
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

### Re: Evolving solutions

Very cool.
TESLACOIL
Posts: 1769
Joined: Jun 20, 2010 16:04
Location: UK
Contact:

### Re: Evolving solutions

worth reading up on Hox genes
http://en.wikipedia.org/wiki/Hox_gene

A homeobox is a DNA sequence found within genes that are involved in the regulation of patterns of anatomical development (morphogenesis) in animals, fungi and plants.

http://en.wikipedia.org/wiki/Homeobox

AFAIK almost any complex shape, Tree, Car, Street map whatever can be generated this way. You might need to do a bit of brute force 'algorithm hunting' if you want do reverse engineer a fixed data set that already exists. You only have to look at mother nature to see what can be achieved.

Fun stuff !
TESLACOIL
Posts: 1769
Joined: Jun 20, 2010 16:04
Location: UK
Contact:

### Re: Evolving solutions

Creating huge procedurally generate worlds