示例#1
0
        private void Elitism()
        {
            var new_generation = new List <Chromosome>();

            var elitism_start = (int)(PopulationSize * ElitismRatio);

            for (int i = 0; i < elitism_start; i++)
            {
                new_generation.Add(Population.Chromosomes[i]);
            }

            for (int i = elitism_start; i < PopulationSize; i++)
            {
                int i1 = rnd.Next() % (Population.Chromosomes.Count / 2);
                int i2 = rnd.Next() % (Population.Chromosomes.Count / 2);

                var ch1_data = Population.Chromosomes[i1].Data;
                var ch2_data = Population.Chromosomes[i2].Data;

                List <object> cross_chromosome_data = null;

                int axis = rnd.Next(1, Population.Chromosomes.First().Data.Count);

                switch (CrossoverType)
                {
                case CrossoverType.OnePointCrossover: cross_chromosome_data = Crossover.OnePointCrossover(ch1_data, ch2_data, axis).Item1; break;

                case CrossoverType.UniformCrossover: cross_chromosome_data = Crossover.UniformCrossover(ch1_data, ch2_data).Item1; break;

                case CrossoverType.PMX: cross_chromosome_data = Crossover.PMX(ch1_data, ch2_data); break;
                }

                var cross_chromosome = new Chromosome(cross_chromosome_data);

                new_generation.Add(cross_chromosome);
            }

            Population.Chromosomes = new_generation;
        }