Esempio n. 1
0
 private static void PrintStatus(int generation, Chromosome[] population)
 {
     int minFitness = population.Min(ch => ch.Fitness);
     int avgFitness = (int)population.Average(ch => ch.Fitness);
     int maxFitness = population.Max(ch => ch.Fitness);
     Console.WriteLine($"Generation {generation.PadLeft(3)}: Fitness; {minFitness.PadLeft(6)}; {avgFitness.PadLeft(6)}; {maxFitness.PadLeft(6)}");
 }
Esempio n. 2
0
        private Chromosome[] CreateInitialPopulation()
        {
            var population = new Chromosome[args.PopulationSize];

            for (int i = 0; i < args.PopulationSize; i++)
            {
                var gens = new BitArray(chromosomeLength);
                for (int j = 0; j < chromosomeLength; j++)
                {
                    if (random.NextDouble() > .5)
                        gens.Set(j, true);
                }
                population[i] = new Chromosome(gens);
                EvaluateFitness(population[i]);
            }

            return population;
        }
Esempio n. 3
0
        private Chromosome Cross(Chromosome chromosome, Chromosome other)
        {
            int randomCutIndex = random.Next(1, chromosomeLength);
            var newGens = new BitArray(chromosome.Gens);
            for (int i = randomCutIndex; i < chromosomeLength; i++)
            {
                newGens.Set(i, other.Gens[i]);
            }
            var offspring = new Chromosome(newGens);

            if (random.NextDouble() < args.MutationProbability)
                MutateRandomChromosome(offspring);

            EvaluateFitness(offspring);
            return offspring;
        }
Esempio n. 4
0
 private void MutateRandomChromosome(Chromosome chromosome)
 {
     int randomGeneIndex = random.Next(0, chromosomeLength);
     chromosome.Gens[randomGeneIndex] = !chromosome.Gens[randomGeneIndex];
 }
Esempio n. 5
0
 private int FindWeakest(Chromosome[] population)
 {
     int minFitness = population[0].Fitness;
     int weakestIndex = 0;
     for (int j = 1; j < args.PopulationSize; j++)
     {
         if (population[j].Fitness < minFitness)
         {
             minFitness = population[j].Fitness;
             weakestIndex = j;
         }
     }
     return weakestIndex;
 }
Esempio n. 6
0
        private List<Chromosome> SelectParents(Chromosome[] population, int parentsCount)
        {
            int populationFitness = population.Sum(ch => ch.Fitness);
            breedingPool.Clear();

            for (int i = 0; i < parentsCount; i++)
            {
                if (args.ParentSelection == ParentSelection.Tournament)
                {
                    Chromosome fittest = null;
                    for (int j = 0; j < args.TournamentSize; j++)
                    {
                        int randomIndex = random.Next(0, args.PopulationSize);
                        var next = population[randomIndex];
                        if (fittest == null || fittest.Fitness < next.Fitness)
                            fittest = next;
                    }
                    breedingPool.Add(fittest);
                }
                else
                {
                    int randomPoint = random.Next(0, populationFitness + 1);
                    int runningFitness = 0;
                    for (int j = 0; j < args.PopulationSize; j++)
                    {
                        runningFitness += population[j].Fitness;
                        if (runningFitness >= randomPoint)
                        {
                            breedingPool.Add(population[j]);
                            break;
                        }
                    }
                }
            }

            return breedingPool;
        }
Esempio n. 7
0
 private Chromosome[] CreateNewGeneration(Chromosome[] currentPopulation, out int elitesCount)
 {
     var newGeneration = new Chromosome[args.PopulationSize];
     elitesCount = 0;
     if (args.ElitesCount > 0)
     {
         var elites = currentPopulation.Where(ch => ch.Fitness > 0)
                                       .OrderByDescending(ch => ch.Fitness)
                                       .Take(args.ElitesCount).ToArray();
         elitesCount = elites.Length;
         Array.Copy(elites, newGeneration, elites.Length);
     }
     return newGeneration;
 }
Esempio n. 8
0
 private bool GetVariableValue(int formulaIndex, Chromosome chromosome)
 {
     return currentFormula.Formula[formulaIndex].ToVariableValue(chromosome.Gens);
 }
Esempio n. 9
0
        private void EvaluateFitness(Chromosome chromosome)
        {
            chromosome.Fitness = 0;
            for (int i = 0; i < currentFormula.Formula.Length; i += 3)
            {
                var firstVar = GetVariableValue(i, chromosome);
                var secondVar = GetVariableValue(i + 1, chromosome);
                var thirdVar = GetVariableValue(i + 2, chromosome);
                if (!(firstVar || secondVar || thirdVar))
                    return;
            }

            for (int i = 0; i < chromosomeLength; i++)
            {
                if (chromosome.Gens[i])
                    chromosome.Fitness += currentFormula.Weights[i];
            }
        }