Exemple #1
0
 public Solver(SurvivalSelection survivalSelection, Breeder breeder, IMutator mutator, int maxStepsWithoutChanges)
 {
     _breeder = breeder;
     _mutator = mutator;
     _maxStepsWithoutChanges = maxStepsWithoutChanges;
     _survivalSelection      = survivalSelection;
 }
Exemple #2
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)}");
            }
        }