/// <summary>
        ///     Finishes Generation, by ordering Genomes by Fitness descending and
        ///     setting the BestGenome Property.
        /// </summary>
        public void FinishGeneration()
        {
            Genomes = Genomes
                      .OrderByDescending(c => c.Fitness.Value)
                      .ToList();

            // TODO: Write AnalyseData

            BestGenome = Genomes.First();
        }
        /// <summary>
        ///     Ends the generation.
        /// </summary>
        /// <param name="genomesNumber">Genomes number to keep on generation.</param>
        public void End(int genomesNumber)
        {
            Genomes = Genomes
                      .Where(ValidateChromosome)
                      .OrderByDescending(c => c.Fitness.Value)
                      .ToList();

            if (Genomes.Count > genomesNumber)
            {
                Genomes = Genomes.Take(genomesNumber).ToList();
            }

            BestGenome = Genomes.First();
        }
Exemplo n.º 3
0
        private void CreateStartPopulation(int genomes, Action <TGenome> genomeCreationMethod)
        {
            //clear existing population
            Genomes.Clear();

            for (int i = 0; i < genomes; i++)
            {
                var genome = new TGenome();
                genomeCreationMethod(genome);
                Genomes.Add(genome);
            }

            //reset all variables
            Generation        = 0;
            TotalFitnessScore = 0;

            GeneLength       = -1;
            ChromosoneLength = Genomes.First().Genes.Sum(g => g.Length);
        }
Exemplo n.º 4
0
        protected virtual void CalculateRemarkableValues()
        {
            // Because Genomes are sorted :
            //   First = worst
            //   Last = best

            WorstFitness = Math.Min(Genomes.First().Fitness, WorstFitness);
            TotalFitness = Genomes.Sum(g => g.Fitness);

            double bestGenomeFitness = Genomes.Last().Fitness;

            if (bestGenomeFitness > BestFitness)
            {
                BestFitnessGenome = Genomes.Last();
                BestFitness       = bestGenomeFitness;

                if (bestGenomeFitness > BestFitnessEver)
                {
                    BestFitnessGenomeEver = BestFitnessGenome;
                    BestFitnessEver       = bestGenomeFitness;
                }
            }
        }