Example #1
0
        private static void LaunchGenetic()
        {
            int production_size = default;

            string[] production_names = default;
            float[]  ecologies        = default;
            int[]    costs            = default;
            int[]    profits          = default;
            int      a = default;

            Console.WriteLine("Choose input data:\n" +
                              "1 - from console;\n" +
                              "2 - from file;\n");
            int choose = int.Parse(Console.ReadLine());

            if (choose == 1)
            {
                ReadStartInfoFromConsole(ref production_size, ref production_names, ref ecologies, ref costs, ref profits, ref a);
            }
            else if (choose == 2)
            {
                ReadStartInfoFromFile(ref production_size, ref production_names, ref ecologies, ref costs, ref profits, ref a);
            }
            Console.Write("Enter population size: ");
            int population_size = Convert.ToInt32(Console.ReadLine());

            GeneticAlgorithm algorithm = new GeneticAlgorithm(production_size, ecologies.ToList(), costs.ToList(), profits.ToList(), a, population_size);
            int i_not = 0;
            int i     = 0;

            string[] rez_names = default;
            float    rez_cf    = default;
            Dictionary <float, List <string> > best = new Dictionary <float, List <string> >();

            best.Add(0, null);
            while (i_not < 30)
            {
                i++;
                //WriteInfoAboutProduction(production_names, algorithm);

                algorithm.GetStartPopulation();
                //WritePopulationMatrix(algorithm);

                WriteBest(algorithm, production_names);

                algorithm.GetParents();
                //WriteParents(algorithm);

                algorithm.CreateMask();
                //WriteMask(algorithm);

                algorithm.GetChildren();
                //WriteChildren(algorithm);

                var rez = algorithm.GetTheBestChromosome();
                rez_names = ConvertNames(rez.Item1, production_names).ToArray();
                rez_cf    = rez.Item2;

                algorithm.UpdatePopulation();

                WriteIterationBestResultToFile(i, rez_cf);

                if (rez.Item2 <= best.Keys.Max())
                {
                    i_not++;
                }
                else
                {
                    i_not = 0;
                }

                if (!best.ContainsKey(rez_cf))
                {
                    best.Add(rez_cf, rez_names.ToList());
                }
            }
            var item = best.First(x => x.Key == best.Keys.Max());

            WriteResultToFile(item.Value.ToArray(), item.Key);
        }