コード例 #1
0
        private static void Run()
        {
            const int    initialSpeciesNumber                 = 4;
            const int    survivalSelectionThreshold           = 3;
            const double mutationProbability                  = 0.3;
            const double betterParentGeneSelectionProbability = 0.8;


            Console.WriteLine("Dataset name:");
            string datasetName = Console.ReadLine();

            Console.WriteLine("Accuracy");
            int maxStepsWithoutChanges = int.Parse(Console.ReadLine());

            var settings = Settings.ParseDataSet(datasetName);

            var             fitnessFun        = new SimpleFitnessFun(settings.MaxVolume, settings.Profits, settings.Weights);
            var             survivalSelection = new SurvivalSelection(survivalSelectionThreshold);
            IGenesCrossover crossover         = new SimpleGenesCrossover(fitnessFun, betterParentGeneSelectionProbability);
            Breeder         breeder           = new Breeder(initialSpeciesNumber, new RandomBreedingSelector(), crossover);
            var             mutator           = new RandomMutator(mutationProbability);
            var             solver            = new Solver(survivalSelection, breeder, mutator, maxStepsWithoutChanges);


            var initialSpecies = GenerateInitialSpecies(initialSpeciesNumber, settings.Profits.Length, fitnessFun);


            Console.WriteLine("Начальные особи");
            PrintSpecies(initialSpecies);


            // while (true)
            // {
            //     initialSpecies = solver.Step(initialSpecies);
            //     PrintSpecies(initialSpecies);
            //     Console.WriteLine();
            //
            //     var key = Console.ReadKey();
            //     if (key.Key == ConsoleKey.X) break;
            // }

            Species best = solver.FindBest(initialSpecies);

            Console.WriteLine();
            Console.WriteLine($"Calcula best:{PrintSpecies(best)}. Steps: {solver.Steps}");
            if (settings.DatasetBest != null)
            {
                Console.WriteLine($"Dataset best:{PrintSpecies(settings.DatasetBest)}");
            }
        }
コード例 #2
0
        public static Settings ParseDataSet(string datasetName)
        {
            string capacityFile = $"{datasetName}_c.txt";
            int    capacity     = File.ReadLines(capacityFile).Select(l => l.Trim()).Select(int.Parse).First();

            string weightFile = $"{datasetName}_w.txt";
            var    weights    = File.ReadLines(weightFile).Select(l => l.Trim()).Select(int.Parse).ToArray();

            string profitsFile = $"{datasetName}_p.txt";
            var    profits     = File.ReadLines(profitsFile).Select(l => l.Trim()).Select(int.Parse).ToArray();

            string optimalFile      = $"{datasetName}_s.txt";
            var    dataSetBestArray = File.ReadLines(optimalFile).Select(l => l.Trim()).Select(int.Parse).Select(i => i == 1)
                                      .ToArray();

            var      simpleFitnessFun = new SimpleFitnessFun(capacity, profits, weights);
            BitArray bitArray         = new BitArray(dataSetBestArray);
            var      datasetBest      = new Species(bitArray, simpleFitnessFun.Calculate(bitArray));

            return(new Settings(capacity, weights, profits, datasetBest));
        }