Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which data types for genetic algorithms in Python?

I am implementing a GA in Python and need to store a sequence of ones and zeros, so I am representing my data as binaries. What is the best data structure for that? A simple string?

like image 406
Ingo Avatar asked Sep 21 '26 18:09

Ingo


1 Answers

If your chromosomes are fixed-length bitstrings, consider using Numpy arrays and vectorized operations on them instead of lists. These may be much faster than Python lists. E.g., one-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, then strings seem quite appropriate; they're much more compact than lists, which store pointers to elements rather than actual elements.

Finally, be sure to have a look at how Pyevolve represents chromosomes; it seems to do so with using Numpy.

like image 163
Fred Foo Avatar answered Sep 24 '26 08:09

Fred Foo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!