What are the data types for genetic algorithms in Python?

I am implementing GA in Python and must store a sequence of ones and zeros, so I represent my data as binary files. What's the best data structure for this? Simple string?

+3


source to share


3 answers


If your chromosomes are fixed bits of a string, consider using Numpy arrays and vectorized operations on them instead of lists. They can be much faster than Python lists. For example, a single point crossover can be done with

def crossover(a, b):
    """Return new individual by combining parents a and b
       with random crossover point"""
    c = np.empty(a.shape, dtype=bool)
    k = np.random.randint(a.shape[0])
    c[:k] = a[:k]
    c[k:] = b[k:]
    return c

      



If you don't want to use Numpy, strings seem to be fine; they are much more compact than lists, which store pointers to elements rather than actual elements.

Finally, be sure to look at how Pyevolve represents chromosomes; this is similar to using Numpy.

+5


source


I think sticking strings is a good idea. You can cut the strings into pieces easily. if you need to act like a list, you can convert it with "list (str)". Once you have the list, you can modify it and put it back into a string using ".join (lst)".

Personally, I won't use a long or other integer type for bit storage. It might be more space, but the headache when working with data when you want to recombine will be significant. Mutations would also be problematic if the mutation consisted of something other than a little flip. Also, the code will be much harder to read.



Just my 2 cents. Hope it helps you.

0


source


You can try using bitarray . Or you can play with buffers .

0


source







All Articles