Ejemplo n.º 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);
        }
Ejemplo n.º 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);
                }
            }
        }
Ejemplo n.º 3
0
        private (Robot, Robot) ReproduceIndividuals(Robot pParentA, Robot pParentB, int pId)
        {
            int hardwarePartitionIndex =
                MathematicalOperations.RandomIntegerInRange(1, Constants.CompleteChromosomeSize);
            int softwarePartitionIndex =
                MathematicalOperations.RandomIntegerInRange(1, Constants.SoftwareChromosomeSize);

            Robot child1 = new Robot(pParentA, pParentB, hardwarePartitionIndex, softwarePartitionIndex, pId, Id);
            Robot child2 = new Robot(pParentB, pParentA, hardwarePartitionIndex, softwarePartitionIndex, pId + 1, Id);

            return(child1, child2);
        }
Ejemplo n.º 4
0
        private Robot SelectMatingPartner(List <Robot> pGeneration)
        {
            var   random0To1Number       = MathematicalOperations.Random0To1Float();
            float accumulatedProbability = 0;
            var   selectedIndex          = -1;

            for (var index = 0; index < pGeneration.Count; index++)
            {
                accumulatedProbability += pGeneration[index].ReproductionProbability;
                if (!(random0To1Number <= accumulatedProbability))
                {
                    continue;
                }
                selectedIndex = index;
                break;
            }
            return(pGeneration[selectedIndex]);
        }