コード例 #1
0
        public List <Hypothesis> CrossoverPopulation(List <Hypothesis> population, double crossoverRate, int childPerPair)
        {
            var newPopulation  = new List <Hypothesis>();
            var popToCrossover = Math.Max(1, population.Count * crossoverRate);
            var totalFitness   = population.Sum(h => h.Fitness);
            var minimumFitness = population.Select(h => h.Fitness).Concat(new[] { 0 }).Min();

            if (minimumFitness < 0)
            {
                totalFitness = population.Sum(h => h.Fitness - minimumFitness);
            }
            for (var j = 0; j < popToCrossover; j++)
            {
                var candidateA = FindCrossOverCandidate(population, totalFitness, -minimumFitness);
                var candidateB = FindCrossOverCandidate(population, totalFitness, -minimumFitness);
                if (candidateA != null && candidateB != null)
                {
                    for (var i = 0; i < childPerPair; i++)
                    {
                        newPopulation.Add(CrossoverMethod.Crossover(candidateA, candidateB));
                    }
                }
            }
            return(newPopulation);
        }
コード例 #2
0
        public List <Hypothesis> CrossoverPopulation(List <Hypothesis> population, double crossoverRate, int childPerPair)
        {
            var newPopulation  = new List <Hypothesis>();
            var popToCrossover = Math.Max(1, population.Count * crossoverRate);

            for (var j = 0; j < popToCrossover; j++)
            {
                var candidateA = FindCrossOverCandidate(population);
                var candidateB = FindCrossOverCandidate(population);
                if (candidateA == null || candidateB == null)
                {
                    continue;
                }
                for (var i = 0; i < childPerPair; i++)
                {
                    newPopulation.Add(CrossoverMethod.Crossover(candidateA, candidateB));
                }
            }
            return(newPopulation);
        }