Exemplo n.º 1
0
        public static List <Evolvi> SortFittest()
        {
            List <Evolvi> sorted = CurrentGeneration.OrderBy(x => x.Energy).ToList();

            return(sorted);
        }
Exemplo n.º 2
0
        public void RunEpoch()
        {
            List <Chromosome> newGeneration = new List <Chromosome>();

            newGeneration.AddRange(CurrentGeneration.OrderBy(x => x.Fitness).Take((int)(CurrentGeneration.Count * ElitismRate)));

            while (newGeneration.Count < CurrentGeneration.Count)
            {
                //Selection

                Tuple <Chromosome, Chromosome> parents = Selection.Select(CurrentGeneration);

                Tuple <Chromosome, Chromosome> children;

                //Crossover

                if (random.NextDouble() < CrossoverRate)
                {
                    children = parents.Item1.Crossover(parents.Item2);

                    children.Item1.FitnessOutdated = true;
                    children.Item2.FitnessOutdated = true;

                    double parentAverage = (parents.Item1.Fitness + parents.Item2.Fitness) / 2;

                    children.Item1.Fitness = parentAverage;
                    children.Item2.Fitness = parentAverage;

                    children.Item1.FitnessOutdated = true;
                    children.Item2.FitnessOutdated = true;

                    if (random.NextDouble() < 0.5)
                    {
                        children.Item1.Priority = parents.Item1.Priority;
                        children.Item2.Priority = parents.Item2.Priority;
                    }
                    else
                    {
                        children.Item1.Priority = parents.Item2.Priority;
                        children.Item2.Priority = parents.Item1.Priority;
                    }

                    double variance          = (Math.Pow(parents.Item1.Fitness - parentAverage, 2) + Math.Pow(parents.Item2.Fitness - parentAverage, 2)) / 2;
                    double stdDev            = Math.Sqrt(variance);
                    double fitnessDifference = Math.Abs(parents.Item1.Fitness - parents.Item2.Fitness);
                    children.Item1.Priority += variance;
                    children.Item2.Priority += variance;
                }
                else
                {
                    children = new Tuple <Chromosome, Chromosome>((Chromosome)parents.Item1.Clone(), (Chromosome)parents.Item2.Clone());
                }

                //Mutation
                if (random.NextDouble() < MutationRate)
                {
                    children.Item1.Mutate();
                    children.Item1.FitnessOutdated = true;
                    children.Item1.Priority       += MutationWeigth;
                }
                if (random.NextDouble() < MutationRate)
                {
                    children.Item2.Mutate();
                    children.Item2.FitnessOutdated = true;
                    children.Item2.Priority       += MutationWeigth;
                }

                newGeneration.Add(children.Item1);

                //Sometimes only one child can be added
                if (newGeneration.Count == CurrentGeneration.Count)
                {
                    newGeneration.Add(children.Item2);
                }
            }
            CurrentGeneration = new Generation(newGeneration);
            EvaluatePopulation();
        }