Ejemplo n.º 1
0
 private void UpdateFitnessOfGeneration(BaseEvaluation <GeneType> CalculatesEvaluation)
 {
     foreach (var chrmosome in Individuals)
     {
         chrmosome.Fitness = CalculatesEvaluation.CalculatesEvaluation(chrmosome);
     }
 }
Ejemplo n.º 2
0
 public void UpdateEvaluation(BaseEvaluation <GeneType> newEvaluation)
 {
     if (newEvaluation != null)
     {
         Evaluation = newEvaluation;
     }
     throw new Exception("the new assessment cannot be null");
 }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
 public GeneticAlgorithm(int numberOfGenerations, BaseEvaluation <GeneType> Evaluation)
 {
     if (numberOfGenerations <= 0)
     {
         throw new Exception("The number of the generations it is invalid");
     }
     if (Evaluation == null)
     {
         throw new Exception("The Evaluation it's not defined");
     }
     NumberOfGenerations = numberOfGenerations;
     this.Evaluation     = Evaluation;
 }
Ejemplo n.º 5
0
        public Chromosome <GeneType> GetBestChromosome()
        {
            if (Population == null || Population.Count == 0)
            {
                throw new Exception("The genetic algorithm hass not been executed");
            }
            Chromosome <GeneType> ValueReturn = null;
            double Evaluation = double.MinValue;

            Population.ForEach(x =>
            {
                x.Individuals.ForEach(y => {
                    if (y.Fitness > Evaluation)
                    {
                        Evaluation  = y.Fitness;
                        ValueReturn = y;
                    }
                });
            });
            return(ValueReturn);
        }