void Replace(Chromosome[] children) { Population nextPopulation = new Population( this, population.PopulationSize ); for(int i=0;i<populationSize;i++) { // // Since the recombination record for a chromosome may contain a // pointer to chromosome's in the previous population, it has to be // cleared out. This prevents us from getting an unbroken reference // chain between the current population's chromosomes all the way back // to the first population (which would prevent the garbage collector // from reclaiming any chromosome memory). // (There is probably some way we can configure how deep we want // to remember recombination records. But that might be overkill // if we get everything serializing nicely.) // population.Chromosomes[i].Parents = null; nextPopulation.Chromosomes[i] = children[i]; nextPopulation.Chromosomes[i].ID = nextChromosomeID++; } NewPopulationEventArgs e = new NewPopulationEventArgs(); e.OldPopulation = population; e.NewPopulation = nextPopulation; e.Generation = generation; OnNewPopulation( e ); population = nextPopulation; // BUGBUG: should move this before the call out }
/* * mode - most frequently occuring score. * median - midpoint of distribution, above which have of the scores fall, and below * which the other half fall. */ /// <summary> /// Creates a PopulationSummary from a Population. /// </summary> /// <param name="ga">The GA.</param> /// <param name="Population">The Population for which the /// PopulationSummary will provide summary information. /// </param> public PopulationSummary( GA ga, Population Population ) { double totalObjective; lowestObjective = System.Double.MaxValue; highestObjective = System.Double.MinValue; totalObjective = 0; foreach( Chromosome c in Population.Chromosomes ) { totalObjective += c.RawObjective; if ( c.RawObjective < LowestObjective ) { lowestObjective = c.RawObjective; if ( ga.ObjectiveType == ObjectiveType.MinimizeObjective ) { bestChromosome = c; } } if ( c.RawObjective > HighestObjective ) { highestObjective = c.RawObjective; if ( ga.ObjectiveType == ObjectiveType.MaximizeObjective ) { bestChromosome = c; } } } meanObjective = totalObjective / Population.Chromosomes.Length; // // Variance is the average squared deviation from the mean. // Standard Deviation is just the square root of this. // double sum = 0; foreach(Chromosome c in Population.Chromosomes) { double deviation; double squaredDeviation; deviation = c.RawObjective - meanObjective; squaredDeviation = deviation * deviation; sum += squaredDeviation; } variance = sum / (Population.Chromosomes.Length-1); standardDeviation = Math.Sqrt( variance ); }
void Initialize() { Population = new Population( this, PopulationSize ); for(int i=0;i<PopulationSize;i++) { Population.Chromosomes[i] = new Chromosome(); Population.Chromosomes[i].Genes = new Gene[ChromosomeLength]; Population.Chromosomes[i].ID = nextChromosomeID++; //GeneDescriptor geneDescriptor = geneDescriptors[0]; bool[] usedLabels = new bool[ChromosomeLength]; for(int j=0;j<ChromosomeLength;j++) { // if ( !Homogeneous ) // { // geneDescriptor = geneDescriptors[j]; // } Population.Chromosomes[i].Genes[j] = geneDescriptor.GetRandomAllele(); // int label; do { label = Utils.Rand.Next(ChromosomeLength); } while ( usedLabels[label] ); usedLabels[label] = true; Population.Chromosomes[i].Genes[j].Label = label; } } }