Genetic context free

Here you can discuss and share functionality improvements and helper programs to make Context Free better.

Moderators: MtnViewJohn, chris, mtnviewmark

andrewborrell
Posts: 8
Joined: Wed Apr 04, 2007 1:50 pm

Genetic context free

Post by andrewborrell »

I was wondering if anybody had tried applying some sort of genetic algorithm to context free art scripts. Basically the idea would be, you start of with one simple script, perhaps containing just one rule with a circle in it. Then 10 mutations of that are made and rendered. A human is required to pick the best of the 10. Then 10 mutations are made of that and so on.

A mutation would consist of adding a rule, or adding an instantiation of a rule to a rule, or changing the parameters used to call another rule somewhere.

You could even quite easily implement sexual reproduction, where two scripts are chosen and the offspring contain a mixture of rules from its two parents along with a bit of mutation thrown in aswell. In the this situation you might get instanciation of rules which are nolonger in the genone, but you could handle this by simply ignoring those rule calls.

I think this could produce some quite diverse cool pictures very quickly.

jeremydouglass
Posts: 17
Joined: Fri Mar 30, 2007 9:10 pm

Genetics and emergence

Post by jeremydouglass »

Interesting idea - vaguely related to this other idea on random rulesets, here: viewtopic.php?p=1708

One great potential of this is that CF would be great at doing this internally. The big problem: "offspring" are going to be the results of specific rules, and as far as I know CF doesn't tell you which rules were active for a given render, so you can't actually get the results for the next iteration.

So, for example, Guigui's Piupole is already a kind of genetic algorithm for producing birds - to create the same bird faces but make the underlying rules look more like genetics, all you would have to do is reorganize the file into two columns of rules and a picker:

Code: Select all

// birth
rule child { pick_body{} pick_face {} }

// parent genome mixing
pick_body { parent1_body {} }
pick_body { parent2_body {} }
pick_face { parent1_face {} }
pick_face { parent2_face {} }

// parent genome 1
rule parent1-body { etc }
rule parent1-body { etc }
rule parent1-face { etc }
rule parent1-face { etc }

// parent genome 2
rule parent2-body { etc }
rule parent2-body { etc }
rule parent2-face { etc }
rule parent2-face { etc }
Now we can output a bunch of children, or one at a time... but we can't take the rules that generated a particular child and write them to a new file unless we try to select them manually - a process that would be slow and fraught with errors even on very simple examples.

This problem doesn't go away if you separate the parents into "include parent1.cfdg" and "include parent2.cfdg" - because 1. unless the resulting rules are mixing it won't look like genetics, 2. only CF can mix the rules, 3. CF doesn't tell you how it mixed them etc.

To the developers: How hard would it be to output the rules that matched on a given render, I wonder - even in an ugly form? CF must handle them internally, but parsed somehow...?

jeremydouglass
Posts: 17
Joined: Fri Mar 30, 2007 9:10 pm

Example up

Post by jeremydouglass »

I adapted Guigui's Puipole into an example: generational.cfdg

You could add as many parents as you wanted, mix and match include files, or just create one child each time... but, as CF works now, you'll never have access to the child's ruleset, so you can't go beyond one generation.

User avatar
Nom
Posts: 30
Joined: Tue Jan 30, 2007 6:45 am

Post by Nom »

There you have the meaning of the words "context free" - any choices that are made while parsing a sentence must be forgotten for ever, as far as the parser goes - they will only be visible in the form of the resulting sentence (in our case a bunch of rules that define circles and squares). Think of trying to make a shadow or a reflection for an object in CF - it's impossible.

One could try reverse-parsing, and that's essentialy what we do while looking at a CF picture and saying, "aha, that's how that one was produced". But going reverse is a weighty operation and error-prone, too, because the choice between any overlapping branches has to be made in random.

Context free only parses rules of the form
A -> B
(convert A into B).

example:

rule A { B { } }

The attributes like s, hue, and the like could be written in this kind of rule form, but we have short hand notions for them in the CFDG-language.

A contextual grammar would parse also rules of the form
[C]A[D] -> [C]B[D]
(convert A into B only if it is preceded by C and followed by D).


Anyway, if we would like to do genetic algorithms, something more complex is needed :)

User avatar
Nom
Posts: 30
Joined: Tue Jan 30, 2007 6:45 am

Post by Nom »

Having read the earlier trails, I find I did some stupid generalizations as usual.

It seems that at least attribute handling is NOT totally context free. But it will not contribute to genetic algorithms, so we can agree on at least that they are quite impossible in CF :]

Cheers!

User avatar
Guigui
Posts: 50
Joined: Sat Aug 05, 2006 5:28 pm
Location: Annecy, France

Post by Guigui »

Aaaah, playing with genes. I hope José Bové will never catch us (J. B. is said to be known wordlwide).

As much I understand Andrewborrell, it's a game proposal. I think 10 mutations is to much. 2 is enough for me. I also believe we need only light mutations. But no limits anyway. And of course we'll see later for sexual reproduction.

First, here comes my Eve (too bad for Adam):

Code: Select all

startshape M
rule M {CIR{}M{r 3 s .99 x 1}}
rule CIR {CIR{}M{s 1 -1}}
rule CIR 60{CIRCLE{}}
and my two firsts mutations:
ABEL son of EVE

Code: Select all

startshape M
rule M {CIR{}M{r 3 s .99 x 1 b .001}}
rule CIR {CIR{}M{s 1 -1}}
rule CIR {CIR{s 12 z -1 b -.05}}
rule CIR 90{CIRCLE{}}
CAÏN son of EVE

Code: Select all

startshape M
rule M {CIR{}M{r 3 s .99 x 1}}
rule M .01{N{}}
rule N {CIR{}N{s .99 x 1}}
rule N .01{M{}}
rule CIR {CIR{}M{s 1 -1}}
rule CIR 60{CIRCLE{}}
Image
So, we are the biosphere. I hope one of my CFA won't be stillbirth and have a productive line of descent :roll: - Hey Andrew,Jeremy & Nom , won't you play ? :P
8^)

jeremydouglass
Posts: 17
Joined: Fri Mar 30, 2007 9:10 pm

I can play...

Post by jeremydouglass »

...although I think the bisexual reproduction case is more interesting, I still think random mutation is a fun game, and easier to play.

Here is my move (I don't know how to embed a gallery image in a forum post): http://www.contextfreeart.org/gallery/view.php?id=594

Note: giving Abel children might be a bit confusing - traditionally I think he didn't have them.

User avatar
Guigui
Posts: 50
Joined: Sat Aug 05, 2006 5:28 pm
Location: Annecy, France

Post by Guigui »

So, Jeremydouglass made SETH, son of Eve:

Code: Select all

startshape M

rule M {CIR{} M{r 3 s .99 x 1 b .005 sat .01 hue .005}}
rule CIR {CIR{} M{s 1 -1}} 
rule CIR {CIR{s 12 z -1 b -.05}} 
rule CIR 90{CIRCLE{ s 1 .2}}
coooool...

I propose that we can continue the mutations like this:

- Each mutation is an independant CFA uploaded in the Gallery
- The code of the mutation should be close of the father's code (!?!)
- Its name must contain 'son of' and the name of the father
- It has to be sent with a [link design:***]The father CFA[/link] in the attached author notes
- And It would be nice to notifie its birth in the comment area of the father.

I redo Eve, Caïn & Abel & Seth to clarify… and let's R'n R
8^)

User avatar
Guigui
Posts: 50
Joined: Sat Aug 05, 2006 5:28 pm
Location: Annecy, France

Post by Guigui »

I want to remind the Andrewborrell's definition of a CFA mutation: it consist of adding a rule, or adding an instantiation of a rule to a rule, or changing the parameters used to call another rule somewhere.

And I would say that a child can have several mutations of his father's code. 10% of change should be reasonable. But nature is sometime temperamental :lol:. And … there's no obligation to be biblical :wink:.
8^)

andrewborrell
Posts: 8
Joined: Wed Apr 04, 2007 1:50 pm

Post by andrewborrell »

While this manual mutation game is fun, I'm sure the mutations we're seeing are far from random. We could do with a simple command line tool that takes a cfdg file and mutates it slightly, (we could still only post the best ones, I'm sure the vast majority of randomly mutated designs will be uninteresting if not completely crippled).

jeremydouglass
Posts: 17
Joined: Fri Mar 30, 2007 9:10 pm

CFDG Evolution: like Sodarace?

Post by jeremydouglass »

This conversation is reminding me in some ways of sites like Sodarace (http://sodarace.net/index.jsp), and the fact that most projects that create algorithms to "evolve" a given pattern do it in relation to some selective evolutionary pressure - e.g. victory goes to the fastest (sodarace) or the hungriest (Life vs. Life) etc. etc. Then you add a LOT of random iterations and start getting interesting results.

The question is - other than aesthetic whim, what pressures might be unique or interesting in a CFDG file, and how if at all could they be measured?

In other words, how could the mutation game be turned into an evolution game? Manually voting? The one with the widest histogram wins? ....

andrewborrell
Posts: 8
Joined: Wed Apr 04, 2007 1:50 pm

Post by andrewborrell »

Manually voting was what I originally had in mind. Something a bit like:http://w-shadow.com/li/index.php. An automated process would be better, maybe you could choose CFDG images which look most similar to some other image, and associate a cost with the complexity of the file, to stop them simply specifying the image pixel by pixel with lots of little squares. It be interesting to apply this to images of trees and other organic images. (It might even yield a super effiecent image compressing method that works only on pictures of trees! :lol: )

I also heard about a project where a system randomly generated images which were displayed on various screens in a room full of bees! The screens were touch sensitive and the screens which registered the highest number of bee touches had their images used as parents of the next generation of images. Theoretically genetic CFDG would work in a system like this, (unfortunately I dont have a room full of bees and touch sensitive screens at my disposal!).

User avatar
Guigui
Posts: 50
Joined: Sat Aug 05, 2006 5:28 pm
Location: Annecy, France

Post by Guigui »

The aesthetic whim can be the measure as contextfreeart is at least about art :roll:.

I'm mostly interesting of what king of images we'll get after a lot of mutations (if we get a lot of mutations). And how differents will be the far cousins.

I also like the idea of a kind of colaborative art experiment (and for that matter, I think I'll try to stop creating mutations for a while and hope some will fall for the game). For instance, I find it fun than Andrewborrell made Crystal mosty by removing shapes from Sugabe.
8^)

User avatar
Guigui
Posts: 50
Joined: Sat Aug 05, 2006 5:28 pm
Location: Annecy, France

Post by Guigui »

Ok, I tried to launch something that doesn't work. After some generations, it's hard to make small changes at the CFDG that can be interesting. Never mind. So I delet my sons & daughters of the Eve CFDG.
8^)

andrewborrell
Posts: 8
Joined: Wed Apr 04, 2007 1:50 pm

Post by andrewborrell »

I know it's been a long time but I hadn't forgotten about this genetic CFDG idea. I've been working on a tool to perform mutations of CFDG files, it will mutate the file exactly in the ways I described a few posts ago. I've also written a python script to automate the process of mutating files then rendering them and then reviewing the results, with a pause after each generation for the user to choose their favourite of the offspring.

The project is still in a fairly early stage, but it has already produced some interesting images, take a look at http://www.wickedbean.co.uk/cfdg/index.html to see some of the pictures created using this tool. The source code is also downloadable from the page.

There's still loads more I would like to make the tool do, like sexual reproduction between two CFDG files rather than just asexual mutations. The page linked to above has more info about what the tool does and the improvements I'd like to make (or for somebody else to make).

Post Reply