views:

73

answers:

3

Hello! I'm developing a Genetic Algorithm in python were chromosomes are composed of strings and integers. To apply the genetic operations, I want to convert these groups of integers and strings into bit strings.

For example, if one chromosome is:

["Hello", 4, "anotherString"]

I'd like it to become something like:

0100100100101001010011110011

(this is not actual translation). So... How can I do this? Chromosomes will contain the same amount of strings and integers, but this numbers can vary from one algorithm run to another.

To be clear, what I want to obtain is the bit representation of each element in the chromosome concatenated.

If you think this would not be the best way to apply genetic operators (such as mutation and simple crossover) just tell me! I'm open to new ideas.

Thanks a lot! Manuel

A: 

Once you describe exactly how the translation from strings to bitstrings should go, the "how" should be fairly easy. If the genetic algorithms should work on a bit-level then obviously a bit level string makes sense, but it is probably way slower than using numbers or character strings.

Lennart Regebro
+2  A: 

You can turn strings and integers into bytestrings (and back) with the struct module, and that's exactly 8 bits to a byte. If for some reason you want these binary bytestrings as text strings made up of 0 and 1 characters, you can print them in binary form, of course.

Edit: forgot to remind you how to format a byte into a text string made up of 0 and 1 characters -- in Python 2.6 or better:

>>> format(23, '08b')
'00010111'

and to get back from such a string to a byte, of course:

>>> int('00010111', 2)
23
Alex Martelli
Thanks! This was what I was looking for! I mean, the first paragraph. I think I must have explained myself very bad. Feel free to edit the question to suit this answer. :D (sorry, my English is not very good)
Manuel
+2  A: 

Hey.
Converting everything into one concatenated string, and than applying genetic operations doesn't seem to be the best idea. Genetic operations can break here many things (especially if you have some constrains on individuals), additionally effectiveness of such solution is probably low. I would suggest different approach.

Try implementing individual using SuperGene concept (wiki). Example of applying it to GA is described here. Additionally as per this they say it improves overall GA performance.
In my opinion it will make design clearer. I would try this approach.

yoosiba
+1 for suggesting something other than decomposing to bits and flipping those. That method tends to destroy most of the original information and will seriously slow the rate of evolution.
Kylotan