Exemple #1
0
        // Создание первоначальной популяции
        void FirstPopulation()
        {
            Random ranGen = new Random();

            for (int i = 0; i < population_count; ++i)
            {
                double x = ranGen.NextDouble() * ranGen.Next(MIN_RND, MAX_RND);
                double y = ranGen.NextDouble() * ranGen.Next(MIN_RND, MAX_RND);

                points p = new points(x, y);
                Population.Add(p);
            }
        }
Exemple #2
0
        // Основной метод, возвращает минимум функции
        public points Gen()
        {
            points p = new points();

            int breed = 0;

            FirstPopulation();
            while (true)
            {
                Console.Write("Поколение: "); Console.Write(breed);
                Console.Write(" Популяция:"); Console.WriteLine(Population.Count);
                Population = CreateFitnesses();
                Population = CrossOver();
                Mutation();
                if (breed >= breed_count)
                {
                    break;
                }
                breed++;
            }
            // сортируем лучшие на вершине
            for (int i = 0; i < Population.Count; i++)
            {
                for (int j = 0; j < Population.Count - i - 1; j++)
                {
                    if (Population[j].evaluation > Population[j + 1].evaluation)
                    {
                        points tmp = new points();
                        tmp               = Population[j];
                        Population[j]     = Population[j + 1];
                        Population[j + 1] = tmp;
                    }
                }
            }

            p.x = Population[0].x;
            p.y = Population[0].y;
            return(p);
        }
Exemple #3
0
        // Скрешиваем особей
        public List <points> CrossOver()
        {
            List <points> NewPopulation = new List <points>();

            for (int i = 2; i < Population.Count; i = i + 2)
            {
                if (i > Population.Count)
                {
                    break;
                }

                // Добавляем родителей
                NewPopulation.Add(Population[i - 1]);
                NewPopulation.Add(Population[i - 2]);

                // добавляем предка
                points p = new points((Population[i - 1].x + Population[i - 2].x) / 2, (Population[i - 1].y + Population[i - 2].y) / 2);
                p.evaluation = Function(p.x, p.y);
                NewPopulation.Add(p);
            }
            return(NewPopulation);
        }
Exemple #4
0
        static void Main(string[] args)
        {
            int     population, breed;
            int     func;
            Genetic Gens;

            Console.Write("Введите популяцию: "); population = Convert.ToUInt16(Console.ReadLine());
            Console.Write("Введите Максимальное число поколений: "); breed = Convert.ToInt16(Console.ReadLine());
            Console.Write("1 - Himmelblau's function;\n2 - Rosenbrock function\n3 - Растригин \n4 - another "); func = Convert.ToInt16(Console.ReadLine());

            Gens = new Genetic(population, breed, func);

            points P = new points();

            P            = Gens.Gen();
            P.evaluation = Gens.Function(P.x, P.y);

            Console.Write("X: "); Console.WriteLine(P.x);
            Console.Write("Y: "); Console.WriteLine(P.y);
            Console.Write("F: "); Console.WriteLine(P.evaluation);

            Console.ReadLine();
        }