예제 #1
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];
        }
예제 #2
0
        public void RunEvolution()
        {
            GeneticAlgorithmParameters parameters = new GeneticAlgorithmParameters(
                populationSize: 10,
                mutationRate: 0.3,
                mutationAmplitute: 0.5);

            //PopulationType specimenType = PopulationType.Arithmetic1D;
            PopulationType specimenType = PopulationType.Arithmetic15D;

            PopulationSetting populationSetting;

            switch (specimenType)
            {
            case (PopulationType.Arithmetic1D):
                populationSetting = new Arithmetic1DPopulation((s) => (Math.Pow(s.Genes[0] - 10, 2) + 5));
                break;

            case (PopulationType.Arithmetic2D):
                populationSetting = new Arithmetic2DPopulation((s) => (Math.Pow((s.Genes[0] + 2 * s.Genes[1] - 7), 2) + Math.Pow((2 * s.Genes[0] + s.Genes[1] - 5), 2)));
                break;

            case (PopulationType.Arithmetic15D):
                populationSetting = new Arithemtic15DPopulation((s) => GetResultOfFitFunctionForMultidimensionalFucntion(s));
                break;

            default: throw new Exception("Тип особей, для которых запускается симуляция, не задан! Завершение работы...");
            }

            geneticAlgorithm = new GeneticAlgorithm(parameters, populationSetting);

            for (int i = 0; i < 5000; i++)
            {
                geneticAlgorithm.NewGeneration();
                //Console.WriteLine();
                //Console.WriteLine("Поколение: " + geneticAlgorithm.GenerationNum);
                //ShowPopulation(geneticAlgorithm.CurrentGeneration);
                //ShowBestInGeneration(geneticAlgorithm.CurrentGeneration, populationSetting);
            }
            ShowBestOfAllTime(geneticAlgorithm.allGenerations, populationSetting);
        }