Ejemplo n.º 1
0
        public static void RouletteWheelSelection <T>(Chromosome <T>[] Chs, double[] Fit)
        {
            Chromosome <T>[] selection = new Chromosome <T> [Chs.Length];
            double           total     = 0;

            for (int i = 0; i < Chs.Length; i++)
            {
                total += 1 / Fit[i];
            }

            double[] sectors = new double[Chs.Length];
            for (int i = 0; i < Chs.Length; i++)
            {
                sectors[i] = (1 / Fit[i]) / total * 100;
            }

            //randomly selecting crossing chromosomes according to their weights
            for (int i = 0; i < Chs.Length; i++)
            {
                double num = Rng.NextDouble() * 100, seek = 0;

                for (int j = 0; j < Chs.Length; j++)
                {
                    seek += sectors[j];
                    if (num < seek)
                    {
                        selection[i] = Chs[j];
                        break;
                    }
                }
            }

            selection.CopyTo(Chs, 0);
        }
Ejemplo n.º 2
0
        public static void Crossover <T>(Chromosome <T>[] Chs)
        {
            Chromosome <T>[] crossed = new Chromosome <T> [Chs.Length];

            //forming pairs
            int[] pairs = new int[Chs.Length];
            for (int i = 0; i < Chs.Length; i++)
            {
                pairs[i] = i;
            }

            //shuffling array of pairs to make them random
            int n = pairs.Length;

            while (n > 1)
            {
                int k = Rng.Next(n--);
                int t = pairs[n];
                pairs[n] = pairs[k];
                pairs[k] = t;
            }

            //crossing
            for (int i = 0; i < pairs.Length / 2; i++)
            {
                int point = Rng.Next(0, Chs[0].ParamNum - 1);
                T[] Ch1   = new T[Chs[0].ParamNum], Ch2 = new T[Chs[0].ParamNum];
                for (int j = 0; j < Chs[0].ParamNum; j++)
                {
                    if (j <= point)
                    {
                        Ch1[j] = Chs[pairs[2 * i]].Genes[j];
                        Ch2[j] = Chs[pairs[2 * i + 1]].Genes[j];
                    }
                    else
                    {
                        Ch1[j] = Chs[pairs[2 * i + 1]].Genes[j];
                        Ch2[j] = Chs[pairs[2 * i]].Genes[j];
                    }
                }

                crossed[2 * i]     = new Chromosome <T>(Ch1);
                crossed[2 * i + 1] = new Chromosome <T>(Ch2);
            }

            crossed.CopyTo(Chs, 0);
        }
Ejemplo n.º 3
0
        private void GenerateNextPopulation()
        {
            Random     rnd = new Random();
            Chromosome oldBestSolution = new Chromosome(ProblemParameters.Dimension);
            Chromosome newBestSolution, newWrostSolution;

            // Store best found solution
            _bestOfCurrentPopulation.CopyTo(oldBestSolution);
            //

            #region Selection (Algorithm: Tournament)
            int    tourSize; // Tournament size
            int    bestChromosomeIndex = 0;
            double bestFitness;

            if (PopulationSize < 32)
            {
                tourSize = 3;
            }
            else
            {
                tourSize = (int)Math.Log(PopulationSize, 2) - 1;
            }


            for (int i = 0; i < PopulationSize; i++)
            {
                bestFitness = Double.NegativeInfinity;
                for (int j = 0; j < tourSize; j++)
                {
                    int rndIndex;
                    rndIndex = rnd.Next(PopulationSize);
                    if (_currentPopulation[rndIndex].FitnessValue > bestFitness)
                    {
                        bestFitness         = _currentPopulation[rndIndex].FitnessValue;
                        bestChromosomeIndex = rndIndex;
                    }
                }

                // Copy the "winner of tournament" to Intermediate Population
                _currentPopulation[bestChromosomeIndex].CopyTo(_intermediatePopulation[i]);
            }
            #endregion

            #region Crossover (Algorithm: Arithmetic Crossover)
            for (int i = 0; i < PopulationSize / 2; i++)
            {
                if (rnd.NextDouble() < ProbabilityOfCrossover)
                {
                    // Do Crossover
                    Chromosome.DoCrossover(_intermediatePopulation[i], _intermediatePopulation[i + 1],
                                           _currentPopulation[i], _currentPopulation[i + 1]);
                }
                else
                {
                    // Do NOT Crossover (Copy parents to the new population without any change)
                    _intermediatePopulation[i].CopyTo(_currentPopulation[i]);
                    _intermediatePopulation[i + 1].CopyTo(_currentPopulation[i + 1]);
                }
            }
            #endregion

            #region Mutation
            newBestSolution = newWrostSolution = _currentPopulation[0];

            for (int i = 0; i < PopulationSize; i++)
            {
                if (rnd.NextDouble() < ProbabilityOfMutation)
                {
                    _currentPopulation[i].DoMutation();
                }

                // Find wrost of this (new) population
                if (_currentPopulation[i].FitnessValue < newWrostSolution.FitnessValue)
                {
                    newWrostSolution = _currentPopulation[i];
                }

                // Find best of this (new) population
                if (_currentPopulation[i].FitnessValue > newBestSolution.FitnessValue)
                {
                    newBestSolution = _currentPopulation[i];
                }
            }

            #region Elitism
            oldBestSolution.CopyTo(newWrostSolution); // After this statement, "newWrostSolution" will point to "Old Best Solution"!
            // Now we must find best of current and old populations
            _bestOfCurrentPopulation = (oldBestSolution.FitnessValue < newBestSolution.FitnessValue)
                ? newBestSolution : newWrostSolution;
            #endregion
            #endregion
        }