Exemplo n.º 1
0
        private void ShowBestInGeneration(List <Specimen> generation, PopulationSetting population)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Лучший в поколении №" + geneticAlgorithm.GenerationNum + ": " + generation[0] + "\n");
            sb.Append("Значение функции: " + population.FittingFunction(generation[0]));
            Console.WriteLine(sb);
        }
Exemplo n.º 2
0
        private void ShowBestOfAllTime(List <List <Specimen> > generations, PopulationSetting population)
        {
            Specimen      best            = null;
            Specimen      worst           = null;
            int           bestGeneration  = 0;
            int           worstGeneration = 0;
            var           bestList        = new List <Specimen>();
            var           tempList        = new List <Specimen>();
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < generations.Count; i++)
            {
                tempList = generations[i];
                tempList.Sort((a, b) => population.FittingFunction(a).CompareTo(population.FittingFunction(b)));
                bestList.Add(tempList[0]);
            }

            for (int i = 0; i < bestList.Count; i++)
            {
                best  = bestList[0];
                worst = bestList[1];

                if (population.FittingFunction(bestList[i]) < population.FittingFunction(best))
                {
                    best           = bestList[i];
                    bestGeneration = i;
                }

                if (population.FittingFunction(bestList[i]) > population.FittingFunction(worst))
                {
                    worst           = bestList[i];
                    worstGeneration = i;
                }
            }
            sb.Append("\n\nВывод\n");
            sb.Append("Лучший из всех в поколении №" + (bestGeneration + 1) + ": " + best + "\n Значение функции: " + population.FittingFunction(best) + "\n\n");
            sb.Append("Худший из всех в поколении №" + (worstGeneration + 1) + ": " + worst + "\n Значение функции: " + population.FittingFunction(worst) + "\n");
            Console.WriteLine(sb);
        }
Exemplo n.º 3
0
        public void NewGeneration()
        {
            if (CurrentGeneration.Count <= 0)
            {
                return;
            }

            newGeneration = new List <Specimen>();
            oldGeneration = new List <Specimen>();
            List <Specimen> bestOfTheBest = new List <Specimen>();

            allGenerations.Add(CurrentGeneration);

            for (int i = 0; i < CurrentGeneration.Count; i++)
            {
                for (int j = 0; j < CurrentGeneration.Count; j++)
                {
                    if (i == j)
                    {
                        j++;
                    }
                    if (j == CurrentGeneration.Count)
                    {
                        break;
                    }

                    var firstParent  = CurrentGeneration[i];
                    var secondParent = CurrentGeneration[j];

                    var child = firstParent.CrossoverWith(secondParent);
                    if (RandomForGA.Generator.NextDouble() < Parameters.MutationRate)
                    {
                        child.Mutate(Parameters.MutationAmplitude);
                    }


                    newGeneration.Add(child);
                }
            }

            for (int i = 0; i < CurrentGeneration.Count; i++)
            {
                oldGeneration.Add(CurrentGeneration[i]);
            }

            CurrentGeneration.Clear();
            for (int i = 0; i < oldGeneration.Count; i++)
            {
                CurrentGeneration.Add(oldGeneration[i]);
            }

            for (int i = 0; i < newGeneration.Count; i++)
            {
                CurrentGeneration.Add(newGeneration[i]);
            }

            CalculateFitness();

            CurrentGeneration.Sort((a, b) => population.FittingFunction(a).CompareTo(population.FittingFunction(b)));

            for (int i = 0; i < Parameters.PopulationSize; i++)
            {
                bestOfTheBest.Add(CurrentGeneration[i]);
            }
            CurrentGeneration = bestOfTheBest;
            GenerationNum++;
        }