i need to implement a genetic algorithm customized for my problem (college project), and the first version had it coded as an matrix of short ( bits per chromosome x size of population).
That was a bad design, since i am declaring a short but only using the “0” and “1” values… but it was just a prototype and it worked as intended, and now it is time for me to develop a new, improved version. Performance is important here, but simplicity is also appreciated.
I researched around and came up with:
for the chromosome :
– String class (like “0100100010”)
– Array of bool
– Vector (vectors appears to be optimized for bool)
– Bitset (sounds the most natural one)
and for the population:
– C Array[]
– Vector
– Queue
I am inclined to pick vector for chromossome and array for pop, but i would like the opinion of anyone with experience on the subject.
Thanks in advance!
I’m guessing you want random access to the population and to the genes. You say performance is important, which I interpret as execution speed. So you’re probably best off using a
vector<>for the chromosomes and avector<char>for the genes. The reason forvector<char>is thatbitset<>andvector<bool>are optimized for memory consumption, and are therefore slow.vector<char>will give you higher speed at the cost of x8 memory (assumingchar= byte on your system). So if you want speed, go withvector<char>. If memory consumption is paramount, then usevector<bool>orbitset<>.bitset<>would seem like a natural choice here, however, bear in mind that it is templated on the number of bits, which means that a) the number of genes must be fixed and known at compile time (which I would guess is a big no-no), and b) if you use different sizes, you end up with one copy perbitsetsize of each of thebitsetmethods you use (though inlining might negate this), i.e., code bloat. Overall, I would guessvector<bool>is better for you if you don’t wantvector<char>.If you’re concerned about the aesthetics of
vector<char>you couldtypedef char gene;and then usevector<gene>, which looks more natural.A
stringis just like avector<char>but more cumbersome.