Esempio n. 1
0
        public Generation <GeneType> GenerateNextGeneration(BaseEvaluation <GeneType> CalculatesEvaluation)
        {
            #region Validation
            if (Environment == null || Individuals == null)
            {
                throw new NullReferenceException();
            }
            if (Individuals.Count <= 0)
            {
                throw new Exception("The number of indiciduals must be greater than zero");
            }
            #endregion Validation

            UpdateFitnessOfGeneration(CalculatesEvaluation);
            var newGenration = new Generation <GeneType>(NumberOfIndividuals, NumberOfGenesOfIndividuals);
            newGenration.Environment        = this.Environment;
            newGenration.NumberOfGeneration = this.NumberOfGeneration + 1;
            newGenration.Individuals        = new List <Chromosome <GeneType> >();

            while (newGenration.Individuals.Count < NumberOfIndividuals)
            {
                var fatherId = GetIdOfChromosomeRoulette();
                var motherId = GetIdOfChromosomeRoulette();
                ValidateParentsId(fatherId, motherId);
                while (fatherId == motherId)
                {
                    motherId = GetIdOfRandomChromosome();
                    ValidateParentsId(fatherId, motherId);
                }
                #region CheckIfExecuteCrossover
                int randPercentage = Random.Next() % 100;
                if (randPercentage <= Environment.RateCrossover)
                {
                    var father = GetChromosome(fatherId);
                    var mother = GetChromosome(motherId);
                    newGenration.AddIndividualChromosome(ExecuteCrossOver(father, mother));
                    newGenration.AddIndividualChromosome(ExecuteCrossOver(mother, father));
                }
                else
                {
                    newGenration.AddIndividualChromosome(GetChromosome(fatherId));
                    newGenration.AddIndividualChromosome(GetChromosome(motherId));
                }
                #endregion CheckIfExecuteCrossover
            }
            #region ExecuteMutation
            int randPercentageMutation = Random.Next() % 100;
            if (randPercentageMutation <= Environment.RateMutation)
            {
                Debug.WriteLine("there was a mutation");
                int randomChromossomePosition = Random.Next() % Individuals.Count;
                Individuals[randomChromossomePosition].PerformsMutation(Random);
            }
            #endregion ExecuteMutation
            return(newGenration);
        }
Esempio n. 2
0
        public void DefineInitialPopulation(int numberOfIndividuals, int numberOfGenesOfIndividuals, List <Chromosome <GeneType> > PopulationInitial)
        {
            if (numberOfIndividuals <= 0)
            {
                throw new Exception("The number of individuals it's invalid");
            }
            if (numberOfGenesOfIndividuals <= 0)
            {
                throw new Exception("The number of genes of individuals it's invalid");
            }
            if (PopulationInitial == null || PopulationInitial.Count <= 0)
            {
                throw new Exception("The population initial has not been set");
            }
            if (PopulationInitial.Count != numberOfIndividuals)
            {
                throw new Exception("The number of the individuals not is equals to the number of the individuals of population initial");
            }
            Generation <GeneType> p = new Generation <GeneType>(numberOfIndividuals, numberOfGenesOfIndividuals);

            PopulationInitial.ForEach(x => p.AddIndividualChromosome(x));
            Population = new List <Generation <GeneType> > {
                p
            };
        }