Best Data Structure for Genetic Algorithm in C++?

 

Questions


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!

 

 

————————————————-

Answer

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 a vector<char> for the genes. The reason for vector<char> is that bitset<> and vector<bool> are optimized for memory consumption, and are therefore slow. vector<char> will give you higher speed at the cost of x8 memory (assuming char = byte on your system). So if you want speed, go with vector<char>. If memory consumption is paramount, then use vector<bool> or bitset<>. 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 per bitset size of each of the bitset methods you use (though inlining might negate this), i.e., code bloat. Overall, I would guess vector<bool> is better for you if you don’t want vector<char>.

If you’re concerned about the aesthetics of vector<char> you could typedef char gene; and then use vector<gene>, which looks more natural.

A string is just like a vector<char> but more cumbersome.

artificial-intelligence,c#,data-structures,genetic-algorithm

Facebook Comments

Post a comment