public void SpeciateAndCalculateSpawnLevels()
{
// calculate compatibility between genomes and species
AdjustCompatibilityThreshold();
// assign genomes to species (if any exist)
foreach (IGenome g in Population.Genomes)
{
var genome = (NEATGenome) g;
bool added = false;
foreach (ISpecies s in Population.Species)
{
double compatibility = genome.GetCompatibilityScore((NEATGenome) s.Leader);
if (compatibility <= paramCompatibilityThreshold)
{
AddSpeciesMember(s, genome);
genome.SpeciesID = s.SpeciesID;
added = true;
break;
}
}
// if this genome did not fall into any existing species, create a
// new species
if (!added)
{
Population.Species.Add(
new BasicSpecies(Population, genome,
Population.AssignSpeciesID()));
}
}
AdjustSpeciesScore();
foreach (IGenome g in Population.Genomes)
{
var genome = (NEATGenome) g;
totalFitAdjustment += genome.AdjustedScore;
}
averageFitAdjustment = totalFitAdjustment
/Population.Size();
foreach (IGenome g in Population.Genomes)
{
var genome = (NEATGenome) g;
double toSpawn = genome.AdjustedScore
/averageFitAdjustment;
genome.AmountToSpawn = toSpawn;
}
foreach (ISpecies species in Population.Species)
{
species.CalculateSpawnAmount();
}
}