Пример #1
0
        /// <summary>
        /// Ends the generation.
        /// </summary>
        /// <param name="chromosomesNumber">Chromosomes number to keep on generation.</param>
        public void End(int chromosomesNumber)
        {
            Chromosomes = Chromosomes
                          .Where(ValidateChromosome)
                          .OrderByDescending(c => c.Fitness.Value)
                          .ToList();

            if (Chromosomes.Count > chromosomesNumber)
            {
                Chromosomes = Chromosomes.Take(chromosomesNumber).ToList();
            }

            BestChromosome = Chromosomes.First();
        }
Пример #2
0
        public void NextGeneration(ISelection selectionScheme)
        {
            var selected = selectionScheme.Select(Chromosomes, 5).OrderByDescending(s => s.Cost).ToList();

            Chromosomes.Remove(selected[0]);
            Chromosomes.Remove(selected[1]);

            // fill the remaining empty space with children generated from selected chromosomes
            Chromosome parent1 = selected[3];
            Chromosome parent2 = selected[4];

            Chromosome child1 = OrderedCrossover.MakeChild(parent1, parent2);
            Chromosome child2 = OrderedCrossover.MakeChild(parent2, parent1);

            if (ThreadSafeRandom.CurrentThreadRandom.Next(100) < 3)
            {
                child1.Mutate();
            }
            if (ThreadSafeRandom.CurrentThreadRandom.Next(100) < 3)
            {
                child2.Mutate();
            }

            Chromosomes.Add(child1);
            Chromosomes.Add(child2);

            double currentTour = Chromosomes.Where(c => c.Cost > 0).Min(c => c.Cost);

            if (currentTour < BestTour)
            {
                BestTour = currentTour;
                Console.WriteLine("Generation " + currentGeneration + " Best: " + BestTour);
            }

            currentGeneration++;
        }