Exemple #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);
        }
Exemple #2
0
        public GeneticAlgorithm(GeneticAlgorithmParameters parameters, PopulationSetting population)
        {
            GenerationNum   = 0;
            this.Parameters = parameters;
            this.population = population;

            CurrentGeneration = new List <Specimen>();
            for (int i = 0; i < parameters.PopulationSize; i++)
            {
                CurrentGeneration.Add(population.CreateOne());
                System.Threading.Thread.Sleep(100);
            }

            BestSpecimen = CurrentGeneration[0];
        }
Exemple #3
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);
        }