Updates and Bug Fixes

 

Bug  in Chapter 4 Code (TSP)

 

The value m_dTotalFitness never gets updated, which is why the evolution gets stuck so easily in a local minima. In CgaTSP::CalculatePopulationsFitness

 

//Now we have calculated all the tour lengths we can assign
//the fitness scores
for (i=0; i<m_iPopSize; ++i)
{
  m_vecPopulation[i].dFitness =
  m_dLongestRoute - m_vecPopulation[i].dFitness;
}

 

should be:

 

//Now we have calculated all the tour lengths we can assign
//the fitness scores
m_dTotalFitness = 0;

for (i=0; i<m_iPopSize; ++i)
{
  m_vecPopulation[i].dFitness =
  m_dLongestRoute - m_vecPopulation[i].dFitness;

  m_dTotalFitness += m_vecPopulation[i].dFitness;
}

 

 

 

Bugs in NEAT code

 

1. There's a bug in the  CGenome Cga::Crossover method. Here's the following fix. The location of the change is in green.

 

//if dads innovation number is less than mums

else if (curDad->InnovationID < curMum->InnovationID)

{

  //if dad is fittest add gene

  if (best == DAD)  //<- '=' replaced with '=='

  {

    SelectedGene = *curDad;

  }

  //move onto dad's next gene

  ++curDad;

}

 

2. In CSpecies::Spawn the following code

 

  else
  {
      int MaxIndexSize = (int) (CPARAMS::dSurvivalRate * m_vecMembers.size())+1;

      int TheOne = RandInt(0, MaxIndexSize);

      baby = *m_vecMembers[TheOne];
  }

 

should be replaced with

 

  else
  {
      int MaxIndexSize = (int) (CPARAMS::dSurvivalRate * m_vecMembers.size())-1;

      if (MaxIndexSize < 0) MaxIndexSize = 0;

            int TheOne = RandInt(0, MaxIndexSize);

      baby = *m_vecMembers[TheOne];
  }

 

 

 

Code Update

 

 I have created a fast Update() method for the basic CNeuralNet class.  You can download the source here. There are two areas where the code is speeded up:

a) The Sigmoid function has been replaced by a pre-calculated lookup table.(25% speed saving approx). The default is for 5000 samples. You can increase this to gain more accuracy. (It would also be worth your while seeing how low this figure can get before performance deteriorates).

b) Memory management has been improved to stop the std::vectors used in the update function from continuously allocating/deallocating memory. (20%  speed saving approx)

 

 

 

 

 

 

 

Please contact me if you have found a bug!

 

bugs_at_aijunkie.com

 

 

 


 

Home