Beispiel #1
0
        public Individual RunSimulation(int maxNumberOfGenerations)
        {
            ResetSimulations();

            for (int i = 0; i < maxNumberOfGenerations; i++)
            {
                var parents = _selectionOperator.GenerateParentPopulation(_population);

                for (int j = 0; j < _numberOfIndividuals - 1; j += 2)
                {
                    if (_random.NextDouble() < CrossoverProbability)
                    {
                        _crossOperator.Crossover(parents[j], parents[j + 1]);

                        _mutationOperator.Mutation(parents[j], MutationProbability);
                        _mutationOperator.Mutation(parents[j + 1], MutationProbability);
                    }
                }

                _population = parents;

                UpdateFitness();

                if (PrintStatistics)
                {
                    Console.WriteLine($"Generation: {i}");
                    Console.WriteLine($"The best is: x = {TakeTheBest().Chromosome.DecodedValue}\tf = {TakeTheBest().Fitness}");
                }
            }

            return(TakeTheBest());
        }
Beispiel #2
0
        public Individual RunSimulation(int maxNumberOfGenerations)
        {
            ResetSimulations();

            for (int i = 0; i < maxNumberOfGenerations; i++)
            {
                //System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                //sw.Start();

                var parents = _selectionOperator.GenerateParentPopulation(_population);

                //sw.Stop();
                //Console.WriteLine(sw.ElapsedMilliseconds);

                Parallel.For(0, _numberOfIndividuals / 2, j =>
                {
                    if (_random.NextDouble() < CrossoverProbability)
                    {
                        int index = j * 2;

                        _crossOperator.Crossover(parents[index], parents[index + 1]);

                        _mutationOperator.Mutation(parents[index], MutationProbability);
                        _mutationOperator.Mutation(parents[index + 1], MutationProbability);
                    }
                });

                //for (int j = 0; j < _numberOfIndividuals - 1; j += 2)
                //{
                //	if (_random.NextDouble() < CrossoverProbability)
                //	{
                //		_crossOperator.Crossover(parents[j], parents[j + 1]);

                //		_mutationOperator.Mutation(parents[j], MutationProbability);
                //		_mutationOperator.Mutation(parents[j + 1], MutationProbability);
                //	}
                //}

                _population = parents;

                UpdateFitness();

                if (PrintStatistics)
                {
                    Console.WriteLine($"Generation: {i}");
                    Console.WriteLine($"The best is: x = {TakeTheBest().Chromosome.DecodedValue}\tf = {TakeTheBest().Fitness}");
                }
            }

            return(TakeTheBest());
        }
Beispiel #3
0
        public Individual RunSimulation(int maxNumberOfGenerations)
        {
            ResetSimulations();

            for (int i = 0; i < maxNumberOfGenerations; i++)
            {
                var parents = _selectionOperator.GenerateParentPopulation(_population);

                for (int j = 0; j < _numberOfIndividuals - 1; j += 2)
                {
                    if (_random.NextDouble() < CrossoverProbability)
                    {
                        _crossOperator.Crossover(parents[j], parents[j + 1]);


                        if (_random.NextDouble() < MutationProbability)
                        {
                            _mutationOperator.Mutation(parents[j], MutationProbability);
                        }

                        if (_random.NextDouble() < MutationProbability)
                        {
                            _mutationOperator.Mutation(parents[j + 1], MutationProbability);
                        }
                    }
                }

                _population = parents;

                UpdateFitness();
            }

            return(_population
                   .OrderByDescending(x => x.Fitness)
                   .FirstOrDefault());
        }
Beispiel #4
0
        public Individual RunSimulation(int maxNumberOfGenerations)
        {
            ResetSimulations();

            // Sekwencyjnie
            //for (int i = 0; i < maxNumberOfGenerations; i++)
            //{
            //    var parents = _selectionOperator.GenerateParentPopulation(_population);

            //    for (int j = 0; j < _numberOfIndividuals - 1; j += 2)
            //    {
            //        if (_random.NextDouble() < CrossoverProbability)
            //        {
            //            _crossOperator.Crossover(parents[j], parents[j + 1]);

            //            _mutationOperator.Mutation(parents[j], MutationProbability);
            //            _mutationOperator.Mutation(parents[j + 1], MutationProbability);
            //        }
            //    }

            //    _population = parents;

            //    UpdateFitness();

            //    if (PrintStatistics)
            //    {
            //        Console.WriteLine($"Generation: {i}");
            //        Console.WriteLine($"The best is: x = {TakeTheBest().Chromosome.DecodedValue}\tf = {TakeTheBest().Fitness}");
            //    }
            //}

            // Parallel
            for (int i = 0; i < maxNumberOfGenerations; i++)
            {
                var parents = _selectionOperator.GenerateParentPopulation(_population);

                Parallel.For(0, _numberOfIndividuals - 1, j =>
                {
                    if (_random.NextDouble() < CrossoverProbability)
                    {
                        _crossOperator.Crossover(parents[j], parents[j + 1]);
                        _mutationOperator.Mutation(parents[j], MutationProbability);

                        if (j == _numberOfIndividuals - 2)
                        {
                            _mutationOperator.Mutation(parents[j + 1], MutationProbability);
                        }
                    }
                });

                _population = parents;

                UpdateFitness();
                if (PrintStatistics)
                {
                    Console.WriteLine($"Generation: {i}");
                    Console.WriteLine($"The best is: x = {TakeTheBest().Chromosome.DecodedValue}\tf = {TakeTheBest().Fitness}");
                }
            }


            // Taski
            //for (int i = 0; i < maxNumberOfGenerations; i++)
            //{
            //    var parents = _selectionOperator.GenerateParentPopulation(_population);

            //    double seed = 10000;
            //    double tasks = (_population.Length / seed);
            //    int amountOfTask = (int)Math.Ceiling(tasks);
            //    Task[] t = new Task[amountOfTask];
            //    int LeftC = 0;
            //    int RightC = (int)seed;

            //    for (int k = 0; k < amountOfTask; k++)
            //    {
            //        if (k == amountOfTask - 1)
            //        {
            //            RightC = _population.Length - 1;
            //            t[k] = GeneticOperations(LeftC, RightC, parents);
            //        }
            //        else
            //        {
            //            t[k] = GeneticOperations(LeftC, RightC, parents);
            //            LeftC = RightC;
            //            RightC += (int)seed;
            //        }
            //    }

            //    for (int k = 0; k < amountOfTask; k++)
            //    {
            //        t[k].Wait();
            //    }

            //    _population = parents;

            //    UpdateFitness();

            //    if (PrintStatistics)
            //    {
            //        Console.WriteLine($"Generation: {i}");
            //        Console.WriteLine($"The best is: x = {TakeTheBest().Chromosome.DecodedValue}\tf = {TakeTheBest().Fitness}");
            //    }
            //}

            return(TakeTheBest());
        }