예제 #1
0
        private void FitGeneration()
        {
            var fitnessList = new List <int>();

            foreach (var robot in Population)
            {
                robot.CalculateFitness();
                fitnessList.Add(robot.Fitness);
                try
                {
                    if (robot.Fitness > BestRun.Fitness)
                    {
                        BestRun = robot;
                    }
                    if (robot.Fitness < WorstRun.Fitness)
                    {
                        WorstRun = robot;
                    }
                }
                catch (Exception e)
                {
                    BestRun  = robot;
                    WorstRun = robot;
                }
            }
            GeneticOperations.SetGenerationReproductionProbabilities(Population);
            FitnessAverage = MathematicalOperations.Average(fitnessList, fitnessList.Count);
        }
예제 #2
0
        private void GenerationalMutation()
        {
            /*
             *  Mutates a bit from random individuals from the population.
             *  Number of mutations dictated in Constants.cs
             */
            int    individualIndex;
            int    bitIndex;
            string completeChromosome;
            int    softwareOrHardwareMutate;

            for (int times = 0; times < Constants.MutationProbability; times++)
            {
                individualIndex =
                    MathematicalOperations.RandomIntegerInRange(0, Constants.PopulationSize);
                softwareOrHardwareMutate = MathematicalOperations.RandomIntegerInRange(0, 2);
                if (softwareOrHardwareMutate == 1)
                {
                    bitIndex =
                        MathematicalOperations.RandomIntegerInRange(0, Constants.CompleteChromosomeSize);
                    completeChromosome = Population[individualIndex].Hardware.CompleteChromosome;
                    completeChromosome =
                        GeneticOperations.Mutate(bitIndex, completeChromosome);
                    Population[individualIndex].Hardware.Mutate(completeChromosome);
                }
                else
                {
                    bitIndex =
                        MathematicalOperations.RandomIntegerInRange(0, Constants.SoftwareChromosomeSize);
                    completeChromosome = Population[individualIndex].Software.CompleteChromosome;
                    GeneticOperations.Mutate(bitIndex, completeChromosome);
                    Population[individualIndex].Software.Mutate(completeChromosome);
                }
            }
        }