title: How to write efficient Genetic Algorithms in C++
body: <p>I am trying to write a C++ program for the <a href="http://en.wikipedia.org/wiki/Genetic_programming" rel="nofollow noreferrer">canonical genetic algorithm</a>, where you have a population of individuals (chromosomes) of length N, where each element is a O or 1.</p>
  
  <p>I have started writing my program using <strong>STL vectors</strong>, but before I go more deeply into it I would like to ask your opinions about how to write the functions and the data structures in the most efficient way.</p>
  
  <p>Memory footprint is not a problem, I have a population about 100 individuals where each of them are a 64 character long string of 0-s and 1-s. The performance on the other hand is very important, as there would be about <strong>thousands of generations, each having thousands of operations.</strong> </p>
  
  <p>Here is my implementation so far (just the most important funcitions and the data structure):</p>
  
  <pre><code>typedef vector&lt;int&gt; chromosome;
  typedef vector&lt;chromosome&gt; population;
  
  population popul;
  float eval[number];
  
  void cross_chromosomes( const chromosome &amp;parent_a, const chromosome &amp;parent_b, chromosome &amp;child_a, chromosome &amp;child_b )
  {
      int crossing_point = crossing_point_gen( gen );
  
      child_a.reserve( length );
      child_a.insert( child_a.end(), parent_a.cbegin(), parent_a.cbegin() + crossing_point );
      child_a.insert( child_a.end(), parent_b.cbegin() + crossing_point, parent_b.cend() );
  
      child_b.reserve( length );
      child_b.insert( child_b.end(), parent_b.cbegin(), parent_b.cbegin() + crossing_point );
      child_b.insert( child_b.end(), parent_a.cbegin() + crossing_point, parent_a.cend() );
  }
  
  void calculate_eval()
  {
      for( int i = 0; i &lt; number; i++ )
      {
          eval[i] = evaluate_chromosome( popul[i] );
      }
  }
  </code></pre>
  
  <p><strong>Do you think it is an efficient way of implementing this algorithm?</strong> I originally used vector for the chromosome, but I have read this question: <a href="https://stackoverflow.com/questions/1932222/c-vector-vs-array-time">C++ Vector vs Array (Time)</a> and I updated my code to <code>vector&lt;int&gt;</code>. </p>
  
  <p>Do you think there are other optimisations I should do with my code to make it more efficient? Is the crossing code efficient as it is now?</p>