Esempio n. 1
0
        private static IEnumerable <double> ComputeSetValues(IEnumerable <TChromosome> chromosomes, FitnessFunction <TChromosome, TGene> fitness)
        {
            var setValues = chromosomes.Select(fitness.ValueFor);

            if (setValues.Any(v => v <= 0))
            {
                return(TranslateSetValues(setValues));
            }

            return(setValues);
        }
Esempio n. 2
0
        public override Population <TChromosome, TGene> Select(Population <TChromosome, TGene> population, FitnessFunction <TChromosome, TGene> fitness)
        {
            var selectedPopulation = new List <TChromosome>();

            var setValues   = ComputeSetValues(population.Chromosomes, fitness);
            var wheelValues = ComputeWheelValues(setValues).ToList();

            for (var i = 0; i < population.Size; i++)
            {
                var random        = new Random().NextDouble();
                var selectedIndex = wheelValues.FindIndex(wv => wv > random);
                if (selectedIndex == wheelValues.Count - 1)
                {
                    selectedIndex--;
                }

                var selectedSet = population.Chromosomes.ElementAt(selectedIndex);
                selectedPopulation.Add(selectedSet);
            }

            return(Population <TChromosome, TGene> .Create(selectedPopulation));
        }
Esempio n. 3
0
 public Population <TChromosome, TGene> Select(PopulationSelectionStrategy <TChromosome, TGene> selectionStrategy, FitnessFunction <TChromosome, TGene> fitness)
 {
     return(selectionStrategy.Select(this, fitness));
 }