Genetic Algorithms & Optimal Solutions 
by Michael Larson


Listing One

void DistrictOrganism::mateWith(GENOrganismBase<PopPoint> &otherOrg, float fWhere)
{
   int iCt = 0, iLoc;
   GENOrganismBase<PopPoint>::GeneIter iter, iterMatch;
   //get number of unique cells between two genes
   iter = m_geneColl.begin();
   while (iter != m_geneColl.end())
   {
      if (otherOrg.has(*iter) == false)
         ++iCt;
      ++iter;
   }
   //scale cell to this number
   iLoc = int(fWhere * float(iCt));
   iCt = 0;
   //splice at this location ensuring uniqueness
   iter = m_geneColl.begin();
   while (iter != m_geneColl.end() && iCt < iLoc)
   {
      if (otherOrg.has(*iter) == false)
      {
         *iter = otherOrg.get(iCt);
      }
      ++iCt;
      ++iter;
   }
}
void DistrictOrganism::mutate(float fRate)
{
   int iCell;
   PopPoint point;
   GENOrganismBase<PopPoint>::GeneIter iter;
   iter = m_geneColl.begin();
   while (iter != m_geneColl.end())
   {
      if (FLIP(fRate))
      {
        *iter = m_pDistrictBucket->randomPick(m_geneColl); //passes in list of
                                                           // exclusion points
      }
      ++iter;
   }
}


Listing Two

void DistrictPopulation::evolve()
{
   OrganismIter iterOne, iterTwo;
   DistrictOrganism geneOne, geneTwo;
   if (m_popPool->size() < 2)
      return;
   iterOne = m_popPool->begin();
   iterTwo = ++(m_popPool->begin());
   while (iterOne != m_popPool->end() && iterTwo != m_popPool->end())
   {
      orgOne = *iterOne;
      orgTwo = *iterTwo;
      if (FLIP(m_fRepro))
      {
         orgOne = doReproduction(orgOne);
         orgTwo = doReproduction(orgTwo);
      }
      doCrossover(orgOne, orgTwo);
      doMutation(geneOne);
      doMutation(geneTwo);
      addToNewGeneration(geneOne);
      addToNewGeneration(geneTwo);
      ++iterOne;++iterOne;
      ++iterTwo;++iterTwo;
   }
   evolutionComplete();
}






1


