Exemplo n.º 1
0
        public string[] StartRandom()
        {
            var data      = LoadData();
            var result    = new string[Config.GenerationsNumber];
            var operators = new GeneticOperators(data);

            var bestValue = double.NegativeInfinity;

            for (var i = 0; i < Config.GenerationsNumber; i++)
            {
                var randomPopulation = operators.GeneratePopulation(Config.PopulationSize);
                operators.CalculateSpecimensValues(randomPopulation);
                var ranking       = randomPopulation.OrderByDescending(specimen => specimen.Value);
                var currBestValue = ranking.First().Value;
                if (currBestValue > bestValue)
                {
                    bestValue = currBestValue;
                }

                WriteLine($"{i}   {bestValue} ");
                result[i] = bestValue.ToString();
            }

            return(result);
        }
Exemplo n.º 2
0
        public string[] Start()
        {
            var data   = LoadData();
            var result = new string[Config.GenerationsNumber];
//            var stringBuilder = new StringBuilder();
            var operators  = new GeneticOperators(data);
            var population = operators.GeneratePopulation(Config.PopulationSize);

            operators.CalculateSpecimensValues(population);

            WriteLine("Capacity of knapsack = " + data.CapacityOfKnapsack);

            for (var i = 0; i < Config.GenerationsNumber; i++)
            {
                var selectedSpecimens = operators.TourSelection(population);
//                var selectedSpecimens = operators.RouletteSelection(population);
                var newPopulation = operators.Crossover(selectedSpecimens);
                operators.Mutation(newPopulation);
                operators.CalculateSpecimensValues(newPopulation);
                var stats = operators.GetStats(newPopulation, i);
                WriteLine($"{i}   {stats} ");
                result[i] = stats;
//                stringBuilder.AppendLine(string.Join(";", stats));
                population = newPopulation;
            }

            return(result);
//            SaveData(stringBuilder.ToString());
        }