Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                RESULT = "";
                GeneticAlgorithm ga = new GeneticAlgorithm(Convert.ToInt32(GenLength.Value), Convert.ToInt32(CountIndividuals.Value), Convert.ToDouble(MinimumX.Text), Convert.ToDouble(MaximumX.Text), FitnessFunction.Text);
                InitializeGA(ga);

                if ((ga.operators[1] == true) && (ga.intcode == false))
                {
                    MessageBox.Show("Уплотнение сетки возможно только при целочисленном кодировании", "Ошибка входных данных", MessageBoxButtons.OK);
                }
                else
                {
                    RESULT      = ga.Start(out best, out Mid);
                    Answer.Text = RESULT;
                }
            }
            catch (FormatException)
            {
                MessageBox.Show("Неверный формат входных данных", "Ошибка входных данных", MessageBoxButtons.OK);
            }
            catch (Exception)
            {
                MessageBox.Show("Неизвестная ошибка", "Ошибка", MessageBoxButtons.OK);
            }
        }
Пример #2
0
        public void Run(int ID, Guid guid)
        {
            DrawSampleName("Binary File Evolution");
            BinaryFileEqualitySampleController bf = new BinaryFileEqualitySampleController(ID, guid);

            bf.Initialize();

            Console.WriteLine("Starting...");

            var selection  = bf.CreateSelection();
            var crossover  = bf.CreateCrossover();
            var mutation   = bf.CreateMutation();
            var fitness    = bf.CreateFitness();
            var population = new Population(10, 20, bf.CreateChromosome())
            {
                GenerationStrategy = new PerformanceGenerationStrategy()
            };

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation)
            {
                Termination = bf.CreateTermination()
            };

            var terminationName = ga.Termination.GetType().Name;

            ga.GenerationRan += delegate
            {
                DrawSampleName("Binary File Evolution");

                var bestChromosome = ga.Population.BestChromosome;
                Console.WriteLine("Termination: {0}", terminationName);
                Console.WriteLine("Generations: {0}", ga.Population.GenerationsNumber);
                Console.WriteLine("Fitness: {0,10}", bestChromosome.Fitness);
                Console.WriteLine("Time: {0}", ga.TimeEvolving);
                Console.WriteLine("Speed (gen/sec): {0:0.0000}", ga.Population.GenerationsNumber / ga.TimeEvolving.TotalSeconds);
                bf.Draw(bestChromosome);
            };

            try
            {
                bf.ConfigGA(ga);
                ga.Start();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine();
                Console.WriteLine("Error: {0}", ex.Message);
                Console.ResetColor();
                Console.ReadKey();
                return;
            }

            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine();
            Console.WriteLine("Evolved.");
            Console.ResetColor();
            Console.ReadKey();
        }